Coverage Report

Created: 2026-06-22 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/stream-fd.c
Line
Count
Source
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 "openvswitch/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
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
 * Takes ownership of 'name'.
57
 *
58
 * Returns 0 if successful, otherwise a positive errno value.  (The current
59
 * implementation never fails.) */
60
int
61
new_fd_stream(char *name, int fd, int connect_status, int fd_type,
62
              struct stream **streamp)
63
0
{
64
0
    struct stream_fd *s;
65
66
0
    s = xmalloc(sizeof *s);
67
0
    stream_init(&s->stream, &stream_fd_class, connect_status, name);
68
0
    s->fd = fd;
69
0
    s->fd_type = fd_type;
70
0
    *streamp = &s->stream;
71
0
    return 0;
72
0
}
73
74
static struct stream_fd *
75
stream_fd_cast(struct stream *stream)
76
0
{
77
0
    stream_assert_class(stream, &stream_fd_class);
78
0
    return CONTAINER_OF(stream, struct stream_fd, stream);
79
0
}
80
81
static void
82
fd_close(struct stream *stream)
83
0
{
84
0
    struct stream_fd *s = stream_fd_cast(stream);
85
0
    closesocket(s->fd);
86
0
    free(s);
87
0
}
88
89
static int
90
fd_connect(struct stream *stream)
91
0
{
92
0
    struct stream_fd *s = stream_fd_cast(stream);
93
0
    int retval = check_connection_completion(s->fd);
94
0
    if (retval == 0 && s->fd_type == AF_INET) {
95
0
        setsockopt_tcp_nodelay(s->fd);
96
0
    }
97
0
    return retval;
98
0
}
99
100
static ssize_t
101
fd_recv(struct stream *stream, void *buffer, size_t n)
102
0
{
103
0
    struct stream_fd *s = stream_fd_cast(stream);
104
0
    ssize_t retval;
105
0
    int error;
106
107
0
    retval = recv(s->fd, buffer, n, 0);
108
0
    if (retval < 0) {
109
0
        error = sock_errno();
110
0
        if (error != EAGAIN) {
111
0
            VLOG_DBG_RL(&rl, "recv: %s", sock_strerror(error));
112
0
        }
113
0
        return -error;
114
0
    }
115
0
    return retval;
116
0
}
117
118
static ssize_t
119
fd_send(struct stream *stream, const void *buffer, size_t n)
120
0
{
121
0
    struct stream_fd *s = stream_fd_cast(stream);
122
0
    ssize_t retval;
123
0
    int error;
124
125
0
    retval = send(s->fd, buffer, n, 0);
126
0
    if (retval < 0) {
127
0
        error = sock_errno();
128
0
        if (error != EAGAIN) {
129
0
            VLOG_DBG_RL(&rl, "send: %s", sock_strerror(error));
130
0
        }
131
0
        return -error;
132
0
    }
133
0
    return (retval > 0 ? retval : -EAGAIN);
134
0
}
135
136
static void
137
fd_wait(struct stream *stream, enum stream_wait_type wait)
138
0
{
139
0
    struct stream_fd *s = stream_fd_cast(stream);
140
0
    switch (wait) {
141
0
    case STREAM_CONNECT:
142
0
    case STREAM_SEND:
143
0
        poll_fd_wait(s->fd, POLLOUT);
144
0
        break;
145
146
0
    case STREAM_RECV:
147
0
        poll_fd_wait(s->fd, POLLIN);
148
0
        break;
149
150
0
    default:
151
0
        OVS_NOT_REACHED();
152
0
    }
153
0
}
154
155
static const struct stream_class stream_fd_class = {
156
    "fd",                       /* name */
157
    false,                      /* needs_probes */
158
    NULL,                       /* open */
159
    fd_close,                   /* close */
160
    fd_connect,                 /* connect */
161
    fd_recv,                    /* recv */
162
    fd_send,                    /* send */
163
    NULL,                       /* run */
164
    NULL,                       /* run_wait */
165
    fd_wait,                    /* wait */
166
};
167

