2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "rtnetlink-link.h"
21 #include <sys/socket.h>
22 #include <linux/rtnetlink.h>
27 #include "rtnetlink.h"
29 static struct rtnetlink *rtn = NULL;
30 static struct rtnetlink_link_change rtn_change;
33 rtnetlink_link_parse(struct ofpbuf *buf,
34 struct rtnetlink_link_change *change)
38 /* Policy for RTNLGRP_LINK messages.
40 * There are *many* more fields in these messages, but currently we
41 * only care about these fields. */
42 static const struct nl_policy policy[] = {
43 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
44 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
47 static struct nlattr *attrs[ARRAY_SIZE(policy)];
49 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
50 policy, attrs, ARRAY_SIZE(policy));
53 const struct nlmsghdr *nlmsg;
54 const struct ifinfomsg *ifinfo;
57 ifinfo = ((const struct ifinfomsg *)
58 ((const char *) buf->data + NLMSG_HDRLEN));
60 change->nlmsg_type = nlmsg->nlmsg_type;
61 change->ifi_index = ifinfo->ifi_index;
62 change->ifname = nl_attr_get_string(attrs[IFLA_IFNAME]);
63 change->master_ifindex = (attrs[IFLA_MASTER]
64 ? nl_attr_get_u32(attrs[IFLA_MASTER])
71 /* Registers 'cb' to be called with auxiliary data 'aux' with network device
72 * change notifications. The notifier is stored in 'notifier', which the
73 * caller must not modify or free.
75 * This is probably not the function that you want. You should probably be
76 * using dpif_port_poll() or netdev_monitor_create(), which unlike this
77 * function are not Linux-specific.
79 * Returns 0 if successful, otherwise a positive errno value. */
81 rtnetlink_link_notifier_register(struct rtnetlink_notifier *notifier,
82 rtnetlink_link_notify_func *cb, void *aux)
84 rtnetlink_parse_func *pf = (rtnetlink_parse_func *) rtnetlink_link_parse;
85 rtnetlink_notify_func *nf = (rtnetlink_notify_func *) cb;
88 rtn = rtnetlink_create(RTNLGRP_LINK, pf, &rtn_change);
91 return rtnetlink_notifier_register(rtn, notifier, nf, aux);
94 /* Cancels notification on 'notifier', which must have previously been
95 * registered with rtnetlink_link_notifier_register(). */
97 rtnetlink_link_notifier_unregister(struct rtnetlink_notifier *notifier)
99 rtnetlink_notifier_unregister(rtn, notifier);
102 /* Calls all of the registered notifiers, passing along any as-yet-unreported
103 * netdev change events. */
105 rtnetlink_link_notifier_run(void)
108 rtnetlink_notifier_run(rtn);
112 /* Causes poll_block() to wake up when network device change notifications are
115 rtnetlink_link_notifier_wait(void)
118 rtnetlink_notifier_wait(rtn);