]>
Commit | Line | Data |
---|---|---|
c34b65c7 | 1 | /* |
c2e3cbaf | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015 Nicira, Inc. |
c34b65c7 BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | #include "stream-provider.h" | |
c34b65c7 BP |
19 | #include <errno.h> |
20 | #include <inttypes.h> | |
b2befd5b | 21 | #include <sys/types.h> |
c34b65c7 BP |
22 | #include <netinet/in.h> |
23 | #include <poll.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include "coverage.h" | |
b302749b | 27 | #include "fatal-signal.h" |
c34b65c7 | 28 | #include "flow.h" |
36359150 | 29 | #include "jsonrpc.h" |
c34b65c7 BP |
30 | #include "openflow/nicira-ext.h" |
31 | #include "openflow/openflow.h" | |
25d436fb BW |
32 | #include "openvswitch/dynamic-string.h" |
33 | #include "openvswitch/ofp-print.h" | |
34 | #include "openvswitch/ofpbuf.h" | |
35 | #include "openvswitch/vlog.h" | |
26e1fdc4 | 36 | #include "ovs-thread.h" |
c34b65c7 | 37 | #include "packets.h" |
fd016ae3 | 38 | #include "openvswitch/poll-loop.h" |
c34b65c7 | 39 | #include "random.h" |
26e1fdc4 | 40 | #include "socket-util.h" |
c34b65c7 | 41 | #include "util.h" |
36359150 JP |
42 | |
43 | VLOG_DEFINE_THIS_MODULE(stream); | |
5136ce49 | 44 | |
d76f09ea BP |
45 | COVERAGE_DEFINE(pstream_open); |
46 | COVERAGE_DEFINE(stream_open); | |
47 | ||
c34b65c7 BP |
48 | /* State of an active stream.*/ |
49 | enum stream_state { | |
50 | SCS_CONNECTING, /* Underlying stream is not connected. */ | |
51 | SCS_CONNECTED, /* Connection established. */ | |
52 | SCS_DISCONNECTED /* Connection failed or connection closed. */ | |
53 | }; | |
54 | ||
da327b18 | 55 | static const struct stream_class *stream_classes[] = { |
c34b65c7 | 56 | &tcp_stream_class, |
6500157f | 57 | #ifndef _WIN32 |
c34b65c7 | 58 | &unix_stream_class, |
e3f512b0 GS |
59 | #else |
60 | &windows_stream_class, | |
7ff04d92 | 61 | #endif |
55709289 BP |
62 | #ifdef HAVE_OPENSSL |
63 | &ssl_stream_class, | |
64 | #endif | |
c34b65c7 BP |
65 | }; |
66 | ||
da327b18 | 67 | static const struct pstream_class *pstream_classes[] = { |
c34b65c7 | 68 | &ptcp_pstream_class, |
6500157f | 69 | #ifndef _WIN32 |
c34b65c7 | 70 | &punix_pstream_class, |
e3f512b0 GS |
71 | #else |
72 | &pwindows_pstream_class, | |
7ff04d92 | 73 | #endif |
55709289 BP |
74 | #ifdef HAVE_OPENSSL |
75 | &pssl_pstream_class, | |
76 | #endif | |
c34b65c7 BP |
77 | }; |
78 | ||
79 | /* Check the validity of the stream class structures. */ | |
80 | static void | |
81 | check_stream_classes(void) | |
82 | { | |
83 | #ifndef NDEBUG | |
84 | size_t i; | |
85 | ||
86 | for (i = 0; i < ARRAY_SIZE(stream_classes); i++) { | |
da327b18 | 87 | const struct stream_class *class = stream_classes[i]; |
cb22974d BP |
88 | ovs_assert(class->name != NULL); |
89 | ovs_assert(class->open != NULL); | |
539e96f6 BP |
90 | if (class->close || class->recv || class->send || class->run |
91 | || class->run_wait || class->wait) { | |
cb22974d BP |
92 | ovs_assert(class->close != NULL); |
93 | ovs_assert(class->recv != NULL); | |
94 | ovs_assert(class->send != NULL); | |
95 | ovs_assert(class->wait != NULL); | |
c34b65c7 BP |
96 | } else { |
97 | /* This class delegates to another one. */ | |
98 | } | |
99 | } | |
100 | ||
101 | for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) { | |
da327b18 | 102 | const struct pstream_class *class = pstream_classes[i]; |
cb22974d BP |
103 | ovs_assert(class->name != NULL); |
104 | ovs_assert(class->listen != NULL); | |
c34b65c7 | 105 | if (class->close || class->accept || class->wait) { |
cb22974d BP |
106 | ovs_assert(class->close != NULL); |
107 | ovs_assert(class->accept != NULL); | |
108 | ovs_assert(class->wait != NULL); | |
c34b65c7 BP |
109 | } else { |
110 | /* This class delegates to another one. */ | |
111 | } | |
112 | } | |
113 | #endif | |
114 | } | |
115 | ||
116 | /* Prints information on active (if 'active') and passive (if 'passive') | |
117 | * connection methods supported by the stream. */ | |
118 | void | |
9467fe62 | 119 | stream_usage(const char *name, bool active, bool passive, |
c69ee87c | 120 | bool bootstrap OVS_UNUSED) |
c34b65c7 BP |
121 | { |
122 | /* Really this should be implemented via callbacks into the stream | |
123 | * providers, but that seems too heavy-weight to bother with at the | |
124 | * moment. */ | |
125 | ||
126 | printf("\n"); | |
127 | if (active) { | |
128 | printf("Active %s connection methods:\n", name); | |
771680d9 YS |
129 | printf(" tcp:HOST:PORT " |
130 | "PORT at remote HOST\n"); | |
9467fe62 | 131 | #ifdef HAVE_OPENSSL |
771680d9 YS |
132 | printf(" ssl:HOST:PORT " |
133 | "SSL PORT at remote HOST\n"); | |
9467fe62 | 134 | #endif |
c34b65c7 BP |
135 | printf(" unix:FILE " |
136 | "Unix domain socket named FILE\n"); | |
137 | } | |
138 | ||
139 | if (passive) { | |
140 | printf("Passive %s connection methods:\n", name); | |
141 | printf(" ptcp:PORT[:IP] " | |
142 | "listen to TCP PORT on IP\n"); | |
9467fe62 BP |
143 | #ifdef HAVE_OPENSSL |
144 | printf(" pssl:PORT[:IP] " | |
145 | "listen for SSL on PORT on IP\n"); | |
146 | #endif | |
c34b65c7 BP |
147 | printf(" punix:FILE " |
148 | "listen on Unix domain socket FILE\n"); | |
149 | } | |
9467fe62 BP |
150 | |
151 | #ifdef HAVE_OPENSSL | |
152 | printf("PKI configuration (required to use SSL):\n" | |
153 | " -p, --private-key=FILE file with private key\n" | |
154 | " -c, --certificate=FILE file with certificate for private key\n" | |
155 | " -C, --ca-cert=FILE file with peer CA certificate\n"); | |
156 | if (bootstrap) { | |
157 | printf(" --bootstrap-ca-cert=FILE file with peer CA certificate " | |
158 | "to read or create\n"); | |
159 | } | |
8ec8b003 LR |
160 | printf("SSL options:\n" |
161 | " --ssl-protocols=PROTOS list of SSL protocols to enable\n" | |
162 | " --ssl-ciphers=CIPHERS list of SSL ciphers to enable\n"); | |
9467fe62 | 163 | #endif |
c34b65c7 BP |
164 | } |
165 | ||
26ad129e BP |
166 | /* Given 'name', a stream name in the form "TYPE:ARGS", stores the class |
167 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
168 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
169 | * class exists. */ | |
170 | static int | |
da327b18 | 171 | stream_lookup_class(const char *name, const struct stream_class **classp) |
c34b65c7 BP |
172 | { |
173 | size_t prefix_len; | |
174 | size_t i; | |
175 | ||
c34b65c7 BP |
176 | check_stream_classes(); |
177 | ||
26ad129e | 178 | *classp = NULL; |
c34b65c7 | 179 | prefix_len = strcspn(name, ":"); |
26ad129e | 180 | if (name[prefix_len] == '\0') { |
c34b65c7 BP |
181 | return EAFNOSUPPORT; |
182 | } | |
183 | for (i = 0; i < ARRAY_SIZE(stream_classes); i++) { | |
da327b18 | 184 | const struct stream_class *class = stream_classes[i]; |
c34b65c7 BP |
185 | if (strlen(class->name) == prefix_len |
186 | && !memcmp(class->name, name, prefix_len)) { | |
26ad129e BP |
187 | *classp = class; |
188 | return 0; | |
c34b65c7 BP |
189 | } |
190 | } | |
191 | return EAFNOSUPPORT; | |
192 | } | |
193 | ||
26ad129e BP |
194 | /* Returns 0 if 'name' is a stream name in the form "TYPE:ARGS" and TYPE is |
195 | * a supported stream type, otherwise EAFNOSUPPORT. */ | |
196 | int | |
197 | stream_verify_name(const char *name) | |
198 | { | |
da327b18 | 199 | const struct stream_class *class; |
26ad129e BP |
200 | return stream_lookup_class(name, &class); |
201 | } | |
202 | ||
203 | /* Attempts to connect a stream to a remote peer. 'name' is a connection name | |
204 | * in the form "TYPE:ARGS", where TYPE is an active stream class's name and | |
205 | * ARGS are stream class-specific. | |
206 | * | |
207 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
208 | * stores a pointer to the new connection in '*streamp', otherwise a null | |
209 | * pointer. */ | |
210 | int | |
f125905c | 211 | stream_open(const char *name, struct stream **streamp, uint8_t dscp) |
26ad129e | 212 | { |
da327b18 | 213 | const struct stream_class *class; |
26ad129e BP |
214 | struct stream *stream; |
215 | char *suffix_copy; | |
216 | int error; | |
217 | ||
218 | COVERAGE_INC(stream_open); | |
219 | ||
220 | /* Look up the class. */ | |
221 | error = stream_lookup_class(name, &class); | |
222 | if (!class) { | |
223 | goto error; | |
224 | } | |
225 | ||
226 | /* Call class's "open" function. */ | |
227 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
f125905c | 228 | error = class->open(name, suffix_copy, &stream, dscp); |
26ad129e BP |
229 | free(suffix_copy); |
230 | if (error) { | |
231 | goto error; | |
232 | } | |
233 | ||
234 | /* Success. */ | |
235 | *streamp = stream; | |
236 | return 0; | |
237 | ||
238 | error: | |
239 | *streamp = NULL; | |
240 | return error; | |
241 | } | |
242 | ||
766407ea BP |
243 | /* Blocks until a previously started stream connection attempt succeeds or |
244 | * fails. 'error' should be the value returned by stream_open() and 'streamp' | |
245 | * should point to the stream pointer set by stream_open(). Returns 0 if | |
246 | * successful, otherwise a positive errno value other than EAGAIN or | |
247 | * EINPROGRESS. If successful, leaves '*streamp' untouched; on error, closes | |
248 | * '*streamp' and sets '*streamp' to null. | |
249 | * | |
250 | * Typical usage: | |
251 | * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream); | |
252 | */ | |
c34b65c7 | 253 | int |
766407ea | 254 | stream_open_block(int error, struct stream **streamp) |
c34b65c7 | 255 | { |
766407ea | 256 | struct stream *stream = *streamp; |
c34b65c7 | 257 | |
b302749b BP |
258 | fatal_signal_run(); |
259 | ||
b0bfeb3e BP |
260 | if (!error) { |
261 | while ((error = stream_connect(stream)) == EAGAIN) { | |
262 | stream_run(stream); | |
263 | stream_run_wait(stream); | |
264 | stream_connect_wait(stream); | |
265 | poll_block(); | |
266 | } | |
cb22974d | 267 | ovs_assert(error != EINPROGRESS); |
c34b65c7 | 268 | } |
b0bfeb3e | 269 | |
c34b65c7 BP |
270 | if (error) { |
271 | stream_close(stream); | |
272 | *streamp = NULL; | |
273 | } else { | |
274 | *streamp = stream; | |
275 | } | |
276 | return error; | |
277 | } | |
278 | ||
279 | /* Closes 'stream'. */ | |
280 | void | |
281 | stream_close(struct stream *stream) | |
282 | { | |
283 | if (stream != NULL) { | |
284 | char *name = stream->name; | |
c19ae4cc | 285 | char *peer_id = stream->peer_id; |
c34b65c7 BP |
286 | (stream->class->close)(stream); |
287 | free(name); | |
c19ae4cc | 288 | free(peer_id); |
c34b65c7 BP |
289 | } |
290 | } | |
291 | ||
292 | /* Returns the name of 'stream', that is, the string passed to | |
293 | * stream_open(). */ | |
294 | const char * | |
295 | stream_get_name(const struct stream *stream) | |
296 | { | |
297 | return stream ? stream->name : "(null)"; | |
298 | } | |
299 | ||
c34b65c7 BP |
300 | static void |
301 | scs_connecting(struct stream *stream) | |
302 | { | |
303 | int retval = (stream->class->connect)(stream); | |
cb22974d | 304 | ovs_assert(retval != EINPROGRESS); |
c34b65c7 BP |
305 | if (!retval) { |
306 | stream->state = SCS_CONNECTED; | |
307 | } else if (retval != EAGAIN) { | |
308 | stream->state = SCS_DISCONNECTED; | |
309 | stream->error = retval; | |
310 | } | |
311 | } | |
312 | ||
294e9fc8 BP |
313 | /* Tries to complete the connection on 'stream'. If 'stream''s connection is |
314 | * complete, returns 0 if the connection was successful or a positive errno | |
315 | * value if it failed. If the connection is still in progress, returns | |
316 | * EAGAIN. */ | |
c34b65c7 BP |
317 | int |
318 | stream_connect(struct stream *stream) | |
319 | { | |
320 | enum stream_state last_state; | |
321 | ||
322 | do { | |
323 | last_state = stream->state; | |
324 | switch (stream->state) { | |
325 | case SCS_CONNECTING: | |
326 | scs_connecting(stream); | |
327 | break; | |
328 | ||
329 | case SCS_CONNECTED: | |
330 | return 0; | |
331 | ||
332 | case SCS_DISCONNECTED: | |
333 | return stream->error; | |
334 | ||
335 | default: | |
428b2edd | 336 | OVS_NOT_REACHED(); |
c34b65c7 BP |
337 | } |
338 | } while (stream->state != last_state); | |
339 | ||
340 | return EAGAIN; | |
341 | } | |
342 | ||
343 | /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns: | |
344 | * | |
345 | * - If successful, the number of bytes received (between 1 and 'n'). | |
346 | * | |
347 | * - On error, a negative errno value. | |
348 | * | |
349 | * - 0, if the connection has been closed in the normal fashion, or if 'n' | |
350 | * is zero. | |
351 | * | |
352 | * The recv function will not block waiting for a packet to arrive. If no | |
353 | * data have been received, it returns -EAGAIN immediately. */ | |
354 | int | |
355 | stream_recv(struct stream *stream, void *buffer, size_t n) | |
356 | { | |
357 | int retval = stream_connect(stream); | |
358 | return (retval ? -retval | |
359 | : n == 0 ? 0 | |
360 | : (stream->class->recv)(stream, buffer, n)); | |
361 | } | |
362 | ||
363 | /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns: | |
364 | * | |
365 | * - If successful, the number of bytes sent (between 1 and 'n'). 0 is | |
366 | * only a valid return value if 'n' is 0. | |
367 | * | |
368 | * - On error, a negative errno value. | |
369 | * | |
370 | * The send function will not block. If no bytes can be immediately accepted | |
371 | * for transmission, it returns -EAGAIN immediately. */ | |
372 | int | |
373 | stream_send(struct stream *stream, const void *buffer, size_t n) | |
374 | { | |
375 | int retval = stream_connect(stream); | |
376 | return (retval ? -retval | |
377 | : n == 0 ? 0 | |
378 | : (stream->class->send)(stream, buffer, n)); | |
379 | } | |
380 | ||
539e96f6 BP |
381 | /* Allows 'stream' to perform maintenance activities, such as flushing |
382 | * output buffers. */ | |
383 | void | |
384 | stream_run(struct stream *stream) | |
385 | { | |
386 | if (stream->class->run) { | |
387 | (stream->class->run)(stream); | |
388 | } | |
389 | } | |
390 | ||
391 | /* Arranges for the poll loop to wake up when 'stream' needs to perform | |
392 | * maintenance activities. */ | |
393 | void | |
394 | stream_run_wait(struct stream *stream) | |
395 | { | |
396 | if (stream->class->run_wait) { | |
397 | (stream->class->run_wait)(stream); | |
398 | } | |
399 | } | |
400 | ||
401 | /* Arranges for the poll loop to wake up when 'stream' is ready to take an | |
402 | * action of the given 'type'. */ | |
c34b65c7 BP |
403 | void |
404 | stream_wait(struct stream *stream, enum stream_wait_type wait) | |
405 | { | |
cb22974d BP |
406 | ovs_assert(wait == STREAM_CONNECT || wait == STREAM_RECV |
407 | || wait == STREAM_SEND); | |
c34b65c7 BP |
408 | |
409 | switch (stream->state) { | |
410 | case SCS_CONNECTING: | |
411 | wait = STREAM_CONNECT; | |
412 | break; | |
413 | ||
414 | case SCS_DISCONNECTED: | |
415 | poll_immediate_wake(); | |
416 | return; | |
417 | } | |
418 | (stream->class->wait)(stream, wait); | |
419 | } | |
420 | ||
421 | void | |
422 | stream_connect_wait(struct stream *stream) | |
423 | { | |
424 | stream_wait(stream, STREAM_CONNECT); | |
425 | } | |
426 | ||
427 | void | |
428 | stream_recv_wait(struct stream *stream) | |
429 | { | |
430 | stream_wait(stream, STREAM_RECV); | |
431 | } | |
432 | ||
433 | void | |
434 | stream_send_wait(struct stream *stream) | |
435 | { | |
436 | stream_wait(stream, STREAM_SEND); | |
437 | } | |
438 | ||
c19ae4cc LR |
439 | void |
440 | stream_set_peer_id(struct stream *stream, const char *peer_id) | |
441 | { | |
442 | free(stream->peer_id); | |
443 | stream->peer_id = xstrdup(peer_id); | |
444 | } | |
445 | ||
446 | const char * | |
447 | stream_get_peer_id(const struct stream *stream) | |
448 | { | |
449 | return stream->peer_id; | |
450 | } | |
451 | ||
26ad129e BP |
452 | /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class |
453 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
454 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
455 | * class exists. */ | |
456 | static int | |
da327b18 | 457 | pstream_lookup_class(const char *name, const struct pstream_class **classp) |
c34b65c7 BP |
458 | { |
459 | size_t prefix_len; | |
460 | size_t i; | |
461 | ||
462 | check_stream_classes(); | |
463 | ||
26ad129e | 464 | *classp = NULL; |
c34b65c7 | 465 | prefix_len = strcspn(name, ":"); |
26ad129e | 466 | if (name[prefix_len] == '\0') { |
c34b65c7 BP |
467 | return EAFNOSUPPORT; |
468 | } | |
469 | for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) { | |
da327b18 | 470 | const struct pstream_class *class = pstream_classes[i]; |
c34b65c7 BP |
471 | if (strlen(class->name) == prefix_len |
472 | && !memcmp(class->name, name, prefix_len)) { | |
26ad129e BP |
473 | *classp = class; |
474 | return 0; | |
c34b65c7 BP |
475 | } |
476 | } | |
477 | return EAFNOSUPPORT; | |
478 | } | |
479 | ||
26ad129e BP |
480 | /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is |
481 | * a supported pstream type, otherwise EAFNOSUPPORT. */ | |
482 | int | |
483 | pstream_verify_name(const char *name) | |
484 | { | |
da327b18 | 485 | const struct pstream_class *class; |
26ad129e BP |
486 | return pstream_lookup_class(name, &class); |
487 | } | |
488 | ||
f1936eb6 EJ |
489 | /* Returns 1 if the stream or pstream specified by 'name' needs periodic probes |
490 | * to verify connectivity. For [p]streams which need probes, it can take a | |
491 | * long time to notice the connection has been dropped. Returns 0 if the | |
492 | * stream or pstream does not need probes, and -1 if 'name' is not valid. */ | |
493 | int | |
494 | stream_or_pstream_needs_probes(const char *name) | |
495 | { | |
496 | const struct pstream_class *pclass; | |
497 | const struct stream_class *class; | |
498 | ||
499 | if (!stream_lookup_class(name, &class)) { | |
500 | return class->needs_probes; | |
501 | } else if (!pstream_lookup_class(name, &pclass)) { | |
502 | return pclass->needs_probes; | |
503 | } else { | |
504 | return -1; | |
505 | } | |
506 | } | |
507 | ||
26ad129e BP |
508 | /* Attempts to start listening for remote stream connections. 'name' is a |
509 | * connection name in the form "TYPE:ARGS", where TYPE is an passive stream | |
510 | * class's name and ARGS are stream class-specific. | |
511 | * | |
512 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
513 | * stores a pointer to the new connection in '*pstreamp', otherwise a null | |
514 | * pointer. */ | |
515 | int | |
f125905c | 516 | pstream_open(const char *name, struct pstream **pstreamp, uint8_t dscp) |
26ad129e | 517 | { |
da327b18 | 518 | const struct pstream_class *class; |
26ad129e BP |
519 | struct pstream *pstream; |
520 | char *suffix_copy; | |
521 | int error; | |
522 | ||
523 | COVERAGE_INC(pstream_open); | |
524 | ||
525 | /* Look up the class. */ | |
526 | error = pstream_lookup_class(name, &class); | |
527 | if (!class) { | |
528 | goto error; | |
529 | } | |
530 | ||
531 | /* Call class's "open" function. */ | |
532 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
f125905c | 533 | error = class->listen(name, suffix_copy, &pstream, dscp); |
26ad129e BP |
534 | free(suffix_copy); |
535 | if (error) { | |
536 | goto error; | |
537 | } | |
538 | ||
539 | /* Success. */ | |
540 | *pstreamp = pstream; | |
541 | return 0; | |
542 | ||
543 | error: | |
544 | *pstreamp = NULL; | |
545 | return error; | |
546 | } | |
547 | ||
c34b65c7 BP |
548 | /* Returns the name that was used to open 'pstream'. The caller must not |
549 | * modify or free the name. */ | |
550 | const char * | |
551 | pstream_get_name(const struct pstream *pstream) | |
552 | { | |
553 | return pstream->name; | |
554 | } | |
555 | ||
556 | /* Closes 'pstream'. */ | |
557 | void | |
558 | pstream_close(struct pstream *pstream) | |
559 | { | |
560 | if (pstream != NULL) { | |
561 | char *name = pstream->name; | |
562 | (pstream->class->close)(pstream); | |
563 | free(name); | |
564 | } | |
565 | } | |
566 | ||
567 | /* Tries to accept a new connection on 'pstream'. If successful, stores the | |
568 | * new connection in '*new_stream' and returns 0. Otherwise, returns a | |
569 | * positive errno value. | |
570 | * | |
571 | * pstream_accept() will not block waiting for a connection. If no connection | |
572 | * is ready to be accepted, it returns EAGAIN immediately. */ | |
573 | int | |
574 | pstream_accept(struct pstream *pstream, struct stream **new_stream) | |
575 | { | |
576 | int retval = (pstream->class->accept)(pstream, new_stream); | |
577 | if (retval) { | |
578 | *new_stream = NULL; | |
579 | } else { | |
cb22974d BP |
580 | ovs_assert((*new_stream)->state != SCS_CONNECTING |
581 | || (*new_stream)->class->connect); | |
c34b65c7 BP |
582 | } |
583 | return retval; | |
584 | } | |
585 | ||
8d76bcca BP |
586 | /* Tries to accept a new connection on 'pstream'. If successful, stores the |
587 | * new connection in '*new_stream' and returns 0. Otherwise, returns a | |
588 | * positive errno value. | |
589 | * | |
590 | * pstream_accept_block() blocks until a connection is ready or until an error | |
591 | * occurs. It will not return EAGAIN. */ | |
592 | int | |
593 | pstream_accept_block(struct pstream *pstream, struct stream **new_stream) | |
594 | { | |
595 | int error; | |
596 | ||
b302749b | 597 | fatal_signal_run(); |
8d76bcca BP |
598 | while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) { |
599 | pstream_wait(pstream); | |
600 | poll_block(); | |
601 | } | |
602 | if (error) { | |
603 | *new_stream = NULL; | |
604 | } | |
605 | return error; | |
606 | } | |
607 | ||
c34b65c7 BP |
608 | void |
609 | pstream_wait(struct pstream *pstream) | |
610 | { | |
611 | (pstream->class->wait)(pstream); | |
612 | } | |
f89b7ce5 | 613 | |
798e1352 BP |
614 | /* Returns the transport port on which 'pstream' is listening, or 0 if the |
615 | * concept doesn't apply. */ | |
616 | ovs_be16 | |
617 | pstream_get_bound_port(const struct pstream *pstream) | |
618 | { | |
619 | return pstream->bound_port; | |
620 | } | |
c34b65c7 BP |
621 | \f |
622 | /* Initializes 'stream' as a new stream named 'name', implemented via 'class'. | |
623 | * The initial connection status, supplied as 'connect_status', is interpreted | |
624 | * as follows: | |
625 | * | |
626 | * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be | |
627 | * called in the normal fashion. | |
628 | * | |
629 | * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect' | |
630 | * function should be called to complete the connection. | |
631 | * | |
632 | * - Other positive errno values indicate that the connection failed with | |
633 | * the specified error. | |
634 | * | |
635 | * After calling this function, stream_close() must be used to destroy | |
636 | * 'stream', otherwise resources will be leaked. | |
637 | * | |
b7636967 | 638 | * Takes ownership of 'name'. */ |
c34b65c7 | 639 | void |
da327b18 | 640 | stream_init(struct stream *stream, const struct stream_class *class, |
b7636967 | 641 | int connect_status, char *name) |
c34b65c7 | 642 | { |
c4bed75b | 643 | memset(stream, 0, sizeof *stream); |
c34b65c7 BP |
644 | stream->class = class; |
645 | stream->state = (connect_status == EAGAIN ? SCS_CONNECTING | |
646 | : !connect_status ? SCS_CONNECTED | |
647 | : SCS_DISCONNECTED); | |
648 | stream->error = connect_status; | |
b7636967 | 649 | stream->name = name; |
cb22974d | 650 | ovs_assert(stream->state != SCS_CONNECTING || class->connect); |
c34b65c7 BP |
651 | } |
652 | ||
b7636967 | 653 | /* Takes ownership of 'name'. */ |
c34b65c7 | 654 | void |
da327b18 | 655 | pstream_init(struct pstream *pstream, const struct pstream_class *class, |
b7636967 | 656 | char *name) |
c34b65c7 | 657 | { |
798e1352 | 658 | memset(pstream, 0, sizeof *pstream); |
c34b65c7 | 659 | pstream->class = class; |
b7636967 | 660 | pstream->name = name; |
c34b65c7 | 661 | } |
798e1352 BP |
662 | |
663 | void | |
664 | pstream_set_bound_port(struct pstream *pstream, ovs_be16 port) | |
665 | { | |
666 | pstream->bound_port = port; | |
667 | } | |
f39dc942 BP |
668 | \f |
669 | static int | |
670 | count_fields(const char *s_) | |
671 | { | |
672 | char *s, *field, *save_ptr; | |
673 | int n = 0; | |
674 | ||
675 | save_ptr = NULL; | |
676 | s = xstrdup(s_); | |
677 | for (field = strtok_r(s, ":", &save_ptr); field != NULL; | |
678 | field = strtok_r(NULL, ":", &save_ptr)) { | |
679 | n++; | |
680 | } | |
681 | free(s); | |
682 | ||
683 | return n; | |
684 | } | |
685 | ||
ca843648 JP |
686 | /* Like stream_open(), but the port defaults to 'default_port' if no port |
687 | * number is given. */ | |
f39dc942 | 688 | int |
ca843648 JP |
689 | stream_open_with_default_port(const char *name_, |
690 | uint16_t default_port, | |
691 | struct stream **streamp, | |
692 | uint8_t dscp) | |
f39dc942 BP |
693 | { |
694 | char *name; | |
695 | int error; | |
696 | ||
ca843648 JP |
697 | if ((!strncmp(name_, "tcp:", 4) || !strncmp(name_, "ssl:", 4)) |
698 | && count_fields(name_) < 3) { | |
d4763d1d JP |
699 | if (default_port == OFP_PORT) { |
700 | VLOG_WARN_ONCE("The default OpenFlow port number has changed " | |
701 | "from %d to %d", | |
36359150 | 702 | OFP_OLD_PORT, OFP_PORT); |
d4763d1d JP |
703 | } else if (default_port == OVSDB_PORT) { |
704 | VLOG_WARN_ONCE("The default OVSDB port number has changed " | |
705 | "from %d to %d", | |
36359150 JP |
706 | OVSDB_OLD_PORT, OVSDB_PORT); |
707 | } | |
ca843648 | 708 | name = xasprintf("%s:%d", name_, default_port); |
f39dc942 BP |
709 | } else { |
710 | name = xstrdup(name_); | |
711 | } | |
f125905c | 712 | error = stream_open(name, streamp, dscp); |
f39dc942 BP |
713 | free(name); |
714 | ||
715 | return error; | |
716 | } | |
717 | ||
ca843648 JP |
718 | /* Like pstream_open(), but port defaults to 'default_port' if no port |
719 | * number is given. */ | |
f39dc942 | 720 | int |
ca843648 JP |
721 | pstream_open_with_default_port(const char *name_, |
722 | uint16_t default_port, | |
723 | struct pstream **pstreamp, | |
724 | uint8_t dscp) | |
f39dc942 BP |
725 | { |
726 | char *name; | |
727 | int error; | |
728 | ||
ca843648 JP |
729 | if ((!strncmp(name_, "ptcp:", 5) || !strncmp(name_, "pssl:", 5)) |
730 | && count_fields(name_) < 2) { | |
731 | name = xasprintf("%s%d", name_, default_port); | |
f39dc942 BP |
732 | } else { |
733 | name = xstrdup(name_); | |
734 | } | |
f125905c | 735 | error = pstream_open(name, pstreamp, dscp); |
f39dc942 BP |
736 | free(name); |
737 | ||
738 | return error; | |
739 | } | |
ac4c900d AA |
740 | |
741 | /* | |
742 | * This function extracts IP address and port from the target string. | |
743 | * | |
e731d71b | 744 | * - On success, function returns true and fills *ss structure with port |
ac4c900d AA |
745 | * and IP address. If port was absent in target string then it will use |
746 | * corresponding default port value. | |
e731d71b | 747 | * - On error, function returns false and *ss contains garbage. |
ac4c900d AA |
748 | */ |
749 | bool | |
1bb01121 | 750 | stream_parse_target_with_default_port(const char *target, int default_port, |
e731d71b | 751 | struct sockaddr_storage *ss) |
ca843648 JP |
752 | { |
753 | return ((!strncmp(target, "tcp:", 4) || !strncmp(target, "ssl:", 4)) | |
e731d71b | 754 | && inet_parse_active(target + 4, default_port, ss)); |
ac4c900d AA |
755 | } |
756 | ||
1e3c0047 BP |
757 | /* Attempts to guess the content type of a stream whose first few bytes were |
758 | * the 'size' bytes of 'data'. */ | |
759 | static enum stream_content_type | |
c55acc2e | 760 | stream_guess_content(const uint8_t *data, ssize_t size) |
1e3c0047 BP |
761 | { |
762 | if (size >= 2) { | |
763 | #define PAIR(A, B) (((A) << 8) | (B)) | |
764 | switch (PAIR(data[0], data[1])) { | |
765 | case PAIR(0x16, 0x03): /* Handshake, version 3. */ | |
766 | return STREAM_SSL; | |
767 | case PAIR('{', '"'): | |
768 | return STREAM_JSONRPC; | |
982697a4 | 769 | case PAIR(OFP10_VERSION, 0 /* OFPT_HELLO */): |
1e3c0047 BP |
770 | return STREAM_OPENFLOW; |
771 | } | |
772 | } | |
773 | ||
774 | return STREAM_UNKNOWN; | |
775 | } | |
776 | ||
777 | /* Returns a string represenation of 'type'. */ | |
778 | static const char * | |
779 | stream_content_type_to_string(enum stream_content_type type) | |
780 | { | |
781 | switch (type) { | |
782 | case STREAM_UNKNOWN: | |
783 | default: | |
784 | return "unknown"; | |
f39dc942 | 785 | |
1e3c0047 BP |
786 | case STREAM_JSONRPC: |
787 | return "JSON-RPC"; | |
f39dc942 | 788 | |
1e3c0047 BP |
789 | case STREAM_OPENFLOW: |
790 | return "OpenFlow"; | |
791 | ||
792 | case STREAM_SSL: | |
793 | return "SSL"; | |
794 | } | |
795 | } | |
796 | ||
797 | /* Attempts to guess the content type of a stream whose first few bytes were | |
798 | * the 'size' bytes of 'data'. If this is done successfully, and the guessed | |
799 | * content type is other than 'expected_type', then log a message in vlog | |
800 | * module 'module', naming 'stream_name' as the source, explaining what | |
801 | * content was expected and what was actually received. */ | |
802 | void | |
c55acc2e | 803 | stream_report_content(const void *data, ssize_t size, |
1e3c0047 | 804 | enum stream_content_type expected_type, |
480ce8ab | 805 | struct vlog_module *module, const char *stream_name) |
1e3c0047 BP |
806 | { |
807 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
808 | enum stream_content_type actual_type; | |
809 | ||
810 | actual_type = stream_guess_content(data, size); | |
811 | if (actual_type != expected_type && actual_type != STREAM_UNKNOWN) { | |
812 | vlog_rate_limit(module, VLL_WARN, &rl, | |
813 | "%s: received %s data on %s channel", | |
814 | stream_name, | |
c662c789 BP |
815 | stream_content_type_to_string(actual_type), |
816 | stream_content_type_to_string(expected_type)); | |
1e3c0047 BP |
817 | } |
818 | } |