aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2017-11-30 20:56:37 +0100
committerDamjan Marion <damarion@cisco.com>2017-11-30 21:13:45 +0100
commit17fdae73a49ea95f2c7bc69bc6bb51f4ad9d0289 (patch)
tree3543c69f49c9b5f523046833cbc92c517caa81ed /src
parent7f0d1d347191f66059d728fd95dcead685869a0d (diff)
tap_v2: move netlink code to separate file
Change-Id: Ib091875f77ea99421aec0947fd17833c4e6d2ec2 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src')
-rw-r--r--src/vnet.am2
-rw-r--r--src/vnet/devices/netlink.c114
-rw-r--r--src/vnet/devices/netlink.h30
-rw-r--r--src/vnet/devices/virtio/tap.c78
4 files changed, 148 insertions, 76 deletions
diff --git a/src/vnet.am b/src/vnet.am
index 00a6b231c7b..5d9fa8885c1 100644
--- a/src/vnet.am
+++ b/src/vnet.am
@@ -34,6 +34,7 @@ libvnet_la_SOURCES += \
vnet/buffer.c \
vnet/config.c \
vnet/devices/devices.c \
+ vnet/devices/netlink.c \
vnet/handoff.c \
vnet/interface.c \
vnet/interface_api.c \
@@ -48,6 +49,7 @@ nobase_include_HEADERS += \
vnet/buffer.h \
vnet/config.h \
vnet/devices/devices.h \
+ vnet/devices/netlink.h \
vnet/global_funcs.h \
vnet/handoff.h \
vnet/interface.h \
diff --git a/src/vnet/devices/netlink.c b/src/vnet/devices/netlink.c
new file mode 100644
index 00000000000..b2a651348a3
--- /dev/null
+++ b/src/vnet/devices/netlink.c
@@ -0,0 +1,114 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <net/if.h>
+#include <linux/if_tun.h>
+#include <sys/ioctl.h>
+#include <linux/virtio_net.h>
+#include <linux/vhost.h>
+#include <sys/eventfd.h>
+
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+
+#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)
+{
+ 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;
+
+ memset (&req, 0, sizeof (req));
+ if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
+ {
+ err = clib_error_return_unix (0, "socket(AF_NETLINK)");
+ goto error;
+ }
+
+ ra.nl_family = AF_NETLINK;
+ ra.nl_pid = getpid ();
+
+ if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
+ return clib_error_return_unix (0, "bind");
+
+ 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)
+ err = clib_error_return_unix (0, "send");
+
+error:
+ return err;
+}
+
+clib_error_t *
+vnet_netlink_set_if_mtu (int ifindex, int mtu)
+{
+ clib_error_t *err;
+
+ err = vnet_netlink_set_if_attr (ifindex, IFLA_MTU, &mtu, sizeof (int));
+ return err;
+}
+
+clib_error_t *
+vnet_netlink_set_if_namespace (int ifindex, char *net_ns)
+{
+ 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;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/devices/netlink.h b/src/vnet/devices/netlink.h
new file mode 100644
index 00000000000..3d5a3660949
--- /dev/null
+++ b/src/vnet/devices/netlink.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef included_vnet_device_netlink_h
+#define included_vnet_device_netlink_h
+
+clib_error_t *vnet_netlink_set_if_mtu (int ifindex, int mtu);
+clib_error_t *vnet_netlink_set_if_namespace (int ifindex, char *net_ns);
+
+#endif /* included_vnet_device_netlink_h */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/devices/virtio/tap.c b/src/vnet/devices/virtio/tap.c
index 658ba6bfc68..f6db2c90616 100644
--- a/src/vnet/devices/virtio/tap.c
+++ b/src/vnet/devices/virtio/tap.c
@@ -31,6 +31,7 @@
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/ethernet/ethernet.h>
+#include <vnet/devices/netlink.h>
#include <vnet/devices/virtio/virtio.h>
#include <vnet/devices/virtio/tap.h>
@@ -49,81 +50,6 @@ virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
return 0;
}
-
-clib_error_t *
-clib_netlink_set_if_attr (int ifindex, unsigned short rta_type, void *data,
- int data_len)
-{
- 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;
-
- memset (&req, 0, sizeof (req));
- if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
- {
- err = clib_error_return_unix (0, "socket(AF_NETLINK)");
- goto error;
- }
-
- ra.nl_family = AF_NETLINK;
- ra.nl_pid = getpid ();
-
- if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
- return clib_error_return_unix (0, "bind");
-
- 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)
- err = clib_error_return_unix (0, "send");
-
-error:
- return err;
-}
-
-clib_error_t *
-clib_netlink_set_if_mtu (int ifindex, int mtu)
-{
- clib_error_t *err;
-
- err = clib_netlink_set_if_attr (ifindex, IFLA_MTU, &mtu, sizeof (int));
- return err;
-}
-
-clib_error_t *
-clib_netlink_set_if_namespace (int ifindex, char *net_ns)
-{
- 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 =
- clib_netlink_set_if_attr (ifindex, IFLA_NET_NS_FD, &ns_fd, sizeof (int));
- close (ns_fd);
- return err;
-}
-
int
tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
{
@@ -196,7 +122,7 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
if (args->net_ns)
{
- err = clib_netlink_set_if_namespace (vif->ifindex,
+ err = vnet_netlink_set_if_namespace (vif->ifindex,
(char *) args->net_ns);
if (err)
{