Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / lib / rtnetlink-link.c
1 /*
2  * Copyright (c) 2009, 2010 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
19 #include "rtnetlink-link.h"
20
21 #include <sys/socket.h>
22 #include <linux/rtnetlink.h>
23 #include <net/if.h>
24
25 #include "netlink.h"
26 #include "ofpbuf.h"
27 #include "rtnetlink.h"
28
29 static struct rtnetlink *rtn = NULL;
30 static struct rtnetlink_link_change rtn_change;
31
32 /* Parses a rtnetlink message 'buf' into 'change'.  If 'buf' is unparseable,
33  * leaves 'change' untouched and returns false.  Otherwise, populates 'change'
34  * and returns true. */
35 bool
36 rtnetlink_link_parse(struct ofpbuf *buf,
37                      struct rtnetlink_link_change *change)
38 {
39     bool parsed;
40
41     /* Policy for RTNLGRP_LINK messages.
42      *
43      * There are *many* more fields in these messages, but currently we
44      * only care about these fields. */
45     static const struct nl_policy policy[] = {
46         [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
47         [IFLA_MASTER] = { .type = NL_A_U32,    .optional = true },
48     };
49
50     static struct nlattr *attrs[ARRAY_SIZE(policy)];
51
52     parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
53                              policy, attrs, ARRAY_SIZE(policy));
54
55     if (parsed) {
56         const struct nlmsghdr *nlmsg;
57         const struct ifinfomsg *ifinfo;
58
59         nlmsg  = buf->data;
60         ifinfo = ((const struct ifinfomsg *)
61                   ((const char *) buf->data + NLMSG_HDRLEN));
62
63         change->nlmsg_type     = nlmsg->nlmsg_type;
64         change->ifi_index      = ifinfo->ifi_index;
65         change->ifname         = nl_attr_get_string(attrs[IFLA_IFNAME]);
66         change->master_ifindex = (attrs[IFLA_MASTER]
67                                   ? nl_attr_get_u32(attrs[IFLA_MASTER])
68                                   : 0);
69     }
70
71     return parsed;
72 }
73
74 /* Registers 'cb' to be called with auxiliary data 'aux' with network device
75  * change notifications.  The notifier is stored in 'notifier', which the
76  * caller must not modify or free.
77  *
78  * This is probably not the function that you want.  You should probably be
79  * using dpif_port_poll() or netdev_monitor_create(), which unlike this
80  * function are not Linux-specific.
81  *
82  * Returns 0 if successful, otherwise a positive errno value. */
83 int
84 rtnetlink_link_notifier_register(struct rtnetlink_notifier *notifier,
85                                  rtnetlink_link_notify_func *cb, void *aux)
86 {
87     rtnetlink_parse_func *pf  = (rtnetlink_parse_func *) rtnetlink_link_parse;
88     rtnetlink_notify_func *nf = (rtnetlink_notify_func *) cb;
89
90     if (!rtn) {
91         rtn = rtnetlink_create(RTNLGRP_LINK, pf, &rtn_change);
92     }
93
94     return rtnetlink_notifier_register(rtn, notifier, nf, aux);
95 }
96
97 /* Cancels notification on 'notifier', which must have previously been
98  * registered with rtnetlink_link_notifier_register(). */
99 void
100 rtnetlink_link_notifier_unregister(struct rtnetlink_notifier *notifier)
101 {
102     rtnetlink_notifier_unregister(rtn, notifier);
103 }
104
105 /* Calls all of the registered notifiers, passing along any as-yet-unreported
106  * netdev change events. */
107 void
108 rtnetlink_link_notifier_run(void)
109 {
110     if (rtn) {
111         rtnetlink_notifier_run(rtn);
112     }
113 }
114
115 /* Causes poll_block() to wake up when network device change notifications are
116  * ready. */
117 void
118 rtnetlink_link_notifier_wait(void)
119 {
120     if (rtn) {
121         rtnetlink_notifier_wait(rtn);
122     }
123 }