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