Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_upstream_zone_module.c @ 6182:07e416ece597
Updated OpenSSL used for win32 builds.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 15 Jun 2015 20:20:12 +0300 |
parents | 68c106e6fa0a |
children | 311d232ad803 |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Ruslan Ermilov | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 static char *ngx_stream_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, | |
14 void *conf); | |
15 static ngx_int_t ngx_stream_upstream_init_zone(ngx_shm_zone_t *shm_zone, | |
16 void *data); | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
17 static ngx_int_t ngx_stream_upstream_zone_copy_peers(ngx_slab_pool_t *shpool, |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
18 ngx_stream_upstream_srv_conf_t *uscf); |
6115 | 19 |
20 | |
21 static ngx_command_t ngx_stream_upstream_zone_commands[] = { | |
22 | |
23 { ngx_string("zone"), | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
24 NGX_STREAM_UPS_CONF|NGX_CONF_TAKE12, |
6115 | 25 ngx_stream_upstream_zone, |
26 0, | |
27 0, | |
28 NULL }, | |
29 | |
30 ngx_null_command | |
31 }; | |
32 | |
33 | |
34 static ngx_stream_module_t ngx_stream_upstream_zone_module_ctx = { | |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6123
diff
changeset
|
35 NULL, /* postconfiguration */ |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6123
diff
changeset
|
36 |
6115 | 37 NULL, /* create main configuration */ |
38 NULL, /* init main configuration */ | |
39 | |
40 NULL, /* create server configuration */ | |
41 NULL, /* merge server configuration */ | |
42 }; | |
43 | |
44 | |
45 ngx_module_t ngx_stream_upstream_zone_module = { | |
46 NGX_MODULE_V1, | |
47 &ngx_stream_upstream_zone_module_ctx, /* module context */ | |
48 ngx_stream_upstream_zone_commands, /* module directives */ | |
49 NGX_STREAM_MODULE, /* module type */ | |
50 NULL, /* init master */ | |
51 NULL, /* init module */ | |
52 NULL, /* init process */ | |
53 NULL, /* init thread */ | |
54 NULL, /* exit thread */ | |
55 NULL, /* exit process */ | |
56 NULL, /* exit master */ | |
57 NGX_MODULE_V1_PADDING | |
58 }; | |
59 | |
60 | |
61 static char * | |
62 ngx_stream_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
63 { | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
64 ssize_t size; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
65 ngx_str_t *value; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
66 ngx_stream_upstream_srv_conf_t *uscf; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
67 ngx_stream_upstream_main_conf_t *umcf; |
6115 | 68 |
69 uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module); | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
70 umcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_upstream_module); |
6115 | 71 |
72 value = cf->args->elts; | |
73 | |
74 if (!value[1].len) { | |
75 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
76 "invalid zone name \"%V\"", &value[1]); | |
77 return NGX_CONF_ERROR; | |
78 } | |
79 | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
80 if (cf->args->nelts == 3) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
81 size = ngx_parse_size(&value[2]); |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
82 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
83 if (size == NGX_ERROR) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
84 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
85 "invalid zone size \"%V\"", &value[2]); |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
86 return NGX_CONF_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
87 } |
6115 | 88 |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
89 if (size < (ssize_t) (8 * ngx_pagesize)) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
90 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
91 "zone \"%V\" is too small", &value[1]); |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
92 return NGX_CONF_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
93 } |
6115 | 94 |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
95 } else { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
96 size = 0; |
6115 | 97 } |
98 | |
99 uscf->shm_zone = ngx_shared_memory_add(cf, &value[1], size, | |
100 &ngx_stream_upstream_module); | |
101 if (uscf->shm_zone == NULL) { | |
102 return NGX_CONF_ERROR; | |
103 } | |
104 | |
105 uscf->shm_zone->init = ngx_stream_upstream_init_zone; | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
106 uscf->shm_zone->data = umcf; |
6115 | 107 |
108 uscf->shm_zone->noreuse = 1; | |
109 | |
110 return NGX_CONF_OK; | |
111 } | |
112 | |
113 | |
114 static ngx_int_t | |
115 ngx_stream_upstream_init_zone(ngx_shm_zone_t *shm_zone, void *data) | |
116 { | |
117 size_t len; | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
118 ngx_uint_t i; |
6115 | 119 ngx_slab_pool_t *shpool; |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
120 ngx_stream_upstream_srv_conf_t *uscf, **uscfp; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
121 ngx_stream_upstream_main_conf_t *umcf; |
6115 | 122 |
123 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; | |
124 | |
125 if (shm_zone->shm.exists) { | |
126 return NGX_ERROR; | |
127 } | |
128 | |
129 len = sizeof(" in upstream zone \"\"") + shm_zone->shm.name.len; | |
130 | |
131 shpool->log_ctx = ngx_slab_alloc(shpool, len); | |
132 if (shpool->log_ctx == NULL) { | |
133 return NGX_ERROR; | |
134 } | |
135 | |
136 ngx_sprintf(shpool->log_ctx, " in upstream zone \"%V\"%Z", | |
137 &shm_zone->shm.name); | |
138 | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
139 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
140 /* copy peers to shared memory */ |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
141 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
142 umcf = shm_zone->data; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
143 uscfp = umcf->upstreams.elts; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
144 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
145 for (i = 0; i < umcf->upstreams.nelts; i++) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
146 uscf = uscfp[i]; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
147 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
148 if (uscf->shm_zone != shm_zone) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
149 continue; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
150 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
151 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
152 if (ngx_stream_upstream_zone_copy_peers(shpool, uscf) != NGX_OK) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
153 return NGX_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
154 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
155 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
156 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
157 return NGX_OK; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
158 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
159 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
160 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
161 static ngx_int_t |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
162 ngx_stream_upstream_zone_copy_peers(ngx_slab_pool_t *shpool, |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
163 ngx_stream_upstream_srv_conf_t *uscf) |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
164 { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
165 ngx_stream_upstream_rr_peer_t *peer, **peerp; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
166 ngx_stream_upstream_rr_peers_t *peers, *backup; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
167 |
6115 | 168 peers = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t)); |
169 if (peers == NULL) { | |
170 return NGX_ERROR; | |
171 } | |
172 | |
173 ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_stream_upstream_rr_peers_t)); | |
174 | |
175 peers->shpool = shpool; | |
176 | |
177 for (peerp = &peers->peer; *peerp; peerp = &peer->next) { | |
178 /* pool is unlocked */ | |
179 peer = ngx_slab_calloc_locked(shpool, | |
180 sizeof(ngx_stream_upstream_rr_peer_t)); | |
181 if (peer == NULL) { | |
182 return NGX_ERROR; | |
183 } | |
184 | |
185 ngx_memcpy(peer, *peerp, sizeof(ngx_stream_upstream_rr_peer_t)); | |
186 | |
187 *peerp = peer; | |
188 } | |
189 | |
190 if (peers->next == NULL) { | |
191 goto done; | |
192 } | |
193 | |
194 backup = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t)); | |
195 if (backup == NULL) { | |
196 return NGX_ERROR; | |
197 } | |
198 | |
199 ngx_memcpy(backup, peers->next, sizeof(ngx_stream_upstream_rr_peers_t)); | |
200 | |
201 backup->shpool = shpool; | |
202 | |
203 for (peerp = &backup->peer; *peerp; peerp = &peer->next) { | |
204 /* pool is unlocked */ | |
205 peer = ngx_slab_calloc_locked(shpool, | |
206 sizeof(ngx_stream_upstream_rr_peer_t)); | |
207 if (peer == NULL) { | |
208 return NGX_ERROR; | |
209 } | |
210 | |
211 ngx_memcpy(peer, *peerp, sizeof(ngx_stream_upstream_rr_peer_t)); | |
212 | |
213 *peerp = peer; | |
214 } | |
215 | |
216 peers->next = backup; | |
217 | |
218 done: | |
219 | |
220 uscf->peer.data = peers; | |
221 | |
222 return NGX_OK; | |
223 } |