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