168
/* Passive file descriptor stream. */
169
170
struct fd_pstream
171
{
172
    struct pstream pstream;
173
    int fd;
174
    int (*accept_cb)(int fd, const struct sockaddr_storage *, size_t ss_len,
175
                     struct stream **);
176
    char *unlink_path;
177
};
178
179
static const struct pstream_class fd_pstream_class;
180
181
static struct fd_pstream *
182
fd_pstream_cast(struct pstream *pstream)
183
0
{
184
0
    pstream_assert_class(pstream, &fd_pstream_class);
185
0
    return CONTAINER_OF(pstream, struct fd_pstream, pstream);
186
0
}
187
188
/* Creates a new pstream named 'name' that will accept new socket connections
189
 * on 'fd' and stores a pointer to the stream in '*pstreamp'.
190
 *
191
 * When a connection has been accepted, 'accept_cb' will be called with the new
192
 * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
193
 * accept_cb must return 0 if the connection is successful, in which case it
194
 * must initialize '*streamp' to the new stream, or a positive errno value on
195
 * error.  In either case accept_cb takes ownership of the 'fd' passed in.
196
 *
197
 * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
198
 * to fatal_signal_unlink_file_now() and freed with free().
199
 *
200
 * Takes ownership of 'name'.
201
 *
202
 * Returns 0 if successful, otherwise a positive errno value.  (The current
203
 * implementation never fails.) */
204
int
205
new_fd_pstream(char *name, int fd,
206
               int (*accept_cb)(int fd, const struct sockaddr_storage *ss,
207
                                size_t ss_len, struct stream **streamp),
208
               char *unlink_path, struct pstream **pstreamp)
209
0
{
210
0
    struct fd_pstream *ps = xmalloc(sizeof *ps);
211
0
    pstream_init(&ps->pstream, &fd_pstream_class, name);
212
0
    ps->fd = fd;
213
0
    ps->accept_cb = accept_cb;
214
0
    ps->unlink_path = unlink_path;
215
0
    *pstreamp = &ps->pstream;
216
0
    return 0;
217
0
}
218
219
static void
220
pfd_close(struct pstream *pstream)
221
0
{
222
0
    struct fd_pstream *ps = fd_pstream_cast(pstream);
223
0
    closesocket(ps->fd);
224
0
    maybe_unlink_and_free(ps->unlink_path);
225
0
    free(ps);
226
0
}
227
228
static int
229
pfd_accept(struct pstream *pstream, struct stream **new_streamp)
230
0
{
231
0
    struct fd_pstream *ps = fd_pstream_cast(pstream);
232
0
    struct sockaddr_storage ss;
233
0
    socklen_t ss_len = sizeof ss;
234
0
    int new_fd;
235
0
    int retval;
236
237
0
    new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
238
0
    if (new_fd < 0) {
239
0
        retval = sock_errno();
240
0
        if (retval != EAGAIN) {
241
0
            VLOG_DBG_RL(&rl, "accept: %s", sock_strerror(retval));
242
0
        }
243
0
        return retval;
244
0
    }
245
246
0
    retval = set_nonblocking(new_fd);
247
0
    if (retval) {
248
0
        closesocket(new_fd);
249
0
        return retval;
250
0
    }
251
252
0
    return ps->accept_cb(new_fd, &ss, ss_len, new_streamp);
253
0
}
254
255
static void
256
pfd_wait(struct pstream *pstream)
257
0
{
258
0
    struct fd_pstream *ps = fd_pstream_cast(pstream);
259
0
    poll_fd_wait(ps->fd, POLLIN);
260
0
}
261
262
static const struct pstream_class fd_pstream_class = {
263
    "pstream",
264
    false,
265
    NULL,
266
    pfd_close,
267
    pfd_accept,
268
    pfd_wait,
269
};
270

271
/* Helper functions. */
272
static void
273
maybe_unlink_and_free(char *path)
274
0
{
275
0
    if (path) {
276
0
        fatal_signal_unlink_file_now(path);
277
0
        free(path);
278
0
    }
279
0
}