]>
Commit | Line | Data |
---|---|---|
2fe27d5a | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 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> | |
22 | #include <sys/types.h> | |
cc75061a | 23 | #include <sys/uio.h> |
2fe27d5a BP |
24 | #include <unistd.h> |
25 | #include "coverage.h" | |
26 | #include "dynamic-string.h" | |
2ad204c8 BP |
27 | #include "hash.h" |
28 | #include "hmap.h" | |
2fe27d5a BP |
29 | #include "netlink.h" |
30 | #include "netlink-protocol.h" | |
31 | #include "ofpbuf.h" | |
32 | #include "poll-loop.h" | |
6b7c12fd | 33 | #include "socket-util.h" |
2fe27d5a | 34 | #include "stress.h" |
cc75061a | 35 | #include "util.h" |
2fe27d5a BP |
36 | #include "vlog.h" |
37 | ||
38 | VLOG_DEFINE_THIS_MODULE(netlink_socket); | |
39 | ||
40 | COVERAGE_DEFINE(netlink_overflow); | |
41 | COVERAGE_DEFINE(netlink_received); | |
fc999dda | 42 | COVERAGE_DEFINE(netlink_recv_jumbo); |
2fe27d5a BP |
43 | COVERAGE_DEFINE(netlink_send); |
44 | COVERAGE_DEFINE(netlink_sent); | |
45 | ||
46 | /* Linux header file confusion causes this to be undefined. */ | |
47 | #ifndef SOL_NETLINK | |
48 | #define SOL_NETLINK 270 | |
49 | #endif | |
50 | ||
51 | /* A single (bad) Netlink message can in theory dump out many, many log | |
52 | * messages, so the burst size is set quite high here to avoid missing useful | |
53 | * information. Also, at high logging levels we log *all* Netlink messages. */ | |
54 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600); | |
55 | ||
7d7447df | 56 | static uint32_t nl_sock_allocate_seq(struct nl_sock *, unsigned int n); |
2fe27d5a | 57 | static void log_nlmsg(const char *function, int error, |
7041c3a9 | 58 | const void *message, size_t size, int protocol); |
2fe27d5a BP |
59 | \f |
60 | /* Netlink sockets. */ | |
61 | ||
62 | struct nl_sock | |
63 | { | |
64 | int fd; | |
7d7447df | 65 | uint32_t next_seq; |
2fe27d5a | 66 | uint32_t pid; |
7041c3a9 | 67 | int protocol; |
c6eab56d | 68 | struct nl_dump *dump; |
cc75061a | 69 | unsigned int rcvbuf; /* Receive buffer size (SO_RCVBUF). */ |
2fe27d5a BP |
70 | }; |
71 | ||
cc75061a BP |
72 | /* Compile-time limit on iovecs, so that we can allocate a maximum-size array |
73 | * of iovecs on the stack. */ | |
74 | #define MAX_IOVS 128 | |
75 | ||
76 | /* Maximum number of iovecs that may be passed to sendmsg, capped at a | |
77 | * minimum of _XOPEN_IOV_MAX (16) and a maximum of MAX_IOVS. | |
78 | * | |
79 | * Initialized by nl_sock_create(). */ | |
80 | static int max_iovs; | |
81 | ||
c6eab56d | 82 | static int nl_sock_cow__(struct nl_sock *); |
2fe27d5a BP |
83 | |
84 | /* Creates a new netlink socket for the given netlink 'protocol' | |
85 | * (NETLINK_ROUTE, NETLINK_GENERIC, ...). Returns 0 and sets '*sockp' to the | |
cceb11f5 | 86 | * new socket if successful, otherwise returns a positive errno value. */ |
2fe27d5a | 87 | int |
cceb11f5 | 88 | nl_sock_create(int protocol, struct nl_sock **sockp) |
2fe27d5a BP |
89 | { |
90 | struct nl_sock *sock; | |
91 | struct sockaddr_nl local, remote; | |
2c5a6834 | 92 | socklen_t local_size; |
d2b9f5b0 | 93 | int rcvbuf; |
2fe27d5a BP |
94 | int retval = 0; |
95 | ||
cc75061a BP |
96 | if (!max_iovs) { |
97 | int save_errno = errno; | |
98 | errno = 0; | |
99 | ||
100 | max_iovs = sysconf(_SC_UIO_MAXIOV); | |
101 | if (max_iovs < _XOPEN_IOV_MAX) { | |
102 | if (max_iovs == -1 && errno) { | |
103 | VLOG_WARN("sysconf(_SC_UIO_MAXIOV): %s", strerror(errno)); | |
104 | } | |
105 | max_iovs = _XOPEN_IOV_MAX; | |
106 | } else if (max_iovs > MAX_IOVS) { | |
107 | max_iovs = MAX_IOVS; | |
108 | } | |
109 | ||
110 | errno = save_errno; | |
111 | } | |
112 | ||
2fe27d5a BP |
113 | *sockp = NULL; |
114 | sock = malloc(sizeof *sock); | |
115 | if (sock == NULL) { | |
116 | return ENOMEM; | |
117 | } | |
118 | ||
119 | sock->fd = socket(AF_NETLINK, SOCK_RAW, protocol); | |
120 | if (sock->fd < 0) { | |
121 | VLOG_ERR("fcntl: %s", strerror(errno)); | |
122 | goto error; | |
123 | } | |
7041c3a9 | 124 | sock->protocol = protocol; |
2ad204c8 | 125 | sock->dump = NULL; |
7d7447df | 126 | sock->next_seq = 1; |
2fe27d5a | 127 | |
d2b9f5b0 BP |
128 | rcvbuf = 1024 * 1024; |
129 | if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUFFORCE, | |
130 | &rcvbuf, sizeof rcvbuf)) { | |
80af5ee5 BP |
131 | /* Only root can use SO_RCVBUFFORCE. Everyone else gets EPERM. |
132 | * Warn only if the failure is therefore unexpected. */ | |
133 | if (errno != EPERM || !getuid()) { | |
134 | VLOG_WARN_RL(&rl, "setting %d-byte socket receive buffer failed " | |
135 | "(%s)", rcvbuf, strerror(errno)); | |
136 | } | |
d2b9f5b0 BP |
137 | } |
138 | ||
cc75061a BP |
139 | retval = get_socket_rcvbuf(sock->fd); |
140 | if (retval < 0) { | |
141 | retval = -retval; | |
142 | goto error; | |
143 | } | |
144 | sock->rcvbuf = retval; | |
145 | ||
2c5a6834 | 146 | /* Connect to kernel (pid 0) as remote address. */ |
2fe27d5a BP |
147 | memset(&remote, 0, sizeof remote); |
148 | remote.nl_family = AF_NETLINK; | |
149 | remote.nl_pid = 0; | |
150 | if (connect(sock->fd, (struct sockaddr *) &remote, sizeof remote) < 0) { | |
151 | VLOG_ERR("connect(0): %s", strerror(errno)); | |
2c5a6834 BP |
152 | goto error; |
153 | } | |
154 | ||
155 | /* Obtain pid assigned by kernel. */ | |
156 | local_size = sizeof local; | |
157 | if (getsockname(sock->fd, (struct sockaddr *) &local, &local_size) < 0) { | |
158 | VLOG_ERR("getsockname: %s", strerror(errno)); | |
159 | goto error; | |
160 | } | |
161 | if (local_size < sizeof local || local.nl_family != AF_NETLINK) { | |
162 | VLOG_ERR("getsockname returned bad Netlink name"); | |
163 | retval = EINVAL; | |
164 | goto error; | |
2fe27d5a | 165 | } |
2c5a6834 | 166 | sock->pid = local.nl_pid; |
2fe27d5a | 167 | |
2fe27d5a BP |
168 | *sockp = sock; |
169 | return 0; | |
170 | ||
2fe27d5a BP |
171 | error: |
172 | if (retval == 0) { | |
173 | retval = errno; | |
174 | if (retval == 0) { | |
175 | retval = EINVAL; | |
176 | } | |
177 | } | |
178 | if (sock->fd >= 0) { | |
179 | close(sock->fd); | |
180 | } | |
181 | free(sock); | |
182 | return retval; | |
183 | } | |
184 | ||
c6eab56d BP |
185 | /* Creates a new netlink socket for the same protocol as 'src'. Returns 0 and |
186 | * sets '*sockp' to the new socket if successful, otherwise returns a positive | |
187 | * errno value. */ | |
188 | int | |
189 | nl_sock_clone(const struct nl_sock *src, struct nl_sock **sockp) | |
190 | { | |
191 | return nl_sock_create(src->protocol, sockp); | |
192 | } | |
193 | ||
2fe27d5a BP |
194 | /* Destroys netlink socket 'sock'. */ |
195 | void | |
196 | nl_sock_destroy(struct nl_sock *sock) | |
197 | { | |
198 | if (sock) { | |
c6eab56d BP |
199 | if (sock->dump) { |
200 | sock->dump = NULL; | |
201 | } else { | |
202 | close(sock->fd); | |
c6eab56d BP |
203 | free(sock); |
204 | } | |
2fe27d5a BP |
205 | } |
206 | } | |
207 | ||
cceb11f5 BP |
208 | /* Tries to add 'sock' as a listener for 'multicast_group'. Returns 0 if |
209 | * successful, otherwise a positive errno value. | |
210 | * | |
a838c4fe BP |
211 | * A socket that is subscribed to a multicast group that receives asynchronous |
212 | * notifications must not be used for Netlink transactions or dumps, because | |
213 | * transactions and dumps can cause notifications to be lost. | |
214 | * | |
cceb11f5 BP |
215 | * Multicast group numbers are always positive. |
216 | * | |
217 | * It is not an error to attempt to join a multicast group to which a socket | |
218 | * already belongs. */ | |
219 | int | |
220 | nl_sock_join_mcgroup(struct nl_sock *sock, unsigned int multicast_group) | |
221 | { | |
c6eab56d BP |
222 | int error = nl_sock_cow__(sock); |
223 | if (error) { | |
224 | return error; | |
225 | } | |
cceb11f5 BP |
226 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, |
227 | &multicast_group, sizeof multicast_group) < 0) { | |
228 | VLOG_WARN("could not join multicast group %u (%s)", | |
229 | multicast_group, strerror(errno)); | |
230 | return errno; | |
231 | } | |
232 | return 0; | |
233 | } | |
234 | ||
235 | /* Tries to make 'sock' stop listening to 'multicast_group'. Returns 0 if | |
236 | * successful, otherwise a positive errno value. | |
237 | * | |
238 | * Multicast group numbers are always positive. | |
239 | * | |
240 | * It is not an error to attempt to leave a multicast group to which a socket | |
241 | * does not belong. | |
242 | * | |
243 | * On success, reading from 'sock' will still return any messages that were | |
244 | * received on 'multicast_group' before the group was left. */ | |
245 | int | |
246 | nl_sock_leave_mcgroup(struct nl_sock *sock, unsigned int multicast_group) | |
247 | { | |
cb22974d | 248 | ovs_assert(!sock->dump); |
cceb11f5 BP |
249 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, |
250 | &multicast_group, sizeof multicast_group) < 0) { | |
251 | VLOG_WARN("could not leave multicast group %u (%s)", | |
252 | multicast_group, strerror(errno)); | |
253 | return errno; | |
254 | } | |
255 | return 0; | |
256 | } | |
257 | ||
c6eab56d | 258 | static int |
ff459dd6 BP |
259 | nl_sock_send__(struct nl_sock *sock, const struct ofpbuf *msg, |
260 | uint32_t nlmsg_seq, bool wait) | |
2fe27d5a BP |
261 | { |
262 | struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(msg); | |
263 | int error; | |
264 | ||
265 | nlmsg->nlmsg_len = msg->size; | |
ff459dd6 | 266 | nlmsg->nlmsg_seq = nlmsg_seq; |
2fe27d5a BP |
267 | nlmsg->nlmsg_pid = sock->pid; |
268 | do { | |
269 | int retval; | |
270 | retval = send(sock->fd, msg->data, msg->size, wait ? 0 : MSG_DONTWAIT); | |
271 | error = retval < 0 ? errno : 0; | |
272 | } while (error == EINTR); | |
7041c3a9 | 273 | log_nlmsg(__func__, error, msg->data, msg->size, sock->protocol); |
2fe27d5a BP |
274 | if (!error) { |
275 | COVERAGE_INC(netlink_sent); | |
276 | } | |
277 | return error; | |
278 | } | |
279 | ||
c6eab56d | 280 | /* Tries to send 'msg', which must contain a Netlink message, to the kernel on |
ff459dd6 BP |
281 | * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, nlmsg_pid |
282 | * will be set to 'sock''s pid, and nlmsg_seq will be initialized to a fresh | |
283 | * sequence number, before the message is sent. | |
c6eab56d BP |
284 | * |
285 | * Returns 0 if successful, otherwise a positive errno value. If | |
286 | * 'wait' is true, then the send will wait until buffer space is ready; | |
287 | * otherwise, returns EAGAIN if the 'sock' send buffer is full. */ | |
288 | int | |
289 | nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait) | |
ff459dd6 BP |
290 | { |
291 | return nl_sock_send_seq(sock, msg, nl_sock_allocate_seq(sock, 1), wait); | |
292 | } | |
293 | ||
294 | /* Tries to send 'msg', which must contain a Netlink message, to the kernel on | |
295 | * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, nlmsg_pid | |
296 | * will be set to 'sock''s pid, and nlmsg_seq will be initialized to | |
297 | * 'nlmsg_seq', before the message is sent. | |
298 | * | |
299 | * Returns 0 if successful, otherwise a positive errno value. If | |
300 | * 'wait' is true, then the send will wait until buffer space is ready; | |
301 | * otherwise, returns EAGAIN if the 'sock' send buffer is full. | |
302 | * | |
303 | * This function is suitable for sending a reply to a request that was received | |
304 | * with sequence number 'nlmsg_seq'. Otherwise, use nl_sock_send() instead. */ | |
305 | int | |
306 | nl_sock_send_seq(struct nl_sock *sock, const struct ofpbuf *msg, | |
307 | uint32_t nlmsg_seq, bool wait) | |
c6eab56d BP |
308 | { |
309 | int error = nl_sock_cow__(sock); | |
310 | if (error) { | |
311 | return error; | |
312 | } | |
ff459dd6 | 313 | return nl_sock_send__(sock, msg, nlmsg_seq, wait); |
c6eab56d BP |
314 | } |
315 | ||
2fe27d5a BP |
316 | /* This stress option is useful for testing that OVS properly tolerates |
317 | * -ENOBUFS on NetLink sockets. Such errors are unavoidable because they can | |
318 | * occur if the kernel cannot temporarily allocate enough GFP_ATOMIC memory to | |
319 | * reply to a request. They can also occur if messages arrive on a multicast | |
320 | * channel faster than OVS can process them. */ | |
321 | STRESS_OPTION( | |
322 | netlink_overflow, "simulate netlink socket receive buffer overflow", | |
323 | 5, 1, -1, 100); | |
324 | ||
c6eab56d | 325 | static int |
72d32ac0 | 326 | nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait) |
2fe27d5a | 327 | { |
72d32ac0 BP |
328 | /* We can't accurately predict the size of the data to be received. The |
329 | * caller is supposed to have allocated enough space in 'buf' to handle the | |
330 | * "typical" case. To handle exceptions, we make available enough space in | |
331 | * 'tail' to allow Netlink messages to be up to 64 kB long (a reasonable | |
332 | * figure since that's the maximum length of a Netlink attribute). */ | |
2fe27d5a | 333 | struct nlmsghdr *nlmsghdr; |
72d32ac0 | 334 | uint8_t tail[65536]; |
fc999dda | 335 | struct iovec iov[2]; |
fc999dda BP |
336 | struct msghdr msg; |
337 | ssize_t retval; | |
338 | ||
cb22974d | 339 | ovs_assert(buf->allocated >= sizeof *nlmsghdr); |
72d32ac0 | 340 | ofpbuf_clear(buf); |
2fe27d5a | 341 | |
72d32ac0 BP |
342 | iov[0].iov_base = buf->base; |
343 | iov[0].iov_len = buf->allocated; | |
fc999dda | 344 | iov[1].iov_base = tail; |
72d32ac0 | 345 | iov[1].iov_len = sizeof tail; |
fc999dda BP |
346 | |
347 | memset(&msg, 0, sizeof msg); | |
348 | msg.msg_iov = iov; | |
349 | msg.msg_iovlen = 2; | |
350 | ||
2fe27d5a | 351 | do { |
fc999dda BP |
352 | retval = recvmsg(sock->fd, &msg, wait ? 0 : MSG_DONTWAIT); |
353 | } while (retval < 0 && errno == EINTR); | |
354 | ||
355 | if (retval < 0) { | |
356 | int error = errno; | |
357 | if (error == ENOBUFS) { | |
358 | /* Socket receive buffer overflow dropped one or more messages that | |
359 | * the kernel tried to send to us. */ | |
360 | COVERAGE_INC(netlink_overflow); | |
361 | } | |
fc999dda | 362 | return error; |
2fe27d5a | 363 | } |
fc999dda | 364 | |
2fe27d5a | 365 | if (msg.msg_flags & MSG_TRUNC) { |
72d32ac0 BP |
366 | VLOG_ERR_RL(&rl, "truncated message (longer than %zu bytes)", |
367 | sizeof tail); | |
fc999dda | 368 | return E2BIG; |
2fe27d5a | 369 | } |
2fe27d5a | 370 | |
fc999dda BP |
371 | nlmsghdr = buf->data; |
372 | if (retval < sizeof *nlmsghdr | |
2fe27d5a | 373 | || nlmsghdr->nlmsg_len < sizeof *nlmsghdr |
fc999dda | 374 | || nlmsghdr->nlmsg_len > retval) { |
72d32ac0 BP |
375 | VLOG_ERR_RL(&rl, "received invalid nlmsg (%zd bytes < %zu)", |
376 | retval, sizeof *nlmsghdr); | |
2fe27d5a BP |
377 | return EPROTO; |
378 | } | |
379 | ||
380 | if (STRESS(netlink_overflow)) { | |
2fe27d5a BP |
381 | return ENOBUFS; |
382 | } | |
383 | ||
72d32ac0 BP |
384 | buf->size = MIN(retval, buf->allocated); |
385 | if (retval > buf->allocated) { | |
386 | COVERAGE_INC(netlink_recv_jumbo); | |
387 | ofpbuf_put(buf, tail, retval - buf->allocated); | |
388 | } | |
389 | ||
7041c3a9 | 390 | log_nlmsg(__func__, 0, buf->data, buf->size, sock->protocol); |
2fe27d5a BP |
391 | COVERAGE_INC(netlink_received); |
392 | ||
393 | return 0; | |
394 | } | |
395 | ||
72d32ac0 BP |
396 | /* Tries to receive a Netlink message from the kernel on 'sock' into 'buf'. If |
397 | * 'wait' is true, waits for a message to be ready. Otherwise, fails with | |
398 | * EAGAIN if the 'sock' receive buffer is empty. | |
399 | * | |
400 | * The caller must have initialized 'buf' with an allocation of at least | |
401 | * NLMSG_HDRLEN bytes. For best performance, the caller should allocate enough | |
402 | * space for a "typical" message. | |
403 | * | |
404 | * On success, returns 0 and replaces 'buf''s previous content by the received | |
405 | * message. This function expands 'buf''s allocated memory, as necessary, to | |
406 | * hold the actual size of the received message. | |
c6eab56d | 407 | * |
72d32ac0 BP |
408 | * On failure, returns a positive errno value and clears 'buf' to zero length. |
409 | * 'buf' retains its previous memory allocation. | |
410 | * | |
411 | * Regardless of success or failure, this function resets 'buf''s headroom to | |
412 | * 0. */ | |
c6eab56d | 413 | int |
72d32ac0 | 414 | nl_sock_recv(struct nl_sock *sock, struct ofpbuf *buf, bool wait) |
c6eab56d BP |
415 | { |
416 | int error = nl_sock_cow__(sock); | |
417 | if (error) { | |
418 | return error; | |
419 | } | |
72d32ac0 | 420 | return nl_sock_recv__(sock, buf, wait); |
cc75061a BP |
421 | } |
422 | ||
423 | static void | |
424 | nl_sock_record_errors__(struct nl_transaction **transactions, size_t n, | |
425 | int error) | |
426 | { | |
427 | size_t i; | |
428 | ||
429 | for (i = 0; i < n; i++) { | |
72d32ac0 BP |
430 | struct nl_transaction *txn = transactions[i]; |
431 | ||
432 | txn->error = error; | |
433 | if (txn->reply) { | |
434 | ofpbuf_clear(txn->reply); | |
435 | } | |
cc75061a BP |
436 | } |
437 | } | |
438 | ||
439 | static int | |
440 | nl_sock_transact_multiple__(struct nl_sock *sock, | |
441 | struct nl_transaction **transactions, size_t n, | |
442 | size_t *done) | |
443 | { | |
72d32ac0 BP |
444 | uint64_t tmp_reply_stub[1024 / 8]; |
445 | struct nl_transaction tmp_txn; | |
446 | struct ofpbuf tmp_reply; | |
447 | ||
448 | uint32_t base_seq; | |
cc75061a BP |
449 | struct iovec iovs[MAX_IOVS]; |
450 | struct msghdr msg; | |
451 | int error; | |
452 | int i; | |
453 | ||
72d32ac0 | 454 | base_seq = nl_sock_allocate_seq(sock, n); |
cc75061a BP |
455 | *done = 0; |
456 | for (i = 0; i < n; i++) { | |
72d32ac0 BP |
457 | struct nl_transaction *txn = transactions[i]; |
458 | struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(txn->request); | |
cc75061a | 459 | |
72d32ac0 BP |
460 | nlmsg->nlmsg_len = txn->request->size; |
461 | nlmsg->nlmsg_seq = base_seq + i; | |
cc75061a | 462 | nlmsg->nlmsg_pid = sock->pid; |
cc75061a | 463 | |
72d32ac0 BP |
464 | iovs[i].iov_base = txn->request->data; |
465 | iovs[i].iov_len = txn->request->size; | |
cc75061a BP |
466 | } |
467 | ||
468 | memset(&msg, 0, sizeof msg); | |
469 | msg.msg_iov = iovs; | |
470 | msg.msg_iovlen = n; | |
471 | do { | |
472 | error = sendmsg(sock->fd, &msg, 0) < 0 ? errno : 0; | |
473 | } while (error == EINTR); | |
474 | ||
475 | for (i = 0; i < n; i++) { | |
72d32ac0 | 476 | struct nl_transaction *txn = transactions[i]; |
cc75061a | 477 | |
72d32ac0 | 478 | log_nlmsg(__func__, error, txn->request->data, txn->request->size, |
cc75061a BP |
479 | sock->protocol); |
480 | } | |
481 | if (!error) { | |
482 | COVERAGE_ADD(netlink_sent, n); | |
483 | } | |
484 | ||
485 | if (error) { | |
486 | return error; | |
487 | } | |
488 | ||
72d32ac0 BP |
489 | ofpbuf_use_stub(&tmp_reply, tmp_reply_stub, sizeof tmp_reply_stub); |
490 | tmp_txn.request = NULL; | |
491 | tmp_txn.reply = &tmp_reply; | |
492 | tmp_txn.error = 0; | |
cc75061a | 493 | while (n > 0) { |
72d32ac0 BP |
494 | struct nl_transaction *buf_txn, *txn; |
495 | uint32_t seq; | |
496 | ||
497 | /* Find a transaction whose buffer we can use for receiving a reply. | |
498 | * If no such transaction is left, use tmp_txn. */ | |
499 | buf_txn = &tmp_txn; | |
500 | for (i = 0; i < n; i++) { | |
501 | if (transactions[i]->reply) { | |
502 | buf_txn = transactions[i]; | |
503 | break; | |
504 | } | |
505 | } | |
cc75061a | 506 | |
72d32ac0 BP |
507 | /* Receive a reply. */ |
508 | error = nl_sock_recv__(sock, buf_txn->reply, false); | |
509 | if (error) { | |
510 | if (error == EAGAIN) { | |
511 | nl_sock_record_errors__(transactions, n, 0); | |
512 | *done += n; | |
513 | error = 0; | |
514 | } | |
515 | break; | |
cc75061a BP |
516 | } |
517 | ||
72d32ac0 BP |
518 | /* Match the reply up with a transaction. */ |
519 | seq = nl_msg_nlmsghdr(buf_txn->reply)->nlmsg_seq; | |
520 | if (seq < base_seq || seq >= base_seq + n) { | |
521 | VLOG_DBG_RL(&rl, "ignoring unexpected seq %#"PRIx32, seq); | |
cc75061a BP |
522 | continue; |
523 | } | |
72d32ac0 BP |
524 | i = seq - base_seq; |
525 | txn = transactions[i]; | |
cc75061a | 526 | |
72d32ac0 BP |
527 | /* Fill in the results for 'txn'. */ |
528 | if (nl_msg_nlmsgerr(buf_txn->reply, &txn->error)) { | |
529 | if (txn->reply) { | |
530 | ofpbuf_clear(txn->reply); | |
531 | } | |
532 | if (txn->error) { | |
cc75061a | 533 | VLOG_DBG_RL(&rl, "received NAK error=%d (%s)", |
72d32ac0 | 534 | error, strerror(txn->error)); |
cc75061a | 535 | } |
cc75061a | 536 | } else { |
72d32ac0 BP |
537 | txn->error = 0; |
538 | if (txn->reply && txn != buf_txn) { | |
539 | /* Swap buffers. */ | |
540 | struct ofpbuf *reply = buf_txn->reply; | |
541 | buf_txn->reply = txn->reply; | |
542 | txn->reply = reply; | |
543 | } | |
cc75061a BP |
544 | } |
545 | ||
72d32ac0 BP |
546 | /* Fill in the results for transactions before 'txn'. (We have to do |
547 | * this after the results for 'txn' itself because of the buffer swap | |
548 | * above.) */ | |
549 | nl_sock_record_errors__(transactions, i, 0); | |
550 | ||
551 | /* Advance. */ | |
cc75061a BP |
552 | *done += i + 1; |
553 | transactions += i + 1; | |
554 | n -= i + 1; | |
72d32ac0 | 555 | base_seq += i + 1; |
cc75061a | 556 | } |
72d32ac0 | 557 | ofpbuf_uninit(&tmp_reply); |
cc75061a | 558 | |
72d32ac0 | 559 | return error; |
cc75061a BP |
560 | } |
561 | ||
72d32ac0 BP |
562 | /* Sends the 'request' member of the 'n' transactions in 'transactions' on |
563 | * 'sock', in order, and receives responses to all of them. Fills in the | |
cc75061a | 564 | * 'error' member of each transaction with 0 if it was successful, otherwise |
72d32ac0 BP |
565 | * with a positive errno value. If 'reply' is nonnull, then it will be filled |
566 | * with the reply if the message receives a detailed reply. In other cases, | |
567 | * i.e. where the request failed or had no reply beyond an indication of | |
568 | * success, 'reply' will be cleared if it is nonnull. | |
cc75061a BP |
569 | * |
570 | * The caller is responsible for destroying each request and reply, and the | |
571 | * transactions array itself. | |
572 | * | |
573 | * Before sending each message, this function will finalize nlmsg_len in each | |
72d32ac0 BP |
574 | * 'request' to match the ofpbuf's size, set nlmsg_pid to 'sock''s pid, and |
575 | * initialize nlmsg_seq. | |
cc75061a BP |
576 | * |
577 | * Bare Netlink is an unreliable transport protocol. This function layers | |
578 | * reliable delivery and reply semantics on top of bare Netlink. See | |
579 | * nl_sock_transact() for some caveats. | |
580 | */ | |
581 | void | |
582 | nl_sock_transact_multiple(struct nl_sock *sock, | |
583 | struct nl_transaction **transactions, size_t n) | |
584 | { | |
585 | int max_batch_count; | |
586 | int error; | |
587 | ||
588 | if (!n) { | |
589 | return; | |
590 | } | |
591 | ||
592 | error = nl_sock_cow__(sock); | |
593 | if (error) { | |
594 | nl_sock_record_errors__(transactions, n, error); | |
595 | return; | |
596 | } | |
597 | ||
598 | /* In theory, every request could have a 64 kB reply. But the default and | |
599 | * maximum socket rcvbuf size with typical Dom0 memory sizes both tend to | |
600 | * be a bit below 128 kB, so that would only allow a single message in a | |
601 | * "batch". So we assume that replies average (at most) 4 kB, which allows | |
602 | * a good deal of batching. | |
603 | * | |
604 | * In practice, most of the requests that we batch either have no reply at | |
605 | * all or a brief reply. */ | |
606 | max_batch_count = MAX(sock->rcvbuf / 4096, 1); | |
607 | max_batch_count = MIN(max_batch_count, max_iovs); | |
608 | ||
609 | while (n > 0) { | |
610 | size_t count, bytes; | |
611 | size_t done; | |
612 | ||
613 | /* Batch up to 'max_batch_count' transactions. But cap it at about a | |
614 | * page of requests total because big skbuffs are expensive to | |
615 | * allocate in the kernel. */ | |
616 | #if defined(PAGESIZE) | |
617 | enum { MAX_BATCH_BYTES = MAX(1, PAGESIZE - 512) }; | |
618 | #else | |
619 | enum { MAX_BATCH_BYTES = 4096 - 512 }; | |
620 | #endif | |
621 | bytes = transactions[0]->request->size; | |
622 | for (count = 1; count < n && count < max_batch_count; count++) { | |
623 | if (bytes + transactions[count]->request->size > MAX_BATCH_BYTES) { | |
624 | break; | |
625 | } | |
626 | bytes += transactions[count]->request->size; | |
627 | } | |
628 | ||
629 | error = nl_sock_transact_multiple__(sock, transactions, count, &done); | |
630 | transactions += done; | |
631 | n -= done; | |
632 | ||
633 | if (error == ENOBUFS) { | |
634 | VLOG_DBG_RL(&rl, "receive buffer overflow, resending request"); | |
635 | } else if (error) { | |
636 | VLOG_ERR_RL(&rl, "transaction error (%s)", strerror(error)); | |
637 | nl_sock_record_errors__(transactions, n, error); | |
638 | } | |
639 | } | |
640 | } | |
641 | ||
2fe27d5a BP |
642 | /* Sends 'request' to the kernel via 'sock' and waits for a response. If |
643 | * successful, returns 0. On failure, returns a positive errno value. | |
644 | * | |
645 | * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's | |
646 | * reply, which the caller is responsible for freeing with ofpbuf_delete(), and | |
647 | * on failure '*replyp' is set to NULL. If 'replyp' is null, then the kernel's | |
648 | * reply, if any, is discarded. | |
649 | * | |
7d7447df BP |
650 | * Before the message is sent, nlmsg_len in 'request' will be finalized to |
651 | * match msg->size, nlmsg_pid will be set to 'sock''s pid, and nlmsg_seq will | |
652 | * be initialized, NLM_F_ACK will be set in nlmsg_flags. | |
2fe27d5a BP |
653 | * |
654 | * The caller is responsible for destroying 'request'. | |
655 | * | |
656 | * Bare Netlink is an unreliable transport protocol. This function layers | |
657 | * reliable delivery and reply semantics on top of bare Netlink. | |
658 | * | |
659 | * In Netlink, sending a request to the kernel is reliable enough, because the | |
660 | * kernel will tell us if the message cannot be queued (and we will in that | |
661 | * case put it on the transmit queue and wait until it can be delivered). | |
662 | * | |
663 | * Receiving the reply is the real problem: if the socket buffer is full when | |
664 | * the kernel tries to send the reply, the reply will be dropped. However, the | |
665 | * kernel sets a flag that a reply has been dropped. The next call to recv | |
666 | * then returns ENOBUFS. We can then re-send the request. | |
667 | * | |
668 | * Caveats: | |
669 | * | |
670 | * 1. Netlink depends on sequence numbers to match up requests and | |
671 | * replies. The sender of a request supplies a sequence number, and | |
672 | * the reply echos back that sequence number. | |
673 | * | |
674 | * This is fine, but (1) some kernel netlink implementations are | |
675 | * broken, in that they fail to echo sequence numbers and (2) this | |
676 | * function will drop packets with non-matching sequence numbers, so | |
677 | * that only a single request can be usefully transacted at a time. | |
678 | * | |
679 | * 2. Resending the request causes it to be re-executed, so the request | |
680 | * needs to be idempotent. | |
681 | */ | |
682 | int | |
cc75061a BP |
683 | nl_sock_transact(struct nl_sock *sock, const struct ofpbuf *request, |
684 | struct ofpbuf **replyp) | |
2fe27d5a | 685 | { |
cc75061a BP |
686 | struct nl_transaction *transactionp; |
687 | struct nl_transaction transaction; | |
2fe27d5a | 688 | |
ebc56baa | 689 | transaction.request = CONST_CAST(struct ofpbuf *, request); |
72d32ac0 | 690 | transaction.reply = replyp ? ofpbuf_new(1024) : NULL; |
cc75061a | 691 | transactionp = &transaction; |
72d32ac0 | 692 | |
cc75061a | 693 | nl_sock_transact_multiple(sock, &transactionp, 1); |
72d32ac0 | 694 | |
2fe27d5a | 695 | if (replyp) { |
72d32ac0 BP |
696 | if (transaction.error) { |
697 | ofpbuf_delete(transaction.reply); | |
698 | *replyp = NULL; | |
699 | } else { | |
700 | *replyp = transaction.reply; | |
701 | } | |
2fe27d5a | 702 | } |
72d32ac0 | 703 | |
cc75061a | 704 | return transaction.error; |
2fe27d5a BP |
705 | } |
706 | ||
6b7c12fd BP |
707 | /* Drain all the messages currently in 'sock''s receive queue. */ |
708 | int | |
709 | nl_sock_drain(struct nl_sock *sock) | |
710 | { | |
c6eab56d BP |
711 | int error = nl_sock_cow__(sock); |
712 | if (error) { | |
713 | return error; | |
714 | } | |
6b7c12fd BP |
715 | return drain_rcvbuf(sock->fd); |
716 | } | |
717 | ||
c6eab56d BP |
718 | /* The client is attempting some operation on 'sock'. If 'sock' has an ongoing |
719 | * dump operation, then replace 'sock''s fd with a new socket and hand 'sock''s | |
720 | * old fd over to the dump. */ | |
721 | static int | |
722 | nl_sock_cow__(struct nl_sock *sock) | |
723 | { | |
724 | struct nl_sock *copy; | |
725 | uint32_t tmp_pid; | |
726 | int tmp_fd; | |
727 | int error; | |
728 | ||
729 | if (!sock->dump) { | |
730 | return 0; | |
731 | } | |
732 | ||
733 | error = nl_sock_clone(sock, ©); | |
734 | if (error) { | |
735 | return error; | |
736 | } | |
737 | ||
738 | tmp_fd = sock->fd; | |
739 | sock->fd = copy->fd; | |
740 | copy->fd = tmp_fd; | |
741 | ||
742 | tmp_pid = sock->pid; | |
743 | sock->pid = copy->pid; | |
744 | copy->pid = tmp_pid; | |
745 | ||
746 | sock->dump->sock = copy; | |
747 | sock->dump = NULL; | |
748 | ||
749 | return 0; | |
750 | } | |
751 | ||
2fe27d5a BP |
752 | /* Starts a Netlink "dump" operation, by sending 'request' to the kernel via |
753 | * 'sock', and initializes 'dump' to reflect the state of the operation. | |
754 | * | |
755 | * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will | |
756 | * be set to 'sock''s pid, before the message is sent. NLM_F_DUMP and | |
757 | * NLM_F_ACK will be set in nlmsg_flags. | |
758 | * | |
c6eab56d BP |
759 | * This Netlink socket library is designed to ensure that the dump is reliable |
760 | * and that it will not interfere with other operations on 'sock', including | |
761 | * destroying or sending and receiving messages on 'sock'. One corner case is | |
762 | * not handled: | |
2fe27d5a | 763 | * |
c6eab56d BP |
764 | * - If 'sock' has been used to send a request (e.g. with nl_sock_send()) |
765 | * whose response has not yet been received (e.g. with nl_sock_recv()). | |
766 | * This is unusual: usually nl_sock_transact() is used to send a message | |
767 | * and receive its reply all in one go. | |
2fe27d5a BP |
768 | * |
769 | * This function provides no status indication. An error status for the entire | |
770 | * dump operation is provided when it is completed by calling nl_dump_done(). | |
771 | * | |
c6eab56d BP |
772 | * The caller is responsible for destroying 'request'. |
773 | * | |
774 | * The new 'dump' is independent of 'sock'. 'sock' and 'dump' may be destroyed | |
775 | * in either order. | |
2fe27d5a BP |
776 | */ |
777 | void | |
778 | nl_dump_start(struct nl_dump *dump, | |
779 | struct nl_sock *sock, const struct ofpbuf *request) | |
780 | { | |
72d32ac0 | 781 | ofpbuf_init(&dump->buffer, 4096); |
a838c4fe BP |
782 | if (sock->dump) { |
783 | /* 'sock' already has an ongoing dump. Clone the socket because | |
784 | * Netlink only allows one dump at a time. */ | |
c6eab56d BP |
785 | dump->status = nl_sock_clone(sock, &dump->sock); |
786 | if (dump->status) { | |
787 | return; | |
788 | } | |
789 | } else { | |
790 | sock->dump = dump; | |
791 | dump->sock = sock; | |
792 | dump->status = 0; | |
793 | } | |
7d7447df BP |
794 | |
795 | nl_msg_nlmsghdr(request)->nlmsg_flags |= NLM_F_DUMP | NLM_F_ACK; | |
ff459dd6 BP |
796 | dump->status = nl_sock_send__(sock, request, nl_sock_allocate_seq(sock, 1), |
797 | true); | |
7d7447df | 798 | dump->seq = nl_msg_nlmsghdr(request)->nlmsg_seq; |
2fe27d5a BP |
799 | } |
800 | ||
801 | /* Helper function for nl_dump_next(). */ | |
802 | static int | |
72d32ac0 | 803 | nl_dump_recv(struct nl_dump *dump) |
2fe27d5a BP |
804 | { |
805 | struct nlmsghdr *nlmsghdr; | |
2fe27d5a BP |
806 | int retval; |
807 | ||
72d32ac0 | 808 | retval = nl_sock_recv__(dump->sock, &dump->buffer, true); |
2fe27d5a BP |
809 | if (retval) { |
810 | return retval == EINTR ? EAGAIN : retval; | |
811 | } | |
2fe27d5a | 812 | |
72d32ac0 | 813 | nlmsghdr = nl_msg_nlmsghdr(&dump->buffer); |
2fe27d5a | 814 | if (dump->seq != nlmsghdr->nlmsg_seq) { |
727ef33f | 815 | VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32, |
2fe27d5a BP |
816 | nlmsghdr->nlmsg_seq, dump->seq); |
817 | return EAGAIN; | |
818 | } | |
819 | ||
72d32ac0 | 820 | if (nl_msg_nlmsgerr(&dump->buffer, &retval)) { |
2fe27d5a BP |
821 | VLOG_INFO_RL(&rl, "netlink dump request error (%s)", |
822 | strerror(retval)); | |
823 | return retval && retval != EAGAIN ? retval : EPROTO; | |
824 | } | |
825 | ||
826 | return 0; | |
827 | } | |
828 | ||
829 | /* Attempts to retrieve another reply from 'dump', which must have been | |
830 | * initialized with nl_dump_start(). | |
831 | * | |
832 | * If successful, returns true and points 'reply->data' and 'reply->size' to | |
833 | * the message that was retrieved. The caller must not modify 'reply' (because | |
834 | * it points into the middle of a larger buffer). | |
835 | * | |
836 | * On failure, returns false and sets 'reply->data' to NULL and 'reply->size' | |
837 | * to 0. Failure might indicate an actual error or merely the end of replies. | |
838 | * An error status for the entire dump operation is provided when it is | |
839 | * completed by calling nl_dump_done(). | |
840 | */ | |
841 | bool | |
842 | nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply) | |
843 | { | |
844 | struct nlmsghdr *nlmsghdr; | |
845 | ||
846 | reply->data = NULL; | |
847 | reply->size = 0; | |
848 | if (dump->status) { | |
849 | return false; | |
850 | } | |
851 | ||
72d32ac0 BP |
852 | while (!dump->buffer.size) { |
853 | int retval = nl_dump_recv(dump); | |
2fe27d5a | 854 | if (retval) { |
72d32ac0 | 855 | ofpbuf_clear(&dump->buffer); |
2fe27d5a BP |
856 | if (retval != EAGAIN) { |
857 | dump->status = retval; | |
858 | return false; | |
859 | } | |
860 | } | |
861 | } | |
862 | ||
72d32ac0 | 863 | nlmsghdr = nl_msg_next(&dump->buffer, reply); |
2fe27d5a BP |
864 | if (!nlmsghdr) { |
865 | VLOG_WARN_RL(&rl, "netlink dump reply contains message fragment"); | |
866 | dump->status = EPROTO; | |
867 | return false; | |
868 | } else if (nlmsghdr->nlmsg_type == NLMSG_DONE) { | |
869 | dump->status = EOF; | |
870 | return false; | |
871 | } | |
872 | ||
873 | return true; | |
874 | } | |
875 | ||
876 | /* Completes Netlink dump operation 'dump', which must have been initialized | |
877 | * with nl_dump_start(). Returns 0 if the dump operation was error-free, | |
878 | * otherwise a positive errno value describing the problem. */ | |
879 | int | |
880 | nl_dump_done(struct nl_dump *dump) | |
881 | { | |
882 | /* Drain any remaining messages that the client didn't read. Otherwise the | |
883 | * kernel will continue to queue them up and waste buffer space. */ | |
884 | while (!dump->status) { | |
885 | struct ofpbuf reply; | |
886 | if (!nl_dump_next(dump, &reply)) { | |
cb22974d | 887 | ovs_assert(dump->status); |
2fe27d5a BP |
888 | } |
889 | } | |
890 | ||
c6eab56d BP |
891 | if (dump->sock) { |
892 | if (dump->sock->dump) { | |
893 | dump->sock->dump = NULL; | |
894 | } else { | |
895 | nl_sock_destroy(dump->sock); | |
896 | } | |
897 | } | |
72d32ac0 | 898 | ofpbuf_uninit(&dump->buffer); |
2fe27d5a BP |
899 | return dump->status == EOF ? 0 : dump->status; |
900 | } | |
901 | ||
902 | /* Causes poll_block() to wake up when any of the specified 'events' (which is | |
903 | * a OR'd combination of POLLIN, POLLOUT, etc.) occur on 'sock'. */ | |
904 | void | |
905 | nl_sock_wait(const struct nl_sock *sock, short int events) | |
906 | { | |
907 | poll_fd_wait(sock->fd, events); | |
908 | } | |
50802adb | 909 | |
8522ba09 BP |
910 | /* Returns the underlying fd for 'sock', for use in "poll()"-like operations |
911 | * that can't use nl_sock_wait(). | |
912 | * | |
913 | * It's a little tricky to use the returned fd correctly, because nl_sock does | |
914 | * "copy on write" to allow a single nl_sock to be used for notifications, | |
915 | * transactions, and dumps. If 'sock' is used only for notifications and | |
916 | * transactions (and never for dump) then the usage is safe. */ | |
917 | int | |
918 | nl_sock_fd(const struct nl_sock *sock) | |
919 | { | |
920 | return sock->fd; | |
921 | } | |
922 | ||
50802adb JG |
923 | /* Returns the PID associated with this socket. */ |
924 | uint32_t | |
925 | nl_sock_pid(const struct nl_sock *sock) | |
926 | { | |
927 | return sock->pid; | |
928 | } | |
2fe27d5a BP |
929 | \f |
930 | /* Miscellaneous. */ | |
931 | ||
2ad204c8 BP |
932 | struct genl_family { |
933 | struct hmap_node hmap_node; | |
934 | uint16_t id; | |
935 | char *name; | |
936 | }; | |
937 | ||
938 | static struct hmap genl_families = HMAP_INITIALIZER(&genl_families); | |
939 | ||
2fe27d5a BP |
940 | static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = { |
941 | [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16}, | |
213a13ed | 942 | [CTRL_ATTR_MCAST_GROUPS] = {.type = NL_A_NESTED, .optional = true}, |
2fe27d5a BP |
943 | }; |
944 | ||
2ad204c8 BP |
945 | static struct genl_family * |
946 | find_genl_family_by_id(uint16_t id) | |
947 | { | |
948 | struct genl_family *family; | |
949 | ||
950 | HMAP_FOR_EACH_IN_BUCKET (family, hmap_node, hash_int(id, 0), | |
951 | &genl_families) { | |
952 | if (family->id == id) { | |
953 | return family; | |
954 | } | |
955 | } | |
956 | return NULL; | |
957 | } | |
958 | ||
959 | static void | |
960 | define_genl_family(uint16_t id, const char *name) | |
961 | { | |
962 | struct genl_family *family = find_genl_family_by_id(id); | |
963 | ||
964 | if (family) { | |
965 | if (!strcmp(family->name, name)) { | |
966 | return; | |
967 | } | |
968 | free(family->name); | |
969 | } else { | |
970 | family = xmalloc(sizeof *family); | |
971 | family->id = id; | |
972 | hmap_insert(&genl_families, &family->hmap_node, hash_int(id, 0)); | |
973 | } | |
974 | family->name = xstrdup(name); | |
975 | } | |
976 | ||
977 | static const char * | |
978 | genl_family_to_name(uint16_t id) | |
979 | { | |
980 | if (id == GENL_ID_CTRL) { | |
981 | return "control"; | |
982 | } else { | |
983 | struct genl_family *family = find_genl_family_by_id(id); | |
984 | return family ? family->name : "unknown"; | |
985 | } | |
986 | } | |
987 | ||
e408762f | 988 | static int |
2a477244 BP |
989 | do_lookup_genl_family(const char *name, struct nlattr **attrs, |
990 | struct ofpbuf **replyp) | |
2fe27d5a BP |
991 | { |
992 | struct nl_sock *sock; | |
993 | struct ofpbuf request, *reply; | |
2a477244 | 994 | int error; |
2fe27d5a | 995 | |
2a477244 BP |
996 | *replyp = NULL; |
997 | error = nl_sock_create(NETLINK_GENERIC, &sock); | |
998 | if (error) { | |
999 | return error; | |
2fe27d5a BP |
1000 | } |
1001 | ||
1002 | ofpbuf_init(&request, 0); | |
1003 | nl_msg_put_genlmsghdr(&request, 0, GENL_ID_CTRL, NLM_F_REQUEST, | |
1004 | CTRL_CMD_GETFAMILY, 1); | |
1005 | nl_msg_put_string(&request, CTRL_ATTR_FAMILY_NAME, name); | |
2a477244 | 1006 | error = nl_sock_transact(sock, &request, &reply); |
2fe27d5a | 1007 | ofpbuf_uninit(&request); |
2a477244 | 1008 | if (error) { |
2fe27d5a | 1009 | nl_sock_destroy(sock); |
2a477244 | 1010 | return error; |
2fe27d5a BP |
1011 | } |
1012 | ||
1013 | if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN, | |
2a477244 BP |
1014 | family_policy, attrs, ARRAY_SIZE(family_policy)) |
1015 | || nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]) == 0) { | |
2fe27d5a BP |
1016 | nl_sock_destroy(sock); |
1017 | ofpbuf_delete(reply); | |
2a477244 | 1018 | return EPROTO; |
2fe27d5a BP |
1019 | } |
1020 | ||
2fe27d5a | 1021 | nl_sock_destroy(sock); |
2a477244 BP |
1022 | *replyp = reply; |
1023 | return 0; | |
2fe27d5a BP |
1024 | } |
1025 | ||
e408762f EJ |
1026 | /* Finds the multicast group called 'group_name' in genl family 'family_name'. |
1027 | * When successful, writes its result to 'multicast_group' and returns 0. | |
213a13ed EJ |
1028 | * Otherwise, clears 'multicast_group' and returns a positive error code. |
1029 | * | |
1030 | * Some kernels do not support looking up a multicast group with this function. | |
1031 | * In this case, 'multicast_group' will be populated with 'fallback'. */ | |
e408762f EJ |
1032 | int |
1033 | nl_lookup_genl_mcgroup(const char *family_name, const char *group_name, | |
213a13ed | 1034 | unsigned int *multicast_group, unsigned int fallback) |
e408762f EJ |
1035 | { |
1036 | struct nlattr *family_attrs[ARRAY_SIZE(family_policy)]; | |
6d23c6f4 | 1037 | const struct nlattr *mc; |
2a477244 | 1038 | struct ofpbuf *reply; |
e408762f | 1039 | unsigned int left; |
2a477244 | 1040 | int error; |
e408762f EJ |
1041 | |
1042 | *multicast_group = 0; | |
2a477244 BP |
1043 | error = do_lookup_genl_family(family_name, family_attrs, &reply); |
1044 | if (error) { | |
1045 | return error; | |
e408762f EJ |
1046 | } |
1047 | ||
213a13ed EJ |
1048 | if (!family_attrs[CTRL_ATTR_MCAST_GROUPS]) { |
1049 | *multicast_group = fallback; | |
1050 | VLOG_WARN("%s-%s: has no multicast group, using fallback %d", | |
1051 | family_name, group_name, *multicast_group); | |
1052 | error = 0; | |
1053 | goto exit; | |
1054 | } | |
1055 | ||
6d23c6f4 | 1056 | NL_NESTED_FOR_EACH (mc, left, family_attrs[CTRL_ATTR_MCAST_GROUPS]) { |
e408762f EJ |
1057 | static const struct nl_policy mc_policy[] = { |
1058 | [CTRL_ATTR_MCAST_GRP_ID] = {.type = NL_A_U32}, | |
1059 | [CTRL_ATTR_MCAST_GRP_NAME] = {.type = NL_A_STRING}, | |
1060 | }; | |
1061 | ||
1062 | struct nlattr *mc_attrs[ARRAY_SIZE(mc_policy)]; | |
1063 | const char *mc_name; | |
1064 | ||
1065 | if (!nl_parse_nested(mc, mc_policy, mc_attrs, ARRAY_SIZE(mc_policy))) { | |
2a477244 BP |
1066 | error = EPROTO; |
1067 | goto exit; | |
e408762f EJ |
1068 | } |
1069 | ||
1070 | mc_name = nl_attr_get_string(mc_attrs[CTRL_ATTR_MCAST_GRP_NAME]); | |
1071 | if (!strcmp(group_name, mc_name)) { | |
1072 | *multicast_group = | |
1073 | nl_attr_get_u32(mc_attrs[CTRL_ATTR_MCAST_GRP_ID]); | |
2a477244 BP |
1074 | error = 0; |
1075 | goto exit; | |
e408762f EJ |
1076 | } |
1077 | } | |
2a477244 | 1078 | error = EPROTO; |
e408762f | 1079 | |
2a477244 BP |
1080 | exit: |
1081 | ofpbuf_delete(reply); | |
1082 | return error; | |
e408762f EJ |
1083 | } |
1084 | ||
2fe27d5a BP |
1085 | /* If '*number' is 0, translates the given Generic Netlink family 'name' to a |
1086 | * number and stores it in '*number'. If successful, returns 0 and the caller | |
1087 | * may use '*number' as the family number. On failure, returns a positive | |
1088 | * errno value and '*number' caches the errno value. */ | |
1089 | int | |
1090 | nl_lookup_genl_family(const char *name, int *number) | |
1091 | { | |
1092 | if (*number == 0) { | |
2a477244 BP |
1093 | struct nlattr *attrs[ARRAY_SIZE(family_policy)]; |
1094 | struct ofpbuf *reply; | |
1095 | int error; | |
1096 | ||
1097 | error = do_lookup_genl_family(name, attrs, &reply); | |
1098 | if (!error) { | |
1099 | *number = nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]); | |
1100 | define_genl_family(*number, name); | |
1101 | } else { | |
1102 | *number = -error; | |
1103 | } | |
1104 | ofpbuf_delete(reply); | |
1105 | ||
cb22974d | 1106 | ovs_assert(*number != 0); |
2fe27d5a BP |
1107 | } |
1108 | return *number > 0 ? 0 : -*number; | |
1109 | } | |
1110 | \f | |
7d7447df BP |
1111 | static uint32_t |
1112 | nl_sock_allocate_seq(struct nl_sock *sock, unsigned int n) | |
1113 | { | |
1114 | uint32_t seq = sock->next_seq; | |
1115 | ||
1116 | sock->next_seq += n; | |
1117 | ||
1118 | /* Make it impossible for the next request for sequence numbers to wrap | |
1119 | * around to 0. Start over with 1 to avoid ever using a sequence number of | |
1120 | * 0, because the kernel uses sequence number 0 for notifications. */ | |
1121 | if (sock->next_seq >= UINT32_MAX / 2) { | |
1122 | sock->next_seq = 1; | |
1123 | } | |
1124 | ||
1125 | return seq; | |
1126 | } | |
1127 | ||
2fe27d5a | 1128 | static void |
2ad204c8 | 1129 | nlmsghdr_to_string(const struct nlmsghdr *h, int protocol, struct ds *ds) |
2fe27d5a BP |
1130 | { |
1131 | struct nlmsg_flag { | |
1132 | unsigned int bits; | |
1133 | const char *name; | |
1134 | }; | |
1135 | static const struct nlmsg_flag flags[] = { | |
1136 | { NLM_F_REQUEST, "REQUEST" }, | |
1137 | { NLM_F_MULTI, "MULTI" }, | |
1138 | { NLM_F_ACK, "ACK" }, | |
1139 | { NLM_F_ECHO, "ECHO" }, | |
1140 | { NLM_F_DUMP, "DUMP" }, | |
1141 | { NLM_F_ROOT, "ROOT" }, | |
1142 | { NLM_F_MATCH, "MATCH" }, | |
1143 | { NLM_F_ATOMIC, "ATOMIC" }, | |
1144 | }; | |
1145 | const struct nlmsg_flag *flag; | |
1146 | uint16_t flags_left; | |
1147 | ||
1148 | ds_put_format(ds, "nl(len:%"PRIu32", type=%"PRIu16, | |
1149 | h->nlmsg_len, h->nlmsg_type); | |
1150 | if (h->nlmsg_type == NLMSG_NOOP) { | |
1151 | ds_put_cstr(ds, "(no-op)"); | |
1152 | } else if (h->nlmsg_type == NLMSG_ERROR) { | |
1153 | ds_put_cstr(ds, "(error)"); | |
1154 | } else if (h->nlmsg_type == NLMSG_DONE) { | |
1155 | ds_put_cstr(ds, "(done)"); | |
1156 | } else if (h->nlmsg_type == NLMSG_OVERRUN) { | |
1157 | ds_put_cstr(ds, "(overrun)"); | |
1158 | } else if (h->nlmsg_type < NLMSG_MIN_TYPE) { | |
1159 | ds_put_cstr(ds, "(reserved)"); | |
2ad204c8 BP |
1160 | } else if (protocol == NETLINK_GENERIC) { |
1161 | ds_put_format(ds, "(%s)", genl_family_to_name(h->nlmsg_type)); | |
2fe27d5a BP |
1162 | } else { |
1163 | ds_put_cstr(ds, "(family-defined)"); | |
1164 | } | |
1165 | ds_put_format(ds, ", flags=%"PRIx16, h->nlmsg_flags); | |
1166 | flags_left = h->nlmsg_flags; | |
1167 | for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) { | |
1168 | if ((flags_left & flag->bits) == flag->bits) { | |
1169 | ds_put_format(ds, "[%s]", flag->name); | |
1170 | flags_left &= ~flag->bits; | |
1171 | } | |
1172 | } | |
1173 | if (flags_left) { | |
1174 | ds_put_format(ds, "[OTHER:%"PRIx16"]", flags_left); | |
1175 | } | |
2c5a6834 BP |
1176 | ds_put_format(ds, ", seq=%"PRIx32", pid=%"PRIu32, |
1177 | h->nlmsg_seq, h->nlmsg_pid); | |
2fe27d5a BP |
1178 | } |
1179 | ||
1180 | static char * | |
7041c3a9 | 1181 | nlmsg_to_string(const struct ofpbuf *buffer, int protocol) |
2fe27d5a BP |
1182 | { |
1183 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1184 | const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN); | |
1185 | if (h) { | |
2ad204c8 | 1186 | nlmsghdr_to_string(h, protocol, &ds); |
2fe27d5a BP |
1187 | if (h->nlmsg_type == NLMSG_ERROR) { |
1188 | const struct nlmsgerr *e; | |
1189 | e = ofpbuf_at(buffer, NLMSG_HDRLEN, | |
1190 | NLMSG_ALIGN(sizeof(struct nlmsgerr))); | |
1191 | if (e) { | |
1192 | ds_put_format(&ds, " error(%d", e->error); | |
1193 | if (e->error < 0) { | |
1194 | ds_put_format(&ds, "(%s)", strerror(-e->error)); | |
1195 | } | |
1196 | ds_put_cstr(&ds, ", in-reply-to("); | |
2ad204c8 | 1197 | nlmsghdr_to_string(&e->msg, protocol, &ds); |
2fe27d5a BP |
1198 | ds_put_cstr(&ds, "))"); |
1199 | } else { | |
1200 | ds_put_cstr(&ds, " error(truncated)"); | |
1201 | } | |
1202 | } else if (h->nlmsg_type == NLMSG_DONE) { | |
1203 | int *error = ofpbuf_at(buffer, NLMSG_HDRLEN, sizeof *error); | |
1204 | if (error) { | |
1205 | ds_put_format(&ds, " done(%d", *error); | |
1206 | if (*error < 0) { | |
1207 | ds_put_format(&ds, "(%s)", strerror(-*error)); | |
1208 | } | |
1209 | ds_put_cstr(&ds, ")"); | |
1210 | } else { | |
1211 | ds_put_cstr(&ds, " done(truncated)"); | |
1212 | } | |
7041c3a9 BP |
1213 | } else if (protocol == NETLINK_GENERIC) { |
1214 | struct genlmsghdr *genl = nl_msg_genlmsghdr(buffer); | |
1215 | if (genl) { | |
1216 | ds_put_format(&ds, ",genl(cmd=%"PRIu8",version=%"PRIu8")", | |
1217 | genl->cmd, genl->version); | |
1218 | } | |
2fe27d5a BP |
1219 | } |
1220 | } else { | |
1221 | ds_put_cstr(&ds, "nl(truncated)"); | |
1222 | } | |
1223 | return ds.string; | |
1224 | } | |
1225 | ||
1226 | static void | |
1227 | log_nlmsg(const char *function, int error, | |
7041c3a9 | 1228 | const void *message, size_t size, int protocol) |
2fe27d5a BP |
1229 | { |
1230 | struct ofpbuf buffer; | |
1231 | char *nlmsg; | |
1232 | ||
1233 | if (!VLOG_IS_DBG_ENABLED()) { | |
1234 | return; | |
1235 | } | |
1236 | ||
1237 | ofpbuf_use_const(&buffer, message, size); | |
7041c3a9 | 1238 | nlmsg = nlmsg_to_string(&buffer, protocol); |
2fe27d5a BP |
1239 | VLOG_DBG_RL(&rl, "%s (%s): %s", function, strerror(error), nlmsg); |
1240 | free(nlmsg); | |
1241 | } |