]>
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 | 243 | /* Blocks until a previously started stream connection attempt succeeds or |
77f42ca5 IM |
244 | * fails, but no more than 'timeout' milliseconds. 'error' should be the |
245 | * value returned by stream_open() and 'streamp' should point to the stream | |
246 | * pointer set by stream_open(). Returns 0 if successful, otherwise a | |
247 | * positive errno value other than EAGAIN or EINPROGRESS. If successful, | |
248 | * leaves '*streamp' untouched; on error, closes '*streamp' and sets | |
249 | * '*streamp' to null. Negative value of 'timeout' means infinite waiting. | |
766407ea BP |
250 | * |
251 | * Typical usage: | |
77f42ca5 IM |
252 | * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), -1, |
253 | * &stream); | |
766407ea | 254 | */ |
c34b65c7 | 255 | int |
77f42ca5 | 256 | stream_open_block(int error, long long int timeout, struct stream **streamp) |
c34b65c7 | 257 | { |
766407ea | 258 | struct stream *stream = *streamp; |
c34b65c7 | 259 | |
b302749b BP |
260 | fatal_signal_run(); |
261 | ||
b0bfeb3e | 262 | if (!error) { |
77f42ca5 IM |
263 | long long int deadline = (timeout >= 0 |
264 | ? time_msec() + timeout | |
265 | : LLONG_MAX); | |
b0bfeb3e | 266 | while ((error = stream_connect(stream)) == EAGAIN) { |
77f42ca5 IM |
267 | if (deadline != LLONG_MAX && time_msec() > deadline) { |
268 | error = ETIMEDOUT; | |
269 | break; | |
270 | } | |
b0bfeb3e BP |
271 | stream_run(stream); |
272 | stream_run_wait(stream); | |
273 | stream_connect_wait(stream); | |
77f42ca5 IM |
274 | if (deadline != LLONG_MAX) { |
275 | poll_timer_wait_until(deadline); | |
276 | } | |
b0bfeb3e BP |
277 | poll_block(); |
278 | } | |
cb22974d | 279 | ovs_assert(error != EINPROGRESS); |
c34b65c7 | 280 | } |
b0bfeb3e | 281 | |
c34b65c7 BP |
282 | if (error) { |
283 | stream_close(stream); | |
284 | *streamp = NULL; | |
285 | } else { | |
286 | *streamp = stream; | |
287 | } | |
288 | return error; | |
289 | } | |
290 | ||
291 | /* Closes 'stream'. */ | |
292 | void | |
293 | stream_close(struct stream *stream) | |
294 | { | |
295 | if (stream != NULL) { | |
296 | char *name = stream->name; | |
c19ae4cc | 297 | char *peer_id = stream->peer_id; |
c34b65c7 BP |
298 | (stream->class->close)(stream); |
299 | free(name); | |
c19ae4cc | 300 | free(peer_id); |
c34b65c7 BP |
301 | } |
302 | } | |
303 | ||
304 | /* Returns the name of 'stream', that is, the string passed to | |
305 | * stream_open(). */ | |
306 | const char * | |
307 | stream_get_name(const struct stream *stream) | |
308 | { | |
309 | return stream ? stream->name : "(null)"; | |
310 | } | |
311 | ||
c34b65c7 BP |
312 | static void |
313 | scs_connecting(struct stream *stream) | |
314 | { | |
315 | int retval = (stream->class->connect)(stream); | |
cb22974d | 316 | ovs_assert(retval != EINPROGRESS); |
c34b65c7 BP |
317 | if (!retval) { |
318 | stream->state = SCS_CONNECTED; | |
319 | } else if (retval != EAGAIN) { | |
320 | stream->state = SCS_DISCONNECTED; | |
321 | stream->error = retval; | |
322 | } | |
323 | } | |
324 | ||
294e9fc8 BP |
325 | /* Tries to complete the connection on 'stream'. If 'stream''s connection is |
326 | * complete, returns 0 if the connection was successful or a positive errno | |
327 | * value if it failed. If the connection is still in progress, returns | |
328 | * EAGAIN. */ | |
c34b65c7 BP |
329 | int |
330 | stream_connect(struct stream *stream) | |
331 | { | |
332 | enum stream_state last_state; | |
333 | ||
334 | do { | |
335 | last_state = stream->state; | |
336 | switch (stream->state) { | |
337 | case SCS_CONNECTING: | |
338 | scs_connecting(stream); | |
339 | break; | |
340 | ||
341 | case SCS_CONNECTED: | |
342 | return 0; | |
343 | ||
344 | case SCS_DISCONNECTED: | |
345 | return stream->error; | |
346 | ||
347 | default: | |
428b2edd | 348 | OVS_NOT_REACHED(); |
c34b65c7 BP |
349 | } |
350 | } while (stream->state != last_state); | |
351 | ||
352 | return EAGAIN; | |
353 | } | |
354 | ||
355 | /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns: | |
356 | * | |
357 | * - If successful, the number of bytes received (between 1 and 'n'). | |
358 | * | |
359 | * - On error, a negative errno value. | |
360 | * | |
361 | * - 0, if the connection has been closed in the normal fashion, or if 'n' | |
362 | * is zero. | |
363 | * | |
364 | * The recv function will not block waiting for a packet to arrive. If no | |
365 | * data have been received, it returns -EAGAIN immediately. */ | |
366 | int | |
367 | stream_recv(struct stream *stream, void *buffer, size_t n) | |
368 | { | |
369 | int retval = stream_connect(stream); | |
370 | return (retval ? -retval | |
371 | : n == 0 ? 0 | |
372 | : (stream->class->recv)(stream, buffer, n)); | |
373 | } | |
374 | ||
375 | /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns: | |
376 | * | |
377 | * - If successful, the number of bytes sent (between 1 and 'n'). 0 is | |
378 | * only a valid return value if 'n' is 0. | |
379 | * | |
380 | * - On error, a negative errno value. | |
381 | * | |
382 | * The send function will not block. If no bytes can be immediately accepted | |
383 | * for transmission, it returns -EAGAIN immediately. */ | |
384 | int | |
385 | stream_send(struct stream *stream, const void *buffer, size_t n) | |
386 | { | |
387 | int retval = stream_connect(stream); | |
388 | return (retval ? -retval | |
389 | : n == 0 ? 0 | |
390 | : (stream->class->send)(stream, buffer, n)); | |
391 | } | |
392 | ||
539e96f6 BP |
393 | /* Allows 'stream' to perform maintenance activities, such as flushing |
394 | * output buffers. */ | |
395 | void | |
396 | stream_run(struct stream *stream) | |
397 | { | |
398 | if (stream->class->run) { | |
399 | (stream->class->run)(stream); | |
400 | } | |
401 | } | |
402 | ||
403 | /* Arranges for the poll loop to wake up when 'stream' needs to perform | |
404 | * maintenance activities. */ | |
405 | void | |
406 | stream_run_wait(struct stream *stream) | |
407 | { | |
408 | if (stream->class->run_wait) { | |
409 | (stream->class->run_wait)(stream); | |
410 | } | |
411 | } | |
412 | ||
413 | /* Arranges for the poll loop to wake up when 'stream' is ready to take an | |
414 | * action of the given 'type'. */ | |
c34b65c7 BP |
415 | void |
416 | stream_wait(struct stream *stream, enum stream_wait_type wait) | |
417 | { | |
cb22974d BP |
418 | ovs_assert(wait == STREAM_CONNECT || wait == STREAM_RECV |
419 | || wait == STREAM_SEND); | |
c34b65c7 BP |
420 | |
421 | switch (stream->state) { | |
422 | case SCS_CONNECTING: | |
423 | wait = STREAM_CONNECT; | |
424 | break; | |
425 | ||
426 | case SCS_DISCONNECTED: | |
427 | poll_immediate_wake(); | |
428 | return; | |
429 | } | |
430 | (stream->class->wait)(stream, wait); | |
431 | } | |
432 | ||
433 | void | |
434 | stream_connect_wait(struct stream *stream) | |
435 | { | |
436 | stream_wait(stream, STREAM_CONNECT); | |
437 | } | |
438 | ||
439 | void | |
440 | stream_recv_wait(struct stream *stream) | |
441 | { | |
442 | stream_wait(stream, STREAM_RECV); | |
443 | } | |
444 | ||
445 | void | |
446 | stream_send_wait(struct stream *stream) | |
447 | { | |
448 | stream_wait(stream, STREAM_SEND); | |
449 | } | |
450 | ||
c19ae4cc LR |
451 | void |
452 | stream_set_peer_id(struct stream *stream, const char *peer_id) | |
453 | { | |
454 | free(stream->peer_id); | |
455 | stream->peer_id = xstrdup(peer_id); | |
456 | } | |
457 | ||
458 | const char * | |
459 | stream_get_peer_id(const struct stream *stream) | |
460 | { | |
461 | return stream->peer_id; | |
462 | } | |
463 | ||
26ad129e BP |
464 | /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class |
465 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
466 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
467 | * class exists. */ | |
468 | static int | |
da327b18 | 469 | pstream_lookup_class(const char *name, const struct pstream_class **classp) |
c34b65c7 BP |
470 | { |
471 | size_t prefix_len; | |
472 | size_t i; | |
473 | ||
474 | check_stream_classes(); | |
475 | ||
26ad129e | 476 | *classp = NULL; |
c34b65c7 | 477 | prefix_len = strcspn(name, ":"); |
26ad129e | 478 | if (name[prefix_len] == '\0') { |
c34b65c7 BP |
479 | return EAFNOSUPPORT; |
480 | } | |
481 | for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) { | |
da327b18 | 482 | const struct pstream_class *class = pstream_classes[i]; |
c34b65c7 BP |
483 | if (strlen(class->name) == prefix_len |
484 | && !memcmp(class->name, name, prefix_len)) { | |
26ad129e BP |
485 | *classp = class; |
486 | return 0; | |
c34b65c7 BP |
487 | } |
488 | } | |
489 | return EAFNOSUPPORT; | |
490 | } | |
491 | ||
26ad129e BP |
492 | /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is |
493 | * a supported pstream type, otherwise EAFNOSUPPORT. */ | |
494 | int | |
495 | pstream_verify_name(const char *name) | |
496 | { | |
da327b18 | 497 | const struct pstream_class *class; |
26ad129e BP |
498 | return pstream_lookup_class(name, &class); |
499 | } | |
500 | ||
f1936eb6 EJ |
501 | /* Returns 1 if the stream or pstream specified by 'name' needs periodic probes |
502 | * to verify connectivity. For [p]streams which need probes, it can take a | |
503 | * long time to notice the connection has been dropped. Returns 0 if the | |
504 | * stream or pstream does not need probes, and -1 if 'name' is not valid. */ | |
505 | int | |
506 | stream_or_pstream_needs_probes(const char *name) | |
507 | { | |
508 | const struct pstream_class *pclass; | |
509 | const struct stream_class *class; | |
510 | ||
511 | if (!stream_lookup_class(name, &class)) { | |
512 | return class->needs_probes; | |
513 | } else if (!pstream_lookup_class(name, &pclass)) { | |
514 | return pclass->needs_probes; | |
515 | } else { | |
516 | return -1; | |
517 | } | |
518 | } | |
519 | ||
26ad129e BP |
520 | /* Attempts to start listening for remote stream connections. 'name' is a |
521 | * connection name in the form "TYPE:ARGS", where TYPE is an passive stream | |
522 | * class's name and ARGS are stream class-specific. | |
523 | * | |
524 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
525 | * stores a pointer to the new connection in '*pstreamp', otherwise a null | |
526 | * pointer. */ | |
527 | int | |
f125905c | 528 | pstream_open(const char *name, struct pstream **pstreamp, uint8_t dscp) |
26ad129e | 529 | { |
da327b18 | 530 | const struct pstream_class *class; |
26ad129e BP |
531 | struct pstream *pstream; |
532 | char *suffix_copy; | |
533 | int error; | |
534 | ||
535 | COVERAGE_INC(pstream_open); | |
536 | ||
537 | /* Look up the class. */ | |
538 | error = pstream_lookup_class(name, &class); | |
539 | if (!class) { | |
540 | goto error; | |
541 | } | |
542 | ||
543 | /* Call class's "open" function. */ | |
544 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
f125905c | 545 | error = class->listen(name, suffix_copy, &pstream, dscp); |
26ad129e BP |
546 | free(suffix_copy); |
547 | if (error) { | |
548 | goto error; | |
549 | } | |
550 | ||
551 | /* Success. */ | |
552 | *pstreamp = pstream; | |
553 | return 0; | |
554 | ||
555 | error: | |
556 | *pstreamp = NULL; | |
557 | return error; | |
558 | } | |
559 | ||
c34b65c7 BP |
560 | /* Returns the name that was used to open 'pstream'. The caller must not |
561 | * modify or free the name. */ | |
562 | const char * | |
563 | pstream_get_name(const struct pstream *pstream) | |
564 | { | |
565 | return pstream->name; | |
566 | } | |
567 | ||
568 | /* Closes 'pstream'. */ | |
569 | void | |
570 | pstream_close(struct pstream *pstream) | |
571 | { | |
572 | if (pstream != NULL) { | |
573 | char *name = pstream->name; | |
574 | (pstream->class->close)(pstream); | |
575 | free(name); | |
576 | } | |
577 | } | |
578 | ||
579 | /* Tries to accept a new connection on 'pstream'. If successful, stores the | |
580 | * new connection in '*new_stream' and returns 0. Otherwise, returns a | |
581 | * positive errno value. | |
582 | * | |
583 | * pstream_accept() will not block waiting for a connection. If no connection | |
584 | * is ready to be accepted, it returns EAGAIN immediately. */ | |
585 | int | |
586 | pstream_accept(struct pstream *pstream, struct stream **new_stream) | |
587 | { | |
588 | int retval = (pstream->class->accept)(pstream, new_stream); | |
589 | if (retval) { | |
590 | *new_stream = NULL; | |
591 | } else { | |
cb22974d BP |
592 | ovs_assert((*new_stream)->state != SCS_CONNECTING |
593 | || (*new_stream)->class->connect); | |
c34b65c7 BP |
594 | } |
595 | return retval; | |
596 | } | |
597 | ||
8d76bcca BP |
598 | /* Tries to accept a new connection on 'pstream'. If successful, stores the |
599 | * new connection in '*new_stream' and returns 0. Otherwise, returns a | |
600 | * positive errno value. | |
601 | * | |
602 | * pstream_accept_block() blocks until a connection is ready or until an error | |
603 | * occurs. It will not return EAGAIN. */ | |
604 | int | |
605 | pstream_accept_block(struct pstream *pstream, struct stream **new_stream) | |
606 | { | |
607 | int error; | |
608 | ||
b302749b | 609 | fatal_signal_run(); |
8d76bcca BP |
610 | while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) { |
611 | pstream_wait(pstream); | |
612 | poll_block(); | |
613 | } | |
614 | if (error) { | |
615 | *new_stream = NULL; | |
616 | } | |
617 | return error; | |
618 | } | |
619 | ||
c34b65c7 BP |
620 | void |
621 | pstream_wait(struct pstream *pstream) | |
622 | { | |
623 | (pstream->class->wait)(pstream); | |
624 | } | |
f89b7ce5 | 625 | |
798e1352 BP |
626 | /* Returns the transport port on which 'pstream' is listening, or 0 if the |
627 | * concept doesn't apply. */ | |
628 | ovs_be16 | |
629 | pstream_get_bound_port(const struct pstream *pstream) | |
630 | { | |
631 | return pstream->bound_port; | |
632 | } | |
c34b65c7 BP |
633 | \f |
634 | /* Initializes 'stream' as a new stream named 'name', implemented via 'class'. | |
635 | * The initial connection status, supplied as 'connect_status', is interpreted | |
636 | * as follows: | |
637 | * | |
638 | * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be | |
639 | * called in the normal fashion. | |
640 | * | |
641 | * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect' | |
642 | * function should be called to complete the connection. | |
643 | * | |
644 | * - Other positive errno values indicate that the connection failed with | |
645 | * the specified error. | |
646 | * | |
647 | * After calling this function, stream_close() must be used to destroy | |
648 | * 'stream', otherwise resources will be leaked. | |
649 | * | |
b7636967 | 650 | * Takes ownership of 'name'. */ |
c34b65c7 | 651 | void |
da327b18 | 652 | stream_init(struct stream *stream, const struct stream_class *class, |
b7636967 | 653 | int connect_status, char *name) |
c34b65c7 | 654 | { |
c4bed75b | 655 | memset(stream, 0, sizeof *stream); |
c34b65c7 BP |
656 | stream->class = class; |
657 | stream->state = (connect_status == EAGAIN ? SCS_CONNECTING | |
658 | : !connect_status ? SCS_CONNECTED | |
659 | : SCS_DISCONNECTED); | |
660 | stream->error = connect_status; | |
b7636967 | 661 | stream->name = name; |
cb22974d | 662 | ovs_assert(stream->state != SCS_CONNECTING || class->connect); |
c34b65c7 BP |
663 | } |
664 | ||
b7636967 | 665 | /* Takes ownership of 'name'. */ |
c34b65c7 | 666 | void |
da327b18 | 667 | pstream_init(struct pstream *pstream, const struct pstream_class *class, |
b7636967 | 668 | char *name) |
c34b65c7 | 669 | { |
798e1352 | 670 | memset(pstream, 0, sizeof *pstream); |
c34b65c7 | 671 | pstream->class = class; |
b7636967 | 672 | pstream->name = name; |
c34b65c7 | 673 | } |
798e1352 BP |
674 | |
675 | void | |
676 | pstream_set_bound_port(struct pstream *pstream, ovs_be16 port) | |
677 | { | |
678 | pstream->bound_port = port; | |
679 | } | |
f39dc942 BP |
680 | \f |
681 | static int | |
682 | count_fields(const char *s_) | |
683 | { | |
684 | char *s, *field, *save_ptr; | |
685 | int n = 0; | |
686 | ||
687 | save_ptr = NULL; | |
688 | s = xstrdup(s_); | |
689 | for (field = strtok_r(s, ":", &save_ptr); field != NULL; | |
690 | field = strtok_r(NULL, ":", &save_ptr)) { | |
691 | n++; | |
692 | } | |
693 | free(s); | |
694 | ||
695 | return n; | |
696 | } | |
697 | ||
ca843648 JP |
698 | /* Like stream_open(), but the port defaults to 'default_port' if no port |
699 | * number is given. */ | |
f39dc942 | 700 | int |
ca843648 JP |
701 | stream_open_with_default_port(const char *name_, |
702 | uint16_t default_port, | |
703 | struct stream **streamp, | |
704 | uint8_t dscp) | |
f39dc942 BP |
705 | { |
706 | char *name; | |
707 | int error; | |
708 | ||
ca843648 JP |
709 | if ((!strncmp(name_, "tcp:", 4) || !strncmp(name_, "ssl:", 4)) |
710 | && count_fields(name_) < 3) { | |
d4763d1d JP |
711 | if (default_port == OFP_PORT) { |
712 | VLOG_WARN_ONCE("The default OpenFlow port number has changed " | |
713 | "from %d to %d", | |
36359150 | 714 | OFP_OLD_PORT, OFP_PORT); |
d4763d1d JP |
715 | } else if (default_port == OVSDB_PORT) { |
716 | VLOG_WARN_ONCE("The default OVSDB port number has changed " | |
717 | "from %d to %d", | |
36359150 JP |
718 | OVSDB_OLD_PORT, OVSDB_PORT); |
719 | } | |
ca843648 | 720 | name = xasprintf("%s:%d", name_, default_port); |
f39dc942 BP |
721 | } else { |
722 | name = xstrdup(name_); | |
723 | } | |
f125905c | 724 | error = stream_open(name, streamp, dscp); |
f39dc942 BP |
725 | free(name); |
726 | ||
727 | return error; | |
728 | } | |
729 | ||
ca843648 JP |
730 | /* Like pstream_open(), but port defaults to 'default_port' if no port |
731 | * number is given. */ | |
f39dc942 | 732 | int |
ca843648 JP |
733 | pstream_open_with_default_port(const char *name_, |
734 | uint16_t default_port, | |
735 | struct pstream **pstreamp, | |
736 | uint8_t dscp) | |
f39dc942 BP |
737 | { |
738 | char *name; | |
739 | int error; | |
740 | ||
ca843648 JP |
741 | if ((!strncmp(name_, "ptcp:", 5) || !strncmp(name_, "pssl:", 5)) |
742 | && count_fields(name_) < 2) { | |
743 | name = xasprintf("%s%d", name_, default_port); | |
f39dc942 BP |
744 | } else { |
745 | name = xstrdup(name_); | |
746 | } | |
f125905c | 747 | error = pstream_open(name, pstreamp, dscp); |
f39dc942 BP |
748 | free(name); |
749 | ||
750 | return error; | |
751 | } | |
ac4c900d AA |
752 | |
753 | /* | |
754 | * This function extracts IP address and port from the target string. | |
755 | * | |
e731d71b | 756 | * - On success, function returns true and fills *ss structure with port |
ac4c900d AA |
757 | * and IP address. If port was absent in target string then it will use |
758 | * corresponding default port value. | |
e731d71b | 759 | * - On error, function returns false and *ss contains garbage. |
ac4c900d AA |
760 | */ |
761 | bool | |
1bb01121 | 762 | stream_parse_target_with_default_port(const char *target, int default_port, |
e731d71b | 763 | struct sockaddr_storage *ss) |
ca843648 JP |
764 | { |
765 | return ((!strncmp(target, "tcp:", 4) || !strncmp(target, "ssl:", 4)) | |
f31b8ae7 | 766 | && inet_parse_active(target + 4, default_port, ss, true)); |
ac4c900d AA |
767 | } |
768 | ||
1e3c0047 BP |
769 | /* Attempts to guess the content type of a stream whose first few bytes were |
770 | * the 'size' bytes of 'data'. */ | |
771 | static enum stream_content_type | |
c55acc2e | 772 | stream_guess_content(const uint8_t *data, ssize_t size) |
1e3c0047 BP |
773 | { |
774 | if (size >= 2) { | |
775 | #define PAIR(A, B) (((A) << 8) | (B)) | |
776 | switch (PAIR(data[0], data[1])) { | |
777 | case PAIR(0x16, 0x03): /* Handshake, version 3. */ | |
778 | return STREAM_SSL; | |
779 | case PAIR('{', '"'): | |
780 | return STREAM_JSONRPC; | |
982697a4 | 781 | case PAIR(OFP10_VERSION, 0 /* OFPT_HELLO */): |
1e3c0047 BP |
782 | return STREAM_OPENFLOW; |
783 | } | |
784 | } | |
785 | ||
786 | return STREAM_UNKNOWN; | |
787 | } | |
788 | ||
789 | /* Returns a string represenation of 'type'. */ | |
790 | static const char * | |
791 | stream_content_type_to_string(enum stream_content_type type) | |
792 | { | |
793 | switch (type) { | |
794 | case STREAM_UNKNOWN: | |
795 | default: | |
796 | return "unknown"; | |
f39dc942 | 797 | |
1e3c0047 BP |
798 | case STREAM_JSONRPC: |
799 | return "JSON-RPC"; | |
f39dc942 | 800 | |
1e3c0047 BP |
801 | case STREAM_OPENFLOW: |
802 | return "OpenFlow"; | |
803 | ||
804 | case STREAM_SSL: | |
805 | return "SSL"; | |
806 | } | |
807 | } | |
808 | ||
809 | /* Attempts to guess the content type of a stream whose first few bytes were | |
810 | * the 'size' bytes of 'data'. If this is done successfully, and the guessed | |
811 | * content type is other than 'expected_type', then log a message in vlog | |
812 | * module 'module', naming 'stream_name' as the source, explaining what | |
813 | * content was expected and what was actually received. */ | |
814 | void | |
c55acc2e | 815 | stream_report_content(const void *data, ssize_t size, |
1e3c0047 | 816 | enum stream_content_type expected_type, |
480ce8ab | 817 | struct vlog_module *module, const char *stream_name) |
1e3c0047 BP |
818 | { |
819 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
820 | enum stream_content_type actual_type; | |
821 | ||
822 | actual_type = stream_guess_content(data, size); | |
823 | if (actual_type != expected_type && actual_type != STREAM_UNKNOWN) { | |
824 | vlog_rate_limit(module, VLL_WARN, &rl, | |
825 | "%s: received %s data on %s channel", | |
826 | stream_name, | |
c662c789 BP |
827 | stream_content_type_to_string(actual_type), |
828 | stream_content_type_to_string(expected_type)); | |
1e3c0047 BP |
829 | } |
830 | } |