LCOV - code coverage report
Current view: top level - lib - stream.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 235 273 86.1 %
Date: 2016-09-14 01:02:56 Functions: 47 49 95.9 %
Branches: 127 187 67.9 %

           Branch data     Line data    Source code
       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 "fatal-signal.h"
      27                 :            : #include "flow.h"
      28                 :            : #include "jsonrpc.h"
      29                 :            : #include "openflow/nicira-ext.h"
      30                 :            : #include "openflow/openflow.h"
      31                 :            : #include "openvswitch/dynamic-string.h"
      32                 :            : #include "openvswitch/ofp-print.h"
      33                 :            : #include "openvswitch/ofpbuf.h"
      34                 :            : #include "openvswitch/vlog.h"
      35                 :            : #include "ovs-thread.h"
      36                 :            : #include "packets.h"
      37                 :            : #include "poll-loop.h"
      38                 :            : #include "random.h"
      39                 :            : #include "socket-util.h"
      40                 :            : #include "util.h"
      41                 :            : 
      42                 :      53956 : VLOG_DEFINE_THIS_MODULE(stream);
      43                 :            : 
      44                 :     172396 : COVERAGE_DEFINE(pstream_open);
      45                 :     298276 : 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                 :     167317 : check_stream_classes(void)
      81                 :            : {
      82                 :            : #ifndef NDEBUG
      83                 :            :     size_t i;
      84                 :            : 
      85         [ +  + ]:     669268 :     for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
      86                 :     501951 :         const struct stream_class *class = stream_classes[i];
      87         [ -  + ]:     501951 :         ovs_assert(class->name != NULL);
      88         [ -  + ]:     501951 :         ovs_assert(class->open != NULL);
      89 [ +  + ][ +  - ]:     501951 :         if (class->close || class->recv || class->send || class->run
         [ +  - ][ +  - ]
      90 [ +  - ][ -  + ]:     334634 :             || class->run_wait || class->wait) {
      91         [ -  + ]:     167317 :             ovs_assert(class->close != NULL);
      92         [ -  + ]:     167317 :             ovs_assert(class->recv != NULL);
      93         [ -  + ]:     167317 :             ovs_assert(class->send != NULL);
      94         [ -  + ]:     167317 :             ovs_assert(class->wait != NULL);
      95                 :            :         } else {
      96                 :            :             /* This class delegates to another one. */
      97                 :            :         }
      98                 :            :     }
      99                 :            : 
     100         [ +  + ]:     669268 :     for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
     101                 :     501951 :         const struct pstream_class *class = pstream_classes[i];
     102         [ -  + ]:     501951 :         ovs_assert(class->name != NULL);
     103         [ -  + ]:     501951 :         ovs_assert(class->listen != NULL);
     104 [ +  + ][ +  - ]:     501951 :         if (class->close || class->accept || class->wait) {
                 [ -  + ]
     105         [ -  + ]:     167317 :             ovs_assert(class->close != NULL);
     106         [ -  + ]:     167317 :             ovs_assert(class->accept != NULL);
     107         [ -  + ]:     167317 :             ovs_assert(class->wait != NULL);
     108                 :            :         } else {
     109                 :            :             /* This class delegates to another one. */
     110                 :            :         }
     111                 :            :     }
     112                 :            : #endif
     113                 :     167317 : }
     114                 :            : 
     115                 :            : /* Prints information on active (if 'active') and passive (if 'passive')
     116                 :            :  * connection methods supported by the stream. */
     117                 :            : void
     118                 :          0 : 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                 :          0 :     printf("\n");
     126         [ #  # ]:          0 :     if (active) {
     127                 :          0 :         printf("Active %s connection methods:\n", name);
     128                 :          0 :         printf("  tcp:IP:PORT             "
     129                 :            :                "PORT at remote IP\n");
     130                 :            : #ifdef HAVE_OPENSSL
     131                 :          0 :         printf("  ssl:IP:PORT             "
     132                 :            :                "SSL PORT at remote IP\n");
     133                 :            : #endif
     134                 :          0 :         printf("  unix:FILE               "
     135                 :            :                "Unix domain socket named FILE\n");
     136                 :            :     }
     137                 :            : 
     138         [ #  # ]:          0 :     if (passive) {
     139                 :          0 :         printf("Passive %s connection methods:\n", name);
     140                 :          0 :         printf("  ptcp:PORT[:IP]          "
     141                 :            :                "listen to TCP PORT on IP\n");
     142                 :            : #ifdef HAVE_OPENSSL
     143                 :          0 :         printf("  pssl:PORT[:IP]          "
     144                 :            :                "listen for SSL on PORT on IP\n");
     145                 :            : #endif
     146                 :          0 :         printf("  punix:FILE              "
     147                 :            :                "listen on Unix domain socket FILE\n");
     148                 :            :     }
     149                 :            : 
     150                 :            : #ifdef HAVE_OPENSSL
     151                 :          0 :     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         [ #  # ]:          0 :     if (bootstrap) {
     156                 :          0 :         printf("  --bootstrap-ca-cert=FILE  file with peer CA certificate "
     157                 :            :                "to read or create\n");
     158                 :            :     }
     159                 :            : #endif
     160                 :          0 : }
     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                 :      94201 : stream_lookup_class(const char *name, const struct stream_class **classp)
     168                 :            : {
     169                 :            :     size_t prefix_len;
     170                 :            :     size_t i;
     171                 :            : 
     172                 :      94201 :     check_stream_classes();
     173                 :            : 
     174                 :      94201 :     *classp = NULL;
     175                 :      94201 :     prefix_len = strcspn(name, ":");
     176         [ +  + ]:      94201 :     if (name[prefix_len] == '\0') {
     177                 :          2 :         return EAFNOSUPPORT;
     178                 :            :     }
     179         [ +  + ]:     310055 :     for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
     180                 :     249196 :         const struct stream_class *class = stream_classes[i];
     181         [ +  + ]:     249196 :         if (strlen(class->name) == prefix_len
     182         [ +  + ]:      36089 :             && !memcmp(class->name, name, prefix_len)) {
     183                 :      33340 :             *classp = class;
     184                 :      33340 :             return 0;
     185                 :            :         }
     186                 :            :     }
     187                 :      60859 :     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                 :          7 : stream_verify_name(const char *name)
     194                 :            : {
     195                 :            :     const struct stream_class *class;
     196                 :          7 :     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                 :      26040 : 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                 :      26040 :     COVERAGE_INC(stream_open);
     215                 :            : 
     216                 :            :     /* Look up the class. */
     217                 :      26040 :     error = stream_lookup_class(name, &class);
     218         [ +  + ]:      26040 :     if (!class) {
     219                 :          1 :         goto error;
     220                 :            :     }
     221                 :            : 
     222                 :            :     /* Call class's "open" function. */
     223                 :      26039 :     suffix_copy = xstrdup(strchr(name, ':') + 1);
     224                 :      26039 :     error = class->open(name, suffix_copy, &stream, dscp);
     225                 :      26039 :     free(suffix_copy);
     226         [ +  + ]:      26039 :     if (error) {
     227                 :       3410 :         goto error;
     228                 :            :     }
     229                 :            : 
     230                 :            :     /* Success. */
     231                 :      22629 :     *streamp = stream;
     232                 :      22629 :     return 0;
     233                 :            : 
     234                 :            : error:
     235                 :       3411 :     *streamp = NULL;
     236                 :      26040 :     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                 :      11886 : stream_open_block(int error, struct stream **streamp)
     251                 :            : {
     252                 :      11886 :     struct stream *stream = *streamp;
     253                 :            : 
     254                 :      11886 :     fatal_signal_run();
     255                 :            : 
     256         [ +  + ]:      11886 :     if (!error) {
     257         [ +  + ]:      11941 :         while ((error = stream_connect(stream)) == EAGAIN) {
     258                 :         65 :             stream_run(stream);
     259                 :         65 :             stream_run_wait(stream);
     260                 :         65 :             stream_connect_wait(stream);
     261                 :         65 :             poll_block();
     262                 :            :         }
     263         [ -  + ]:      11876 :         ovs_assert(error != EINPROGRESS);
     264                 :            :     }
     265                 :            : 
     266         [ +  + ]:      11886 :     if (error) {
     267                 :         10 :         stream_close(stream);
     268                 :         10 :         *streamp = NULL;
     269                 :            :     } else {
     270                 :      11876 :         *streamp = stream;
     271                 :            :     }
     272                 :      11886 :     return error;
     273                 :            : }
     274                 :            : 
     275                 :            : /* Closes 'stream'. */
     276                 :            : void
     277                 :      80281 : stream_close(struct stream *stream)
     278                 :            : {
     279         [ +  + ]:      80281 :     if (stream != NULL) {
     280                 :      44786 :         char *name = stream->name;
     281                 :      44786 :         (stream->class->close)(stream);
     282                 :      44786 :         free(name);
     283                 :            :     }
     284                 :      80281 : }
     285                 :            : 
     286                 :            : /* Returns the name of 'stream', that is, the string passed to
     287                 :            :  * stream_open(). */
     288                 :            : const char *
     289                 :      45455 : stream_get_name(const struct stream *stream)
     290                 :            : {
     291         [ +  - ]:      45455 :     return stream ? stream->name : "(null)";
     292                 :            : }
     293                 :            : 
     294                 :            : static void
     295                 :       1012 : scs_connecting(struct stream *stream)
     296                 :            : {
     297                 :       1012 :     int retval = (stream->class->connect)(stream);
     298         [ -  + ]:       1012 :     ovs_assert(retval != EINPROGRESS);
     299         [ +  + ]:       1012 :     if (!retval) {
     300                 :        388 :         stream->state = SCS_CONNECTED;
     301         [ +  + ]:        624 :     } else if (retval != EAGAIN) {
     302                 :          5 :         stream->state = SCS_DISCONNECTED;
     303                 :          5 :         stream->error = retval;
     304                 :            :     }
     305                 :       1012 : }
     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                 :    1421856 : stream_connect(struct stream *stream)
     313                 :            : {
     314                 :            :     enum stream_state last_state;
     315                 :            : 
     316                 :            :     do {
     317                 :    1422249 :         last_state = stream->state;
     318   [ +  +  +  - ]:    1422249 :         switch (stream->state) {
     319                 :            :         case SCS_CONNECTING:
     320                 :       1012 :             scs_connecting(stream);
     321                 :       1012 :             break;
     322                 :            : 
     323                 :            :         case SCS_CONNECTED:
     324                 :    1421230 :             return 0;
     325                 :            : 
     326                 :            :         case SCS_DISCONNECTED:
     327                 :          7 :             return stream->error;
     328                 :            : 
     329                 :            :         default:
     330                 :          0 :             OVS_NOT_REACHED();
     331                 :            :         }
     332         [ +  + ]:       1012 :     } while (stream->state != last_state);
     333                 :            : 
     334                 :        619 :     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                 :    1205260 : stream_recv(struct stream *stream, void *buffer, size_t n)
     350                 :            : {
     351                 :    1205260 :     int retval = stream_connect(stream);
     352 [ +  + ][ +  - ]:    2410322 :     return (retval ? -retval
     353                 :            :             : n == 0 ? 0
     354                 :    1205062 :             : (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                 :     193595 : stream_send(struct stream *stream, const void *buffer, size_t n)
     368                 :            : {
     369                 :     193595 :     int retval = stream_connect(stream);
     370 [ +  + ][ +  - ]:     387185 :     return (retval ? -retval
     371                 :            :             : n == 0 ? 0
     372                 :     193590 :             : (stream->class->send)(stream, buffer, n));
     373                 :            : }
     374                 :            : 
     375                 :            : /* Allows 'stream' to perform maintenance activities, such as flushing
     376                 :            :  * output buffers. */
     377                 :            : void
     378                 :     566251 : stream_run(struct stream *stream)
     379                 :            : {
     380         [ +  + ]:     566251 :     if (stream->class->run) {
     381                 :       1100 :         (stream->class->run)(stream);
     382                 :            :     }
     383                 :     566251 : }
     384                 :            : 
     385                 :            : /* Arranges for the poll loop to wake up when 'stream' needs to perform
     386                 :            :  * maintenance activities. */
     387                 :            : void
     388                 :     439480 : stream_run_wait(struct stream *stream)
     389                 :            : {
     390         [ +  + ]:     439480 :     if (stream->class->run_wait) {
     391                 :        642 :         (stream->class->run_wait)(stream);
     392                 :            :     }
     393                 :     439480 : }
     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                 :     456330 : stream_wait(struct stream *stream, enum stream_wait_type wait)
     399                 :            : {
     400 [ +  + ][ +  + ]:     456330 :     ovs_assert(wait == STREAM_CONNECT || wait == STREAM_RECV
         [ +  + ][ -  + ]
     401                 :            :                || wait == STREAM_SEND);
     402                 :            : 
     403      [ +  -  + ]:     456330 :     switch (stream->state) {
     404                 :            :     case SCS_CONNECTING:
     405                 :        336 :         wait = STREAM_CONNECT;
     406                 :        336 :         break;
     407                 :            : 
     408                 :            :     case SCS_DISCONNECTED:
     409                 :          0 :         poll_immediate_wake();
     410                 :          0 :         return;
     411                 :            :     }
     412                 :     456330 :     (stream->class->wait)(stream, wait);
     413                 :            : }
     414                 :            : 
     415                 :            : void
     416                 :       7323 : stream_connect_wait(struct stream *stream)
     417                 :            : {
     418                 :       7323 :     stream_wait(stream, STREAM_CONNECT);
     419                 :       7323 : }
     420                 :            : 
     421                 :            : void
     422                 :     426269 : stream_recv_wait(struct stream *stream)
     423                 :            : {
     424                 :     426269 :     stream_wait(stream, STREAM_RECV);
     425                 :     426269 : }
     426                 :            : 
     427                 :            : void
     428                 :      22738 : stream_send_wait(struct stream *stream)
     429                 :            : {
     430                 :      22738 :     stream_wait(stream, STREAM_SEND);
     431                 :      22738 : }
     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                 :      73116 : pstream_lookup_class(const char *name, const struct pstream_class **classp)
     439                 :            : {
     440                 :            :     size_t prefix_len;
     441                 :            :     size_t i;
     442                 :            : 
     443                 :      73116 :     check_stream_classes();
     444                 :            : 
     445                 :      73116 :     *classp = NULL;
     446                 :      73116 :     prefix_len = strcspn(name, ":");
     447         [ +  + ]:      73116 :     if (name[prefix_len] == '\0') {
     448                 :          2 :         return EAFNOSUPPORT;
     449                 :            :     }
     450         [ +  + ]:     159681 :     for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
     451                 :     152485 :         const struct pstream_class *class = pstream_classes[i];
     452         [ +  + ]:     152485 :         if (strlen(class->name) == prefix_len
     453         [ +  + ]:      81210 :             && !memcmp(class->name, name, prefix_len)) {
     454                 :      65918 :             *classp = class;
     455                 :      65918 :             return 0;
     456                 :            :         }
     457                 :            :     }
     458                 :       7196 :     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                 :       7196 : pstream_verify_name(const char *name)
     465                 :            : {
     466                 :            :     const struct pstream_class *class;
     467                 :       7196 :     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                 :      68154 : stream_or_pstream_needs_probes(const char *name)
     476                 :            : {
     477                 :            :     const struct pstream_class *pclass;
     478                 :            :     const struct stream_class *class;
     479                 :            : 
     480         [ +  + ]:      68154 :     if (!stream_lookup_class(name, &class)) {
     481                 :       7294 :         return class->needs_probes;
     482         [ +  + ]:      60860 :     } else if (!pstream_lookup_class(name, &pclass)) {
     483                 :      60859 :         return pclass->needs_probes;
     484                 :            :     } else {
     485                 :      68154 :         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                 :       5060 : 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                 :       5060 :     COVERAGE_INC(pstream_open);
     505                 :            : 
     506                 :            :     /* Look up the class. */
     507                 :       5060 :     error = pstream_lookup_class(name, &class);
     508         [ +  + ]:       5060 :     if (!class) {
     509                 :          1 :         goto error;
     510                 :            :     }
     511                 :            : 
     512                 :            :     /* Call class's "open" function. */
     513                 :       5059 :     suffix_copy = xstrdup(strchr(name, ':') + 1);
     514                 :       5059 :     error = class->listen(name, suffix_copy, &pstream, dscp);
     515                 :       5059 :     free(suffix_copy);
     516         [ +  + ]:       5059 :     if (error) {
     517                 :          2 :         goto error;
     518                 :            :     }
     519                 :            : 
     520                 :            :     /* Success. */
     521                 :       5057 :     *pstreamp = pstream;
     522                 :       5057 :     return 0;
     523                 :            : 
     524                 :            : error:
     525                 :          3 :     *pstreamp = NULL;
     526                 :       5060 :     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                 :         70 : pstream_get_name(const struct pstream *pstream)
     533                 :            : {
     534                 :         70 :     return pstream->name;
     535                 :            : }
     536                 :            : 
     537                 :            : /* Closes 'pstream'. */
     538                 :            : void
     539                 :      20298 : pstream_close(struct pstream *pstream)
     540                 :            : {
     541         [ +  + ]:      20298 :     if (pstream != NULL) {
     542                 :       4944 :         char *name = pstream->name;
     543                 :       4944 :         (pstream->class->close)(pstream);
     544                 :       4944 :         free(name);
     545                 :            :     }
     546                 :      20298 : }
     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                 :     585580 : pstream_accept(struct pstream *pstream, struct stream **new_stream)
     556                 :            : {
     557                 :     585580 :     int retval = (pstream->class->accept)(pstream, new_stream);
     558         [ +  + ]:     585580 :     if (retval) {
     559                 :     563294 :         *new_stream = NULL;
     560                 :            :     } else {
     561 [ +  + ][ -  + ]:      22286 :         ovs_assert((*new_stream)->state != SCS_CONNECTING
     562                 :            :                    || (*new_stream)->class->connect);
     563                 :            :     }
     564                 :     585580 :     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                 :         21 : pstream_accept_block(struct pstream *pstream, struct stream **new_stream)
     575                 :            : {
     576                 :            :     int error;
     577                 :            : 
     578                 :         21 :     fatal_signal_run();
     579         [ -  + ]:         21 :     while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) {
     580                 :          0 :         pstream_wait(pstream);
     581                 :          0 :         poll_block();
     582                 :            :     }
     583         [ -  + ]:         21 :     if (error) {
     584                 :          0 :         *new_stream = NULL;
     585                 :            :     }
     586                 :         21 :     return error;
     587                 :            : }
     588                 :            : 
     589                 :            : void
     590                 :     569434 : pstream_wait(struct pstream *pstream)
     591                 :            : {
     592                 :     569434 :     (pstream->class->wait)(pstream);
     593                 :     569434 : }
     594                 :            : 
     595                 :            : /* Returns the transport port on which 'pstream' is listening, or 0 if the
     596                 :            :  * concept doesn't apply. */
     597                 :            : ovs_be16
     598                 :          1 : pstream_get_bound_port(const struct pstream *pstream)
     599                 :            : {
     600                 :          1 :     return pstream->bound_port;
     601                 :            : }
     602                 :            : 
     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                 :      44915 : stream_init(struct stream *stream, const struct stream_class *class,
     622                 :            :             int connect_status, const char *name)
     623                 :            : {
     624                 :      44915 :     memset(stream, 0, sizeof *stream);
     625                 :      44915 :     stream->class = class;
     626                 :      44915 :     stream->state = (connect_status == EAGAIN ? SCS_CONNECTING
     627         [ +  + ]:      89432 :                     : !connect_status ? SCS_CONNECTED
     628         [ +  - ]:      44517 :                     : SCS_DISCONNECTED);
     629                 :      44915 :     stream->error = connect_status;
     630                 :      44915 :     stream->name = xstrdup(name);
     631 [ +  + ][ -  + ]:      44915 :     ovs_assert(stream->state != SCS_CONNECTING || class->connect);
     632                 :      44915 : }
     633                 :            : 
     634                 :            : void
     635                 :       5057 : pstream_init(struct pstream *pstream, const struct pstream_class *class,
     636                 :            :             const char *name)
     637                 :            : {
     638                 :       5057 :     memset(pstream, 0, sizeof *pstream);
     639                 :       5057 :     pstream->class = class;
     640                 :       5057 :     pstream->name = xstrdup(name);
     641                 :       5057 : }
     642                 :            : 
     643                 :            : void
     644                 :        145 : pstream_set_bound_port(struct pstream *pstream, ovs_be16 port)
     645                 :            : {
     646                 :        145 :     pstream->bound_port = port;
     647                 :        145 : }
     648                 :            : 
     649                 :            : static int
     650                 :        411 : count_fields(const char *s_)
     651                 :            : {
     652                 :            :     char *s, *field, *save_ptr;
     653                 :        411 :     int n = 0;
     654                 :            : 
     655                 :        411 :     save_ptr = NULL;
     656                 :        411 :     s = xstrdup(s_);
     657         [ +  + ]:       1710 :     for (field = strtok_r(s, ":", &save_ptr); field != NULL;
     658                 :       1299 :          field = strtok_r(NULL, ":", &save_ptr)) {
     659                 :       1299 :         n++;
     660                 :            :     }
     661                 :        411 :     free(s);
     662                 :            : 
     663                 :        411 :     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                 :      15024 : 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 [ +  + ][ +  + ]:      15024 :     if ((!strncmp(name_, "tcp:", 4) || !strncmp(name_, "ssl:", 4))
     678         [ -  + ]:        282 :         && count_fields(name_) < 3) {
     679         [ #  # ]:          0 :         if (default_port == OFP_PORT) {
     680         [ #  # ]:          0 :             VLOG_WARN_ONCE("The default OpenFlow port number has changed "
     681                 :            :                            "from %d to %d",
     682                 :            :                            OFP_OLD_PORT, OFP_PORT);
     683         [ #  # ]:          0 :         } else if (default_port == OVSDB_PORT) {
     684         [ #  # ]:          0 :             VLOG_WARN_ONCE("The default OVSDB port number has changed "
     685                 :            :                            "from %d to %d",
     686                 :            :                            OVSDB_OLD_PORT, OVSDB_PORT);
     687                 :            :         }
     688                 :          0 :         name = xasprintf("%s:%d", name_, default_port);
     689                 :            :     } else {
     690                 :      15024 :         name = xstrdup(name_);
     691                 :            :     }
     692                 :      15024 :     error = stream_open(name, streamp, dscp);
     693                 :      15024 :     free(name);
     694                 :            : 
     695                 :      15024 :     return error;
     696                 :            : }
     697                 :            : 
     698                 :            : /* Like pstream_open(), but port defaults to 'default_port' if no port
     699                 :            :  * number is given. */
     700                 :            : int
     701                 :       2796 : 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 [ +  + ][ +  + ]:       2796 :     if ((!strncmp(name_, "ptcp:", 5) || !strncmp(name_, "pssl:", 5))
     710         [ -  + ]:        129 :         && count_fields(name_) < 2) {
     711                 :          0 :         name = xasprintf("%s%d", name_, default_port);
     712                 :            :     } else {
     713                 :       2796 :         name = xstrdup(name_);
     714                 :            :     }
     715                 :       2796 :     error = pstream_open(name, pstreamp, dscp);
     716                 :       2796 :     free(name);
     717                 :            : 
     718                 :       2796 :     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                 :          1 : stream_parse_target_with_default_port(const char *target,
     731                 :            :                                       uint16_t default_port,
     732                 :            :                                       struct sockaddr_storage *ss)
     733                 :            : {
     734         [ #  # ]:          1 :     return ((!strncmp(target, "tcp:", 4) || !strncmp(target, "ssl:", 4))
     735 [ -  + ][ +  - ]:          1 :             && 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                 :         10 : stream_guess_content(const uint8_t *data, ssize_t size)
     742                 :            : {
     743         [ +  + ]:         10 :     if (size >= 2) {
     744                 :            : #define PAIR(A, B) (((A) << 8) | (B))
     745   [ -  -  -  + ]:          3 :         switch (PAIR(data[0], data[1])) {
     746                 :            :         case PAIR(0x16, 0x03):  /* Handshake, version 3. */
     747                 :          0 :             return STREAM_SSL;
     748                 :            :         case PAIR('{', '"'):
     749                 :          0 :             return STREAM_JSONRPC;
     750                 :            :         case PAIR(OFP10_VERSION, 0 /* OFPT_HELLO */):
     751                 :          0 :             return STREAM_OPENFLOW;
     752                 :            :         }
     753                 :            :     }
     754                 :            : 
     755                 :         10 :     return STREAM_UNKNOWN;
     756                 :            : }
     757                 :            : 
     758                 :            : /* Returns a string represenation of 'type'. */
     759                 :            : static const char *
     760                 :          0 : stream_content_type_to_string(enum stream_content_type type)
     761                 :            : {
     762   [ #  #  #  # ]:          0 :     switch (type) {
     763                 :            :     case STREAM_UNKNOWN:
     764                 :            :     default:
     765                 :          0 :         return "unknown";
     766                 :            : 
     767                 :            :     case STREAM_JSONRPC:
     768                 :          0 :         return "JSON-RPC";
     769                 :            : 
     770                 :            :     case STREAM_OPENFLOW:
     771                 :          0 :         return "OpenFlow";
     772                 :            : 
     773                 :            :     case STREAM_SSL:
     774                 :          0 :         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                 :         10 : 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                 :         10 :     actual_type = stream_guess_content(data, size);
     792 [ +  - ][ -  + ]:         10 :     if (actual_type != expected_type && actual_type != STREAM_UNKNOWN) {
     793                 :          0 :         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                 :         10 : }

Generated by: LCOV version 1.12