]>
Commit | Line | Data |
---|---|---|
2fe27d5a | 1 | /* |
0a2869d5 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc. |
2fe27d5a BP |
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 "netlink-socket.h" | |
2fe27d5a BP |
19 | #include <errno.h> |
20 | #include <inttypes.h> | |
21 | #include <stdlib.h> | |
a86bd14e | 22 | #include <sys/socket.h> |
2fe27d5a | 23 | #include <sys/types.h> |
cc75061a | 24 | #include <sys/uio.h> |
2fe27d5a BP |
25 | #include <unistd.h> |
26 | #include "coverage.h" | |
3e8a2ad1 | 27 | #include "openvswitch/dynamic-string.h" |
2ad204c8 | 28 | #include "hash.h" |
ee89ea7b | 29 | #include "openvswitch/hmap.h" |
2fe27d5a BP |
30 | #include "netlink.h" |
31 | #include "netlink-protocol.h" | |
a86bd14e | 32 | #include "netnsid.h" |
886dd35a | 33 | #include "odp-netlink.h" |
64c96779 | 34 | #include "openvswitch/ofpbuf.h" |
0bd01224 | 35 | #include "ovs-thread.h" |
fd016ae3 | 36 | #include "openvswitch/poll-loop.h" |
0672776e | 37 | #include "seq.h" |
6b7c12fd | 38 | #include "socket-util.h" |
cc75061a | 39 | #include "util.h" |
e6211adc | 40 | #include "openvswitch/vlog.h" |
2fe27d5a BP |
41 | |
42 | VLOG_DEFINE_THIS_MODULE(netlink_socket); | |
43 | ||
44 | COVERAGE_DEFINE(netlink_overflow); | |
45 | COVERAGE_DEFINE(netlink_received); | |
fc999dda | 46 | COVERAGE_DEFINE(netlink_recv_jumbo); |
2fe27d5a BP |
47 | COVERAGE_DEFINE(netlink_sent); |
48 | ||
49 | /* Linux header file confusion causes this to be undefined. */ | |
50 | #ifndef SOL_NETLINK | |
51 | #define SOL_NETLINK 270 | |
52 | #endif | |
53 | ||
54 | /* A single (bad) Netlink message can in theory dump out many, many log | |
55 | * messages, so the burst size is set quite high here to avoid missing useful | |
56 | * information. Also, at high logging levels we log *all* Netlink messages. */ | |
57 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600); | |
58 | ||
7d7447df | 59 | static uint32_t nl_sock_allocate_seq(struct nl_sock *, unsigned int n); |
2fe27d5a | 60 | static void log_nlmsg(const char *function, int error, |
7041c3a9 | 61 | const void *message, size_t size, int protocol); |
886dd35a | 62 | #ifdef _WIN32 |
ebac7fb7 | 63 | static int get_sock_pid_from_kernel(struct nl_sock *sock); |
e70f55ed | 64 | static int set_sock_property(struct nl_sock *sock); |
74804b21 AS |
65 | static int nl_sock_transact(struct nl_sock *sock, const struct ofpbuf *request, |
66 | struct ofpbuf **replyp); | |
d553dd54 AS |
67 | |
68 | /* In the case DeviceIoControl failed and GetLastError returns with | |
69 | * ERROR_NOT_FOUND means we lost communication with the kernel device. | |
70 | * CloseHandle will fail because the handle in 'theory' does not exist. | |
71 | * The only remaining option is to crash and allow the service to be restarted | |
72 | * via service manager. This is the only way to close the handle from both | |
73 | * userspace and kernel. */ | |
74 | void | |
75 | lost_communication(DWORD last_err) | |
76 | { | |
77 | if (last_err == ERROR_NOT_FOUND) { | |
78 | ovs_abort(0, "lost communication with the kernel device"); | |
79 | } | |
80 | } | |
886dd35a | 81 | #endif |
2fe27d5a BP |
82 | \f |
83 | /* Netlink sockets. */ | |
84 | ||
0d121c73 | 85 | struct nl_sock { |
22326ba6 AS |
86 | #ifdef _WIN32 |
87 | HANDLE handle; | |
7fa09611 | 88 | OVERLAPPED overlapped; |
64513e68 | 89 | DWORD read_ioctl; |
22326ba6 | 90 | #else |
2fe27d5a | 91 | int fd; |
22326ba6 | 92 | #endif |
7d7447df | 93 | uint32_t next_seq; |
2fe27d5a | 94 | uint32_t pid; |
7041c3a9 | 95 | int protocol; |
cc75061a | 96 | unsigned int rcvbuf; /* Receive buffer size (SO_RCVBUF). */ |
2fe27d5a BP |
97 | }; |
98 | ||
cc75061a BP |
99 | /* Compile-time limit on iovecs, so that we can allocate a maximum-size array |
100 | * of iovecs on the stack. */ | |
101 | #define MAX_IOVS 128 | |
102 | ||
103 | /* Maximum number of iovecs that may be passed to sendmsg, capped at a | |
104 | * minimum of _XOPEN_IOV_MAX (16) and a maximum of MAX_IOVS. | |
105 | * | |
106 | * Initialized by nl_sock_create(). */ | |
107 | static int max_iovs; | |
108 | ||
a88b4e04 BP |
109 | static int nl_pool_alloc(int protocol, struct nl_sock **sockp); |
110 | static void nl_pool_release(struct nl_sock *); | |
2fe27d5a BP |
111 | |
112 | /* Creates a new netlink socket for the given netlink 'protocol' | |
113 | * (NETLINK_ROUTE, NETLINK_GENERIC, ...). Returns 0 and sets '*sockp' to the | |
a88b4e04 | 114 | * new socket if successful, otherwise returns a positive errno value. */ |
2fe27d5a | 115 | int |
cceb11f5 | 116 | nl_sock_create(int protocol, struct nl_sock **sockp) |
2fe27d5a | 117 | { |
0bd01224 | 118 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
2fe27d5a | 119 | struct nl_sock *sock; |
22326ba6 | 120 | #ifndef _WIN32 |
2fe27d5a | 121 | struct sockaddr_nl local, remote; |
22326ba6 | 122 | #endif |
2c5a6834 | 123 | socklen_t local_size; |
d2b9f5b0 | 124 | int rcvbuf; |
2fe27d5a BP |
125 | int retval = 0; |
126 | ||
0bd01224 | 127 | if (ovsthread_once_start(&once)) { |
cc75061a BP |
128 | int save_errno = errno; |
129 | errno = 0; | |
130 | ||
131 | max_iovs = sysconf(_SC_UIO_MAXIOV); | |
132 | if (max_iovs < _XOPEN_IOV_MAX) { | |
133 | if (max_iovs == -1 && errno) { | |
10a89ef0 | 134 | VLOG_WARN("sysconf(_SC_UIO_MAXIOV): %s", ovs_strerror(errno)); |
cc75061a BP |
135 | } |
136 | max_iovs = _XOPEN_IOV_MAX; | |
137 | } else if (max_iovs > MAX_IOVS) { | |
138 | max_iovs = MAX_IOVS; | |
139 | } | |
140 | ||
141 | errno = save_errno; | |
0bd01224 | 142 | ovsthread_once_done(&once); |
cc75061a BP |
143 | } |
144 | ||
2fe27d5a | 145 | *sockp = NULL; |
488232b7 | 146 | sock = xmalloc(sizeof *sock); |
2fe27d5a | 147 | |
22326ba6 | 148 | #ifdef _WIN32 |
e6b298ef | 149 | sock->overlapped.hEvent = NULL; |
7fa09611 EE |
150 | sock->handle = CreateFile(OVS_DEVICE_NAME_USER, |
151 | GENERIC_READ | GENERIC_WRITE, | |
152 | FILE_SHARE_READ | FILE_SHARE_WRITE, | |
153 | NULL, OPEN_EXISTING, | |
154 | FILE_FLAG_OVERLAPPED, NULL); | |
22326ba6 AS |
155 | |
156 | if (sock->handle == INVALID_HANDLE_VALUE) { | |
52a1540a | 157 | VLOG_ERR("fcntl: %s", ovs_lasterror_to_string()); |
7fa09611 EE |
158 | goto error; |
159 | } | |
160 | ||
161 | memset(&sock->overlapped, 0, sizeof sock->overlapped); | |
162 | sock->overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
163 | if (sock->overlapped.hEvent == NULL) { | |
52a1540a | 164 | VLOG_ERR("fcntl: %s", ovs_lasterror_to_string()); |
22326ba6 AS |
165 | goto error; |
166 | } | |
64513e68 EE |
167 | /* Initialize the type/ioctl to Generic */ |
168 | sock->read_ioctl = OVS_IOCTL_READ; | |
22326ba6 | 169 | #else |
2fe27d5a BP |
170 | sock->fd = socket(AF_NETLINK, SOCK_RAW, protocol); |
171 | if (sock->fd < 0) { | |
10a89ef0 | 172 | VLOG_ERR("fcntl: %s", ovs_strerror(errno)); |
2fe27d5a BP |
173 | goto error; |
174 | } | |
22326ba6 AS |
175 | #endif |
176 | ||
7041c3a9 | 177 | sock->protocol = protocol; |
7d7447df | 178 | sock->next_seq = 1; |
2fe27d5a | 179 | |
d2b9f5b0 | 180 | rcvbuf = 1024 * 1024; |
22326ba6 AS |
181 | #ifdef _WIN32 |
182 | sock->rcvbuf = rcvbuf; | |
ebac7fb7 | 183 | retval = get_sock_pid_from_kernel(sock); |
886dd35a NR |
184 | if (retval != 0) { |
185 | goto error; | |
186 | } | |
e70f55ed SV |
187 | retval = set_sock_property(sock); |
188 | if (retval != 0) { | |
189 | goto error; | |
190 | } | |
22326ba6 | 191 | #else |
d2b9f5b0 BP |
192 | if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUFFORCE, |
193 | &rcvbuf, sizeof rcvbuf)) { | |
80af5ee5 BP |
194 | /* Only root can use SO_RCVBUFFORCE. Everyone else gets EPERM. |
195 | * Warn only if the failure is therefore unexpected. */ | |
f28b6dd3 | 196 | if (errno != EPERM) { |
80af5ee5 | 197 | VLOG_WARN_RL(&rl, "setting %d-byte socket receive buffer failed " |
10a89ef0 | 198 | "(%s)", rcvbuf, ovs_strerror(errno)); |
80af5ee5 | 199 | } |
d2b9f5b0 BP |
200 | } |
201 | ||
cc75061a BP |
202 | retval = get_socket_rcvbuf(sock->fd); |
203 | if (retval < 0) { | |
204 | retval = -retval; | |
205 | goto error; | |
206 | } | |
207 | sock->rcvbuf = retval; | |
2e467950 | 208 | retval = 0; |
cc75061a | 209 | |
2c5a6834 | 210 | /* Connect to kernel (pid 0) as remote address. */ |
2fe27d5a BP |
211 | memset(&remote, 0, sizeof remote); |
212 | remote.nl_family = AF_NETLINK; | |
213 | remote.nl_pid = 0; | |
214 | if (connect(sock->fd, (struct sockaddr *) &remote, sizeof remote) < 0) { | |
10a89ef0 | 215 | VLOG_ERR("connect(0): %s", ovs_strerror(errno)); |
2c5a6834 BP |
216 | goto error; |
217 | } | |
218 | ||
219 | /* Obtain pid assigned by kernel. */ | |
220 | local_size = sizeof local; | |
221 | if (getsockname(sock->fd, (struct sockaddr *) &local, &local_size) < 0) { | |
10a89ef0 | 222 | VLOG_ERR("getsockname: %s", ovs_strerror(errno)); |
2c5a6834 BP |
223 | goto error; |
224 | } | |
225 | if (local_size < sizeof local || local.nl_family != AF_NETLINK) { | |
226 | VLOG_ERR("getsockname returned bad Netlink name"); | |
227 | retval = EINVAL; | |
228 | goto error; | |
2fe27d5a | 229 | } |
2c5a6834 | 230 | sock->pid = local.nl_pid; |
22326ba6 | 231 | #endif |
2fe27d5a | 232 | |
2fe27d5a BP |
233 | *sockp = sock; |
234 | return 0; | |
235 | ||
2fe27d5a BP |
236 | error: |
237 | if (retval == 0) { | |
238 | retval = errno; | |
239 | if (retval == 0) { | |
240 | retval = EINVAL; | |
241 | } | |
242 | } | |
22326ba6 | 243 | #ifdef _WIN32 |
7fa09611 EE |
244 | if (sock->overlapped.hEvent) { |
245 | CloseHandle(sock->overlapped.hEvent); | |
246 | } | |
22326ba6 AS |
247 | if (sock->handle != INVALID_HANDLE_VALUE) { |
248 | CloseHandle(sock->handle); | |
249 | } | |
250 | #else | |
2fe27d5a BP |
251 | if (sock->fd >= 0) { |
252 | close(sock->fd); | |
253 | } | |
22326ba6 | 254 | #endif |
2fe27d5a BP |
255 | free(sock); |
256 | return retval; | |
257 | } | |
258 | ||
c6eab56d BP |
259 | /* Creates a new netlink socket for the same protocol as 'src'. Returns 0 and |
260 | * sets '*sockp' to the new socket if successful, otherwise returns a positive | |
261 | * errno value. */ | |
262 | int | |
263 | nl_sock_clone(const struct nl_sock *src, struct nl_sock **sockp) | |
264 | { | |
265 | return nl_sock_create(src->protocol, sockp); | |
266 | } | |
267 | ||
2fe27d5a BP |
268 | /* Destroys netlink socket 'sock'. */ |
269 | void | |
270 | nl_sock_destroy(struct nl_sock *sock) | |
271 | { | |
272 | if (sock) { | |
22326ba6 | 273 | #ifdef _WIN32 |
7fa09611 EE |
274 | if (sock->overlapped.hEvent) { |
275 | CloseHandle(sock->overlapped.hEvent); | |
276 | } | |
22326ba6 AS |
277 | CloseHandle(sock->handle); |
278 | #else | |
a88b4e04 | 279 | close(sock->fd); |
22326ba6 | 280 | #endif |
a88b4e04 | 281 | free(sock); |
2fe27d5a BP |
282 | } |
283 | } | |
284 | ||
886dd35a NR |
285 | #ifdef _WIN32 |
286 | /* Reads the pid for 'sock' generated in the kernel datapath. The function | |
190cf533 SV |
287 | * uses a separate IOCTL instead of a transaction semantic to avoid unnecessary |
288 | * message overhead. */ | |
886dd35a | 289 | static int |
ebac7fb7 | 290 | get_sock_pid_from_kernel(struct nl_sock *sock) |
886dd35a | 291 | { |
190cf533 SV |
292 | uint32_t pid = 0; |
293 | int retval = 0; | |
294 | DWORD bytes = 0; | |
886dd35a | 295 | |
190cf533 SV |
296 | if (!DeviceIoControl(sock->handle, OVS_IOCTL_GET_PID, |
297 | NULL, 0, &pid, sizeof(pid), | |
886dd35a | 298 | &bytes, NULL)) { |
d553dd54 | 299 | lost_communication(GetLastError()); |
886dd35a | 300 | retval = EINVAL; |
886dd35a | 301 | } else { |
190cf533 | 302 | if (bytes < sizeof(pid)) { |
886dd35a | 303 | retval = EINVAL; |
190cf533 SV |
304 | } else { |
305 | sock->pid = pid; | |
886dd35a | 306 | } |
886dd35a | 307 | } |
886dd35a | 308 | |
886dd35a NR |
309 | return retval; |
310 | } | |
e70f55ed SV |
311 | |
312 | /* Used for setting and managing socket properties in userspace and kernel. | |
313 | * Currently two attributes are tracked - pid and protocol | |
314 | * protocol - supplied by userspace based on the netlink family. Windows uses | |
315 | * this property to set the value in kernel datapath. | |
316 | * eg: (NETLINK_GENERIC/ NETLINK_NETFILTER) | |
317 | * pid - generated by windows kernel and set in userspace. The property | |
318 | * is not modified. | |
319 | * Also verify if Protocol and PID in Kernel reflects the values in userspace | |
320 | * */ | |
321 | static int | |
322 | set_sock_property(struct nl_sock *sock) | |
323 | { | |
324 | static const struct nl_policy ovs_socket_policy[] = { | |
325 | [OVS_NL_ATTR_SOCK_PROTO] = { .type = NL_A_BE32, .optional = true }, | |
326 | [OVS_NL_ATTR_SOCK_PID] = { .type = NL_A_BE32, .optional = true } | |
327 | }; | |
328 | ||
329 | struct ofpbuf request, *reply; | |
330 | struct ovs_header *ovs_header; | |
331 | struct nlattr *attrs[ARRAY_SIZE(ovs_socket_policy)]; | |
332 | int retval = 0; | |
333 | int error; | |
334 | ||
335 | ofpbuf_init(&request, 0); | |
336 | nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0, | |
337 | OVS_CTRL_CMD_SOCK_PROP, OVS_WIN_CONTROL_VERSION); | |
338 | ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header); | |
339 | ovs_header->dp_ifindex = 0; | |
340 | ||
341 | nl_msg_put_be32(&request, OVS_NL_ATTR_SOCK_PROTO, sock->protocol); | |
342 | /* pid is already set as part of get_sock_pid_from_kernel() | |
343 | * This is added to maintain consistency | |
344 | */ | |
345 | nl_msg_put_be32(&request, OVS_NL_ATTR_SOCK_PID, sock->pid); | |
346 | ||
347 | error = nl_sock_transact(sock, &request, &reply); | |
348 | ofpbuf_uninit(&request); | |
349 | if (error) { | |
350 | retval = EINVAL; | |
351 | } | |
352 | ||
353 | if (!nl_policy_parse(reply, | |
354 | NLMSG_HDRLEN + GENL_HDRLEN + sizeof *ovs_header, | |
355 | ovs_socket_policy, attrs, | |
356 | ARRAY_SIZE(ovs_socket_policy))) { | |
357 | ofpbuf_delete(reply); | |
358 | retval = EINVAL; | |
359 | } | |
360 | /* Verify if the properties are setup properly */ | |
361 | if (attrs[OVS_NL_ATTR_SOCK_PROTO]) { | |
362 | int protocol = nl_attr_get_be32(attrs[OVS_NL_ATTR_SOCK_PROTO]); | |
363 | if (protocol != sock->protocol) { | |
364 | VLOG_ERR("Invalid protocol returned:%d expected:%d", | |
365 | protocol, sock->protocol); | |
366 | retval = EINVAL; | |
367 | } | |
368 | } | |
369 | ||
370 | if (attrs[OVS_NL_ATTR_SOCK_PID]) { | |
371 | int pid = nl_attr_get_be32(attrs[OVS_NL_ATTR_SOCK_PID]); | |
372 | if (pid != sock->pid) { | |
373 | VLOG_ERR("Invalid pid returned:%d expected:%d", | |
374 | pid, sock->pid); | |
375 | retval = EINVAL; | |
376 | } | |
377 | } | |
378 | ||
379 | return retval; | |
380 | } | |
886dd35a NR |
381 | #endif /* _WIN32 */ |
382 | ||
b8f958ea EE |
383 | #ifdef _WIN32 |
384 | static int __inline | |
385 | nl_sock_mcgroup(struct nl_sock *sock, unsigned int multicast_group, bool join) | |
386 | { | |
387 | struct ofpbuf request; | |
388 | uint64_t request_stub[128]; | |
389 | struct ovs_header *ovs_header; | |
390 | struct nlmsghdr *nlmsg; | |
391 | int error; | |
392 | ||
393 | ofpbuf_use_stub(&request, request_stub, sizeof request_stub); | |
394 | ||
395 | nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0, | |
396 | OVS_CTRL_CMD_MC_SUBSCRIBE_REQ, | |
397 | OVS_WIN_CONTROL_VERSION); | |
398 | ||
399 | ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header); | |
400 | ovs_header->dp_ifindex = 0; | |
401 | ||
402 | nl_msg_put_u32(&request, OVS_NL_ATTR_MCAST_GRP, multicast_group); | |
403 | nl_msg_put_u8(&request, OVS_NL_ATTR_MCAST_JOIN, join ? 1 : 0); | |
404 | ||
405 | error = nl_sock_send(sock, &request, true); | |
406 | ofpbuf_uninit(&request); | |
407 | return error; | |
408 | } | |
409 | #endif | |
cceb11f5 BP |
410 | /* Tries to add 'sock' as a listener for 'multicast_group'. Returns 0 if |
411 | * successful, otherwise a positive errno value. | |
412 | * | |
a838c4fe BP |
413 | * A socket that is subscribed to a multicast group that receives asynchronous |
414 | * notifications must not be used for Netlink transactions or dumps, because | |
415 | * transactions and dumps can cause notifications to be lost. | |
416 | * | |
cceb11f5 BP |
417 | * Multicast group numbers are always positive. |
418 | * | |
419 | * It is not an error to attempt to join a multicast group to which a socket | |
420 | * already belongs. */ | |
421 | int | |
422 | nl_sock_join_mcgroup(struct nl_sock *sock, unsigned int multicast_group) | |
423 | { | |
22326ba6 | 424 | #ifdef _WIN32 |
64513e68 EE |
425 | /* Set the socket type as a "multicast" socket */ |
426 | sock->read_ioctl = OVS_IOCTL_READ_EVENT; | |
b8f958ea EE |
427 | int error = nl_sock_mcgroup(sock, multicast_group, true); |
428 | if (error) { | |
64513e68 | 429 | sock->read_ioctl = OVS_IOCTL_READ; |
b8f958ea | 430 | VLOG_WARN("could not join multicast group %u (%s)", |
15fd9052 NR |
431 | multicast_group, ovs_strerror(error)); |
432 | return error; | |
b8f958ea | 433 | } |
22326ba6 | 434 | #else |
cceb11f5 BP |
435 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, |
436 | &multicast_group, sizeof multicast_group) < 0) { | |
437 | VLOG_WARN("could not join multicast group %u (%s)", | |
10a89ef0 | 438 | multicast_group, ovs_strerror(errno)); |
cceb11f5 BP |
439 | return errno; |
440 | } | |
22326ba6 | 441 | #endif |
cceb11f5 BP |
442 | return 0; |
443 | } | |
444 | ||
cf114a7f FL |
445 | /* When 'enable' is true, it tries to enable 'sock' to receive netlink |
446 | * notifications form all network namespaces that have an nsid assigned | |
447 | * into the network namespace where the socket has been opened. The | |
448 | * running kernel needs to provide support for that. When 'enable' is | |
449 | * false, it will receive netlink notifications only from the network | |
450 | * namespace where the socket has been opened. | |
451 | * | |
452 | * Returns 0 if successful, otherwise a positive errno. */ | |
453 | int | |
454 | nl_sock_listen_all_nsid(struct nl_sock *sock, bool enable) | |
455 | { | |
456 | int error; | |
457 | int val = enable ? 1 : 0; | |
458 | ||
459 | #ifndef _WIN32 | |
460 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &val, | |
461 | sizeof val) < 0) { | |
462 | error = errno; | |
463 | VLOG_INFO("netlink: could not %s listening to all nsid (%s)", | |
464 | enable ? "enable" : "disable", ovs_strerror(error)); | |
465 | return errno; | |
466 | } | |
467 | #endif | |
468 | ||
469 | return 0; | |
470 | } | |
471 | ||
36791e21 | 472 | #ifdef _WIN32 |
74804b21 AS |
473 | int |
474 | nl_sock_subscribe_packet__(struct nl_sock *sock, bool subscribe) | |
475 | { | |
476 | struct ofpbuf request; | |
477 | uint64_t request_stub[128]; | |
478 | struct ovs_header *ovs_header; | |
479 | struct nlmsghdr *nlmsg; | |
480 | int error; | |
481 | ||
482 | ofpbuf_use_stub(&request, request_stub, sizeof request_stub); | |
483 | nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0, | |
484 | OVS_CTRL_CMD_PACKET_SUBSCRIBE_REQ, | |
485 | OVS_WIN_CONTROL_VERSION); | |
486 | ||
487 | ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header); | |
488 | ovs_header->dp_ifindex = 0; | |
489 | nl_msg_put_u8(&request, OVS_NL_ATTR_PACKET_SUBSCRIBE, subscribe ? 1 : 0); | |
490 | nl_msg_put_u32(&request, OVS_NL_ATTR_PACKET_PID, sock->pid); | |
491 | ||
492 | error = nl_sock_send(sock, &request, true); | |
493 | ofpbuf_uninit(&request); | |
494 | return error; | |
495 | } | |
496 | ||
36791e21 NR |
497 | int |
498 | nl_sock_subscribe_packets(struct nl_sock *sock) | |
499 | { | |
500 | int error; | |
501 | ||
502 | if (sock->read_ioctl != OVS_IOCTL_READ) { | |
503 | return EINVAL; | |
504 | } | |
505 | ||
506 | error = nl_sock_subscribe_packet__(sock, true); | |
507 | if (error) { | |
d8d1ef2f AS |
508 | VLOG_WARN("could not subscribe packets (%s)", |
509 | ovs_strerror(error)); | |
36791e21 NR |
510 | return error; |
511 | } | |
512 | sock->read_ioctl = OVS_IOCTL_READ_PACKET; | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
517 | int | |
518 | nl_sock_unsubscribe_packets(struct nl_sock *sock) | |
519 | { | |
520 | ovs_assert(sock->read_ioctl == OVS_IOCTL_READ_PACKET); | |
521 | ||
522 | int error = nl_sock_subscribe_packet__(sock, false); | |
523 | if (error) { | |
d8d1ef2f AS |
524 | VLOG_WARN("could not unsubscribe to packets (%s)", |
525 | ovs_strerror(error)); | |
36791e21 NR |
526 | return error; |
527 | } | |
528 | ||
529 | sock->read_ioctl = OVS_IOCTL_READ; | |
530 | return 0; | |
531 | } | |
36791e21 NR |
532 | #endif |
533 | ||
cceb11f5 BP |
534 | /* Tries to make 'sock' stop listening to 'multicast_group'. Returns 0 if |
535 | * successful, otherwise a positive errno value. | |
536 | * | |
537 | * Multicast group numbers are always positive. | |
538 | * | |
539 | * It is not an error to attempt to leave a multicast group to which a socket | |
540 | * does not belong. | |
541 | * | |
542 | * On success, reading from 'sock' will still return any messages that were | |
543 | * received on 'multicast_group' before the group was left. */ | |
544 | int | |
545 | nl_sock_leave_mcgroup(struct nl_sock *sock, unsigned int multicast_group) | |
546 | { | |
22326ba6 | 547 | #ifdef _WIN32 |
b8f958ea EE |
548 | int error = nl_sock_mcgroup(sock, multicast_group, false); |
549 | if (error) { | |
550 | VLOG_WARN("could not leave multicast group %u (%s)", | |
15fd9052 NR |
551 | multicast_group, ovs_strerror(error)); |
552 | return error; | |
b8f958ea | 553 | } |
64513e68 | 554 | sock->read_ioctl = OVS_IOCTL_READ; |
22326ba6 | 555 | #else |
cceb11f5 BP |
556 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, |
557 | &multicast_group, sizeof multicast_group) < 0) { | |
558 | VLOG_WARN("could not leave multicast group %u (%s)", | |
10a89ef0 | 559 | multicast_group, ovs_strerror(errno)); |
cceb11f5 BP |
560 | return errno; |
561 | } | |
22326ba6 | 562 | #endif |
cceb11f5 BP |
563 | return 0; |
564 | } | |
565 | ||
c6eab56d | 566 | static int |
ff459dd6 BP |
567 | nl_sock_send__(struct nl_sock *sock, const struct ofpbuf *msg, |
568 | uint32_t nlmsg_seq, bool wait) | |
2fe27d5a BP |
569 | { |
570 | struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(msg); | |
571 | int error; | |
572 | ||
6fd6ed71 | 573 | nlmsg->nlmsg_len = msg->size; |
ff459dd6 | 574 | nlmsg->nlmsg_seq = nlmsg_seq; |
2fe27d5a BP |
575 | nlmsg->nlmsg_pid = sock->pid; |
576 | do { | |
577 | int retval; | |
22326ba6 | 578 | #ifdef _WIN32 |
fd972eb8 NR |
579 | DWORD bytes; |
580 | ||
581 | if (!DeviceIoControl(sock->handle, OVS_IOCTL_WRITE, | |
6fd6ed71 | 582 | msg->data, msg->size, NULL, 0, |
0fd22ae2 | 583 | &bytes, NULL)) { |
d553dd54 | 584 | lost_communication(GetLastError()); |
22326ba6 | 585 | retval = -1; |
fd972eb8 NR |
586 | /* XXX: Map to a more appropriate error based on GetLastError(). */ |
587 | errno = EINVAL; | |
a51a5086 | 588 | VLOG_DBG_RL(&rl, "fatal driver failure in write: %s", |
d553dd54 | 589 | ovs_lasterror_to_string()); |
fd972eb8 | 590 | } else { |
6fd6ed71 | 591 | retval = msg->size; |
22326ba6 AS |
592 | } |
593 | #else | |
6fd6ed71 | 594 | retval = send(sock->fd, msg->data, msg->size, |
fd972eb8 | 595 | wait ? 0 : MSG_DONTWAIT); |
22326ba6 | 596 | #endif |
2fe27d5a BP |
597 | error = retval < 0 ? errno : 0; |
598 | } while (error == EINTR); | |
6fd6ed71 | 599 | log_nlmsg(__func__, error, msg->data, msg->size, sock->protocol); |
2fe27d5a BP |
600 | if (!error) { |
601 | COVERAGE_INC(netlink_sent); | |
602 | } | |
603 | return error; | |
604 | } | |
605 | ||
c6eab56d | 606 | /* Tries to send 'msg', which must contain a Netlink message, to the kernel on |
6fd6ed71 | 607 | * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, nlmsg_pid |
ff459dd6 BP |
608 | * will be set to 'sock''s pid, and nlmsg_seq will be initialized to a fresh |
609 | * sequence number, before the message is sent. | |
c6eab56d BP |
610 | * |
611 | * Returns 0 if successful, otherwise a positive errno value. If | |
612 | * 'wait' is true, then the send will wait until buffer space is ready; | |
613 | * otherwise, returns EAGAIN if the 'sock' send buffer is full. */ | |
614 | int | |
615 | nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait) | |
ff459dd6 BP |
616 | { |
617 | return nl_sock_send_seq(sock, msg, nl_sock_allocate_seq(sock, 1), wait); | |
618 | } | |
619 | ||
620 | /* Tries to send 'msg', which must contain a Netlink message, to the kernel on | |
6fd6ed71 | 621 | * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, nlmsg_pid |
ff459dd6 BP |
622 | * will be set to 'sock''s pid, and nlmsg_seq will be initialized to |
623 | * 'nlmsg_seq', before the message is sent. | |
624 | * | |
625 | * Returns 0 if successful, otherwise a positive errno value. If | |
626 | * 'wait' is true, then the send will wait until buffer space is ready; | |
627 | * otherwise, returns EAGAIN if the 'sock' send buffer is full. | |
628 | * | |
629 | * This function is suitable for sending a reply to a request that was received | |
630 | * with sequence number 'nlmsg_seq'. Otherwise, use nl_sock_send() instead. */ | |
631 | int | |
632 | nl_sock_send_seq(struct nl_sock *sock, const struct ofpbuf *msg, | |
633 | uint32_t nlmsg_seq, bool wait) | |
c6eab56d | 634 | { |
ff459dd6 | 635 | return nl_sock_send__(sock, msg, nlmsg_seq, wait); |
c6eab56d BP |
636 | } |
637 | ||
c6eab56d | 638 | static int |
a86bd14e | 639 | nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, int *nsid, bool wait) |
2fe27d5a | 640 | { |
72d32ac0 BP |
641 | /* We can't accurately predict the size of the data to be received. The |
642 | * caller is supposed to have allocated enough space in 'buf' to handle the | |
643 | * "typical" case. To handle exceptions, we make available enough space in | |
644 | * 'tail' to allow Netlink messages to be up to 64 kB long (a reasonable | |
645 | * figure since that's the maximum length of a Netlink attribute). */ | |
2fe27d5a | 646 | struct nlmsghdr *nlmsghdr; |
72d32ac0 | 647 | uint8_t tail[65536]; |
fc999dda | 648 | struct iovec iov[2]; |
fc999dda | 649 | struct msghdr msg; |
a86bd14e FL |
650 | uint8_t msgctrl[64]; |
651 | struct cmsghdr *cmsg; | |
fc999dda | 652 | ssize_t retval; |
a86bd14e | 653 | int *ptr; |
8f20fd98 | 654 | int error; |
fc999dda | 655 | |
cb22974d | 656 | ovs_assert(buf->allocated >= sizeof *nlmsghdr); |
72d32ac0 | 657 | ofpbuf_clear(buf); |
2fe27d5a | 658 | |
6fd6ed71 | 659 | iov[0].iov_base = buf->base; |
72d32ac0 | 660 | iov[0].iov_len = buf->allocated; |
fc999dda | 661 | iov[1].iov_base = tail; |
72d32ac0 | 662 | iov[1].iov_len = sizeof tail; |
fc999dda BP |
663 | |
664 | memset(&msg, 0, sizeof msg); | |
665 | msg.msg_iov = iov; | |
666 | msg.msg_iovlen = 2; | |
a86bd14e FL |
667 | msg.msg_control = msgctrl; |
668 | msg.msg_controllen = sizeof msgctrl; | |
fc999dda | 669 | |
8f20fd98 BP |
670 | /* Receive a Netlink message from the kernel. |
671 | * | |
672 | * This works around a kernel bug in which the kernel returns an error code | |
673 | * as if it were the number of bytes read. It doesn't actually modify | |
674 | * anything in the receive buffer in that case, so we can initialize the | |
675 | * Netlink header with an impossible message length and then, upon success, | |
676 | * check whether it changed. */ | |
6fd6ed71 | 677 | nlmsghdr = buf->base; |
2fe27d5a | 678 | do { |
8f20fd98 | 679 | nlmsghdr->nlmsg_len = UINT32_MAX; |
22326ba6 | 680 | #ifdef _WIN32 |
fd972eb8 | 681 | DWORD bytes; |
64513e68 | 682 | if (!DeviceIoControl(sock->handle, sock->read_ioctl, |
fd972eb8 | 683 | NULL, 0, tail, sizeof tail, &bytes, NULL)) { |
d553dd54 | 684 | lost_communication(GetLastError()); |
a51a5086 | 685 | VLOG_DBG_RL(&rl, "fatal driver failure in transact: %s", |
d71c423e | 686 | ovs_lasterror_to_string()); |
22326ba6 | 687 | retval = -1; |
a51a5086 | 688 | /* XXX: Map to a more appropriate error. */ |
fd972eb8 | 689 | errno = EINVAL; |
22326ba6 | 690 | } else { |
fd972eb8 NR |
691 | retval = bytes; |
692 | if (retval == 0) { | |
693 | retval = -1; | |
694 | errno = EAGAIN; | |
695 | } else { | |
696 | if (retval >= buf->allocated) { | |
697 | ofpbuf_reinit(buf, retval); | |
6fd6ed71 | 698 | nlmsghdr = buf->base; |
15fd9052 | 699 | nlmsghdr->nlmsg_len = UINT32_MAX; |
fd972eb8 | 700 | } |
6fd6ed71 PS |
701 | memcpy(buf->data, tail, retval); |
702 | buf->size = retval; | |
fd972eb8 | 703 | } |
22326ba6 AS |
704 | } |
705 | #else | |
fc999dda | 706 | retval = recvmsg(sock->fd, &msg, wait ? 0 : MSG_DONTWAIT); |
22326ba6 | 707 | #endif |
8f20fd98 BP |
708 | error = (retval < 0 ? errno |
709 | : retval == 0 ? ECONNRESET /* not possible? */ | |
710 | : nlmsghdr->nlmsg_len != UINT32_MAX ? 0 | |
7f8e2646 | 711 | : retval); |
8f20fd98 BP |
712 | } while (error == EINTR); |
713 | if (error) { | |
fc999dda BP |
714 | if (error == ENOBUFS) { |
715 | /* Socket receive buffer overflow dropped one or more messages that | |
716 | * the kernel tried to send to us. */ | |
717 | COVERAGE_INC(netlink_overflow); | |
718 | } | |
fc999dda | 719 | return error; |
2fe27d5a | 720 | } |
fc999dda | 721 | |
2fe27d5a | 722 | if (msg.msg_flags & MSG_TRUNC) { |
34582733 | 723 | VLOG_ERR_RL(&rl, "truncated message (longer than %"PRIuSIZE" bytes)", |
72d32ac0 | 724 | sizeof tail); |
fc999dda | 725 | return E2BIG; |
2fe27d5a | 726 | } |
2fe27d5a | 727 | |
fc999dda | 728 | if (retval < sizeof *nlmsghdr |
2fe27d5a | 729 | || nlmsghdr->nlmsg_len < sizeof *nlmsghdr |
fc999dda | 730 | || nlmsghdr->nlmsg_len > retval) { |
e5e4b47c | 731 | VLOG_ERR_RL(&rl, "received invalid nlmsg (%"PRIuSIZE" bytes < %"PRIuSIZE")", |
72d32ac0 | 732 | retval, sizeof *nlmsghdr); |
2fe27d5a BP |
733 | return EPROTO; |
734 | } | |
22326ba6 | 735 | #ifndef _WIN32 |
6fd6ed71 | 736 | buf->size = MIN(retval, buf->allocated); |
72d32ac0 BP |
737 | if (retval > buf->allocated) { |
738 | COVERAGE_INC(netlink_recv_jumbo); | |
739 | ofpbuf_put(buf, tail, retval - buf->allocated); | |
740 | } | |
22326ba6 | 741 | #endif |
72d32ac0 | 742 | |
a86bd14e FL |
743 | if (nsid) { |
744 | /* The network namespace id from which the message was sent comes | |
745 | * as ancillary data. For older kernels, this data is either not | |
746 | * available or it might be -1, so it falls back to local network | |
747 | * namespace (no id). Latest kernels return a valid ID only if | |
748 | * available or nothing. */ | |
749 | netnsid_set_local(nsid); | |
16b9bae8 | 750 | #ifndef _WIN32 |
a86bd14e FL |
751 | cmsg = CMSG_FIRSTHDR(&msg); |
752 | while (cmsg != NULL) { | |
753 | if (cmsg->cmsg_level == SOL_NETLINK | |
754 | && cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID) { | |
755 | ptr = ALIGNED_CAST(int *, CMSG_DATA(cmsg)); | |
756 | netnsid_set(nsid, *ptr); | |
757 | } | |
758 | if (cmsg->cmsg_level == SOL_SOCKET | |
759 | && cmsg->cmsg_type == SCM_RIGHTS) { | |
760 | /* This is unexpected and unwanted, close all fds */ | |
761 | int nfds; | |
762 | int i; | |
763 | nfds = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) | |
764 | / sizeof(int); | |
765 | ptr = ALIGNED_CAST(int *, CMSG_DATA(cmsg)); | |
766 | for (i = 0; i < nfds; i++) { | |
767 | VLOG_ERR_RL(&rl, "closing unexpected received fd (%d).", | |
768 | ptr[i]); | |
769 | close(ptr[i]); | |
770 | } | |
771 | } | |
772 | ||
773 | cmsg = CMSG_NXTHDR(&msg, cmsg); | |
774 | } | |
16b9bae8 | 775 | #endif |
a86bd14e FL |
776 | } |
777 | ||
6fd6ed71 | 778 | log_nlmsg(__func__, 0, buf->data, buf->size, sock->protocol); |
2fe27d5a BP |
779 | COVERAGE_INC(netlink_received); |
780 | ||
781 | return 0; | |
782 | } | |
783 | ||
72d32ac0 BP |
784 | /* Tries to receive a Netlink message from the kernel on 'sock' into 'buf'. If |
785 | * 'wait' is true, waits for a message to be ready. Otherwise, fails with | |
a86bd14e FL |
786 | * EAGAIN if the 'sock' receive buffer is empty. If 'nsid' is provided, the |
787 | * network namespace id from which the message was sent will be provided. | |
72d32ac0 BP |
788 | * |
789 | * The caller must have initialized 'buf' with an allocation of at least | |
790 | * NLMSG_HDRLEN bytes. For best performance, the caller should allocate enough | |
791 | * space for a "typical" message. | |
792 | * | |
793 | * On success, returns 0 and replaces 'buf''s previous content by the received | |
794 | * message. This function expands 'buf''s allocated memory, as necessary, to | |
795 | * hold the actual size of the received message. | |
c6eab56d | 796 | * |
72d32ac0 BP |
797 | * On failure, returns a positive errno value and clears 'buf' to zero length. |
798 | * 'buf' retains its previous memory allocation. | |
799 | * | |
800 | * Regardless of success or failure, this function resets 'buf''s headroom to | |
801 | * 0. */ | |
c6eab56d | 802 | int |
a86bd14e | 803 | nl_sock_recv(struct nl_sock *sock, struct ofpbuf *buf, int *nsid, bool wait) |
c6eab56d | 804 | { |
a86bd14e | 805 | return nl_sock_recv__(sock, buf, nsid, wait); |
cc75061a BP |
806 | } |
807 | ||
808 | static void | |
809 | nl_sock_record_errors__(struct nl_transaction **transactions, size_t n, | |
810 | int error) | |
811 | { | |
812 | size_t i; | |
813 | ||
814 | for (i = 0; i < n; i++) { | |
72d32ac0 BP |
815 | struct nl_transaction *txn = transactions[i]; |
816 | ||
817 | txn->error = error; | |
818 | if (txn->reply) { | |
819 | ofpbuf_clear(txn->reply); | |
820 | } | |
cc75061a BP |
821 | } |
822 | } | |
823 | ||
824 | static int | |
825 | nl_sock_transact_multiple__(struct nl_sock *sock, | |
826 | struct nl_transaction **transactions, size_t n, | |
827 | size_t *done) | |
828 | { | |
72d32ac0 BP |
829 | uint64_t tmp_reply_stub[1024 / 8]; |
830 | struct nl_transaction tmp_txn; | |
831 | struct ofpbuf tmp_reply; | |
832 | ||
833 | uint32_t base_seq; | |
cc75061a BP |
834 | struct iovec iovs[MAX_IOVS]; |
835 | struct msghdr msg; | |
836 | int error; | |
837 | int i; | |
838 | ||
72d32ac0 | 839 | base_seq = nl_sock_allocate_seq(sock, n); |
cc75061a BP |
840 | *done = 0; |
841 | for (i = 0; i < n; i++) { | |
72d32ac0 BP |
842 | struct nl_transaction *txn = transactions[i]; |
843 | struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(txn->request); | |
cc75061a | 844 | |
6fd6ed71 | 845 | nlmsg->nlmsg_len = txn->request->size; |
72d32ac0 | 846 | nlmsg->nlmsg_seq = base_seq + i; |
cc75061a | 847 | nlmsg->nlmsg_pid = sock->pid; |
cc75061a | 848 | |
6fd6ed71 PS |
849 | iovs[i].iov_base = txn->request->data; |
850 | iovs[i].iov_len = txn->request->size; | |
cc75061a BP |
851 | } |
852 | ||
0fd22ae2 | 853 | #ifndef _WIN32 |
cc75061a BP |
854 | memset(&msg, 0, sizeof msg); |
855 | msg.msg_iov = iovs; | |
856 | msg.msg_iovlen = n; | |
857 | do { | |
858 | error = sendmsg(sock->fd, &msg, 0) < 0 ? errno : 0; | |
859 | } while (error == EINTR); | |
860 | ||
861 | for (i = 0; i < n; i++) { | |
72d32ac0 | 862 | struct nl_transaction *txn = transactions[i]; |
cc75061a | 863 | |
6fd6ed71 PS |
864 | log_nlmsg(__func__, error, txn->request->data, |
865 | txn->request->size, sock->protocol); | |
cc75061a BP |
866 | } |
867 | if (!error) { | |
868 | COVERAGE_ADD(netlink_sent, n); | |
869 | } | |
870 | ||
871 | if (error) { | |
872 | return error; | |
873 | } | |
874 | ||
72d32ac0 BP |
875 | ofpbuf_use_stub(&tmp_reply, tmp_reply_stub, sizeof tmp_reply_stub); |
876 | tmp_txn.request = NULL; | |
877 | tmp_txn.reply = &tmp_reply; | |
878 | tmp_txn.error = 0; | |
cc75061a | 879 | while (n > 0) { |
72d32ac0 BP |
880 | struct nl_transaction *buf_txn, *txn; |
881 | uint32_t seq; | |
882 | ||
883 | /* Find a transaction whose buffer we can use for receiving a reply. | |
884 | * If no such transaction is left, use tmp_txn. */ | |
885 | buf_txn = &tmp_txn; | |
886 | for (i = 0; i < n; i++) { | |
887 | if (transactions[i]->reply) { | |
888 | buf_txn = transactions[i]; | |
889 | break; | |
890 | } | |
891 | } | |
cc75061a | 892 | |
72d32ac0 | 893 | /* Receive a reply. */ |
a86bd14e | 894 | error = nl_sock_recv__(sock, buf_txn->reply, NULL, false); |
72d32ac0 BP |
895 | if (error) { |
896 | if (error == EAGAIN) { | |
897 | nl_sock_record_errors__(transactions, n, 0); | |
898 | *done += n; | |
899 | error = 0; | |
900 | } | |
901 | break; | |
cc75061a BP |
902 | } |
903 | ||
72d32ac0 BP |
904 | /* Match the reply up with a transaction. */ |
905 | seq = nl_msg_nlmsghdr(buf_txn->reply)->nlmsg_seq; | |
906 | if (seq < base_seq || seq >= base_seq + n) { | |
907 | VLOG_DBG_RL(&rl, "ignoring unexpected seq %#"PRIx32, seq); | |
cc75061a BP |
908 | continue; |
909 | } | |
72d32ac0 BP |
910 | i = seq - base_seq; |
911 | txn = transactions[i]; | |
cc75061a | 912 | |
72d32ac0 BP |
913 | /* Fill in the results for 'txn'. */ |
914 | if (nl_msg_nlmsgerr(buf_txn->reply, &txn->error)) { | |
915 | if (txn->reply) { | |
916 | ofpbuf_clear(txn->reply); | |
917 | } | |
918 | if (txn->error) { | |
cc75061a | 919 | VLOG_DBG_RL(&rl, "received NAK error=%d (%s)", |
10a89ef0 | 920 | error, ovs_strerror(txn->error)); |
cc75061a | 921 | } |
cc75061a | 922 | } else { |
72d32ac0 BP |
923 | txn->error = 0; |
924 | if (txn->reply && txn != buf_txn) { | |
925 | /* Swap buffers. */ | |
926 | struct ofpbuf *reply = buf_txn->reply; | |
927 | buf_txn->reply = txn->reply; | |
928 | txn->reply = reply; | |
929 | } | |
cc75061a BP |
930 | } |
931 | ||
72d32ac0 BP |
932 | /* Fill in the results for transactions before 'txn'. (We have to do |
933 | * this after the results for 'txn' itself because of the buffer swap | |
934 | * above.) */ | |
935 | nl_sock_record_errors__(transactions, i, 0); | |
936 | ||
937 | /* Advance. */ | |
cc75061a BP |
938 | *done += i + 1; |
939 | transactions += i + 1; | |
940 | n -= i + 1; | |
72d32ac0 | 941 | base_seq += i + 1; |
cc75061a | 942 | } |
72d32ac0 | 943 | ofpbuf_uninit(&tmp_reply); |
0fd22ae2 NR |
944 | #else |
945 | error = 0; | |
9189184d | 946 | uint8_t reply_buf[65536]; |
0fd22ae2 NR |
947 | for (i = 0; i < n; i++) { |
948 | DWORD reply_len; | |
a51a5086 | 949 | bool ret; |
0fd22ae2 NR |
950 | struct nl_transaction *txn = transactions[i]; |
951 | struct nlmsghdr *request_nlmsg, *reply_nlmsg; | |
952 | ||
a51a5086 NR |
953 | ret = DeviceIoControl(sock->handle, OVS_IOCTL_TRANSACT, |
954 | txn->request->data, | |
955 | txn->request->size, | |
956 | reply_buf, sizeof reply_buf, | |
957 | &reply_len, NULL); | |
958 | ||
959 | if (ret && reply_len == 0) { | |
960 | /* | |
961 | * The current transaction did not produce any data to read and that | |
962 | * is not an error as such. Continue with the remainder of the | |
963 | * transactions. | |
964 | */ | |
965 | txn->error = 0; | |
966 | if (txn->reply) { | |
967 | ofpbuf_clear(txn->reply); | |
968 | } | |
969 | } else if (!ret) { | |
0fd22ae2 | 970 | /* XXX: Map to a more appropriate error. */ |
d553dd54 | 971 | lost_communication(GetLastError()); |
0fd22ae2 | 972 | error = EINVAL; |
b937e116 BP |
973 | VLOG_DBG_RL(&rl, "fatal driver failure: %s", |
974 | ovs_lasterror_to_string()); | |
0fd22ae2 NR |
975 | break; |
976 | } | |
977 | ||
a51a5086 | 978 | if (reply_len != 0) { |
7e86fe82 AS |
979 | request_nlmsg = nl_msg_nlmsghdr(txn->request); |
980 | ||
a51a5086 NR |
981 | if (reply_len < sizeof *reply_nlmsg) { |
982 | nl_sock_record_errors__(transactions, n, 0); | |
983 | VLOG_DBG_RL(&rl, "insufficient length of reply %#"PRIu32 | |
984 | " for seq: %#"PRIx32, reply_len, request_nlmsg->nlmsg_seq); | |
985 | break; | |
986 | } | |
0fd22ae2 | 987 | |
a51a5086 | 988 | /* Validate the sequence number in the reply. */ |
a51a5086 | 989 | reply_nlmsg = (struct nlmsghdr *)reply_buf; |
0fd22ae2 | 990 | |
a51a5086 NR |
991 | if (request_nlmsg->nlmsg_seq != reply_nlmsg->nlmsg_seq) { |
992 | ovs_assert(request_nlmsg->nlmsg_seq == reply_nlmsg->nlmsg_seq); | |
993 | VLOG_DBG_RL(&rl, "mismatched seq request %#"PRIx32 | |
994 | ", reply %#"PRIx32, request_nlmsg->nlmsg_seq, | |
995 | reply_nlmsg->nlmsg_seq); | |
996 | break; | |
0fd22ae2 | 997 | } |
a51a5086 NR |
998 | |
999 | /* Handle errors embedded within the netlink message. */ | |
1000 | ofpbuf_use_stub(&tmp_reply, reply_buf, sizeof reply_buf); | |
1001 | tmp_reply.size = sizeof reply_buf; | |
1002 | if (nl_msg_nlmsgerr(&tmp_reply, &txn->error)) { | |
1003 | if (txn->reply) { | |
1004 | ofpbuf_clear(txn->reply); | |
1005 | } | |
1006 | if (txn->error) { | |
1007 | VLOG_DBG_RL(&rl, "received NAK error=%d (%s)", | |
1008 | error, ovs_strerror(txn->error)); | |
1009 | } | |
1010 | } else { | |
1011 | txn->error = 0; | |
1012 | if (txn->reply) { | |
1013 | /* Copy the reply to the buffer specified by the caller. */ | |
1014 | if (reply_len > txn->reply->allocated) { | |
1015 | ofpbuf_reinit(txn->reply, reply_len); | |
1016 | } | |
1017 | memcpy(txn->reply->data, reply_buf, reply_len); | |
1018 | txn->reply->size = reply_len; | |
0fd22ae2 | 1019 | } |
0fd22ae2 | 1020 | } |
a51a5086 | 1021 | ofpbuf_uninit(&tmp_reply); |
0fd22ae2 NR |
1022 | } |
1023 | ||
1024 | /* Count the number of successful transactions. */ | |
1025 | (*done)++; | |
9189184d | 1026 | |
0fd22ae2 NR |
1027 | } |
1028 | ||
1029 | if (!error) { | |
1030 | COVERAGE_ADD(netlink_sent, n); | |
1031 | } | |
1032 | #endif | |
cc75061a | 1033 | |
72d32ac0 | 1034 | return error; |
cc75061a BP |
1035 | } |
1036 | ||
022ad2b9 | 1037 | static void |
cc75061a BP |
1038 | nl_sock_transact_multiple(struct nl_sock *sock, |
1039 | struct nl_transaction **transactions, size_t n) | |
1040 | { | |
1041 | int max_batch_count; | |
1042 | int error; | |
1043 | ||
1044 | if (!n) { | |
1045 | return; | |
1046 | } | |
1047 | ||
cc75061a BP |
1048 | /* In theory, every request could have a 64 kB reply. But the default and |
1049 | * maximum socket rcvbuf size with typical Dom0 memory sizes both tend to | |
1050 | * be a bit below 128 kB, so that would only allow a single message in a | |
1051 | * "batch". So we assume that replies average (at most) 4 kB, which allows | |
1052 | * a good deal of batching. | |
1053 | * | |
1054 | * In practice, most of the requests that we batch either have no reply at | |
1055 | * all or a brief reply. */ | |
1056 | max_batch_count = MAX(sock->rcvbuf / 4096, 1); | |
1057 | max_batch_count = MIN(max_batch_count, max_iovs); | |
1058 | ||
1059 | while (n > 0) { | |
1060 | size_t count, bytes; | |
1061 | size_t done; | |
1062 | ||
1063 | /* Batch up to 'max_batch_count' transactions. But cap it at about a | |
1064 | * page of requests total because big skbuffs are expensive to | |
1065 | * allocate in the kernel. */ | |
1066 | #if defined(PAGESIZE) | |
1067 | enum { MAX_BATCH_BYTES = MAX(1, PAGESIZE - 512) }; | |
1068 | #else | |
1069 | enum { MAX_BATCH_BYTES = 4096 - 512 }; | |
1070 | #endif | |
6fd6ed71 | 1071 | bytes = transactions[0]->request->size; |
cc75061a | 1072 | for (count = 1; count < n && count < max_batch_count; count++) { |
6fd6ed71 | 1073 | if (bytes + transactions[count]->request->size > MAX_BATCH_BYTES) { |
cc75061a BP |
1074 | break; |
1075 | } | |
6fd6ed71 | 1076 | bytes += transactions[count]->request->size; |
cc75061a BP |
1077 | } |
1078 | ||
1079 | error = nl_sock_transact_multiple__(sock, transactions, count, &done); | |
1080 | transactions += done; | |
1081 | n -= done; | |
1082 | ||
1083 | if (error == ENOBUFS) { | |
1084 | VLOG_DBG_RL(&rl, "receive buffer overflow, resending request"); | |
1085 | } else if (error) { | |
10a89ef0 | 1086 | VLOG_ERR_RL(&rl, "transaction error (%s)", ovs_strerror(error)); |
cc75061a | 1087 | nl_sock_record_errors__(transactions, n, error); |
b937e116 BP |
1088 | if (error != EAGAIN) { |
1089 | /* A fatal error has occurred. Abort the rest of | |
1090 | * transactions. */ | |
1091 | break; | |
1092 | } | |
cc75061a BP |
1093 | } |
1094 | } | |
1095 | } | |
1096 | ||
022ad2b9 | 1097 | static int |
cc75061a BP |
1098 | nl_sock_transact(struct nl_sock *sock, const struct ofpbuf *request, |
1099 | struct ofpbuf **replyp) | |
2fe27d5a | 1100 | { |
cc75061a BP |
1101 | struct nl_transaction *transactionp; |
1102 | struct nl_transaction transaction; | |
2fe27d5a | 1103 | |
ebc56baa | 1104 | transaction.request = CONST_CAST(struct ofpbuf *, request); |
72d32ac0 | 1105 | transaction.reply = replyp ? ofpbuf_new(1024) : NULL; |
cc75061a | 1106 | transactionp = &transaction; |
72d32ac0 | 1107 | |
cc75061a | 1108 | nl_sock_transact_multiple(sock, &transactionp, 1); |
72d32ac0 | 1109 | |
2fe27d5a | 1110 | if (replyp) { |
72d32ac0 BP |
1111 | if (transaction.error) { |
1112 | ofpbuf_delete(transaction.reply); | |
1113 | *replyp = NULL; | |
1114 | } else { | |
1115 | *replyp = transaction.reply; | |
1116 | } | |
2fe27d5a | 1117 | } |
72d32ac0 | 1118 | |
cc75061a | 1119 | return transaction.error; |
2fe27d5a BP |
1120 | } |
1121 | ||
6b7c12fd BP |
1122 | /* Drain all the messages currently in 'sock''s receive queue. */ |
1123 | int | |
1124 | nl_sock_drain(struct nl_sock *sock) | |
1125 | { | |
22326ba6 AS |
1126 | #ifdef _WIN32 |
1127 | return 0; | |
1128 | #else | |
6b7c12fd | 1129 | return drain_rcvbuf(sock->fd); |
22326ba6 | 1130 | #endif |
6b7c12fd BP |
1131 | } |
1132 | ||
a88b4e04 BP |
1133 | /* Starts a Netlink "dump" operation, by sending 'request' to the kernel on a |
1134 | * Netlink socket created with the given 'protocol', and initializes 'dump' to | |
1135 | * reflect the state of the operation. | |
2fe27d5a | 1136 | * |
db1fc210 JS |
1137 | * 'request' must contain a Netlink message. Before sending the message, |
1138 | * nlmsg_len will be finalized to match request->size, and nlmsg_pid will be | |
1139 | * set to the Netlink socket's pid. NLM_F_DUMP and NLM_F_ACK will be set in | |
1140 | * nlmsg_flags. | |
2fe27d5a | 1141 | * |
a88b4e04 | 1142 | * The design of this Netlink socket library ensures that the dump is reliable. |
2fe27d5a | 1143 | * |
db1fc210 JS |
1144 | * This function provides no status indication. nl_dump_done() provides an |
1145 | * error status for the entire dump operation. | |
2fe27d5a | 1146 | * |
db1fc210 | 1147 | * The caller must eventually destroy 'request'. |
2fe27d5a BP |
1148 | */ |
1149 | void | |
a88b4e04 | 1150 | nl_dump_start(struct nl_dump *dump, int protocol, const struct ofpbuf *request) |
2fe27d5a | 1151 | { |
7d7447df | 1152 | nl_msg_nlmsghdr(request)->nlmsg_flags |= NLM_F_DUMP | NLM_F_ACK; |
93295354 BP |
1153 | |
1154 | ovs_mutex_init(&dump->mutex); | |
1155 | ovs_mutex_lock(&dump->mutex); | |
1156 | dump->status = nl_pool_alloc(protocol, &dump->sock); | |
1157 | if (!dump->status) { | |
1158 | dump->status = nl_sock_send__(dump->sock, request, | |
1159 | nl_sock_allocate_seq(dump->sock, 1), | |
1160 | true); | |
b2d1c78a | 1161 | } |
9c8ad495 | 1162 | dump->nl_seq = nl_msg_nlmsghdr(request)->nlmsg_seq; |
93295354 BP |
1163 | ovs_mutex_unlock(&dump->mutex); |
1164 | } | |
1165 | ||
1166 | static int | |
1167 | nl_dump_refill(struct nl_dump *dump, struct ofpbuf *buffer) | |
1168 | OVS_REQUIRES(dump->mutex) | |
1169 | { | |
1170 | struct nlmsghdr *nlmsghdr; | |
1171 | int error; | |
1172 | ||
6fd6ed71 | 1173 | while (!buffer->size) { |
a86bd14e | 1174 | error = nl_sock_recv__(dump->sock, buffer, NULL, false); |
93295354 | 1175 | if (error) { |
1738803a AW |
1176 | /* The kernel never blocks providing the results of a dump, so |
1177 | * error == EAGAIN means that we've read the whole thing, and | |
1178 | * therefore transform it into EOF. (The kernel always provides | |
1179 | * NLMSG_DONE as a sentinel. Some other thread must have received | |
1180 | * that already but not yet signaled it in 'status'.) | |
1181 | * | |
1182 | * Any other error is just an error. */ | |
93295354 BP |
1183 | return error == EAGAIN ? EOF : error; |
1184 | } | |
1185 | ||
1186 | nlmsghdr = nl_msg_nlmsghdr(buffer); | |
1187 | if (dump->nl_seq != nlmsghdr->nlmsg_seq) { | |
1188 | VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32, | |
1189 | nlmsghdr->nlmsg_seq, dump->nl_seq); | |
1190 | ofpbuf_clear(buffer); | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | if (nl_msg_nlmsgerr(buffer, &error) && error) { | |
1195 | VLOG_INFO_RL(&rl, "netlink dump request error (%s)", | |
1196 | ovs_strerror(error)); | |
1197 | ofpbuf_clear(buffer); | |
1198 | return error; | |
1199 | } | |
1200 | ||
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | static int | |
1205 | nl_dump_next__(struct ofpbuf *reply, struct ofpbuf *buffer) | |
1206 | { | |
1207 | struct nlmsghdr *nlmsghdr = nl_msg_next(buffer, reply); | |
1208 | if (!nlmsghdr) { | |
1209 | VLOG_WARN_RL(&rl, "netlink dump contains message fragment"); | |
1210 | return EPROTO; | |
1211 | } else if (nlmsghdr->nlmsg_type == NLMSG_DONE) { | |
1212 | return EOF; | |
1213 | } else { | |
1214 | return 0; | |
1215 | } | |
2fe27d5a BP |
1216 | } |
1217 | ||
d57695d7 JS |
1218 | /* Attempts to retrieve another reply from 'dump' into 'buffer'. 'dump' must |
1219 | * have been initialized with nl_dump_start(), and 'buffer' must have been | |
1220 | * initialized. 'buffer' should be at least NL_DUMP_BUFSIZE bytes long. | |
2fe27d5a | 1221 | * |
19aa20a0 | 1222 | * If successful, returns true and points 'reply->data' and |
6fd6ed71 | 1223 | * 'reply->size' to the message that was retrieved. The caller must not |
19aa20a0 BP |
1224 | * modify 'reply' (because it points within 'buffer', which will be used by |
1225 | * future calls to this function). | |
1226 | * | |
1227 | * On failure, returns false and sets 'reply->data' to NULL and | |
6fd6ed71 | 1228 | * 'reply->size' to 0. Failure might indicate an actual error or merely |
19aa20a0 BP |
1229 | * the end of replies. An error status for the entire dump operation is |
1230 | * provided when it is completed by calling nl_dump_done(). | |
0672776e JS |
1231 | * |
1232 | * Multiple threads may call this function, passing the same nl_dump, however | |
1233 | * each must provide independent buffers. This function may cache multiple | |
1234 | * replies in the buffer, and these will be processed before more replies are | |
1235 | * fetched. When this function returns false, other threads may continue to | |
1236 | * process replies in their buffers, but they will not fetch more replies. | |
2fe27d5a BP |
1237 | */ |
1238 | bool | |
d57695d7 | 1239 | nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply, struct ofpbuf *buffer) |
2fe27d5a | 1240 | { |
93295354 | 1241 | int retval = 0; |
0672776e | 1242 | |
93295354 BP |
1243 | /* If the buffer is empty, refill it. |
1244 | * | |
1245 | * If the buffer is not empty, we don't check the dump's status. | |
1246 | * Otherwise, we could end up skipping some of the dump results if thread A | |
1247 | * hits EOF while thread B is in the midst of processing a batch. */ | |
6fd6ed71 | 1248 | if (!buffer->size) { |
0791315e | 1249 | ovs_mutex_lock(&dump->mutex); |
93295354 BP |
1250 | if (!dump->status) { |
1251 | /* Take the mutex here to avoid an in-kernel race. If two threads | |
1252 | * try to read from a Netlink dump socket at once, then the socket | |
1253 | * error can be set to EINVAL, which will be encountered on the | |
1254 | * next recv on that socket, which could be anywhere due to the way | |
1255 | * that we pool Netlink sockets. Serializing the recv calls avoids | |
1256 | * the issue. */ | |
1257 | dump->status = nl_dump_refill(dump, buffer); | |
1258 | } | |
1259 | retval = dump->status; | |
0791315e | 1260 | ovs_mutex_unlock(&dump->mutex); |
93295354 | 1261 | } |
0791315e | 1262 | |
93295354 BP |
1263 | /* Fetch the next message from the buffer. */ |
1264 | if (!retval) { | |
1265 | retval = nl_dump_next__(reply, buffer); | |
2fe27d5a | 1266 | if (retval) { |
93295354 BP |
1267 | /* Record 'retval' as the dump status, but don't overwrite an error |
1268 | * with EOF. */ | |
1269 | ovs_mutex_lock(&dump->mutex); | |
1270 | if (dump->status <= 0) { | |
1271 | dump->status = retval; | |
2fe27d5a | 1272 | } |
93295354 | 1273 | ovs_mutex_unlock(&dump->mutex); |
2fe27d5a | 1274 | } |
2fe27d5a BP |
1275 | } |
1276 | ||
93295354 | 1277 | if (retval) { |
6fd6ed71 PS |
1278 | reply->data = NULL; |
1279 | reply->size = 0; | |
0672776e | 1280 | } |
93295354 | 1281 | return !retval; |
2fe27d5a BP |
1282 | } |
1283 | ||
1284 | /* Completes Netlink dump operation 'dump', which must have been initialized | |
1285 | * with nl_dump_start(). Returns 0 if the dump operation was error-free, | |
1286 | * otherwise a positive errno value describing the problem. */ | |
1287 | int | |
1288 | nl_dump_done(struct nl_dump *dump) | |
1289 | { | |
0672776e | 1290 | int status; |
d57695d7 | 1291 | |
93295354 BP |
1292 | ovs_mutex_lock(&dump->mutex); |
1293 | status = dump->status; | |
1294 | ovs_mutex_unlock(&dump->mutex); | |
1295 | ||
2fe27d5a | 1296 | /* Drain any remaining messages that the client didn't read. Otherwise the |
a88b4e04 BP |
1297 | * kernel will continue to queue them up and waste buffer space. |
1298 | * | |
1299 | * XXX We could just destroy and discard the socket in this case. */ | |
0672776e JS |
1300 | if (!status) { |
1301 | uint64_t tmp_reply_stub[NL_DUMP_BUFSIZE / 8]; | |
1302 | struct ofpbuf reply, buf; | |
1303 | ||
1304 | ofpbuf_use_stub(&buf, tmp_reply_stub, sizeof tmp_reply_stub); | |
1305 | while (nl_dump_next(dump, &reply, &buf)) { | |
1306 | /* Nothing to do. */ | |
2fe27d5a | 1307 | } |
0672776e | 1308 | ofpbuf_uninit(&buf); |
93295354 BP |
1309 | |
1310 | ovs_mutex_lock(&dump->mutex); | |
1311 | status = dump->status; | |
1312 | ovs_mutex_unlock(&dump->mutex); | |
1313 | ovs_assert(status); | |
2fe27d5a | 1314 | } |
93295354 | 1315 | |
a88b4e04 | 1316 | nl_pool_release(dump->sock); |
0791315e | 1317 | ovs_mutex_destroy(&dump->mutex); |
93295354 BP |
1318 | |
1319 | return status == EOF ? 0 : status; | |
2fe27d5a BP |
1320 | } |
1321 | ||
7fa09611 EE |
1322 | #ifdef _WIN32 |
1323 | /* Pend an I/O request in the driver. The driver completes the I/O whenever | |
1324 | * an event or a packet is ready to be read. Once the I/O is completed | |
1325 | * the overlapped structure event associated with the pending I/O will be set | |
1326 | */ | |
1327 | static int | |
8341662d | 1328 | pend_io_request(struct nl_sock *sock) |
7fa09611 EE |
1329 | { |
1330 | struct ofpbuf request; | |
1331 | uint64_t request_stub[128]; | |
1332 | struct ovs_header *ovs_header; | |
1333 | struct nlmsghdr *nlmsg; | |
1334 | uint32_t seq; | |
b91d3d03 | 1335 | int retval = 0; |
7fa09611 EE |
1336 | int error; |
1337 | DWORD bytes; | |
64513e68 | 1338 | OVERLAPPED *overlapped = CONST_CAST(OVERLAPPED *, &sock->overlapped); |
b91d3d03 NR |
1339 | uint16_t cmd = OVS_CTRL_CMD_WIN_PEND_PACKET_REQ; |
1340 | ||
1341 | ovs_assert(sock->read_ioctl == OVS_IOCTL_READ_PACKET || | |
1342 | sock->read_ioctl == OVS_IOCTL_READ_EVENT); | |
1343 | if (sock->read_ioctl == OVS_IOCTL_READ_EVENT) { | |
1344 | cmd = OVS_CTRL_CMD_WIN_PEND_REQ; | |
1345 | } | |
7fa09611 EE |
1346 | |
1347 | int ovs_msg_size = sizeof (struct nlmsghdr) + sizeof (struct genlmsghdr) + | |
1348 | sizeof (struct ovs_header); | |
1349 | ||
1350 | ofpbuf_use_stub(&request, request_stub, sizeof request_stub); | |
1351 | ||
1352 | seq = nl_sock_allocate_seq(sock, 1); | |
1353 | nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0, | |
b91d3d03 | 1354 | cmd, OVS_WIN_CONTROL_VERSION); |
7fa09611 EE |
1355 | nlmsg = nl_msg_nlmsghdr(&request); |
1356 | nlmsg->nlmsg_seq = seq; | |
92a5068f | 1357 | nlmsg->nlmsg_pid = sock->pid; |
7fa09611 EE |
1358 | |
1359 | ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header); | |
1360 | ovs_header->dp_ifindex = 0; | |
e6b298ef | 1361 | nlmsg->nlmsg_len = request.size; |
7fa09611 EE |
1362 | |
1363 | if (!DeviceIoControl(sock->handle, OVS_IOCTL_WRITE, | |
6fd6ed71 | 1364 | request.data, request.size, |
7fa09611 EE |
1365 | NULL, 0, &bytes, overlapped)) { |
1366 | error = GetLastError(); | |
1367 | /* Check if the I/O got pended */ | |
1368 | if (error != ERROR_IO_INCOMPLETE && error != ERROR_IO_PENDING) { | |
d553dd54 | 1369 | lost_communication(error); |
7fa09611 EE |
1370 | VLOG_ERR("nl_sock_wait failed - %s\n", ovs_format_message(error)); |
1371 | retval = EINVAL; | |
7fa09611 EE |
1372 | } |
1373 | } else { | |
b91d3d03 | 1374 | retval = EAGAIN; |
7fa09611 | 1375 | } |
7fa09611 EE |
1376 | |
1377 | done: | |
1378 | ofpbuf_uninit(&request); | |
1379 | return retval; | |
1380 | } | |
1381 | #endif /* _WIN32 */ | |
1382 | ||
2fe27d5a | 1383 | /* Causes poll_block() to wake up when any of the specified 'events' (which is |
8341662d NR |
1384 | * a OR'd combination of POLLIN, POLLOUT, etc.) occur on 'sock'. |
1385 | * On Windows, 'sock' is not treated as const, and may be modified. */ | |
2fe27d5a BP |
1386 | void |
1387 | nl_sock_wait(const struct nl_sock *sock, short int events) | |
1388 | { | |
22326ba6 | 1389 | #ifdef _WIN32 |
7fa09611 | 1390 | if (sock->overlapped.Internal != STATUS_PENDING) { |
b91d3d03 NR |
1391 | int ret = pend_io_request(CONST_CAST(struct nl_sock *, sock)); |
1392 | if (ret == 0) { | |
1393 | poll_wevent_wait(sock->overlapped.hEvent); | |
1394 | } else { | |
1395 | poll_immediate_wake(); | |
1396 | } | |
1397 | } else { | |
1398 | poll_wevent_wait(sock->overlapped.hEvent); | |
7fa09611 | 1399 | } |
22326ba6 | 1400 | #else |
2fe27d5a | 1401 | poll_fd_wait(sock->fd, events); |
22326ba6 | 1402 | #endif |
2fe27d5a | 1403 | } |
50802adb | 1404 | |
9667de98 | 1405 | #ifndef _WIN32 |
8522ba09 BP |
1406 | /* Returns the underlying fd for 'sock', for use in "poll()"-like operations |
1407 | * that can't use nl_sock_wait(). | |
1408 | * | |
1409 | * It's a little tricky to use the returned fd correctly, because nl_sock does | |
1410 | * "copy on write" to allow a single nl_sock to be used for notifications, | |
1411 | * transactions, and dumps. If 'sock' is used only for notifications and | |
1412 | * transactions (and never for dump) then the usage is safe. */ | |
1413 | int | |
1414 | nl_sock_fd(const struct nl_sock *sock) | |
1415 | { | |
1416 | return sock->fd; | |
1417 | } | |
9667de98 | 1418 | #endif |
8522ba09 | 1419 | |
50802adb JG |
1420 | /* Returns the PID associated with this socket. */ |
1421 | uint32_t | |
1422 | nl_sock_pid(const struct nl_sock *sock) | |
1423 | { | |
1424 | return sock->pid; | |
1425 | } | |
2fe27d5a BP |
1426 | \f |
1427 | /* Miscellaneous. */ | |
1428 | ||
2ad204c8 BP |
1429 | struct genl_family { |
1430 | struct hmap_node hmap_node; | |
1431 | uint16_t id; | |
1432 | char *name; | |
1433 | }; | |
1434 | ||
1435 | static struct hmap genl_families = HMAP_INITIALIZER(&genl_families); | |
1436 | ||
2fe27d5a BP |
1437 | static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = { |
1438 | [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16}, | |
213a13ed | 1439 | [CTRL_ATTR_MCAST_GROUPS] = {.type = NL_A_NESTED, .optional = true}, |
2fe27d5a BP |
1440 | }; |
1441 | ||
2ad204c8 BP |
1442 | static struct genl_family * |
1443 | find_genl_family_by_id(uint16_t id) | |
1444 | { | |
1445 | struct genl_family *family; | |
1446 | ||
1447 | HMAP_FOR_EACH_IN_BUCKET (family, hmap_node, hash_int(id, 0), | |
1448 | &genl_families) { | |
1449 | if (family->id == id) { | |
1450 | return family; | |
1451 | } | |
1452 | } | |
1453 | return NULL; | |
1454 | } | |
1455 | ||
1456 | static void | |
1457 | define_genl_family(uint16_t id, const char *name) | |
1458 | { | |
1459 | struct genl_family *family = find_genl_family_by_id(id); | |
1460 | ||
1461 | if (family) { | |
1462 | if (!strcmp(family->name, name)) { | |
1463 | return; | |
1464 | } | |
1465 | free(family->name); | |
1466 | } else { | |
1467 | family = xmalloc(sizeof *family); | |
1468 | family->id = id; | |
1469 | hmap_insert(&genl_families, &family->hmap_node, hash_int(id, 0)); | |
1470 | } | |
1471 | family->name = xstrdup(name); | |
1472 | } | |
1473 | ||
1474 | static const char * | |
1475 | genl_family_to_name(uint16_t id) | |
1476 | { | |
1477 | if (id == GENL_ID_CTRL) { | |
1478 | return "control"; | |
1479 | } else { | |
1480 | struct genl_family *family = find_genl_family_by_id(id); | |
1481 | return family ? family->name : "unknown"; | |
1482 | } | |
1483 | } | |
1484 | ||
b3fca8a8 | 1485 | #ifndef _WIN32 |
e408762f | 1486 | static int |
2a477244 BP |
1487 | do_lookup_genl_family(const char *name, struct nlattr **attrs, |
1488 | struct ofpbuf **replyp) | |
2fe27d5a BP |
1489 | { |
1490 | struct nl_sock *sock; | |
1491 | struct ofpbuf request, *reply; | |
2a477244 | 1492 | int error; |
2fe27d5a | 1493 | |
2a477244 BP |
1494 | *replyp = NULL; |
1495 | error = nl_sock_create(NETLINK_GENERIC, &sock); | |
1496 | if (error) { | |
1497 | return error; | |
2fe27d5a BP |
1498 | } |
1499 | ||
1500 | ofpbuf_init(&request, 0); | |
1501 | nl_msg_put_genlmsghdr(&request, 0, GENL_ID_CTRL, NLM_F_REQUEST, | |
1502 | CTRL_CMD_GETFAMILY, 1); | |
1503 | nl_msg_put_string(&request, CTRL_ATTR_FAMILY_NAME, name); | |
2a477244 | 1504 | error = nl_sock_transact(sock, &request, &reply); |
2fe27d5a | 1505 | ofpbuf_uninit(&request); |
2a477244 | 1506 | if (error) { |
2fe27d5a | 1507 | nl_sock_destroy(sock); |
2a477244 | 1508 | return error; |
2fe27d5a BP |
1509 | } |
1510 | ||
1511 | if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN, | |
2a477244 BP |
1512 | family_policy, attrs, ARRAY_SIZE(family_policy)) |
1513 | || nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]) == 0) { | |
2fe27d5a BP |
1514 | nl_sock_destroy(sock); |
1515 | ofpbuf_delete(reply); | |
2a477244 | 1516 | return EPROTO; |
2fe27d5a BP |
1517 | } |
1518 | ||
2fe27d5a | 1519 | nl_sock_destroy(sock); |
2a477244 BP |
1520 | *replyp = reply; |
1521 | return 0; | |
2fe27d5a | 1522 | } |
b3fca8a8 NR |
1523 | #else |
1524 | static int | |
1525 | do_lookup_genl_family(const char *name, struct nlattr **attrs, | |
1526 | struct ofpbuf **replyp) | |
1527 | { | |
4c484aca | 1528 | struct nlmsghdr *nlmsg; |
b3fca8a8 NR |
1529 | struct ofpbuf *reply; |
1530 | int error; | |
1531 | uint16_t family_id; | |
1532 | const char *family_name; | |
1533 | uint32_t family_version; | |
1534 | uint32_t family_attrmax; | |
4c484aca NR |
1535 | uint32_t mcgrp_id = OVS_WIN_NL_INVALID_MCGRP_ID; |
1536 | const char *mcgrp_name = NULL; | |
b3fca8a8 NR |
1537 | |
1538 | *replyp = NULL; | |
1539 | reply = ofpbuf_new(1024); | |
1540 | ||
4c484aca | 1541 | /* CTRL_ATTR_MCAST_GROUPS is supported only for VPORT family. */ |
b3fca8a8 NR |
1542 | if (!strcmp(name, OVS_WIN_CONTROL_FAMILY)) { |
1543 | family_id = OVS_WIN_NL_CTRL_FAMILY_ID; | |
1544 | family_name = OVS_WIN_CONTROL_FAMILY; | |
1545 | family_version = OVS_WIN_CONTROL_VERSION; | |
1546 | family_attrmax = OVS_WIN_CONTROL_ATTR_MAX; | |
1547 | } else if (!strcmp(name, OVS_DATAPATH_FAMILY)) { | |
1548 | family_id = OVS_WIN_NL_DATAPATH_FAMILY_ID; | |
1549 | family_name = OVS_DATAPATH_FAMILY; | |
1550 | family_version = OVS_DATAPATH_VERSION; | |
1551 | family_attrmax = OVS_DP_ATTR_MAX; | |
1552 | } else if (!strcmp(name, OVS_PACKET_FAMILY)) { | |
1553 | family_id = OVS_WIN_NL_PACKET_FAMILY_ID; | |
1554 | family_name = OVS_PACKET_FAMILY; | |
1555 | family_version = OVS_PACKET_VERSION; | |
1556 | family_attrmax = OVS_PACKET_ATTR_MAX; | |
1557 | } else if (!strcmp(name, OVS_VPORT_FAMILY)) { | |
1558 | family_id = OVS_WIN_NL_VPORT_FAMILY_ID; | |
1559 | family_name = OVS_VPORT_FAMILY; | |
1560 | family_version = OVS_VPORT_VERSION; | |
1561 | family_attrmax = OVS_VPORT_ATTR_MAX; | |
4c484aca NR |
1562 | mcgrp_id = OVS_WIN_NL_VPORT_MCGRP_ID; |
1563 | mcgrp_name = OVS_VPORT_MCGROUP; | |
b3fca8a8 NR |
1564 | } else if (!strcmp(name, OVS_FLOW_FAMILY)) { |
1565 | family_id = OVS_WIN_NL_FLOW_FAMILY_ID; | |
1566 | family_name = OVS_FLOW_FAMILY; | |
1567 | family_version = OVS_FLOW_VERSION; | |
1568 | family_attrmax = OVS_FLOW_ATTR_MAX; | |
83cc9d56 NR |
1569 | } else if (!strcmp(name, OVS_WIN_NETDEV_FAMILY)) { |
1570 | family_id = OVS_WIN_NL_NETDEV_FAMILY_ID; | |
1571 | family_name = OVS_WIN_NETDEV_FAMILY; | |
1572 | family_version = OVS_WIN_NETDEV_VERSION; | |
1573 | family_attrmax = OVS_WIN_NETDEV_ATTR_MAX; | |
b3fca8a8 NR |
1574 | } else { |
1575 | ofpbuf_delete(reply); | |
1576 | return EINVAL; | |
1577 | } | |
1578 | ||
1579 | nl_msg_put_genlmsghdr(reply, 0, GENL_ID_CTRL, 0, | |
1580 | CTRL_CMD_NEWFAMILY, family_version); | |
1581 | /* CTRL_ATTR_HDRSIZE and CTRL_ATTR_OPS are not populated, but the | |
1582 | * callers do not seem to need them. */ | |
1583 | nl_msg_put_u16(reply, CTRL_ATTR_FAMILY_ID, family_id); | |
1584 | nl_msg_put_string(reply, CTRL_ATTR_FAMILY_NAME, family_name); | |
1585 | nl_msg_put_u32(reply, CTRL_ATTR_VERSION, family_version); | |
1586 | nl_msg_put_u32(reply, CTRL_ATTR_MAXATTR, family_attrmax); | |
1587 | ||
4c484aca NR |
1588 | if (mcgrp_id != OVS_WIN_NL_INVALID_MCGRP_ID) { |
1589 | size_t mcgrp_ofs1 = nl_msg_start_nested(reply, CTRL_ATTR_MCAST_GROUPS); | |
1590 | size_t mcgrp_ofs2= nl_msg_start_nested(reply, | |
1591 | OVS_WIN_NL_VPORT_MCGRP_ID - OVS_WIN_NL_MCGRP_START_ID); | |
1592 | nl_msg_put_u32(reply, CTRL_ATTR_MCAST_GRP_ID, mcgrp_id); | |
1593 | ovs_assert(mcgrp_name != NULL); | |
1594 | nl_msg_put_string(reply, CTRL_ATTR_MCAST_GRP_NAME, mcgrp_name); | |
1595 | nl_msg_end_nested(reply, mcgrp_ofs2); | |
1596 | nl_msg_end_nested(reply, mcgrp_ofs1); | |
1597 | } | |
1598 | ||
1599 | /* Set the total length of the netlink message. */ | |
1600 | nlmsg = nl_msg_nlmsghdr(reply); | |
6fd6ed71 | 1601 | nlmsg->nlmsg_len = reply->size; |
4c484aca | 1602 | |
b3fca8a8 NR |
1603 | if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN, |
1604 | family_policy, attrs, ARRAY_SIZE(family_policy)) | |
1605 | || nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]) == 0) { | |
b3fca8a8 NR |
1606 | ofpbuf_delete(reply); |
1607 | return EPROTO; | |
1608 | } | |
1609 | ||
1610 | *replyp = reply; | |
1611 | return 0; | |
1612 | } | |
1613 | #endif | |
2fe27d5a | 1614 | |
e408762f EJ |
1615 | /* Finds the multicast group called 'group_name' in genl family 'family_name'. |
1616 | * When successful, writes its result to 'multicast_group' and returns 0. | |
213a13ed | 1617 | * Otherwise, clears 'multicast_group' and returns a positive error code. |
b3dcb73c | 1618 | */ |
e408762f EJ |
1619 | int |
1620 | nl_lookup_genl_mcgroup(const char *family_name, const char *group_name, | |
b3dcb73c | 1621 | unsigned int *multicast_group) |
e408762f EJ |
1622 | { |
1623 | struct nlattr *family_attrs[ARRAY_SIZE(family_policy)]; | |
6d23c6f4 | 1624 | const struct nlattr *mc; |
2a477244 | 1625 | struct ofpbuf *reply; |
e408762f | 1626 | unsigned int left; |
2a477244 | 1627 | int error; |
e408762f EJ |
1628 | |
1629 | *multicast_group = 0; | |
2a477244 BP |
1630 | error = do_lookup_genl_family(family_name, family_attrs, &reply); |
1631 | if (error) { | |
1632 | return error; | |
e408762f EJ |
1633 | } |
1634 | ||
213a13ed | 1635 | if (!family_attrs[CTRL_ATTR_MCAST_GROUPS]) { |
b3dcb73c | 1636 | error = EPROTO; |
213a13ed EJ |
1637 | goto exit; |
1638 | } | |
1639 | ||
6d23c6f4 | 1640 | NL_NESTED_FOR_EACH (mc, left, family_attrs[CTRL_ATTR_MCAST_GROUPS]) { |
e408762f EJ |
1641 | static const struct nl_policy mc_policy[] = { |
1642 | [CTRL_ATTR_MCAST_GRP_ID] = {.type = NL_A_U32}, | |
1643 | [CTRL_ATTR_MCAST_GRP_NAME] = {.type = NL_A_STRING}, | |
1644 | }; | |
1645 | ||
1646 | struct nlattr *mc_attrs[ARRAY_SIZE(mc_policy)]; | |
1647 | const char *mc_name; | |
1648 | ||
1649 | if (!nl_parse_nested(mc, mc_policy, mc_attrs, ARRAY_SIZE(mc_policy))) { | |
2a477244 BP |
1650 | error = EPROTO; |
1651 | goto exit; | |
e408762f EJ |
1652 | } |
1653 | ||
1654 | mc_name = nl_attr_get_string(mc_attrs[CTRL_ATTR_MCAST_GRP_NAME]); | |
1655 | if (!strcmp(group_name, mc_name)) { | |
1656 | *multicast_group = | |
1657 | nl_attr_get_u32(mc_attrs[CTRL_ATTR_MCAST_GRP_ID]); | |
2a477244 BP |
1658 | error = 0; |
1659 | goto exit; | |
e408762f EJ |
1660 | } |
1661 | } | |
2a477244 | 1662 | error = EPROTO; |
e408762f | 1663 | |
2a477244 BP |
1664 | exit: |
1665 | ofpbuf_delete(reply); | |
1666 | return error; | |
e408762f EJ |
1667 | } |
1668 | ||
2fe27d5a BP |
1669 | /* If '*number' is 0, translates the given Generic Netlink family 'name' to a |
1670 | * number and stores it in '*number'. If successful, returns 0 and the caller | |
1671 | * may use '*number' as the family number. On failure, returns a positive | |
1672 | * errno value and '*number' caches the errno value. */ | |
1673 | int | |
1674 | nl_lookup_genl_family(const char *name, int *number) | |
1675 | { | |
1676 | if (*number == 0) { | |
2a477244 BP |
1677 | struct nlattr *attrs[ARRAY_SIZE(family_policy)]; |
1678 | struct ofpbuf *reply; | |
1679 | int error; | |
1680 | ||
1681 | error = do_lookup_genl_family(name, attrs, &reply); | |
1682 | if (!error) { | |
1683 | *number = nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]); | |
1684 | define_genl_family(*number, name); | |
1685 | } else { | |
1686 | *number = -error; | |
1687 | } | |
1688 | ofpbuf_delete(reply); | |
1689 | ||
cb22974d | 1690 | ovs_assert(*number != 0); |
2fe27d5a BP |
1691 | } |
1692 | return *number > 0 ? 0 : -*number; | |
1693 | } | |
a88b4e04 BP |
1694 | \f |
1695 | struct nl_pool { | |
1696 | struct nl_sock *socks[16]; | |
1697 | int n; | |
1698 | }; | |
1699 | ||
834d6caf | 1700 | static struct ovs_mutex pool_mutex = OVS_MUTEX_INITIALIZER; |
97be1538 | 1701 | static struct nl_pool pools[MAX_LINKS] OVS_GUARDED_BY(pool_mutex); |
a88b4e04 BP |
1702 | |
1703 | static int | |
1704 | nl_pool_alloc(int protocol, struct nl_sock **sockp) | |
1705 | { | |
0bd01224 | 1706 | struct nl_sock *sock = NULL; |
a88b4e04 BP |
1707 | struct nl_pool *pool; |
1708 | ||
1709 | ovs_assert(protocol >= 0 && protocol < ARRAY_SIZE(pools)); | |
1710 | ||
97be1538 | 1711 | ovs_mutex_lock(&pool_mutex); |
a88b4e04 BP |
1712 | pool = &pools[protocol]; |
1713 | if (pool->n > 0) { | |
0bd01224 BP |
1714 | sock = pool->socks[--pool->n]; |
1715 | } | |
97be1538 | 1716 | ovs_mutex_unlock(&pool_mutex); |
0bd01224 BP |
1717 | |
1718 | if (sock) { | |
1719 | *sockp = sock; | |
a88b4e04 BP |
1720 | return 0; |
1721 | } else { | |
1722 | return nl_sock_create(protocol, sockp); | |
1723 | } | |
1724 | } | |
1725 | ||
1726 | static void | |
1727 | nl_pool_release(struct nl_sock *sock) | |
1728 | { | |
1729 | if (sock) { | |
1730 | struct nl_pool *pool = &pools[sock->protocol]; | |
1731 | ||
97be1538 | 1732 | ovs_mutex_lock(&pool_mutex); |
a88b4e04 BP |
1733 | if (pool->n < ARRAY_SIZE(pool->socks)) { |
1734 | pool->socks[pool->n++] = sock; | |
0bd01224 | 1735 | sock = NULL; |
a88b4e04 | 1736 | } |
97be1538 | 1737 | ovs_mutex_unlock(&pool_mutex); |
0bd01224 BP |
1738 | |
1739 | nl_sock_destroy(sock); | |
a88b4e04 BP |
1740 | } |
1741 | } | |
1742 | ||
022ad2b9 BP |
1743 | /* Sends 'request' to the kernel on a Netlink socket for the given 'protocol' |
1744 | * (e.g. NETLINK_ROUTE or NETLINK_GENERIC) and waits for a response. If | |
1745 | * successful, returns 0. On failure, returns a positive errno value. | |
1746 | * | |
1747 | * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's | |
1748 | * reply, which the caller is responsible for freeing with ofpbuf_delete(), and | |
1749 | * on failure '*replyp' is set to NULL. If 'replyp' is null, then the kernel's | |
1750 | * reply, if any, is discarded. | |
1751 | * | |
1752 | * Before the message is sent, nlmsg_len in 'request' will be finalized to | |
6fd6ed71 | 1753 | * match msg->size, nlmsg_pid will be set to the pid of the socket used |
022ad2b9 BP |
1754 | * for sending the request, and nlmsg_seq will be initialized. |
1755 | * | |
1756 | * The caller is responsible for destroying 'request'. | |
1757 | * | |
1758 | * Bare Netlink is an unreliable transport protocol. This function layers | |
1759 | * reliable delivery and reply semantics on top of bare Netlink. | |
1760 | * | |
1761 | * In Netlink, sending a request to the kernel is reliable enough, because the | |
1762 | * kernel will tell us if the message cannot be queued (and we will in that | |
1763 | * case put it on the transmit queue and wait until it can be delivered). | |
1764 | * | |
1765 | * Receiving the reply is the real problem: if the socket buffer is full when | |
1766 | * the kernel tries to send the reply, the reply will be dropped. However, the | |
1767 | * kernel sets a flag that a reply has been dropped. The next call to recv | |
1768 | * then returns ENOBUFS. We can then re-send the request. | |
1769 | * | |
1770 | * Caveats: | |
1771 | * | |
1772 | * 1. Netlink depends on sequence numbers to match up requests and | |
1773 | * replies. The sender of a request supplies a sequence number, and | |
1774 | * the reply echos back that sequence number. | |
1775 | * | |
1776 | * This is fine, but (1) some kernel netlink implementations are | |
1777 | * broken, in that they fail to echo sequence numbers and (2) this | |
1778 | * function will drop packets with non-matching sequence numbers, so | |
1779 | * that only a single request can be usefully transacted at a time. | |
1780 | * | |
1781 | * 2. Resending the request causes it to be re-executed, so the request | |
1782 | * needs to be idempotent. | |
1783 | */ | |
a88b4e04 BP |
1784 | int |
1785 | nl_transact(int protocol, const struct ofpbuf *request, | |
1786 | struct ofpbuf **replyp) | |
1787 | { | |
1788 | struct nl_sock *sock; | |
1789 | int error; | |
1790 | ||
1791 | error = nl_pool_alloc(protocol, &sock); | |
1792 | if (error) { | |
d9c194a1 RD |
1793 | if (replyp) { |
1794 | *replyp = NULL; | |
1795 | } | |
a88b4e04 BP |
1796 | return error; |
1797 | } | |
1798 | ||
1799 | error = nl_sock_transact(sock, request, replyp); | |
1800 | ||
1801 | nl_pool_release(sock); | |
1802 | return error; | |
1803 | } | |
1804 | ||
022ad2b9 BP |
1805 | /* Sends the 'request' member of the 'n' transactions in 'transactions' on a |
1806 | * Netlink socket for the given 'protocol' (e.g. NETLINK_ROUTE or | |
1807 | * NETLINK_GENERIC), in order, and receives responses to all of them. Fills in | |
1808 | * the 'error' member of each transaction with 0 if it was successful, | |
1809 | * otherwise with a positive errno value. If 'reply' is nonnull, then it will | |
1810 | * be filled with the reply if the message receives a detailed reply. In other | |
1811 | * cases, i.e. where the request failed or had no reply beyond an indication of | |
1812 | * success, 'reply' will be cleared if it is nonnull. | |
1813 | * | |
1814 | * The caller is responsible for destroying each request and reply, and the | |
1815 | * transactions array itself. | |
1816 | * | |
1817 | * Before sending each message, this function will finalize nlmsg_len in each | |
1818 | * 'request' to match the ofpbuf's size, set nlmsg_pid to the pid of the socket | |
1819 | * used for the transaction, and initialize nlmsg_seq. | |
1820 | * | |
1821 | * Bare Netlink is an unreliable transport protocol. This function layers | |
1822 | * reliable delivery and reply semantics on top of bare Netlink. See | |
1823 | * nl_transact() for some caveats. | |
1824 | */ | |
a88b4e04 BP |
1825 | void |
1826 | nl_transact_multiple(int protocol, | |
1827 | struct nl_transaction **transactions, size_t n) | |
1828 | { | |
1829 | struct nl_sock *sock; | |
1830 | int error; | |
1831 | ||
1832 | error = nl_pool_alloc(protocol, &sock); | |
1833 | if (!error) { | |
1834 | nl_sock_transact_multiple(sock, transactions, n); | |
1835 | nl_pool_release(sock); | |
1836 | } else { | |
1837 | nl_sock_record_errors__(transactions, n, error); | |
1838 | } | |
1839 | } | |
1840 | ||
2fe27d5a | 1841 | \f |
7d7447df BP |
1842 | static uint32_t |
1843 | nl_sock_allocate_seq(struct nl_sock *sock, unsigned int n) | |
1844 | { | |
1845 | uint32_t seq = sock->next_seq; | |
1846 | ||
1847 | sock->next_seq += n; | |
1848 | ||
1849 | /* Make it impossible for the next request for sequence numbers to wrap | |
1850 | * around to 0. Start over with 1 to avoid ever using a sequence number of | |
1851 | * 0, because the kernel uses sequence number 0 for notifications. */ | |
1852 | if (sock->next_seq >= UINT32_MAX / 2) { | |
1853 | sock->next_seq = 1; | |
1854 | } | |
1855 | ||
1856 | return seq; | |
1857 | } | |
1858 | ||
2fe27d5a | 1859 | static void |
2ad204c8 | 1860 | nlmsghdr_to_string(const struct nlmsghdr *h, int protocol, struct ds *ds) |
2fe27d5a BP |
1861 | { |
1862 | struct nlmsg_flag { | |
1863 | unsigned int bits; | |
1864 | const char *name; | |
1865 | }; | |
1866 | static const struct nlmsg_flag flags[] = { | |
1867 | { NLM_F_REQUEST, "REQUEST" }, | |
1868 | { NLM_F_MULTI, "MULTI" }, | |
1869 | { NLM_F_ACK, "ACK" }, | |
1870 | { NLM_F_ECHO, "ECHO" }, | |
1871 | { NLM_F_DUMP, "DUMP" }, | |
1872 | { NLM_F_ROOT, "ROOT" }, | |
1873 | { NLM_F_MATCH, "MATCH" }, | |
1874 | { NLM_F_ATOMIC, "ATOMIC" }, | |
1875 | }; | |
1876 | const struct nlmsg_flag *flag; | |
1877 | uint16_t flags_left; | |
1878 | ||
1879 | ds_put_format(ds, "nl(len:%"PRIu32", type=%"PRIu16, | |
1880 | h->nlmsg_len, h->nlmsg_type); | |
1881 | if (h->nlmsg_type == NLMSG_NOOP) { | |
1882 | ds_put_cstr(ds, "(no-op)"); | |
1883 | } else if (h->nlmsg_type == NLMSG_ERROR) { | |
1884 | ds_put_cstr(ds, "(error)"); | |
1885 | } else if (h->nlmsg_type == NLMSG_DONE) { | |
1886 | ds_put_cstr(ds, "(done)"); | |
1887 | } else if (h->nlmsg_type == NLMSG_OVERRUN) { | |
1888 | ds_put_cstr(ds, "(overrun)"); | |
1889 | } else if (h->nlmsg_type < NLMSG_MIN_TYPE) { | |
1890 | ds_put_cstr(ds, "(reserved)"); | |
2ad204c8 BP |
1891 | } else if (protocol == NETLINK_GENERIC) { |
1892 | ds_put_format(ds, "(%s)", genl_family_to_name(h->nlmsg_type)); | |
2fe27d5a BP |
1893 | } else { |
1894 | ds_put_cstr(ds, "(family-defined)"); | |
1895 | } | |
1896 | ds_put_format(ds, ", flags=%"PRIx16, h->nlmsg_flags); | |
1897 | flags_left = h->nlmsg_flags; | |
1898 | for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) { | |
1899 | if ((flags_left & flag->bits) == flag->bits) { | |
1900 | ds_put_format(ds, "[%s]", flag->name); | |
1901 | flags_left &= ~flag->bits; | |
1902 | } | |
1903 | } | |
1904 | if (flags_left) { | |
1905 | ds_put_format(ds, "[OTHER:%"PRIx16"]", flags_left); | |
1906 | } | |
2c5a6834 BP |
1907 | ds_put_format(ds, ", seq=%"PRIx32", pid=%"PRIu32, |
1908 | h->nlmsg_seq, h->nlmsg_pid); | |
2fe27d5a BP |
1909 | } |
1910 | ||
1911 | static char * | |
7041c3a9 | 1912 | nlmsg_to_string(const struct ofpbuf *buffer, int protocol) |
2fe27d5a BP |
1913 | { |
1914 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1915 | const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN); | |
1916 | if (h) { | |
2ad204c8 | 1917 | nlmsghdr_to_string(h, protocol, &ds); |
2fe27d5a BP |
1918 | if (h->nlmsg_type == NLMSG_ERROR) { |
1919 | const struct nlmsgerr *e; | |
1920 | e = ofpbuf_at(buffer, NLMSG_HDRLEN, | |
1921 | NLMSG_ALIGN(sizeof(struct nlmsgerr))); | |
1922 | if (e) { | |
1923 | ds_put_format(&ds, " error(%d", e->error); | |
1924 | if (e->error < 0) { | |
10a89ef0 | 1925 | ds_put_format(&ds, "(%s)", ovs_strerror(-e->error)); |
2fe27d5a BP |
1926 | } |
1927 | ds_put_cstr(&ds, ", in-reply-to("); | |
2ad204c8 | 1928 | nlmsghdr_to_string(&e->msg, protocol, &ds); |
2fe27d5a BP |
1929 | ds_put_cstr(&ds, "))"); |
1930 | } else { | |
1931 | ds_put_cstr(&ds, " error(truncated)"); | |
1932 | } | |
1933 | } else if (h->nlmsg_type == NLMSG_DONE) { | |
1934 | int *error = ofpbuf_at(buffer, NLMSG_HDRLEN, sizeof *error); | |
1935 | if (error) { | |
1936 | ds_put_format(&ds, " done(%d", *error); | |
1937 | if (*error < 0) { | |
10a89ef0 | 1938 | ds_put_format(&ds, "(%s)", ovs_strerror(-*error)); |
2fe27d5a BP |
1939 | } |
1940 | ds_put_cstr(&ds, ")"); | |
1941 | } else { | |
1942 | ds_put_cstr(&ds, " done(truncated)"); | |
1943 | } | |
7041c3a9 BP |
1944 | } else if (protocol == NETLINK_GENERIC) { |
1945 | struct genlmsghdr *genl = nl_msg_genlmsghdr(buffer); | |
1946 | if (genl) { | |
1947 | ds_put_format(&ds, ",genl(cmd=%"PRIu8",version=%"PRIu8")", | |
1948 | genl->cmd, genl->version); | |
1949 | } | |
2fe27d5a BP |
1950 | } |
1951 | } else { | |
1952 | ds_put_cstr(&ds, "nl(truncated)"); | |
1953 | } | |
1954 | return ds.string; | |
1955 | } | |
1956 | ||
1957 | static void | |
1958 | log_nlmsg(const char *function, int error, | |
7041c3a9 | 1959 | const void *message, size_t size, int protocol) |
2fe27d5a | 1960 | { |
2fe27d5a BP |
1961 | if (!VLOG_IS_DBG_ENABLED()) { |
1962 | return; | |
1963 | } | |
1964 | ||
0a2869d5 BP |
1965 | struct ofpbuf buffer = ofpbuf_const_initializer(message, size); |
1966 | char *nlmsg = nlmsg_to_string(&buffer, protocol); | |
10a89ef0 | 1967 | VLOG_DBG_RL(&rl, "%s (%s): %s", function, ovs_strerror(error), nlmsg); |
2fe27d5a BP |
1968 | free(nlmsg); |
1969 | } |