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