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