netlink-socket: Remove unused nl_sock_sendv() function.
[cascardo/ovs.git] / lib / netlink-socket.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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"
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include "coverage.h"
26 #include "dynamic-string.h"
27 #include "hash.h"
28 #include "hmap.h"
29 #include "netlink.h"
30 #include "netlink-protocol.h"
31 #include "ofpbuf.h"
32 #include "poll-loop.h"
33 #include "socket-util.h"
34 #include "stress.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(netlink_socket);
38
39 COVERAGE_DEFINE(netlink_overflow);
40 COVERAGE_DEFINE(netlink_received);
41 COVERAGE_DEFINE(netlink_recv_retry);
42 COVERAGE_DEFINE(netlink_send);
43 COVERAGE_DEFINE(netlink_sent);
44
45 /* Linux header file confusion causes this to be undefined. */
46 #ifndef SOL_NETLINK
47 #define SOL_NETLINK 270
48 #endif
49
50 /* A single (bad) Netlink message can in theory dump out many, many log
51  * messages, so the burst size is set quite high here to avoid missing useful
52  * information.  Also, at high logging levels we log *all* Netlink messages. */
53 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600);
54
55 static void log_nlmsg(const char *function, int error,
56                       const void *message, size_t size, int protocol);
57 \f
58 /* Netlink sockets. */
59
60 struct nl_sock
61 {
62     int fd;
63     uint32_t pid;
64     int protocol;
65     bool any_groups;
66     struct nl_dump *dump;
67 };
68
69 static int alloc_pid(uint32_t *);
70 static void free_pid(uint32_t);
71 static int nl_sock_cow__(struct nl_sock *);
72
73 /* Creates a new netlink socket for the given netlink 'protocol'
74  * (NETLINK_ROUTE, NETLINK_GENERIC, ...).  Returns 0 and sets '*sockp' to the
75  * new socket if successful, otherwise returns a positive errno value.  */
76 int
77 nl_sock_create(int protocol, struct nl_sock **sockp)
78 {
79     struct nl_sock *sock;
80     struct sockaddr_nl local, remote;
81     int retval = 0;
82
83     *sockp = NULL;
84     sock = malloc(sizeof *sock);
85     if (sock == NULL) {
86         return ENOMEM;
87     }
88
89     sock->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
90     if (sock->fd < 0) {
91         VLOG_ERR("fcntl: %s", strerror(errno));
92         goto error;
93     }
94     sock->protocol = protocol;
95     sock->any_groups = false;
96     sock->dump = NULL;
97
98     retval = alloc_pid(&sock->pid);
99     if (retval) {
100         goto error;
101     }
102
103     /* Bind local address as our selected pid. */
104     memset(&local, 0, sizeof local);
105     local.nl_family = AF_NETLINK;
106     local.nl_pid = sock->pid;
107     if (bind(sock->fd, (struct sockaddr *) &local, sizeof local) < 0) {
108         VLOG_ERR("bind(%"PRIu32"): %s", sock->pid, strerror(errno));
109         goto error_free_pid;
110     }
111
112     /* Bind remote address as the kernel (pid 0). */
113     memset(&remote, 0, sizeof remote);
114     remote.nl_family = AF_NETLINK;
115     remote.nl_pid = 0;
116     if (connect(sock->fd, (struct sockaddr *) &remote, sizeof remote) < 0) {
117         VLOG_ERR("connect(0): %s", strerror(errno));
118         goto error_free_pid;
119     }
120
121     *sockp = sock;
122     return 0;
123
124 error_free_pid:
125     free_pid(sock->pid);
126 error:
127     if (retval == 0) {
128         retval = errno;
129         if (retval == 0) {
130             retval = EINVAL;
131         }
132     }
133     if (sock->fd >= 0) {
134         close(sock->fd);
135     }
136     free(sock);
137     return retval;
138 }
139
140 /* Creates a new netlink socket for the same protocol as 'src'.  Returns 0 and
141  * sets '*sockp' to the new socket if successful, otherwise returns a positive
142  * errno value.  */
143 int
144 nl_sock_clone(const struct nl_sock *src, struct nl_sock **sockp)
145 {
146     return nl_sock_create(src->protocol, sockp);
147 }
148
149 /* Destroys netlink socket 'sock'. */
150 void
151 nl_sock_destroy(struct nl_sock *sock)
152 {
153     if (sock) {
154         if (sock->dump) {
155             sock->dump = NULL;
156         } else {
157             close(sock->fd);
158             free_pid(sock->pid);
159             free(sock);
160         }
161     }
162 }
163
164 /* Tries to add 'sock' as a listener for 'multicast_group'.  Returns 0 if
165  * successful, otherwise a positive errno value.
166  *
167  * Multicast group numbers are always positive.
168  *
169  * It is not an error to attempt to join a multicast group to which a socket
170  * already belongs. */
171 int
172 nl_sock_join_mcgroup(struct nl_sock *sock, unsigned int multicast_group)
173 {
174     int error = nl_sock_cow__(sock);
175     if (error) {
176         return error;
177     }
178     if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
179                    &multicast_group, sizeof multicast_group) < 0) {
180         VLOG_WARN("could not join multicast group %u (%s)",
181                   multicast_group, strerror(errno));
182         return errno;
183     }
184     sock->any_groups = true;
185     return 0;
186 }
187
188 /* Tries to make 'sock' stop listening to 'multicast_group'.  Returns 0 if
189  * successful, otherwise a positive errno value.
190  *
191  * Multicast group numbers are always positive.
192  *
193  * It is not an error to attempt to leave a multicast group to which a socket
194  * does not belong.
195  *
196  * On success, reading from 'sock' will still return any messages that were
197  * received on 'multicast_group' before the group was left. */
198 int
199 nl_sock_leave_mcgroup(struct nl_sock *sock, unsigned int multicast_group)
200 {
201     assert(!sock->dump);
202     if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
203                    &multicast_group, sizeof multicast_group) < 0) {
204         VLOG_WARN("could not leave multicast group %u (%s)",
205                   multicast_group, strerror(errno));
206         return errno;
207     }
208     return 0;
209 }
210
211 static int
212 nl_sock_send__(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
213 {
214     struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(msg);
215     int error;
216
217     nlmsg->nlmsg_len = msg->size;
218     nlmsg->nlmsg_pid = sock->pid;
219     do {
220         int retval;
221         retval = send(sock->fd, msg->data, msg->size, wait ? 0 : MSG_DONTWAIT);
222         error = retval < 0 ? errno : 0;
223     } while (error == EINTR);
224     log_nlmsg(__func__, error, msg->data, msg->size, sock->protocol);
225     if (!error) {
226         COVERAGE_INC(netlink_sent);
227     }
228     return error;
229 }
230
231 /* Tries to send 'msg', which must contain a Netlink message, to the kernel on
232  * 'sock'.  nlmsg_len in 'msg' will be finalized to match msg->size, and
233  * nlmsg_pid will be set to 'sock''s pid, before the message is sent.
234  *
235  * Returns 0 if successful, otherwise a positive errno value.  If
236  * 'wait' is true, then the send will wait until buffer space is ready;
237  * otherwise, returns EAGAIN if the 'sock' send buffer is full. */
238 int
239 nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
240 {
241     int error = nl_sock_cow__(sock);
242     if (error) {
243         return error;
244     }
245     return nl_sock_send__(sock, msg, wait);
246 }
247
248 /* This stress option is useful for testing that OVS properly tolerates
249  * -ENOBUFS on NetLink sockets.  Such errors are unavoidable because they can
250  * occur if the kernel cannot temporarily allocate enough GFP_ATOMIC memory to
251  * reply to a request.  They can also occur if messages arrive on a multicast
252  * channel faster than OVS can process them. */
253 STRESS_OPTION(
254     netlink_overflow, "simulate netlink socket receive buffer overflow",
255     5, 1, -1, 100);
256
257 static int
258 nl_sock_recv__(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
259 {
260     uint8_t tmp;
261     ssize_t bufsize = 2048;
262     ssize_t nbytes, nbytes2;
263     struct ofpbuf *buf;
264     struct nlmsghdr *nlmsghdr;
265     struct iovec iov;
266     struct msghdr msg = {
267         .msg_name = NULL,
268         .msg_namelen = 0,
269         .msg_iov = &iov,
270         .msg_iovlen = 1,
271         .msg_control = NULL,
272         .msg_controllen = 0,
273         .msg_flags = 0
274     };
275
276     buf = ofpbuf_new(bufsize);
277     *bufp = NULL;
278
279 try_again:
280     /* Attempt to read the message.  We don't know the size of the data
281      * yet, so we take a guess at 2048.  If we're wrong, we keep trying
282      * and doubling the buffer size each time.
283      */
284     nlmsghdr = ofpbuf_put_uninit(buf, bufsize);
285     iov.iov_base = nlmsghdr;
286     iov.iov_len = bufsize;
287     do {
288         nbytes = recvmsg(sock->fd, &msg, (wait ? 0 : MSG_DONTWAIT) | MSG_PEEK);
289     } while (nbytes < 0 && errno == EINTR);
290     if (nbytes < 0) {
291         ofpbuf_delete(buf);
292         return errno;
293     }
294     if (msg.msg_flags & MSG_TRUNC) {
295         COVERAGE_INC(netlink_recv_retry);
296         bufsize *= 2;
297         ofpbuf_reinit(buf, bufsize);
298         goto try_again;
299     }
300     buf->size = nbytes;
301
302     /* We successfully read the message, so recv again to clear the queue */
303     iov.iov_base = &tmp;
304     iov.iov_len = 1;
305     do {
306         nbytes2 = recvmsg(sock->fd, &msg, MSG_DONTWAIT);
307     } while (nbytes2 < 0 && errno == EINTR);
308     if (nbytes2 < 0) {
309         if (errno == ENOBUFS) {
310             /* The kernel is notifying us that a message it tried to send to us
311              * was dropped.  We have to pass this along to the caller in case
312              * it wants to retry a request.  So kill the buffer, which we can
313              * re-read next time. */
314             COVERAGE_INC(netlink_overflow);
315             ofpbuf_delete(buf);
316             return ENOBUFS;
317         } else {
318             VLOG_ERR_RL(&rl, "failed to remove nlmsg from socket: %s\n",
319                         strerror(errno));
320         }
321     }
322     if (nbytes < sizeof *nlmsghdr
323         || nlmsghdr->nlmsg_len < sizeof *nlmsghdr
324         || nlmsghdr->nlmsg_len > nbytes) {
325         VLOG_ERR_RL(&rl, "received invalid nlmsg (%zd bytes < %d)",
326                     bufsize, NLMSG_HDRLEN);
327         ofpbuf_delete(buf);
328         return EPROTO;
329     }
330
331     if (STRESS(netlink_overflow)) {
332         ofpbuf_delete(buf);
333         return ENOBUFS;
334     }
335
336     *bufp = buf;
337     log_nlmsg(__func__, 0, buf->data, buf->size, sock->protocol);
338     COVERAGE_INC(netlink_received);
339
340     return 0;
341 }
342
343 /* Tries to receive a netlink message from the kernel on 'sock'.  If
344  * successful, stores the received message into '*bufp' and returns 0.  The
345  * caller is responsible for destroying the message with ofpbuf_delete().  On
346  * failure, returns a positive errno value and stores a null pointer into
347  * '*bufp'.
348  *
349  * If 'wait' is true, nl_sock_recv waits for a message to be ready; otherwise,
350  * returns EAGAIN if the 'sock' receive buffer is empty. */
351 int
352 nl_sock_recv(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
353 {
354     int error = nl_sock_cow__(sock);
355     if (error) {
356         return error;
357     }
358     return nl_sock_recv__(sock, bufp, wait);
359 }
360
361 /* Sends 'request' to the kernel via 'sock' and waits for a response.  If
362  * successful, returns 0.  On failure, returns a positive errno value.
363  *
364  * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's
365  * reply, which the caller is responsible for freeing with ofpbuf_delete(), and
366  * on failure '*replyp' is set to NULL.  If 'replyp' is null, then the kernel's
367  * reply, if any, is discarded.
368  *
369  * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will
370  * be set to 'sock''s pid, before the message is sent.  NLM_F_ACK will be set
371  * in nlmsg_flags.
372  *
373  * The caller is responsible for destroying 'request'.
374  *
375  * Bare Netlink is an unreliable transport protocol.  This function layers
376  * reliable delivery and reply semantics on top of bare Netlink.
377  *
378  * In Netlink, sending a request to the kernel is reliable enough, because the
379  * kernel will tell us if the message cannot be queued (and we will in that
380  * case put it on the transmit queue and wait until it can be delivered).
381  *
382  * Receiving the reply is the real problem: if the socket buffer is full when
383  * the kernel tries to send the reply, the reply will be dropped.  However, the
384  * kernel sets a flag that a reply has been dropped.  The next call to recv
385  * then returns ENOBUFS.  We can then re-send the request.
386  *
387  * Caveats:
388  *
389  *      1. Netlink depends on sequence numbers to match up requests and
390  *         replies.  The sender of a request supplies a sequence number, and
391  *         the reply echos back that sequence number.
392  *
393  *         This is fine, but (1) some kernel netlink implementations are
394  *         broken, in that they fail to echo sequence numbers and (2) this
395  *         function will drop packets with non-matching sequence numbers, so
396  *         that only a single request can be usefully transacted at a time.
397  *
398  *      2. Resending the request causes it to be re-executed, so the request
399  *         needs to be idempotent.
400  */
401 int
402 nl_sock_transact(struct nl_sock *sock,
403                  const struct ofpbuf *request, struct ofpbuf **replyp)
404 {
405     uint32_t seq = nl_msg_nlmsghdr(request)->nlmsg_seq;
406     struct nlmsghdr *nlmsghdr;
407     struct ofpbuf *reply;
408     int retval;
409
410     if (replyp) {
411         *replyp = NULL;
412     }
413
414     /* Ensure that we get a reply even if this message doesn't ordinarily call
415      * for one. */
416     nl_msg_nlmsghdr(request)->nlmsg_flags |= NLM_F_ACK;
417
418 send:
419     retval = nl_sock_send(sock, request, true);
420     if (retval) {
421         return retval;
422     }
423
424 recv:
425     retval = nl_sock_recv(sock, &reply, true);
426     if (retval) {
427         if (retval == ENOBUFS) {
428             COVERAGE_INC(netlink_overflow);
429             VLOG_DBG_RL(&rl, "receive buffer overflow, resending request");
430             goto send;
431         } else {
432             return retval;
433         }
434     }
435     nlmsghdr = nl_msg_nlmsghdr(reply);
436     if (seq != nlmsghdr->nlmsg_seq) {
437         VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32,
438                     nl_msg_nlmsghdr(reply)->nlmsg_seq, seq);
439         ofpbuf_delete(reply);
440         goto recv;
441     }
442
443     /* If the reply is an error, discard the reply and return the error code.
444      *
445      * Except: if the reply is just an acknowledgement (error code of 0), and
446      * the caller is interested in the reply (replyp != NULL), pass the reply
447      * up to the caller.  Otherwise the caller will get a return value of 0
448      * and null '*replyp', which makes unwary callers likely to segfault. */
449     if (nl_msg_nlmsgerr(reply, &retval) && (retval || !replyp)) {
450         ofpbuf_delete(reply);
451         if (retval) {
452             VLOG_DBG_RL(&rl, "received NAK error=%d (%s)",
453                         retval, strerror(retval));
454         }
455         return retval != EAGAIN ? retval : EPROTO;
456     }
457
458     if (replyp) {
459         *replyp = reply;
460     } else {
461         ofpbuf_delete(reply);
462     }
463     return 0;
464 }
465
466 /* Drain all the messages currently in 'sock''s receive queue. */
467 int
468 nl_sock_drain(struct nl_sock *sock)
469 {
470     int error = nl_sock_cow__(sock);
471     if (error) {
472         return error;
473     }
474     return drain_rcvbuf(sock->fd);
475 }
476
477 /* The client is attempting some operation on 'sock'.  If 'sock' has an ongoing
478  * dump operation, then replace 'sock''s fd with a new socket and hand 'sock''s
479  * old fd over to the dump. */
480 static int
481 nl_sock_cow__(struct nl_sock *sock)
482 {
483     struct nl_sock *copy;
484     uint32_t tmp_pid;
485     int tmp_fd;
486     int error;
487
488     if (!sock->dump) {
489         return 0;
490     }
491
492     error = nl_sock_clone(sock, &copy);
493     if (error) {
494         return error;
495     }
496
497     tmp_fd = sock->fd;
498     sock->fd = copy->fd;
499     copy->fd = tmp_fd;
500
501     tmp_pid = sock->pid;
502     sock->pid = copy->pid;
503     copy->pid = tmp_pid;
504
505     sock->dump->sock = copy;
506     sock->dump = NULL;
507
508     return 0;
509 }
510
511 /* Starts a Netlink "dump" operation, by sending 'request' to the kernel via
512  * 'sock', and initializes 'dump' to reflect the state of the operation.
513  *
514  * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will
515  * be set to 'sock''s pid, before the message is sent.  NLM_F_DUMP and
516  * NLM_F_ACK will be set in nlmsg_flags.
517  *
518  * This Netlink socket library is designed to ensure that the dump is reliable
519  * and that it will not interfere with other operations on 'sock', including
520  * destroying or sending and receiving messages on 'sock'.  One corner case is
521  * not handled:
522  *
523  *   - If 'sock' has been used to send a request (e.g. with nl_sock_send())
524  *     whose response has not yet been received (e.g. with nl_sock_recv()).
525  *     This is unusual: usually nl_sock_transact() is used to send a message
526  *     and receive its reply all in one go.
527  *
528  * This function provides no status indication.  An error status for the entire
529  * dump operation is provided when it is completed by calling nl_dump_done().
530  *
531  * The caller is responsible for destroying 'request'.
532  *
533  * The new 'dump' is independent of 'sock'.  'sock' and 'dump' may be destroyed
534  * in either order.
535  */
536 void
537 nl_dump_start(struct nl_dump *dump,
538               struct nl_sock *sock, const struct ofpbuf *request)
539 {
540     struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(request);
541     nlmsghdr->nlmsg_flags |= NLM_F_DUMP | NLM_F_ACK;
542     dump->seq = nlmsghdr->nlmsg_seq;
543     dump->buffer = NULL;
544     if (sock->any_groups || sock->dump) {
545         /* 'sock' might belong to some multicast group, or it already has an
546          * onoging dump.  Clone the socket to avoid possibly intermixing
547          * multicast messages or previous dump results with our results. */
548         dump->status = nl_sock_clone(sock, &dump->sock);
549         if (dump->status) {
550             return;
551         }
552     } else {
553         sock->dump = dump;
554         dump->sock = sock;
555         dump->status = 0;
556     }
557     dump->status = nl_sock_send__(sock, request, true);
558 }
559
560 /* Helper function for nl_dump_next(). */
561 static int
562 nl_dump_recv(struct nl_dump *dump, struct ofpbuf **bufferp)
563 {
564     struct nlmsghdr *nlmsghdr;
565     struct ofpbuf *buffer;
566     int retval;
567
568     retval = nl_sock_recv__(dump->sock, bufferp, true);
569     if (retval) {
570         return retval == EINTR ? EAGAIN : retval;
571     }
572     buffer = *bufferp;
573
574     nlmsghdr = nl_msg_nlmsghdr(buffer);
575     if (dump->seq != nlmsghdr->nlmsg_seq) {
576         VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32,
577                     nlmsghdr->nlmsg_seq, dump->seq);
578         return EAGAIN;
579     }
580
581     if (nl_msg_nlmsgerr(buffer, &retval)) {
582         VLOG_INFO_RL(&rl, "netlink dump request error (%s)",
583                      strerror(retval));
584         return retval && retval != EAGAIN ? retval : EPROTO;
585     }
586
587     return 0;
588 }
589
590 /* Attempts to retrieve another reply from 'dump', which must have been
591  * initialized with nl_dump_start().
592  *
593  * If successful, returns true and points 'reply->data' and 'reply->size' to
594  * the message that was retrieved.  The caller must not modify 'reply' (because
595  * it points into the middle of a larger buffer).
596  *
597  * On failure, returns false and sets 'reply->data' to NULL and 'reply->size'
598  * to 0.  Failure might indicate an actual error or merely the end of replies.
599  * An error status for the entire dump operation is provided when it is
600  * completed by calling nl_dump_done().
601  */
602 bool
603 nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply)
604 {
605     struct nlmsghdr *nlmsghdr;
606
607     reply->data = NULL;
608     reply->size = 0;
609     if (dump->status) {
610         return false;
611     }
612
613     if (dump->buffer && !dump->buffer->size) {
614         ofpbuf_delete(dump->buffer);
615         dump->buffer = NULL;
616     }
617     while (!dump->buffer) {
618         int retval = nl_dump_recv(dump, &dump->buffer);
619         if (retval) {
620             ofpbuf_delete(dump->buffer);
621             dump->buffer = NULL;
622             if (retval != EAGAIN) {
623                 dump->status = retval;
624                 return false;
625             }
626         }
627     }
628
629     nlmsghdr = nl_msg_next(dump->buffer, reply);
630     if (!nlmsghdr) {
631         VLOG_WARN_RL(&rl, "netlink dump reply contains message fragment");
632         dump->status = EPROTO;
633         return false;
634     } else if (nlmsghdr->nlmsg_type == NLMSG_DONE) {
635         dump->status = EOF;
636         return false;
637     }
638
639     return true;
640 }
641
642 /* Completes Netlink dump operation 'dump', which must have been initialized
643  * with nl_dump_start().  Returns 0 if the dump operation was error-free,
644  * otherwise a positive errno value describing the problem. */
645 int
646 nl_dump_done(struct nl_dump *dump)
647 {
648     /* Drain any remaining messages that the client didn't read.  Otherwise the
649      * kernel will continue to queue them up and waste buffer space. */
650     while (!dump->status) {
651         struct ofpbuf reply;
652         if (!nl_dump_next(dump, &reply)) {
653             assert(dump->status);
654         }
655     }
656
657     if (dump->sock) {
658         if (dump->sock->dump) {
659             dump->sock->dump = NULL;
660         } else {
661             nl_sock_destroy(dump->sock);
662         }
663     }
664     ofpbuf_delete(dump->buffer);
665     return dump->status == EOF ? 0 : dump->status;
666 }
667
668 /* Causes poll_block() to wake up when any of the specified 'events' (which is
669  * a OR'd combination of POLLIN, POLLOUT, etc.) occur on 'sock'. */
670 void
671 nl_sock_wait(const struct nl_sock *sock, short int events)
672 {
673     poll_fd_wait(sock->fd, events);
674 }
675 \f
676 /* Miscellaneous.  */
677
678 struct genl_family {
679     struct hmap_node hmap_node;
680     uint16_t id;
681     char *name;
682 };
683
684 static struct hmap genl_families = HMAP_INITIALIZER(&genl_families);
685
686 static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = {
687     [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16},
688 };
689
690 static struct genl_family *
691 find_genl_family_by_id(uint16_t id)
692 {
693     struct genl_family *family;
694
695     HMAP_FOR_EACH_IN_BUCKET (family, hmap_node, hash_int(id, 0),
696                              &genl_families) {
697         if (family->id == id) {
698             return family;
699         }
700     }
701     return NULL;
702 }
703
704 static void
705 define_genl_family(uint16_t id, const char *name)
706 {
707     struct genl_family *family = find_genl_family_by_id(id);
708
709     if (family) {
710         if (!strcmp(family->name, name)) {
711             return;
712         }
713         free(family->name);
714     } else {
715         family = xmalloc(sizeof *family);
716         family->id = id;
717         hmap_insert(&genl_families, &family->hmap_node, hash_int(id, 0));
718     }
719     family->name = xstrdup(name);
720 }
721
722 static const char *
723 genl_family_to_name(uint16_t id)
724 {
725     if (id == GENL_ID_CTRL) {
726         return "control";
727     } else {
728         struct genl_family *family = find_genl_family_by_id(id);
729         return family ? family->name : "unknown";
730     }
731 }
732
733 static int do_lookup_genl_family(const char *name)
734 {
735     struct nl_sock *sock;
736     struct ofpbuf request, *reply;
737     struct nlattr *attrs[ARRAY_SIZE(family_policy)];
738     int retval;
739
740     retval = nl_sock_create(NETLINK_GENERIC, &sock);
741     if (retval) {
742         return -retval;
743     }
744
745     ofpbuf_init(&request, 0);
746     nl_msg_put_genlmsghdr(&request, 0, GENL_ID_CTRL, NLM_F_REQUEST,
747                           CTRL_CMD_GETFAMILY, 1);
748     nl_msg_put_string(&request, CTRL_ATTR_FAMILY_NAME, name);
749     retval = nl_sock_transact(sock, &request, &reply);
750     ofpbuf_uninit(&request);
751     if (retval) {
752         nl_sock_destroy(sock);
753         return -retval;
754     }
755
756     if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
757                          family_policy, attrs, ARRAY_SIZE(family_policy))) {
758         nl_sock_destroy(sock);
759         ofpbuf_delete(reply);
760         return -EPROTO;
761     }
762
763     retval = nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]);
764     if (retval == 0) {
765         retval = -EPROTO;
766     } else {
767         define_genl_family(retval, name);
768     }
769     nl_sock_destroy(sock);
770     ofpbuf_delete(reply);
771
772     return retval;
773 }
774
775 /* If '*number' is 0, translates the given Generic Netlink family 'name' to a
776  * number and stores it in '*number'.  If successful, returns 0 and the caller
777  * may use '*number' as the family number.  On failure, returns a positive
778  * errno value and '*number' caches the errno value. */
779 int
780 nl_lookup_genl_family(const char *name, int *number)
781 {
782     if (*number == 0) {
783         *number = do_lookup_genl_family(name);
784         assert(*number != 0);
785     }
786     return *number > 0 ? 0 : -*number;
787 }
788 \f
789 /* Netlink PID.
790  *
791  * Every Netlink socket must be bound to a unique 32-bit PID.  By convention,
792  * programs that have a single Netlink socket use their Unix process ID as PID,
793  * and programs with multiple Netlink sockets add a unique per-socket
794  * identifier in the bits above the Unix process ID.
795  *
796  * The kernel has Netlink PID 0.
797  */
798
799 /* Parameters for how many bits in the PID should come from the Unix process ID
800  * and how many unique per-socket. */
801 #define SOCKET_BITS 10
802 #define MAX_SOCKETS (1u << SOCKET_BITS)
803
804 #define PROCESS_BITS (32 - SOCKET_BITS)
805 #define MAX_PROCESSES (1u << PROCESS_BITS)
806 #define PROCESS_MASK ((uint32_t) (MAX_PROCESSES - 1))
807
808 /* Bit vector of unused socket identifiers. */
809 static uint32_t avail_sockets[ROUND_UP(MAX_SOCKETS, 32)];
810
811 /* Allocates and returns a new Netlink PID. */
812 static int
813 alloc_pid(uint32_t *pid)
814 {
815     int i;
816
817     for (i = 0; i < MAX_SOCKETS; i++) {
818         if ((avail_sockets[i / 32] & (1u << (i % 32))) == 0) {
819             avail_sockets[i / 32] |= 1u << (i % 32);
820             *pid = (getpid() & PROCESS_MASK) | (i << PROCESS_BITS);
821             return 0;
822         }
823     }
824     VLOG_ERR("netlink pid space exhausted");
825     return ENOBUFS;
826 }
827
828 /* Makes the specified 'pid' available for reuse. */
829 static void
830 free_pid(uint32_t pid)
831 {
832     int sock = pid >> PROCESS_BITS;
833     assert(avail_sockets[sock / 32] & (1u << (sock % 32)));
834     avail_sockets[sock / 32] &= ~(1u << (sock % 32));
835 }
836 \f
837 static void
838 nlmsghdr_to_string(const struct nlmsghdr *h, int protocol, struct ds *ds)
839 {
840     struct nlmsg_flag {
841         unsigned int bits;
842         const char *name;
843     };
844     static const struct nlmsg_flag flags[] = {
845         { NLM_F_REQUEST, "REQUEST" },
846         { NLM_F_MULTI, "MULTI" },
847         { NLM_F_ACK, "ACK" },
848         { NLM_F_ECHO, "ECHO" },
849         { NLM_F_DUMP, "DUMP" },
850         { NLM_F_ROOT, "ROOT" },
851         { NLM_F_MATCH, "MATCH" },
852         { NLM_F_ATOMIC, "ATOMIC" },
853     };
854     const struct nlmsg_flag *flag;
855     uint16_t flags_left;
856
857     ds_put_format(ds, "nl(len:%"PRIu32", type=%"PRIu16,
858                   h->nlmsg_len, h->nlmsg_type);
859     if (h->nlmsg_type == NLMSG_NOOP) {
860         ds_put_cstr(ds, "(no-op)");
861     } else if (h->nlmsg_type == NLMSG_ERROR) {
862         ds_put_cstr(ds, "(error)");
863     } else if (h->nlmsg_type == NLMSG_DONE) {
864         ds_put_cstr(ds, "(done)");
865     } else if (h->nlmsg_type == NLMSG_OVERRUN) {
866         ds_put_cstr(ds, "(overrun)");
867     } else if (h->nlmsg_type < NLMSG_MIN_TYPE) {
868         ds_put_cstr(ds, "(reserved)");
869     } else if (protocol == NETLINK_GENERIC) {
870         ds_put_format(ds, "(%s)", genl_family_to_name(h->nlmsg_type));
871     } else {
872         ds_put_cstr(ds, "(family-defined)");
873     }
874     ds_put_format(ds, ", flags=%"PRIx16, h->nlmsg_flags);
875     flags_left = h->nlmsg_flags;
876     for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
877         if ((flags_left & flag->bits) == flag->bits) {
878             ds_put_format(ds, "[%s]", flag->name);
879             flags_left &= ~flag->bits;
880         }
881     }
882     if (flags_left) {
883         ds_put_format(ds, "[OTHER:%"PRIx16"]", flags_left);
884     }
885     ds_put_format(ds, ", seq=%"PRIx32", pid=%"PRIu32"(%d:%d))",
886                   h->nlmsg_seq, h->nlmsg_pid,
887                   (int) (h->nlmsg_pid & PROCESS_MASK),
888                   (int) (h->nlmsg_pid >> PROCESS_BITS));
889 }
890
891 static char *
892 nlmsg_to_string(const struct ofpbuf *buffer, int protocol)
893 {
894     struct ds ds = DS_EMPTY_INITIALIZER;
895     const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN);
896     if (h) {
897         nlmsghdr_to_string(h, protocol, &ds);
898         if (h->nlmsg_type == NLMSG_ERROR) {
899             const struct nlmsgerr *e;
900             e = ofpbuf_at(buffer, NLMSG_HDRLEN,
901                           NLMSG_ALIGN(sizeof(struct nlmsgerr)));
902             if (e) {
903                 ds_put_format(&ds, " error(%d", e->error);
904                 if (e->error < 0) {
905                     ds_put_format(&ds, "(%s)", strerror(-e->error));
906                 }
907                 ds_put_cstr(&ds, ", in-reply-to(");
908                 nlmsghdr_to_string(&e->msg, protocol, &ds);
909                 ds_put_cstr(&ds, "))");
910             } else {
911                 ds_put_cstr(&ds, " error(truncated)");
912             }
913         } else if (h->nlmsg_type == NLMSG_DONE) {
914             int *error = ofpbuf_at(buffer, NLMSG_HDRLEN, sizeof *error);
915             if (error) {
916                 ds_put_format(&ds, " done(%d", *error);
917                 if (*error < 0) {
918                     ds_put_format(&ds, "(%s)", strerror(-*error));
919                 }
920                 ds_put_cstr(&ds, ")");
921             } else {
922                 ds_put_cstr(&ds, " done(truncated)");
923             }
924         } else if (protocol == NETLINK_GENERIC) {
925             struct genlmsghdr *genl = nl_msg_genlmsghdr(buffer);
926             if (genl) {
927                 ds_put_format(&ds, ",genl(cmd=%"PRIu8",version=%"PRIu8")",
928                               genl->cmd, genl->version);
929             }
930         }
931     } else {
932         ds_put_cstr(&ds, "nl(truncated)");
933     }
934     return ds.string;
935 }
936
937 static void
938 log_nlmsg(const char *function, int error,
939           const void *message, size_t size, int protocol)
940 {
941     struct ofpbuf buffer;
942     char *nlmsg;
943
944     if (!VLOG_IS_DBG_ENABLED()) {
945         return;
946     }
947
948     ofpbuf_use_const(&buffer, message, size);
949     nlmsg = nlmsg_to_string(&buffer, protocol);
950     VLOG_DBG_RL(&rl, "%s (%s): %s", function, strerror(error), nlmsg);
951     free(nlmsg);
952 }
953
954