Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Ruslan Ermilov
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_stream.h>
0011 
0012 
0013 static char *ngx_stream_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd,
0014     void *conf);
0015 static ngx_int_t ngx_stream_upstream_init_zone(ngx_shm_zone_t *shm_zone,
0016     void *data);
0017 static ngx_stream_upstream_rr_peers_t *ngx_stream_upstream_zone_copy_peers(
0018     ngx_slab_pool_t *shpool, ngx_stream_upstream_srv_conf_t *uscf);
0019 static ngx_stream_upstream_rr_peer_t *ngx_stream_upstream_zone_copy_peer(
0020     ngx_stream_upstream_rr_peers_t *peers, ngx_stream_upstream_rr_peer_t *src);
0021 
0022 
0023 static ngx_command_t  ngx_stream_upstream_zone_commands[] = {
0024 
0025     { ngx_string("zone"),
0026       NGX_STREAM_UPS_CONF|NGX_CONF_TAKE12,
0027       ngx_stream_upstream_zone,
0028       0,
0029       0,
0030       NULL },
0031 
0032       ngx_null_command
0033 };
0034 
0035 
0036 static ngx_stream_module_t  ngx_stream_upstream_zone_module_ctx = {
0037     NULL,                                  /* preconfiguration */
0038     NULL,                                  /* postconfiguration */
0039 
0040     NULL,                                  /* create main configuration */
0041     NULL,                                  /* init main configuration */
0042 
0043     NULL,                                  /* create server configuration */
0044     NULL                                   /* merge server configuration */
0045 };
0046 
0047 
0048 ngx_module_t  ngx_stream_upstream_zone_module = {
0049     NGX_MODULE_V1,
0050     &ngx_stream_upstream_zone_module_ctx,  /* module context */
0051     ngx_stream_upstream_zone_commands,     /* module directives */
0052     NGX_STREAM_MODULE,                     /* module type */
0053     NULL,                                  /* init master */
0054     NULL,                                  /* init module */
0055     NULL,                                  /* init process */
0056     NULL,                                  /* init thread */
0057     NULL,                                  /* exit thread */
0058     NULL,                                  /* exit process */
0059     NULL,                                  /* exit master */
0060     NGX_MODULE_V1_PADDING
0061 };
0062 
0063 
0064 static char *
0065 ngx_stream_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0066 {
0067     ssize_t                           size;
0068     ngx_str_t                        *value;
0069     ngx_stream_upstream_srv_conf_t   *uscf;
0070     ngx_stream_upstream_main_conf_t  *umcf;
0071 
0072     uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module);
0073     umcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_upstream_module);
0074 
0075     value = cf->args->elts;
0076 
0077     if (!value[1].len) {
0078         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0079                            "invalid zone name \"%V\"", &value[1]);
0080         return NGX_CONF_ERROR;
0081     }
0082 
0083     if (cf->args->nelts == 3) {
0084         size = ngx_parse_size(&value[2]);
0085 
0086         if (size == NGX_ERROR) {
0087             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0088                                "invalid zone size \"%V\"", &value[2]);
0089             return NGX_CONF_ERROR;
0090         }
0091 
0092         if (size < (ssize_t) (8 * ngx_pagesize)) {
0093             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0094                                "zone \"%V\" is too small", &value[1]);
0095             return NGX_CONF_ERROR;
0096         }
0097 
0098     } else {
0099         size = 0;
0100     }
0101 
0102     uscf->shm_zone = ngx_shared_memory_add(cf, &value[1], size,
0103                                            &ngx_stream_upstream_module);
0104     if (uscf->shm_zone == NULL) {
0105         return NGX_CONF_ERROR;
0106     }
0107 
0108     uscf->shm_zone->init = ngx_stream_upstream_init_zone;
0109     uscf->shm_zone->data = umcf;
0110 
0111     uscf->shm_zone->noreuse = 1;
0112 
0113     return NGX_CONF_OK;
0114 }
0115 
0116 
0117 static ngx_int_t
0118 ngx_stream_upstream_init_zone(ngx_shm_zone_t *shm_zone, void *data)
0119 {
0120     size_t                            len;
0121     ngx_uint_t                        i;
0122     ngx_slab_pool_t                  *shpool;
0123     ngx_stream_upstream_rr_peers_t   *peers, **peersp;
0124     ngx_stream_upstream_srv_conf_t   *uscf, **uscfp;
0125     ngx_stream_upstream_main_conf_t  *umcf;
0126 
0127     shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
0128     umcf = shm_zone->data;
0129     uscfp = umcf->upstreams.elts;
0130 
0131     if (shm_zone->shm.exists) {
0132         peers = shpool->data;
0133 
0134         for (i = 0; i < umcf->upstreams.nelts; i++) {
0135             uscf = uscfp[i];
0136 
0137             if (uscf->shm_zone != shm_zone) {
0138                 continue;
0139             }
0140 
0141             uscf->peer.data = peers;
0142             peers = peers->zone_next;
0143         }
0144 
0145         return NGX_OK;
0146     }
0147 
0148     len = sizeof(" in upstream zone \"\"") + shm_zone->shm.name.len;
0149 
0150     shpool->log_ctx = ngx_slab_alloc(shpool, len);
0151     if (shpool->log_ctx == NULL) {
0152         return NGX_ERROR;
0153     }
0154 
0155     ngx_sprintf(shpool->log_ctx, " in upstream zone \"%V\"%Z",
0156                 &shm_zone->shm.name);
0157 
0158 
0159     /* copy peers to shared memory */
0160 
0161     peersp = (ngx_stream_upstream_rr_peers_t **) (void *) &shpool->data;
0162 
0163     for (i = 0; i < umcf->upstreams.nelts; i++) {
0164         uscf = uscfp[i];
0165 
0166         if (uscf->shm_zone != shm_zone) {
0167             continue;
0168         }
0169 
0170         peers = ngx_stream_upstream_zone_copy_peers(shpool, uscf);
0171         if (peers == NULL) {
0172             return NGX_ERROR;
0173         }
0174 
0175         *peersp = peers;
0176         peersp = &peers->zone_next;
0177     }
0178 
0179     return NGX_OK;
0180 }
0181 
0182 
0183 static ngx_stream_upstream_rr_peers_t *
0184 ngx_stream_upstream_zone_copy_peers(ngx_slab_pool_t *shpool,
0185     ngx_stream_upstream_srv_conf_t *uscf)
0186 {
0187     ngx_str_t                       *name;
0188     ngx_stream_upstream_rr_peer_t   *peer, **peerp;
0189     ngx_stream_upstream_rr_peers_t  *peers, *backup;
0190 
0191     peers = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t));
0192     if (peers == NULL) {
0193         return NULL;
0194     }
0195 
0196     ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_stream_upstream_rr_peers_t));
0197 
0198     name = ngx_slab_alloc(shpool, sizeof(ngx_str_t));
0199     if (name == NULL) {
0200         return NULL;
0201     }
0202 
0203     name->data = ngx_slab_alloc(shpool, peers->name->len);
0204     if (name->data == NULL) {
0205         return NULL;
0206     }
0207 
0208     ngx_memcpy(name->data, peers->name->data, peers->name->len);
0209     name->len = peers->name->len;
0210 
0211     peers->name = name;
0212 
0213     peers->shpool = shpool;
0214 
0215     for (peerp = &peers->peer; *peerp; peerp = &peer->next) {
0216         /* pool is unlocked */
0217         peer = ngx_stream_upstream_zone_copy_peer(peers, *peerp);
0218         if (peer == NULL) {
0219             return NULL;
0220         }
0221 
0222         *peerp = peer;
0223     }
0224 
0225     if (peers->next == NULL) {
0226         goto done;
0227     }
0228 
0229     backup = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t));
0230     if (backup == NULL) {
0231         return NULL;
0232     }
0233 
0234     ngx_memcpy(backup, peers->next, sizeof(ngx_stream_upstream_rr_peers_t));
0235 
0236     backup->name = name;
0237 
0238     backup->shpool = shpool;
0239 
0240     for (peerp = &backup->peer; *peerp; peerp = &peer->next) {
0241         /* pool is unlocked */
0242         peer = ngx_stream_upstream_zone_copy_peer(backup, *peerp);
0243         if (peer == NULL) {
0244             return NULL;
0245         }
0246 
0247         *peerp = peer;
0248     }
0249 
0250     peers->next = backup;
0251 
0252 done:
0253 
0254     uscf->peer.data = peers;
0255 
0256     return peers;
0257 }
0258 
0259 
0260 static ngx_stream_upstream_rr_peer_t *
0261 ngx_stream_upstream_zone_copy_peer(ngx_stream_upstream_rr_peers_t *peers,
0262     ngx_stream_upstream_rr_peer_t *src)
0263 {
0264     ngx_slab_pool_t                *pool;
0265     ngx_stream_upstream_rr_peer_t  *dst;
0266 
0267     pool = peers->shpool;
0268 
0269     dst = ngx_slab_calloc_locked(pool, sizeof(ngx_stream_upstream_rr_peer_t));
0270     if (dst == NULL) {
0271         return NULL;
0272     }
0273 
0274     if (src) {
0275         ngx_memcpy(dst, src, sizeof(ngx_stream_upstream_rr_peer_t));
0276         dst->sockaddr = NULL;
0277         dst->name.data = NULL;
0278         dst->server.data = NULL;
0279     }
0280 
0281     dst->sockaddr = ngx_slab_calloc_locked(pool, sizeof(ngx_sockaddr_t));
0282     if (dst->sockaddr == NULL) {
0283         goto failed;
0284     }
0285 
0286     dst->name.data = ngx_slab_calloc_locked(pool, NGX_SOCKADDR_STRLEN);
0287     if (dst->name.data == NULL) {
0288         goto failed;
0289     }
0290 
0291     if (src) {
0292         ngx_memcpy(dst->sockaddr, src->sockaddr, src->socklen);
0293         ngx_memcpy(dst->name.data, src->name.data, src->name.len);
0294 
0295         dst->server.data = ngx_slab_alloc_locked(pool, src->server.len);
0296         if (dst->server.data == NULL) {
0297             goto failed;
0298         }
0299 
0300         ngx_memcpy(dst->server.data, src->server.data, src->server.len);
0301     }
0302 
0303     return dst;
0304 
0305 failed:
0306 
0307     if (dst->server.data) {
0308         ngx_slab_free_locked(pool, dst->server.data);
0309     }
0310 
0311     if (dst->name.data) {
0312         ngx_slab_free_locked(pool, dst->name.data);
0313     }
0314 
0315     if (dst->sockaddr) {
0316         ngx_slab_free_locked(pool, dst->sockaddr);
0317     }
0318 
0319     ngx_slab_free_locked(pool, dst);
0320 
0321     return NULL;
0322 }