diff options
author | Damjan Marion <damarion@cisco.com> | 2017-12-01 13:34:24 +0100 |
---|---|---|
committer | Damjan Marion <dmarion.lists@gmail.com> | 2017-12-02 10:11:25 +0000 |
commit | 91c6ef7cae2d20ca17a69003a44090614412c63f (patch) | |
tree | 28a0ae8030e840a0bc8f65e28f9d5bc2868bf39d /src/vnet/devices/netlink.c | |
parent | 9fa1581cc40b656b6e00d77479fc2424cd50a126 (diff) |
tap_v2: multiple improvements
- add support for assigning tap interface to the bridge
- add support for assigning tap interface host side ip4 and ip6 address
- host namespace can be specified as PID (pid:12345) or full path to file
- automatically bring linux interface up
Change-Id: I1cf7c3cad9a740e430cc1b9c2bb0aad0ba4cc8d8
Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vnet/devices/netlink.c')
-rw-r--r-- | src/vnet/devices/netlink.c | 186 |
1 files changed, 145 insertions, 41 deletions
diff --git a/src/vnet/devices/netlink.c b/src/vnet/devices/netlink.c index b05daf2674a..b3330dd89b9 100644 --- a/src/vnet/devices/netlink.c +++ b/src/vnet/devices/netlink.c @@ -31,22 +31,51 @@ #include <vlib/vlib.h> #include <vlib/unix/unix.h> -clib_error_t * -vnet_netlink_set_if_attr (int ifindex, unsigned short rta_type, void *data, - int data_len) +typedef struct +{ + u8 *data; +} vnet_netlink_msg_t; + +void +vnet_netlink_msg_init (vnet_netlink_msg_t * m, u16 type, u16 flags, + void *msg_data, int msg_len) +{ + struct nlmsghdr *nh; + u8 *p; + int len = NLMSG_LENGTH (msg_len); + memset (m, 0, sizeof (vnet_netlink_msg_t)); + vec_add2 (m->data, p, len); + ASSERT (m->data == p); + + nh = (struct nlmsghdr *) p; + nh->nlmsg_flags = flags; + nh->nlmsg_type = type; + clib_memcpy (m->data + sizeof (struct nlmsghdr), msg_data, msg_len); +} + +static void +vnet_netlink_msg_add_rtattr (vnet_netlink_msg_t * m, u16 rta_type, + void *rta_data, int rta_data_len) +{ + struct rtattr *rta; + u8 *p; + + vec_add2 (m->data, p, RTA_LENGTH (rta_data_len)); + rta = (struct rtattr *) p; + rta->rta_type = rta_type; + rta->rta_len = RTA_LENGTH (rta_data_len); + clib_memcpy (RTA_DATA (rta), rta_data, rta_data_len); +} + +static clib_error_t * +vnet_netlink_msg_send (vnet_netlink_msg_t * m) { clib_error_t *err = 0; - int sock; struct sockaddr_nl ra = { 0 }; - struct - { - struct nlmsghdr nh; - struct ifinfomsg ifmsg; - char attrbuf[512]; - } req; - struct rtattr *rta; + int sock; + struct nlmsghdr *nh = (struct nlmsghdr *) m->data; + nh->nlmsg_len = vec_len (m->data); - memset (&req, 0, sizeof (req)); if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) return clib_error_return_unix (0, "socket(AF_NETLINK)"); @@ -59,51 +88,126 @@ vnet_netlink_set_if_attr (int ifindex, unsigned short rta_type, void *data, goto error; } - req.nh.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifinfomsg)); - req.nh.nlmsg_flags = NLM_F_REQUEST; - req.nh.nlmsg_type = RTM_SETLINK; - req.ifmsg.ifi_family = AF_UNSPEC; - req.ifmsg.ifi_index = ifindex; - req.ifmsg.ifi_change = 0xffffffff; - rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN (req.nh.nlmsg_len)); - rta->rta_type = rta_type; - rta->rta_len = RTA_LENGTH (data_len); - req.nh.nlmsg_len = NLMSG_ALIGN (req.nh.nlmsg_len) + RTA_LENGTH (data_len); - memcpy (RTA_DATA (rta), data, data_len); - - if ((send (sock, &req, req.nh.nlmsg_len, 0)) == -1) + if ((send (sock, m->data, vec_len (m->data), 0)) == -1) err = clib_error_return_unix (0, "send"); error: close (sock); + vec_free (m->data); return err; } clib_error_t * -vnet_netlink_set_if_mtu (int ifindex, int mtu) +vnet_netlink_set_if_namespace (int ifindex, char *net_ns) { + vnet_netlink_msg_t m; + struct ifinfomsg ifmsg = { 0 }; + clib_error_t *err; + int data; + u16 type; + u8 *s; + + if (strncmp (net_ns, "pid:", 4) == 0) + { + data = atoi (net_ns + 4); + type = IFLA_NET_NS_PID; + } + else + { + if (net_ns[0] == '/') + s = format (0, "%s%c", net_ns, 0); + else + s = format (0, "/var/run/netns/%s%c", net_ns, 0); + + data = open ((char *) s, O_RDONLY); + type = IFLA_NET_NS_FD; + vec_free (s); + if (data == -1) + return clib_error_return (0, "namespace '%s' doesn't exist", net_ns); + } - err = vnet_netlink_set_if_attr (ifindex, IFLA_MTU, &mtu, sizeof (int)); + ifmsg.ifi_family = AF_UNSPEC; + ifmsg.ifi_index = ifindex; + ifmsg.ifi_change = 0xffffffff; + vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST, + &ifmsg, sizeof (struct ifinfomsg)); + + vnet_netlink_msg_add_rtattr (&m, type, &data, sizeof (int)); + err = vnet_netlink_msg_send (&m); + + if (type == IFLA_NET_NS_FD) + close (data); return err; } clib_error_t * -vnet_netlink_set_if_namespace (int ifindex, char *net_ns) +vnet_netlink_set_if_master (int ifindex, int master_ifindex) { - clib_error_t *err; - int ns_fd; - u8 *s; - s = format (0, "/var/run/netns/%s%c", net_ns, 0); - ns_fd = open ((char *) s, O_RDONLY); - vec_free (s); - if (ns_fd == -1) - return clib_error_return (0, "namespace '%s' doesn't exist", net_ns); - - err = - vnet_netlink_set_if_attr (ifindex, IFLA_NET_NS_FD, &ns_fd, sizeof (int)); - close (ns_fd); - return err; + vnet_netlink_msg_t m; + struct ifinfomsg ifmsg = { 0 }; + + ifmsg.ifi_family = AF_UNSPEC; + ifmsg.ifi_index = ifindex; + ifmsg.ifi_change = 0xffffffff; + vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST, + &ifmsg, sizeof (struct ifinfomsg)); + vnet_netlink_msg_add_rtattr (&m, IFLA_MASTER, &master_ifindex, + sizeof (int)); + return vnet_netlink_msg_send (&m); +} + +clib_error_t * +vnet_netlink_set_if_mtu (int ifindex, int mtu) +{ + vnet_netlink_msg_t m; + struct ifinfomsg ifmsg = { 0 }; + + ifmsg.ifi_family = AF_UNSPEC; + ifmsg.ifi_index = ifindex; + ifmsg.ifi_change = 0xffffffff; + vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST, + &ifmsg, sizeof (struct ifinfomsg)); + vnet_netlink_msg_add_rtattr (&m, IFLA_MTU, &mtu, sizeof (int)); + return vnet_netlink_msg_send (&m); +} + +clib_error_t * +vnet_netlink_add_ip4_addr (int ifindex, void *addr, int pfx_len) +{ + vnet_netlink_msg_t m; + struct ifaddrmsg ifa = { 0 }; + + ifa.ifa_family = AF_INET; + ifa.ifa_prefixlen = pfx_len; + ifa.ifa_index = ifindex; + + vnet_netlink_msg_init (&m, RTM_NEWADDR, + NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL, + &ifa, sizeof (struct ifaddrmsg)); + + vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4); + vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4); + return vnet_netlink_msg_send (&m); +} + +clib_error_t * +vnet_netlink_add_ip6_addr (int ifindex, void *addr, int pfx_len) +{ + vnet_netlink_msg_t m; + struct ifaddrmsg ifa = { 0 }; + + ifa.ifa_family = AF_INET6; + ifa.ifa_prefixlen = pfx_len; + ifa.ifa_index = ifindex; + + vnet_netlink_msg_init (&m, RTM_NEWADDR, + NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL, + &ifa, sizeof (struct ifaddrmsg)); + + vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16); + vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16); + return vnet_netlink_msg_send (&m); } /* |