Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_upstream_zone_module.c @ 6964:5d3d9b52327d
Fixed type.
author | hucongcong <hucong.c@foxmail.com> |
---|---|
date | Mon, 03 Apr 2017 14:29:40 +0800 |
parents | 8ed51b02f655 |
children | 1eb753aa8e5e |
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); | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
17 static ngx_stream_upstream_rr_peers_t *ngx_stream_upstream_zone_copy_peers( |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
18 ngx_slab_pool_t *shpool, 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 = { | |
6606
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6214
diff
changeset
|
35 NULL, /* preconfiguration */ |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6123
diff
changeset
|
36 NULL, /* postconfiguration */ |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6123
diff
changeset
|
37 |
6115 | 38 NULL, /* create main configuration */ |
39 NULL, /* init main configuration */ | |
40 | |
41 NULL, /* create server configuration */ | |
6629 | 42 NULL /* merge server configuration */ |
6115 | 43 }; |
44 | |
45 | |
46 ngx_module_t ngx_stream_upstream_zone_module = { | |
47 NGX_MODULE_V1, | |
48 &ngx_stream_upstream_zone_module_ctx, /* module context */ | |
49 ngx_stream_upstream_zone_commands, /* module directives */ | |
50 NGX_STREAM_MODULE, /* module type */ | |
51 NULL, /* init master */ | |
52 NULL, /* init module */ | |
53 NULL, /* init process */ | |
54 NULL, /* init thread */ | |
55 NULL, /* exit thread */ | |
56 NULL, /* exit process */ | |
57 NULL, /* exit master */ | |
58 NGX_MODULE_V1_PADDING | |
59 }; | |
60 | |
61 | |
62 static char * | |
63 ngx_stream_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
64 { | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
65 ssize_t size; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
66 ngx_str_t *value; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
67 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
|
68 ngx_stream_upstream_main_conf_t *umcf; |
6115 | 69 |
70 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
|
71 umcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_upstream_module); |
6115 | 72 |
73 value = cf->args->elts; | |
74 | |
75 if (!value[1].len) { | |
76 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
77 "invalid zone name \"%V\"", &value[1]); | |
78 return NGX_CONF_ERROR; | |
79 } | |
80 | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
81 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
|
82 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
|
83 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
84 if (size == NGX_ERROR) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
85 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
|
86 "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
|
87 return NGX_CONF_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
88 } |
6115 | 89 |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
90 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
|
91 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
|
92 "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
|
93 return NGX_CONF_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
94 } |
6115 | 95 |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
96 } else { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
97 size = 0; |
6115 | 98 } |
99 | |
100 uscf->shm_zone = ngx_shared_memory_add(cf, &value[1], size, | |
101 &ngx_stream_upstream_module); | |
102 if (uscf->shm_zone == NULL) { | |
103 return NGX_CONF_ERROR; | |
104 } | |
105 | |
106 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
|
107 uscf->shm_zone->data = umcf; |
6115 | 108 |
109 uscf->shm_zone->noreuse = 1; | |
110 | |
111 return NGX_CONF_OK; | |
112 } | |
113 | |
114 | |
115 static ngx_int_t | |
116 ngx_stream_upstream_init_zone(ngx_shm_zone_t *shm_zone, void *data) | |
117 { | |
118 size_t len; | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
119 ngx_uint_t i; |
6115 | 120 ngx_slab_pool_t *shpool; |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
121 ngx_stream_upstream_rr_peers_t *peers, **peersp; |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
122 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
|
123 ngx_stream_upstream_main_conf_t *umcf; |
6115 | 124 |
125 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
126 umcf = shm_zone->data; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
127 uscfp = umcf->upstreams.elts; |
6115 | 128 |
129 if (shm_zone->shm.exists) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
130 peers = shpool->data; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
131 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
132 for (i = 0; i < umcf->upstreams.nelts; i++) { |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
133 uscf = uscfp[i]; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
134 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
135 if (uscf->shm_zone != shm_zone) { |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
136 continue; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
137 } |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
138 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
139 uscf->peer.data = peers; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
140 peers = peers->zone_next; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
141 } |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
142 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
143 return NGX_OK; |
6115 | 144 } |
145 | |
146 len = sizeof(" in upstream zone \"\"") + shm_zone->shm.name.len; | |
147 | |
148 shpool->log_ctx = ngx_slab_alloc(shpool, len); | |
149 if (shpool->log_ctx == NULL) { | |
150 return NGX_ERROR; | |
151 } | |
152 | |
153 ngx_sprintf(shpool->log_ctx, " in upstream zone \"%V\"%Z", | |
154 &shm_zone->shm.name); | |
155 | |
6123
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 /* 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
|
158 |
6214
341e4303d25b
Fixed strict aliasing warnings with old GCC versions.
Ruslan Ermilov <ru@nginx.com>
parents:
6198
diff
changeset
|
159 peersp = (ngx_stream_upstream_rr_peers_t **) (void *) &shpool->data; |
6123
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 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
|
162 uscf = uscfp[i]; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
163 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
164 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
|
165 continue; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
166 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
167 |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
168 peers = ngx_stream_upstream_zone_copy_peers(shpool, uscf); |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
169 if (peers == NULL) { |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
170 return NGX_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
171 } |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
172 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
173 *peersp = peers; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
174 peersp = &peers->zone_next; |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
175 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
176 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
177 return NGX_OK; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
178 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
179 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
180 |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
181 static ngx_stream_upstream_rr_peers_t * |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
182 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
|
183 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
|
184 { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
185 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
|
186 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
|
187 |
6115 | 188 peers = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t)); |
189 if (peers == NULL) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
190 return NULL; |
6115 | 191 } |
192 | |
193 ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_stream_upstream_rr_peers_t)); | |
194 | |
195 peers->shpool = shpool; | |
196 | |
197 for (peerp = &peers->peer; *peerp; peerp = &peer->next) { | |
198 /* pool is unlocked */ | |
199 peer = ngx_slab_calloc_locked(shpool, | |
200 sizeof(ngx_stream_upstream_rr_peer_t)); | |
201 if (peer == NULL) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
202 return NULL; |
6115 | 203 } |
204 | |
205 ngx_memcpy(peer, *peerp, sizeof(ngx_stream_upstream_rr_peer_t)); | |
206 | |
207 *peerp = peer; | |
208 } | |
209 | |
210 if (peers->next == NULL) { | |
211 goto done; | |
212 } | |
213 | |
214 backup = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t)); | |
215 if (backup == NULL) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
216 return NULL; |
6115 | 217 } |
218 | |
219 ngx_memcpy(backup, peers->next, sizeof(ngx_stream_upstream_rr_peers_t)); | |
220 | |
221 backup->shpool = shpool; | |
222 | |
223 for (peerp = &backup->peer; *peerp; peerp = &peer->next) { | |
224 /* pool is unlocked */ | |
225 peer = ngx_slab_calloc_locked(shpool, | |
226 sizeof(ngx_stream_upstream_rr_peer_t)); | |
227 if (peer == NULL) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
228 return NULL; |
6115 | 229 } |
230 | |
231 ngx_memcpy(peer, *peerp, sizeof(ngx_stream_upstream_rr_peer_t)); | |
232 | |
233 *peerp = peer; | |
234 } | |
235 | |
236 peers->next = backup; | |
237 | |
238 done: | |
239 | |
240 uscf->peer.data = peers; | |
241 | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
242 return peers; |
6115 | 243 } |