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