1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #ifndef _SYS_SOCKFS_NL7CURI_H
  28 #define _SYS_SOCKFS_NL7CURI_H
  29 
  30 #ifdef  __cplusplus
  31 extern "C" {
  32 #endif
  33 
  34 #include <sys/types.h>
  35 #include <sys/atomic.h>
  36 #include <sys/cmn_err.h>
  37 #include <sys/stropts.h>
  38 #include <sys/socket.h>
  39 #include <sys/socketvar.h>
  40 
  41 #undef  PROMIF_DEBUG
  42 
  43 /*
  44  * Some usefull chararcter macros:
  45  */
  46 
  47 #ifndef tolower
  48 #define tolower(c) ((c) >= 'A' && (c) <= 'Z' ? (c) | 0x20 : (c))
  49 #endif
  50 
  51 #ifndef isdigit
  52 #define isdigit(c) ((c) >= '0' && (c) <= '9')
  53 #endif
  54 
  55 #ifndef isalpha
  56 #define isalpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  57 #endif
  58 
  59 #ifndef isspace
  60 #define isspace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || \
  61                 (c) == '\r' || (c) == '\f' || (c) == '\013')
  62 #endif
  63 
  64 /*
  65  * ref_t - reference type, ...
  66  *
  67  * Note, all struct's must contain a single ref_t, all must use
  68  * kmem_cache, all must use the REF_* macros for free.
  69  */
  70 
  71 typedef struct ref_s {
  72         uint32_t        cnt;            /* Reference count */
  73         void            (*last)(void *); /* Call-back for last ref */
  74         kmem_cache_t    *kmc;           /* Container allocator cache */
  75 } ref_t;
  76 
  77 #define REF_INIT(container, count, inactive, kmem) {                    \
  78         (container)->ref.cnt = (count);                                      \
  79         (container)->ref.last = (void (*)(void *))((inactive));              \
  80         (container)->ref.kmc = (kmem);                                       \
  81 }
  82 
  83 #define REF_HOLD(container) {                                           \
  84         atomic_inc_32(&(container)->ref.cnt);                    \
  85         ASSERT((container)->ref.cnt != 0);                           \
  86 }
  87 
  88 #define REF_RELE(container) {                                           \
  89         if (atomic_dec_32_nv(&(container)->ref.cnt) == 0) {              \
  90                 (container)->ref.last((container));                  \
  91                 kmem_cache_free((container)->ref.kmc, (container));  \
  92         }                                                               \
  93 }
  94 
  95 #define REF_COUNT(container) (container)->ref.cnt
  96 
  97 #define REF_ASSERT(container, count)                                    \
  98         ASSERT((container)->ref.cnt == (count));
  99 
 100 /*
 101  * str_t - string type, used to access a an arbitrary span of a char[].
 102  */
 103 
 104 typedef struct str_s {
 105         char    *cp;                    /* Char pointer current char */
 106         char    *ep;                    /* Char pointer past end of string */
 107 } str_t;
 108 
 109 /*
 110  * uri_*_t - URI descriptor, used to describe a cached URI object.
 111  */
 112 
 113 typedef struct uri_rd_s {
 114         size_t          sz;             /* Size of data */
 115         offset_t        off;            /* Offset into file or -1 for kmem */
 116         union {                         /* Response data */
 117                 char    *kmem;          /* Data in kmem */
 118                 vnode_t *vnode;         /* Data in vnode */
 119         } data;
 120         struct uri_rd_s *next;          /* Next response descriptor */
 121 } uri_rd_t;
 122 
 123 typedef struct uri_desc_s {
 124         struct uri_desc_s *hash;        /* Hash *next */
 125         uint64_t        hit;            /* Hit counter */
 126         clock_t         expire;         /* URI lbolt expires on (-1 = NEVER) */
 127 #ifdef notyet
 128         void            *sslctx;        /* SSL context */
 129 #endif
 130         boolean_t       nocache;        /* URI no cache */
 131         boolean_t       conditional;    /* Conditional response */
 132         uint32_t        hvalue;         /* Hashed value */
 133 
 134         mblk_t          *reqmp;         /* Request mblk_t */
 135         str_t           path;           /* Path name of response  */
 136         str_t           auth;           /* Authority for response */
 137         ssize_t         resplen;        /* Response length */
 138         ssize_t         respclen;       /* Response chunk length */
 139         char            *eoh;           /* End of header pointer */
 140         void            *scheme;        /* Scheme private state */
 141 
 142         ref_t           ref;            /* Reference stuff */
 143 
 144         size_t          count;          /* rd_t chain byte count */
 145         uri_rd_t        *tail;          /* Last response descriptor */
 146         uri_rd_t        response;       /* First response descriptor */
 147 
 148         struct sonode   *proc;          /* Socket processing this uri */
 149         kcondvar_t      waiting;        /* Socket(s) waiting for processing */
 150         kmutex_t        proclock;       /* Lock for proc and waiting */
 151 } uri_desc_t;
 152 
 153 /* Hash the (char)c to the hash accumulator (uint32_t)hv */
 154 #define CHASH(hv, c) (hv) = ((hv) << 5) + (hv) + c; (hv) &= 0x7FFFFFFF
 155 
 156 #define URI_TEMP (uri_desc_t *)-1       /* Temp (nocache) uri_t.hash pointer */
 157 
 158 #define URI_LEN_NOVALUE -1              /* Length (int) counter no value yet */
 159 #define URI_LEN_CONSUMED -2             /* Length (int) counter consumed */
 160 
 161 typedef struct uri_segmap_s {
 162         ref_t           ref;            /* Reference, one per uri_desb_t */
 163         caddr_t         base;           /* Base addr of segmap mapping */
 164         size_t          len;            /* Length of segmap mapping */
 165         vnode_t         *vp;            /* Vnode mapped */
 166 } uri_segmap_t;
 167 
 168 typedef struct uri_desb_s {
 169         frtn_t          frtn;           /* For use by esballoc() and freinds */
 170         uri_desc_t      *uri;           /* Containing URI of REF_HOLD() */
 171         uri_segmap_t    *segmap;        /* If segmap mapped else NULL */
 172 } uri_desb_t;
 173 
 174 /*
 175  * Add (and create if need be) a new uri_rd_t to a uri.
 176  *
 177  * Note, macro can block, must be called from a blockable context.
 178  */
 179 #define URI_RD_ADD(uri, rdp, size, offset) {                            \
 180         if ((uri)->tail == NULL) {                                   \
 181                 (rdp) = &(uri)->response;                                \
 182         } else {                                                        \
 183                 (rdp) = kmem_cache_alloc(nl7c_uri_rd_kmc, KM_SLEEP);    \
 184                 (uri)->tail->next = (rdp);                                \
 185         }                                                               \
 186         (rdp)->sz = size;                                            \
 187         (rdp)->off = offset;                                         \
 188         (rdp)->next = NULL;                                          \
 189         (uri)->tail = rdp;                                           \
 190         (uri)->count += size;                                                \
 191 }
 192 
 193 #ifdef  __cplusplus
 194 }
 195 #endif
 196 
 197 #endif  /* _SYS_SOCKFS_NL7CURI_H */