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