LCOV - code coverage report
Current view: top level - lib - stream-fd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 85 89 95.5 %
Date: 2016-09-14 01:02:56 Functions: 14 14 100.0 %
Branches: 24 31 77.4 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014, 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-fd.h"
      19                 :            : #include <errno.h>
      20                 :            : #include <poll.h>
      21                 :            : #include <stdlib.h>
      22                 :            : #include <string.h>
      23                 :            : #include <sys/socket.h>
      24                 :            : #include <sys/types.h>
      25                 :            : #include <unistd.h>
      26                 :            : #include "fatal-signal.h"
      27                 :            : #include "poll-loop.h"
      28                 :            : #include "socket-util.h"
      29                 :            : #include "util.h"
      30                 :            : #include "stream-provider.h"
      31                 :            : #include "stream.h"
      32                 :            : #include "openvswitch/vlog.h"
      33                 :            : 
      34                 :      53956 : VLOG_DEFINE_THIS_MODULE(stream_fd);
      35                 :            : 
      36                 :            : /* Active file descriptor stream. */
      37                 :            : 
      38                 :            : struct stream_fd
      39                 :            : {
      40                 :            :     struct stream stream;
      41                 :            :     int fd;
      42                 :            :     int fd_type;
      43                 :            : };
      44                 :            : 
      45                 :            : static const struct stream_class stream_fd_class;
      46                 :            : 
      47                 :            : static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
      48                 :            : 
      49                 :            : static void maybe_unlink_and_free(char *path);
      50                 :            : 
      51                 :            : /* Creates a new stream named 'name' that will send and receive data on 'fd'
      52                 :            :  * and stores a pointer to the stream in '*streamp'.  Initial connection status
      53                 :            :  * 'connect_status' is interpreted as described for stream_init(). 'fd_type'
      54                 :            :  * tells whether the socket is TCP or Unix domain socket.
      55                 :            :  *
      56                 :            :  * Returns 0 if successful, otherwise a positive errno value.  (The current
      57                 :            :  * implementation never fails.) */
      58                 :            : int
      59                 :      44682 : new_fd_stream(const char *name, int fd, int connect_status, int fd_type,
      60                 :            :               struct stream **streamp)
      61                 :            : {
      62                 :            :     struct stream_fd *s;
      63                 :            : 
      64                 :      44682 :     s = xmalloc(sizeof *s);
      65                 :      44682 :     stream_init(&s->stream, &stream_fd_class, connect_status, name);
      66                 :      44682 :     s->fd = fd;
      67                 :      44682 :     s->fd_type = fd_type;
      68                 :      44682 :     *streamp = &s->stream;
      69                 :      44682 :     return 0;
      70                 :            : }
      71                 :            : 
      72                 :            : static struct stream_fd *
      73                 :    1898133 : stream_fd_cast(struct stream *stream)
      74                 :            : {
      75                 :    1898133 :     stream_assert_class(stream, &stream_fd_class);
      76                 :    1898133 :     return CONTAINER_OF(stream, struct stream_fd, stream);
      77                 :            : }
      78                 :            : 
      79                 :            : static void
      80                 :      44553 : fd_close(struct stream *stream)
      81                 :            : {
      82                 :      44553 :     struct stream_fd *s = stream_fd_cast(stream);
      83                 :      44553 :     closesocket(s->fd);
      84                 :      44553 :     free(s);
      85                 :      44553 : }
      86                 :            : 
      87                 :            : static int
      88                 :        218 : fd_connect(struct stream *stream)
      89                 :            : {
      90                 :        218 :     struct stream_fd *s = stream_fd_cast(stream);
      91                 :        218 :     int retval = check_connection_completion(s->fd);
      92 [ +  + ][ +  - ]:        218 :     if (retval == 0 && s->fd_type == AF_INET) {
      93                 :        160 :         setsockopt_tcp_nodelay(s->fd);
      94                 :            :     }
      95                 :        218 :     return retval;
      96                 :            : }
      97                 :            : 
      98                 :            : static ssize_t
      99                 :    1204337 : fd_recv(struct stream *stream, void *buffer, size_t n)
     100                 :            : {
     101                 :    1204337 :     struct stream_fd *s = stream_fd_cast(stream);
     102                 :            :     ssize_t retval;
     103                 :            :     int error;
     104                 :            : 
     105                 :    1204337 :     retval = recv(s->fd, buffer, n, 0);
     106         [ +  + ]:    1204337 :     if (retval < 0) {
     107                 :     592795 :         error = sock_errno();
     108                 :            : #ifdef _WIN32
     109                 :            :         if (error == WSAEWOULDBLOCK) {
     110                 :            :            error = EAGAIN;
     111                 :            :         }
     112                 :            : #endif
     113         [ +  + ]:     592795 :         if (error != EAGAIN) {
     114         [ +  + ]:         18 :             VLOG_DBG_RL(&rl, "recv: %s", sock_strerror(error));
     115                 :            :         }
     116                 :     592795 :         return -error;
     117                 :            :     }
     118                 :     611542 :     return retval;
     119                 :            : }
     120                 :            : 
     121                 :            : static ssize_t
     122                 :     193351 : fd_send(struct stream *stream, const void *buffer, size_t n)
     123                 :            : {
     124                 :     193351 :     struct stream_fd *s = stream_fd_cast(stream);
     125                 :            :     ssize_t retval;
     126                 :            :     int error;
     127                 :            : 
     128                 :     193351 :     retval = send(s->fd, buffer, n, 0);
     129         [ +  + ]:     193351 :     if (retval < 0) {
     130                 :      13330 :         error = sock_errno();
     131                 :            : #ifdef _WIN32
     132                 :            :         if (error == WSAEWOULDBLOCK) {
     133                 :            :            error = EAGAIN;
     134                 :            :         }
     135                 :            : #endif
     136         [ +  + ]:      13330 :         if (error != EAGAIN) {
     137         [ +  + ]:         52 :             VLOG_DBG_RL(&rl, "send: %s", sock_strerror(error));
     138                 :            :         }
     139                 :      13330 :         return -error;
     140                 :            :     }
     141         [ +  - ]:     180021 :     return (retval > 0 ? retval : -EAGAIN);
     142                 :            : }
     143                 :            : 
     144                 :            : static void
     145                 :     455674 : fd_wait(struct stream *stream, enum stream_wait_type wait)
     146                 :            : {
     147                 :     455674 :     struct stream_fd *s = stream_fd_cast(stream);
     148      [ +  +  - ]:     455674 :     switch (wait) {
     149                 :            :     case STREAM_CONNECT:
     150                 :            :     case STREAM_SEND:
     151                 :      29975 :         poll_fd_wait(s->fd, POLLOUT);
     152                 :      29975 :         break;
     153                 :            : 
     154                 :            :     case STREAM_RECV:
     155                 :     425699 :         poll_fd_wait(s->fd, POLLIN);
     156                 :     425699 :         break;
     157                 :            : 
     158                 :            :     default:
     159                 :          0 :         OVS_NOT_REACHED();
     160                 :            :     }
     161                 :     455674 : }
     162                 :            : 
     163                 :            : static const struct stream_class stream_fd_class = {
     164                 :            :     "fd",                       /* name */
     165                 :            :     false,                      /* needs_probes */
     166                 :            :     NULL,                       /* open */
     167                 :            :     fd_close,                   /* close */
     168                 :            :     fd_connect,                 /* connect */
     169                 :            :     fd_recv,                    /* recv */
     170                 :            :     fd_send,                    /* send */
     171                 :            :     NULL,                       /* run */
     172                 :            :     NULL,                       /* run_wait */
     173                 :            :     fd_wait,                    /* wait */
     174                 :            : };
     175                 :            : 
     176                 :            : /* Passive file descriptor stream. */
     177                 :            : 
     178                 :            : struct fd_pstream
     179                 :            : {
     180                 :            :     struct pstream pstream;
     181                 :            :     int fd;
     182                 :            :     int (*accept_cb)(int fd, const struct sockaddr_storage *, size_t ss_len,
     183                 :            :                      struct stream **);
     184                 :            :     char *unlink_path;
     185                 :            : };
     186                 :            : 
     187                 :            : static const struct pstream_class fd_pstream_class;
     188                 :            : 
     189                 :            : static struct fd_pstream *
     190                 :    1158214 : fd_pstream_cast(struct pstream *pstream)
     191                 :            : {
     192                 :    1158214 :     pstream_assert_class(pstream, &fd_pstream_class);
     193                 :    1158214 :     return CONTAINER_OF(pstream, struct fd_pstream, pstream);
     194                 :            : }
     195                 :            : 
     196                 :            : /* Creates a new pstream named 'name' that will accept new socket connections
     197                 :            :  * on 'fd' and stores a pointer to the stream in '*pstreamp'.
     198                 :            :  *
     199                 :            :  * When a connection has been accepted, 'accept_cb' will be called with the new
     200                 :            :  * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
     201                 :            :  * accept_cb must return 0 if the connection is successful, in which case it
     202                 :            :  * must initialize '*streamp' to the new stream, or a positive errno value on
     203                 :            :  * error.  In either case accept_cb takes ownership of the 'fd' passed in.
     204                 :            :  *
     205                 :            :  * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
     206                 :            :  * to fatal_signal_unlink_file_now() and freed with free().
     207                 :            :  *
     208                 :            :  * Returns 0 if successful, otherwise a positive errno value.  (The current
     209                 :            :  * implementation never fails.) */
     210                 :            : int
     211                 :       5020 : new_fd_pstream(const char *name, int fd,
     212                 :            :                int (*accept_cb)(int fd, const struct sockaddr_storage *ss,
     213                 :            :                                 size_t ss_len, struct stream **streamp),
     214                 :            :                char *unlink_path, struct pstream **pstreamp)
     215                 :            : {
     216                 :       5020 :     struct fd_pstream *ps = xmalloc(sizeof *ps);
     217                 :       5020 :     pstream_init(&ps->pstream, &fd_pstream_class, name);
     218                 :       5020 :     ps->fd = fd;
     219                 :       5020 :     ps->accept_cb = accept_cb;
     220                 :       5020 :     ps->unlink_path = unlink_path;
     221                 :       5020 :     *pstreamp = &ps->pstream;
     222                 :       5020 :     return 0;
     223                 :            : }
     224                 :            : 
     225                 :            : static void
     226                 :       4907 : pfd_close(struct pstream *pstream)
     227                 :            : {
     228                 :       4907 :     struct fd_pstream *ps = fd_pstream_cast(pstream);
     229                 :       4907 :     closesocket(ps->fd);
     230                 :       4907 :     maybe_unlink_and_free(ps->unlink_path);
     231                 :       4907 :     free(ps);
     232                 :       4907 : }
     233                 :            : 
     234                 :            : static int
     235                 :     584723 : pfd_accept(struct pstream *pstream, struct stream **new_streamp)
     236                 :            : {
     237                 :     584723 :     struct fd_pstream *ps = fd_pstream_cast(pstream);
     238                 :            :     struct sockaddr_storage ss;
     239                 :     584723 :     socklen_t ss_len = sizeof ss;
     240                 :            :     int new_fd;
     241                 :            :     int retval;
     242                 :            : 
     243                 :     584723 :     new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
     244         [ +  + ]:     584723 :     if (new_fd < 0) {
     245                 :     562553 :         retval = sock_errno();
     246                 :            : #ifdef _WIN32
     247                 :            :         if (retval == WSAEWOULDBLOCK) {
     248                 :            :             retval = EAGAIN;
     249                 :            :         }
     250                 :            : #endif
     251         [ -  + ]:     562553 :         if (retval != EAGAIN) {
     252         [ #  # ]:          0 :             VLOG_DBG_RL(&rl, "accept: %s", sock_strerror(retval));
     253                 :            :         }
     254                 :     562553 :         return retval;
     255                 :            :     }
     256                 :            : 
     257                 :      22170 :     retval = set_nonblocking(new_fd);
     258         [ -  + ]:      22170 :     if (retval) {
     259                 :          0 :         closesocket(new_fd);
     260                 :          0 :         return retval;
     261                 :            :     }
     262                 :            : 
     263                 :     584723 :     return ps->accept_cb(new_fd, &ss, ss_len, new_streamp);
     264                 :            : }
     265                 :            : 
     266                 :            : static void
     267                 :     568584 : pfd_wait(struct pstream *pstream)
     268                 :            : {
     269                 :     568584 :     struct fd_pstream *ps = fd_pstream_cast(pstream);
     270                 :     568584 :     poll_fd_wait(ps->fd, POLLIN);
     271                 :     568584 : }
     272                 :            : 
     273                 :            : static const struct pstream_class fd_pstream_class = {
     274                 :            :     "pstream",
     275                 :            :     false,
     276                 :            :     NULL,
     277                 :            :     pfd_close,
     278                 :            :     pfd_accept,
     279                 :            :     pfd_wait,
     280                 :            : };
     281                 :            : 
     282                 :            : /* Helper functions. */
     283                 :            : static void
     284                 :       4907 : maybe_unlink_and_free(char *path)
     285                 :            : {
     286         [ +  + ]:       4907 :     if (path) {
     287                 :       4799 :         fatal_signal_unlink_file_now(path);
     288                 :       4799 :         free(path);
     289                 :            :     }
     290                 :       4907 : }

Generated by: LCOV version 1.12