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