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