]>
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 | 36 | #include "packets.h" |
fd016ae3 | 37 | #include "openvswitch/poll-loop.h" |
c34b65c7 | 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 | } | |
8ec8b003 LR |
159 | printf("SSL options:\n" |
160 | " --ssl-protocols=PROTOS list of SSL protocols to enable\n" | |
161 | " --ssl-ciphers=CIPHERS list of SSL ciphers to enable\n"); | |
9467fe62 | 162 | #endif |
c34b65c7 BP |
163 | } |
164 | ||
26ad129e BP |
165 | /* Given 'name', a stream name in the form "TYPE:ARGS", stores the class |
166 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
167 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
168 | * class exists. */ | |
169 | static int | |
da327b18 | 170 | stream_lookup_class(const char *name, const struct stream_class **classp) |
c34b65c7 BP |
171 | { |
172 | size_t prefix_len; | |
173 | size_t i; | |
174 | ||
c34b65c7 BP |
175 | check_stream_classes(); |
176 | ||
26ad129e | 177 | *classp = NULL; |
c34b65c7 | 178 | prefix_len = strcspn(name, ":"); |
26ad129e | 179 | if (name[prefix_len] == '\0') { |
c34b65c7 BP |
180 | return EAFNOSUPPORT; |
181 | } | |
182 | for (i = 0; i < ARRAY_SIZE(stream_classes); i++) { | |
da327b18 | 183 | const struct stream_class *class = stream_classes[i]; |
c34b65c7 BP |
184 | if (strlen(class->name) == prefix_len |
185 | && !memcmp(class->name, name, prefix_len)) { | |
26ad129e BP |
186 | *classp = class; |
187 | return 0; | |
c34b65c7 BP |
188 | } |
189 | } | |
190 | return EAFNOSUPPORT; | |
191 | } | |
192 | ||
26ad129e BP |
193 | /* Returns 0 if 'name' is a stream name in the form "TYPE:ARGS" and TYPE is |
194 | * a supported stream type, otherwise EAFNOSUPPORT. */ | |
195 | int | |
196 | stream_verify_name(const char *name) | |
197 | { | |
da327b18 | 198 | const struct stream_class *class; |
26ad129e BP |
199 | return stream_lookup_class(name, &class); |
200 | } | |
201 | ||
202 | /* Attempts to connect a stream to a remote peer. 'name' is a connection name | |
203 | * in the form "TYPE:ARGS", where TYPE is an active stream class's name and | |
204 | * ARGS are stream class-specific. | |
205 | * | |
206 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
207 | * stores a pointer to the new connection in '*streamp', otherwise a null | |
208 | * pointer. */ | |
209 | int | |
f125905c | 210 | stream_open(const char *name, struct stream **streamp, uint8_t dscp) |
26ad129e | 211 | { |
da327b18 | 212 | const struct stream_class *class; |
26ad129e BP |
213 | struct stream *stream; |
214 | char *suffix_copy; | |
215 | int error; | |
216 | ||
217 | COVERAGE_INC(stream_open); | |
218 | ||
219 | /* Look up the class. */ | |
220 | error = stream_lookup_class(name, &class); | |
221 | if (!class) { | |
222 | goto error; | |
223 | } | |
224 | ||
225 | /* Call class's "open" function. */ | |
226 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
f125905c | 227 | error = class->open(name, suffix_copy, &stream, dscp); |
26ad129e BP |
228 | free(suffix_copy); |
229 | if (error) { | |
230 | goto error; | |
231 | } | |
232 | ||
233 | /* Success. */ | |
234 | *streamp = stream; | |
235 | return 0; | |
236 | ||
237 | error: | |
238 | *streamp = NULL; | |
239 | return error; | |
240 | } | |
241 | ||
766407ea BP |
242 | /* Blocks until a previously started stream connection attempt succeeds or |
243 | * fails. 'error' should be the value returned by stream_open() and 'streamp' | |
244 | * should point to the stream pointer set by stream_open(). Returns 0 if | |
245 | * successful, otherwise a positive errno value other than EAGAIN or | |
246 | * EINPROGRESS. If successful, leaves '*streamp' untouched; on error, closes | |
247 | * '*streamp' and sets '*streamp' to null. | |
248 | * | |
249 | * Typical usage: | |
250 | * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream); | |
251 | */ | |
c34b65c7 | 252 | int |
766407ea | 253 | stream_open_block(int error, struct stream **streamp) |
c34b65c7 | 254 | { |
766407ea | 255 | struct stream *stream = *streamp; |
c34b65c7 | 256 | |
b302749b BP |
257 | fatal_signal_run(); |
258 | ||
b0bfeb3e BP |
259 | if (!error) { |
260 | while ((error = stream_connect(stream)) == EAGAIN) { | |
261 | stream_run(stream); | |
262 | stream_run_wait(stream); | |
263 | stream_connect_wait(stream); | |
264 | poll_block(); | |
265 | } | |
cb22974d | 266 | ovs_assert(error != EINPROGRESS); |
c34b65c7 | 267 | } |
b0bfeb3e | 268 | |
c34b65c7 BP |
269 | if (error) { |
270 | stream_close(stream); | |
271 | *streamp = NULL; | |
272 | } else { | |
273 | *streamp = stream; | |
274 | } | |
275 | return error; | |
276 | } | |
277 | ||
278 | /* Closes 'stream'. */ | |
279 | void | |
280 | stream_close(struct stream *stream) | |
281 | { | |
282 | if (stream != NULL) { | |
283 | char *name = stream->name; | |
c19ae4cc | 284 | char *peer_id = stream->peer_id; |
c34b65c7 BP |
285 | (stream->class->close)(stream); |
286 | free(name); | |
c19ae4cc | 287 | free(peer_id); |
c34b65c7 BP |
288 | } |
289 | } | |
290 | ||
291 | /* Returns the name of 'stream', that is, the string passed to | |
292 | * stream_open(). */ | |
293 | const char * | |
294 | stream_get_name(const struct stream *stream) | |
295 | { | |
296 | return stream ? stream->name : "(null)"; | |
297 | } | |
298 | ||
c34b65c7 BP |
299 | static void |
300 | scs_connecting(struct stream *stream) | |
301 | { | |
302 | int retval = (stream->class->connect)(stream); | |
cb22974d | 303 | ovs_assert(retval != EINPROGRESS); |
c34b65c7 BP |
304 | if (!retval) { |
305 | stream->state = SCS_CONNECTED; | |
306 | } else if (retval != EAGAIN) { | |
307 | stream->state = SCS_DISCONNECTED; | |
308 | stream->error = retval; | |
309 | } | |
310 | } | |
311 | ||
294e9fc8 BP |
312 | /* Tries to complete the connection on 'stream'. If 'stream''s connection is |
313 | * complete, returns 0 if the connection was successful or a positive errno | |
314 | * value if it failed. If the connection is still in progress, returns | |
315 | * EAGAIN. */ | |
c34b65c7 BP |
316 | int |
317 | stream_connect(struct stream *stream) | |
318 | { | |
319 | enum stream_state last_state; | |
320 | ||
321 | do { | |
322 | last_state = stream->state; | |
323 | switch (stream->state) { | |
324 | case SCS_CONNECTING: | |
325 | scs_connecting(stream); | |
326 | break; | |
327 | ||
328 | case SCS_CONNECTED: | |
329 | return 0; | |
330 | ||
331 | case SCS_DISCONNECTED: | |
332 | return stream->error; | |
333 | ||
334 | default: | |
428b2edd | 335 | OVS_NOT_REACHED(); |
c34b65c7 BP |
336 | } |
337 | } while (stream->state != last_state); | |
338 | ||
339 | return EAGAIN; | |
340 | } | |
341 | ||
342 | /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns: | |
343 | * | |
344 | * - If successful, the number of bytes received (between 1 and 'n'). | |
345 | * | |
346 | * - On error, a negative errno value. | |
347 | * | |
348 | * - 0, if the connection has been closed in the normal fashion, or if 'n' | |
349 | * is zero. | |
350 | * | |
351 | * The recv function will not block waiting for a packet to arrive. If no | |
352 | * data have been received, it returns -EAGAIN immediately. */ | |
353 | int | |
354 | stream_recv(struct stream *stream, void *buffer, size_t n) | |
355 | { | |
356 | int retval = stream_connect(stream); | |
357 | return (retval ? -retval | |
358 | : n == 0 ? 0 | |
359 | : (stream->class->recv)(stream, buffer, n)); | |
360 | } | |
361 | ||
362 | /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns: | |
363 | * | |
364 | * - If successful, the number of bytes sent (between 1 and 'n'). 0 is | |
365 | * only a valid return value if 'n' is 0. | |
366 | * | |
367 | * - On error, a negative errno value. | |
368 | * | |
369 | * The send function will not block. If no bytes can be immediately accepted | |
370 | * for transmission, it returns -EAGAIN immediately. */ | |
371 | int | |
372 | stream_send(struct stream *stream, const void *buffer, size_t n) | |
373 | { | |
374 | int retval = stream_connect(stream); | |
375 | return (retval ? -retval | |
376 | : n == 0 ? 0 | |
377 | : (stream->class->send)(stream, buffer, n)); | |
378 | } | |
379 | ||
539e96f6 BP |
380 | /* Allows 'stream' to perform maintenance activities, such as flushing |
381 | * output buffers. */ | |
382 | void | |
383 | stream_run(struct stream *stream) | |
384 | { | |
385 | if (stream->class->run) { | |
386 | (stream->class->run)(stream); | |
387 | } | |
388 | } | |
389 | ||
390 | /* Arranges for the poll loop to wake up when 'stream' needs to perform | |
391 | * maintenance activities. */ | |
392 | void | |
393 | stream_run_wait(struct stream *stream) | |
394 | { | |
395 | if (stream->class->run_wait) { | |
396 | (stream->class->run_wait)(stream); | |
397 | } | |
398 | } | |
399 | ||
400 | /* Arranges for the poll loop to wake up when 'stream' is ready to take an | |
401 | * action of the given 'type'. */ | |
c34b65c7 BP |
402 | void |
403 | stream_wait(struct stream *stream, enum stream_wait_type wait) | |
404 | { | |
cb22974d BP |
405 | ovs_assert(wait == STREAM_CONNECT || wait == STREAM_RECV |
406 | || wait == STREAM_SEND); | |
c34b65c7 BP |
407 | |
408 | switch (stream->state) { | |
409 | case SCS_CONNECTING: | |
410 | wait = STREAM_CONNECT; | |
411 | break; | |
412 | ||
413 | case SCS_DISCONNECTED: | |
414 | poll_immediate_wake(); | |
415 | return; | |
416 | } | |
417 | (stream->class->wait)(stream, wait); | |
418 | } | |
419 | ||
420 | void | |
421 | stream_connect_wait(struct stream *stream) | |
422 | { | |
423 | stream_wait(stream, STREAM_CONNECT); | |
424 | } | |
425 | ||
426 | void | |
427 | stream_recv_wait(struct stream *stream) | |
428 | { | |
429 | stream_wait(stream, STREAM_RECV); | |
430 | } | |
431 | ||
432 | void | |
433 | stream_send_wait(struct stream *stream) | |
434 | { | |
435 | stream_wait(stream, STREAM_SEND); | |
436 | } | |
437 | ||
c19ae4cc LR |
438 | void |
439 | stream_set_peer_id(struct stream *stream, const char *peer_id) | |
440 | { | |
441 | free(stream->peer_id); | |
442 | stream->peer_id = xstrdup(peer_id); | |
443 | } | |
444 | ||
445 | const char * | |
446 | stream_get_peer_id(const struct stream *stream) | |
447 | { | |
448 | return stream->peer_id; | |
449 | } | |
450 | ||
26ad129e BP |
451 | /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class |
452 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
453 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
454 | * class exists. */ | |
455 | static int | |
da327b18 | 456 | pstream_lookup_class(const char *name, const struct pstream_class **classp) |
c34b65c7 BP |
457 | { |
458 | size_t prefix_len; | |
459 | size_t i; | |
460 | ||
461 | check_stream_classes(); | |
462 | ||
26ad129e | 463 | *classp = NULL; |
c34b65c7 | 464 | prefix_len = strcspn(name, ":"); |
26ad129e | 465 | if (name[prefix_len] == '\0') { |
c34b65c7 BP |
466 | return EAFNOSUPPORT; |
467 | } | |
468 | for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) { | |
da327b18 | 469 | const struct pstream_class *class = pstream_classes[i]; |
c34b65c7 BP |
470 | if (strlen(class->name) == prefix_len |
471 | && !memcmp(class->name, name, prefix_len)) { | |
26ad129e BP |
472 | *classp = class; |
473 | return 0; | |
c34b65c7 BP |
474 | } |
475 | } | |
476 | return EAFNOSUPPORT; | |
477 | } | |
478 | ||
26ad129e BP |
479 | /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is |
480 | * a supported pstream type, otherwise EAFNOSUPPORT. */ | |
481 | int | |
482 | pstream_verify_name(const char *name) | |
483 | { | |
da327b18 | 484 | const struct pstream_class *class; |
26ad129e BP |
485 | return pstream_lookup_class(name, &class); |
486 | } | |
487 | ||
f1936eb6 EJ |
488 | /* Returns 1 if the stream or pstream specified by 'name' needs periodic probes |
489 | * to verify connectivity. For [p]streams which need probes, it can take a | |
490 | * long time to notice the connection has been dropped. Returns 0 if the | |
491 | * stream or pstream does not need probes, and -1 if 'name' is not valid. */ | |
492 | int | |
493 | stream_or_pstream_needs_probes(const char *name) | |
494 | { | |
495 | const struct pstream_class *pclass; | |
496 | const struct stream_class *class; | |
497 | ||
498 | if (!stream_lookup_class(name, &class)) { | |
499 | return class->needs_probes; | |
500 | } else if (!pstream_lookup_class(name, &pclass)) { | |
501 | return pclass->needs_probes; | |
502 | } else { | |
503 | return -1; | |
504 | } | |
505 | } | |
506 | ||
26ad129e BP |
507 | /* Attempts to start listening for remote stream connections. 'name' is a |
508 | * connection name in the form "TYPE:ARGS", where TYPE is an passive stream | |
509 | * class's name and ARGS are stream class-specific. | |
510 | * | |
511 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
512 | * stores a pointer to the new connection in '*pstreamp', otherwise a null | |
513 | * pointer. */ | |
514 | int | |
f125905c | 515 | pstream_open(const char *name, struct pstream **pstreamp, uint8_t dscp) |
26ad129e | 516 | { |
da327b18 | 517 | const struct pstream_class *class; |
26ad129e BP |
518 | struct pstream *pstream; |
519 | char *suffix_copy; | |
520 | int error; | |
521 | ||
522 | COVERAGE_INC(pstream_open); | |
523 | ||
524 | /* Look up the class. */ | |
525 | error = pstream_lookup_class(name, &class); | |
526 | if (!class) { | |
527 | goto error; | |
528 | } | |
529 | ||
530 | /* Call class's "open" function. */ | |
531 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
f125905c | 532 | error = class->listen(name, suffix_copy, &pstream, dscp); |
26ad129e BP |
533 | free(suffix_copy); |
534 | if (error) { | |
535 | goto error; | |
536 | } | |
537 | ||
538 | /* Success. */ | |
539 | *pstreamp = pstream; | |
540 | return 0; | |
541 | ||
542 | error: | |
543 | *pstreamp = NULL; | |
544 | return error; | |
545 | } | |
546 | ||
c34b65c7 BP |
547 | /* Returns the name that was used to open 'pstream'. The caller must not |
548 | * modify or free the name. */ | |
549 | const char * | |
550 | pstream_get_name(const struct pstream *pstream) | |
551 | { | |
552 | return pstream->name; | |
553 | } | |
554 | ||
555 | /* Closes 'pstream'. */ | |
556 | void | |
557 | pstream_close(struct pstream *pstream) | |
558 | { | |
559 | if (pstream != NULL) { | |
560 | char *name = pstream->name; | |
561 | (pstream->class->close)(pstream); | |
562 | free(name); | |
563 | } | |
564 | } | |
565 | ||
566 | /* Tries to accept a new connection on 'pstream'. If successful, stores the | |
567 | * new connection in '*new_stream' and returns 0. Otherwise, returns a | |
568 | * positive errno value. | |
569 | * | |
570 | * pstream_accept() will not block waiting for a connection. If no connection | |
571 | * is ready to be accepted, it returns EAGAIN immediately. */ | |
572 | int | |
573 | pstream_accept(struct pstream *pstream, struct stream **new_stream) | |
574 | { | |
575 | int retval = (pstream->class->accept)(pstream, new_stream); | |
576 | if (retval) { | |
577 | *new_stream = NULL; | |
578 | } else { | |
cb22974d BP |
579 | ovs_assert((*new_stream)->state != SCS_CONNECTING |
580 | || (*new_stream)->class->connect); | |
c34b65c7 BP |
581 | } |
582 | return retval; | |
583 | } | |
584 | ||
8d76bcca BP |
585 | /* Tries to accept a new connection on 'pstream'. If successful, stores the |
586 | * new connection in '*new_stream' and returns 0. Otherwise, returns a | |
587 | * positive errno value. | |
588 | * | |
589 | * pstream_accept_block() blocks until a connection is ready or until an error | |
590 | * occurs. It will not return EAGAIN. */ | |
591 | int | |
592 | pstream_accept_block(struct pstream *pstream, struct stream **new_stream) | |
593 | { | |
594 | int error; | |
595 | ||
b302749b | 596 | fatal_signal_run(); |
8d76bcca BP |
597 | while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) { |
598 | pstream_wait(pstream); | |
599 | poll_block(); | |
600 | } | |
601 | if (error) { | |
602 | *new_stream = NULL; | |
603 | } | |
604 | return error; | |
605 | } | |
606 | ||
c34b65c7 BP |
607 | void |
608 | pstream_wait(struct pstream *pstream) | |
609 | { | |
610 | (pstream->class->wait)(pstream); | |
611 | } | |
f89b7ce5 | 612 | |
798e1352 BP |
613 | /* Returns the transport port on which 'pstream' is listening, or 0 if the |
614 | * concept doesn't apply. */ | |
615 | ovs_be16 | |
616 | pstream_get_bound_port(const struct pstream *pstream) | |
617 | { | |
618 | return pstream->bound_port; | |
619 | } | |
c34b65c7 BP |
620 | \f |
621 | /* Initializes 'stream' as a new stream named 'name', implemented via 'class'. | |
622 | * The initial connection status, supplied as 'connect_status', is interpreted | |
623 | * as follows: | |
624 | * | |
625 | * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be | |
626 | * called in the normal fashion. | |
627 | * | |
628 | * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect' | |
629 | * function should be called to complete the connection. | |
630 | * | |
631 | * - Other positive errno values indicate that the connection failed with | |
632 | * the specified error. | |
633 | * | |
634 | * After calling this function, stream_close() must be used to destroy | |
635 | * 'stream', otherwise resources will be leaked. | |
636 | * | |
b7636967 | 637 | * Takes ownership of 'name'. */ |
c34b65c7 | 638 | void |
da327b18 | 639 | stream_init(struct stream *stream, const struct stream_class *class, |
b7636967 | 640 | int connect_status, char *name) |
c34b65c7 | 641 | { |
c4bed75b | 642 | memset(stream, 0, sizeof *stream); |
c34b65c7 BP |
643 | stream->class = class; |
644 | stream->state = (connect_status == EAGAIN ? SCS_CONNECTING | |
645 | : !connect_status ? SCS_CONNECTED | |
646 | : SCS_DISCONNECTED); | |
647 | stream->error = connect_status; | |
b7636967 | 648 | stream->name = name; |
cb22974d | 649 | ovs_assert(stream->state != SCS_CONNECTING || class->connect); |
c34b65c7 BP |
650 | } |
651 | ||
b7636967 | 652 | /* Takes ownership of 'name'. */ |
c34b65c7 | 653 | void |
da327b18 | 654 | pstream_init(struct pstream *pstream, const struct pstream_class *class, |
b7636967 | 655 | char *name) |
c34b65c7 | 656 | { |
798e1352 | 657 | memset(pstream, 0, sizeof *pstream); |
c34b65c7 | 658 | pstream->class = class; |
b7636967 | 659 | pstream->name = name; |
c34b65c7 | 660 | } |
798e1352 BP |
661 | |
662 | void | |
663 | pstream_set_bound_port(struct pstream *pstream, ovs_be16 port) | |
664 | { | |
665 | pstream->bound_port = port; | |
666 | } | |
f39dc942 BP |
667 | \f |
668 | static int | |
669 | count_fields(const char *s_) | |
670 | { | |
671 | char *s, *field, *save_ptr; | |
672 | int n = 0; | |
673 | ||
674 | save_ptr = NULL; | |
675 | s = xstrdup(s_); | |
676 | for (field = strtok_r(s, ":", &save_ptr); field != NULL; | |
677 | field = strtok_r(NULL, ":", &save_ptr)) { | |
678 | n++; | |
679 | } | |
680 | free(s); | |
681 | ||
682 | return n; | |
683 | } | |
684 | ||
ca843648 JP |
685 | /* Like stream_open(), but the port defaults to 'default_port' if no port |
686 | * number is given. */ | |
f39dc942 | 687 | int |
ca843648 JP |
688 | stream_open_with_default_port(const char *name_, |
689 | uint16_t default_port, | |
690 | struct stream **streamp, | |
691 | uint8_t dscp) | |
f39dc942 BP |
692 | { |
693 | char *name; | |
694 | int error; | |
695 | ||
ca843648 JP |
696 | if ((!strncmp(name_, "tcp:", 4) || !strncmp(name_, "ssl:", 4)) |
697 | && count_fields(name_) < 3) { | |
d4763d1d JP |
698 | if (default_port == OFP_PORT) { |
699 | VLOG_WARN_ONCE("The default OpenFlow port number has changed " | |
700 | "from %d to %d", | |
36359150 | 701 | OFP_OLD_PORT, OFP_PORT); |
d4763d1d JP |
702 | } else if (default_port == OVSDB_PORT) { |
703 | VLOG_WARN_ONCE("The default OVSDB port number has changed " | |
704 | "from %d to %d", | |
36359150 JP |
705 | OVSDB_OLD_PORT, OVSDB_PORT); |
706 | } | |
ca843648 | 707 | name = xasprintf("%s:%d", name_, default_port); |
f39dc942 BP |
708 | } else { |
709 | name = xstrdup(name_); | |
710 | } | |
f125905c | 711 | error = stream_open(name, streamp, dscp); |
f39dc942 BP |
712 | free(name); |
713 | ||
714 | return error; | |
715 | } | |
716 | ||
ca843648 JP |
717 | /* Like pstream_open(), but port defaults to 'default_port' if no port |
718 | * number is given. */ | |
f39dc942 | 719 | int |
ca843648 JP |
720 | pstream_open_with_default_port(const char *name_, |
721 | uint16_t default_port, | |
722 | struct pstream **pstreamp, | |
723 | uint8_t dscp) | |
f39dc942 BP |
724 | { |
725 | char *name; | |
726 | int error; | |
727 | ||
ca843648 JP |
728 | if ((!strncmp(name_, "ptcp:", 5) || !strncmp(name_, "pssl:", 5)) |
729 | && count_fields(name_) < 2) { | |
730 | name = xasprintf("%s%d", name_, default_port); | |
f39dc942 BP |
731 | } else { |
732 | name = xstrdup(name_); | |
733 | } | |
f125905c | 734 | error = pstream_open(name, pstreamp, dscp); |
f39dc942 BP |
735 | free(name); |
736 | ||
737 | return error; | |
738 | } | |
ac4c900d AA |
739 | |
740 | /* | |
741 | * This function extracts IP address and port from the target string. | |
742 | * | |
e731d71b | 743 | * - On success, function returns true and fills *ss structure with port |
ac4c900d AA |
744 | * and IP address. If port was absent in target string then it will use |
745 | * corresponding default port value. | |
e731d71b | 746 | * - On error, function returns false and *ss contains garbage. |
ac4c900d AA |
747 | */ |
748 | bool | |
ca843648 JP |
749 | stream_parse_target_with_default_port(const char *target, |
750 | uint16_t 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 | } |