From patchwork Thu Nov 9 17:31:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Leitner X-Patchwork-Id: 836446 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXqwz16Y3z9sPt for ; Fri, 10 Nov 2017 04:32:19 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id C0A8CABC; Thu, 9 Nov 2017 17:31:40 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 8251BAB2 for ; Thu, 9 Nov 2017 17:31:39 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-qt0-f196.google.com (mail-qt0-f196.google.com [209.85.216.196]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id A54C98A for ; Thu, 9 Nov 2017 17:31:38 +0000 (UTC) Received: by mail-qt0-f196.google.com with SMTP id 1so8615513qtn.3 for ; Thu, 09 Nov 2017 09:31:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=dFPXPtj3z8s3fC6aCMBXSeQEAWKW1poLq+kXDo6YGXk=; b=GP0/FX6Xu0jyu81FWSYZOrE6PSCy4MrOcIH9qrcpIIMqcmjnF3gk9JetBA1ekPyg7K K19AewZ6mW0m52jL/RoSWybBF8x5wi2JE38ZXLzfv4U2aDxgxLRXWNFllUb+bvfbmj6l Ls0rqwsjk/m0ufosEuspZcSty5KCZ/9HqwMGZ0IF26GL5/bIUk9VHrri00dl6LhVeUB5 i670oXo8+P1OzKyysca6SjI0PY/QSzNTotUn06k8x7sRQ3lw3BMaP3d5xHzU5S1FiyQT gZvdvYjwYubPA9Zbyhm2SscLUF68tnxQaFYMvqE7r3rrWMmebM95Zm/36hskwmXyy4QI 1mwg== X-Gm-Message-State: AJaThX5/dfXrmqufSnYKWaGaNva3itu5MvKBx5YbQxvtnngfUzFLMvfc Hc0IZ1jJUcvDdG3vnc7AzoKFZIzZP6g= X-Google-Smtp-Source: AGs4zMaK9fQbMTKs+YjMHIdIIsOhXhRchXPaWBulcMOm7pZEDzGo7v3p57YsIp1NFHxPcO4dPIzoiw== X-Received: by 10.200.57.55 with SMTP id s52mr2122460qtb.129.1510248697511; Thu, 09 Nov 2017 09:31:37 -0800 (PST) Received: from localhost ([2804:7f4:5087:e653:e8e6:b1:eeb0:fb4]) by smtp.gmail.com with ESMTPSA id g47sm5017483qtc.1.2017.11.09.09.31.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2017 09:31:37 -0800 (PST) From: Flavio Leitner To: dev@openvswitch.org Date: Thu, 9 Nov 2017 15:31:00 -0200 Message-Id: <20171109173107.26256-2-fbl@redhat.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171109173107.26256-1-fbl@redhat.com> References: <20171109173107.26256-1-fbl@redhat.com> X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Flavio Leitner , Jiri Benc Subject: [ovs-dev] [PATCH v2 1/8] netlink: provide network namespace id from a msg. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org The netlink notification's ancillary data contains the network namespace id (netnsid) needed to identify the device correctly. (ifindex and netnsid). Signed-off-by: Flavio Leitner --- ChangeLog: * V2 - report and close unexpected file descriptors. configure.ac | 3 +- lib/automake.mk | 1 + lib/dpif-netlink.c | 6 +-- lib/netdev-linux.c | 2 +- lib/netlink-notifier.c | 2 +- lib/netlink-socket.c | 53 +++++++++++++++++++--- lib/netlink-socket.h | 4 +- lib/netns.h | 119 +++++++++++++++++++++++++++++++++++++++++++++++++ utilities/nlmon.c | 2 +- 9 files changed, 178 insertions(+), 14 deletions(-) create mode 100644 lib/netns.h diff --git a/configure.ac b/configure.ac index 9e0081832..ad036fc67 100644 --- a/configure.ac +++ b/configure.ac @@ -113,7 +113,8 @@ AC_CHECK_MEMBERS([struct sockaddr_in6.sin6_scope_id], [], [], [[#include #include ]]) AC_CHECK_FUNCS([mlockall strnlen getloadavg statvfs getmntent_r sendmmsg clock_gettime]) -AC_CHECK_HEADERS([mntent.h sys/statvfs.h linux/types.h linux/if_ether.h stdatomic.h]) +AC_CHECK_HEADERS([mntent.h sys/statvfs.h linux/types.h linux/if_ether.h]) +AC_CHECK_HEADERS([linux/net_namespace.h stdatomic.h]) AC_CHECK_HEADERS([net/if_mib.h], [], [], [[#include #include ]]) diff --git a/lib/automake.mk b/lib/automake.mk index effe5b5c2..6b89716ae 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -140,6 +140,7 @@ lib_libopenvswitch_la_SOURCES = \ lib/netflow.h \ lib/netlink.c \ lib/netlink.h \ + lib/netns.h \ lib/nx-match.c \ lib/nx-match.h \ lib/object-collection.c \ diff --git a/lib/dpif-netlink.c b/lib/dpif-netlink.c index c265909f8..fd333094d 100644 --- a/lib/dpif-netlink.c +++ b/lib/dpif-netlink.c @@ -1287,7 +1287,7 @@ dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep) int error; ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); - error = nl_sock_recv(dpif->port_notifier, &buf, false); + error = nl_sock_recv(dpif->port_notifier, &buf, NULL, false); if (!error) { struct dpif_netlink_vport vport; @@ -2621,7 +2621,7 @@ dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id, return EAGAIN; } - error = nl_sock_recv(sock_pool[i].nl_sock, buf, false); + error = nl_sock_recv(sock_pool[i].nl_sock, buf, NULL, false); if (error == ENOBUFS) { /* ENOBUFS typically means that we've received so many * packets that the buffer overflowed. Try again @@ -2696,7 +2696,7 @@ dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id, return EAGAIN; } - error = nl_sock_recv(ch->sock, buf, false); + error = nl_sock_recv(ch->sock, buf, NULL, false); if (error == ENOBUFS) { /* ENOBUFS typically means that we've received so many * packets that the buffer overflowed. Try again diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index fbbbb7205..184822816 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -638,7 +638,7 @@ netdev_linux_run(const struct netdev_class *netdev_class OVS_UNUSED) struct ofpbuf buf; ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); - error = nl_sock_recv(sock, &buf, false); + error = nl_sock_recv(sock, &buf, NULL, false); if (!error) { struct rtnetlink_change change; diff --git a/lib/netlink-notifier.c b/lib/netlink-notifier.c index 3acded418..d33904658 100644 --- a/lib/netlink-notifier.c +++ b/lib/netlink-notifier.c @@ -187,7 +187,7 @@ nln_run(struct nln *nln) int error; ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); - error = nl_sock_recv(nln->notify_sock, &buf, false); + error = nl_sock_recv(nln->notify_sock, &buf, NULL, false); if (!error) { int group = nln->parse(&buf, nln->change); diff --git a/lib/netlink-socket.c b/lib/netlink-socket.c index 317bf907f..4e0317fbe 100644 --- a/lib/netlink-socket.c +++ b/lib/netlink-socket.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -607,7 +608,8 @@ nl_sock_send_seq(struct nl_sock *sock, const struct ofpbuf *msg, } static int -nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait) +nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, struct netns *ns, + bool wait) { /* We can't accurately predict the size of the data to be received. The * caller is supposed to have allocated enough space in 'buf' to handle the @@ -618,7 +620,10 @@ nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait) uint8_t tail[65536]; struct iovec iov[2]; struct msghdr msg; + uint8_t msgctrl[64]; + struct cmsghdr *cmsg; ssize_t retval; + int *ptr; int error; ovs_assert(buf->allocated >= sizeof *nlmsghdr); @@ -632,6 +637,8 @@ nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait) memset(&msg, 0, sizeof msg); msg.msg_iov = iov; msg.msg_iovlen = 2; + msg.msg_control = msgctrl; + msg.msg_controllen = sizeof msgctrl; /* Receive a Netlink message from the kernel. * @@ -706,6 +713,38 @@ nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait) } #endif + if (ns) { + /* The network namespace id comes as ancillary data. For older + * kernels, this data is either not available or it might be -1, + * so it falls back to local network namespace (no id). Latest + * kernels return a valid ID only if available or nothing. */ + netns_set_local(ns); + cmsg = CMSG_FIRSTHDR(&msg); + while (cmsg != NULL) { + if (cmsg->cmsg_level == SOL_NETLINK + && cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID) { + ptr = ALIGNED_CAST(int *, CMSG_DATA(cmsg)); + netns_set_id(ns, *ptr); + } + if (cmsg->cmsg_level == SOL_SOCKET + && cmsg->cmsg_type == SCM_RIGHTS) { + /* This is unexpected and unwanted, close all fds */ + int nfds; + int i; + nfds = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) + / sizeof(int); + ptr = ALIGNED_CAST(int *, CMSG_DATA(cmsg)); + for (i = 0; i < nfds; i++) { + VLOG_ERR_RL(&rl, "closing unexpected received fd (%d).", + ptr[i]); + close(ptr[i]); + } + } + + cmsg = CMSG_NXTHDR(&msg, cmsg); + } + } + log_nlmsg(__func__, 0, buf->data, buf->size, sock->protocol); COVERAGE_INC(netlink_received); @@ -714,7 +753,8 @@ nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait) /* Tries to receive a Netlink message from the kernel on 'sock' into 'buf'. If * 'wait' is true, waits for a message to be ready. Otherwise, fails with - * EAGAIN if the 'sock' receive buffer is empty. + * EAGAIN if the 'sock' receive buffer is empty. If 'ns' is provided, the + * network namespace information will be provided. * * The caller must have initialized 'buf' with an allocation of at least * NLMSG_HDRLEN bytes. For best performance, the caller should allocate enough @@ -730,9 +770,10 @@ nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait) * Regardless of success or failure, this function resets 'buf''s headroom to * 0. */ int -nl_sock_recv(struct nl_sock *sock, struct ofpbuf *buf, bool wait) +nl_sock_recv(struct nl_sock *sock, struct ofpbuf *buf, struct netns *ns, + bool wait) { - return nl_sock_recv__(sock, buf, wait); + return nl_sock_recv__(sock, buf, ns, wait); } static void @@ -821,7 +862,7 @@ nl_sock_transact_multiple__(struct nl_sock *sock, } /* Receive a reply. */ - error = nl_sock_recv__(sock, buf_txn->reply, false); + error = nl_sock_recv__(sock, buf_txn->reply, NULL, false); if (error) { if (error == EAGAIN) { nl_sock_record_errors__(transactions, n, 0); @@ -1101,7 +1142,7 @@ nl_dump_refill(struct nl_dump *dump, struct ofpbuf *buffer) int error; while (!buffer->size) { - error = nl_sock_recv__(dump->sock, buffer, false); + error = nl_sock_recv__(dump->sock, buffer, NULL, false); if (error) { /* The kernel never blocks providing the results of a dump, so * error == EAGAIN means that we've read the whole thing, and diff --git a/lib/netlink-socket.h b/lib/netlink-socket.h index d3cc64288..348483fad 100644 --- a/lib/netlink-socket.h +++ b/lib/netlink-socket.h @@ -193,6 +193,7 @@ #include #include #include +#include "netns.h" #include "openvswitch/ofpbuf.h" #include "ovs-atomic.h" #include "ovs-thread.h" @@ -221,7 +222,8 @@ int nl_sock_unsubscribe_packets(struct nl_sock *sock); int nl_sock_send(struct nl_sock *, const struct ofpbuf *, bool wait); int nl_sock_send_seq(struct nl_sock *, const struct ofpbuf *, uint32_t nlmsg_seq, bool wait); -int nl_sock_recv(struct nl_sock *, struct ofpbuf *, bool wait); +int nl_sock_recv(struct nl_sock *, struct ofpbuf *, struct netns *, + bool wait); int nl_sock_drain(struct nl_sock *); diff --git a/lib/netns.h b/lib/netns.h new file mode 100644 index 000000000..2e8bd8b0c --- /dev/null +++ b/lib/netns.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2017 Red Hat Inc. + * + * 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 NETNS_H +#define NETNS_H 1 + +#include + +#ifdef HAVE_LINUX_NET_NAMESPACE_H +#include +#define NETNS_NOT_ASSIGNED NETNSA_NSID_NOT_ASSIGNED +#else +#define NETNS_NOT_ASSIGNED -1 +#endif + +enum netns_state { + NETNS_INVALID, /* not initialized yet */ + NETNS_LOCAL, /* local or not supported on older kernels */ + NETNS_REMOTE /* on another network namespace with valid ID */ +}; + +struct netns { + enum netns_state state; + int id; +}; + +/* Prototypes */ +static inline void netns_set_id(struct netns *ns, int id); +static inline void netns_set_invalid(struct netns *ns); +static inline bool netns_is_invalid(struct netns *ns); +static inline void netns_set_local(struct netns *ns); +static inline bool netns_is_local(struct netns *ns); +static inline bool netns_is_remote(struct netns *ns); +static inline bool netns_eq(const struct netns *a, const struct netns *b); +static inline void netns_copy(struct netns *dst, const struct netns *src); + +/* Functions */ +static inline void +netns_set_id(struct netns *ns, int id) +{ + if (!ns) { + return; + } + + if (id == NETNS_NOT_ASSIGNED) { + ns->state = NETNS_LOCAL; + } else { + ns->state = NETNS_REMOTE; + ns->id = id; + } +} + +static inline void +netns_set_invalid(struct netns *ns) +{ + ns->state = NETNS_INVALID; +} + +static inline bool +netns_is_invalid(struct netns *ns) +{ + return ns->state == NETNS_INVALID; +} + +static inline void +netns_set_local(struct netns *ns) +{ + ns->state = NETNS_LOCAL; +} + +static inline bool +netns_is_local(struct netns *ns) +{ + return (ns->state == NETNS_LOCAL); +} + +static inline bool +netns_is_remote(struct netns *ns) +{ + return (ns->state == NETNS_REMOTE); +} + +static inline void +netns_copy(struct netns *dst, const struct netns *src) +{ + if (src->state == NETNS_LOCAL || src->state == NETNS_REMOTE) { + *dst = *src; + } +} + +static inline bool +netns_eq(const struct netns *a, const struct netns *b) +{ + if (a->state == NETNS_LOCAL && b->state == NETNS_LOCAL) { + return true; + } + + if (a->state == NETNS_REMOTE && b->state == NETNS_REMOTE && + a->id == b->id) { + return true; + } + + return false; +} + +#endif diff --git a/utilities/nlmon.c b/utilities/nlmon.c index b91fa09b3..d38a70b6f 100644 --- a/utilities/nlmon.c +++ b/utilities/nlmon.c @@ -59,7 +59,7 @@ main(int argc OVS_UNUSED, char *argv[]) ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); for (;;) { - error = nl_sock_recv(sock, &buf, false); + error = nl_sock_recv(sock, &buf, NULL, false); if (error == EAGAIN) { /* Nothing to do. */ } else if (error == ENOBUFS) { From patchwork Thu Nov 9 17:31:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Leitner X-Patchwork-Id: 836448 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXqxq3cRJz9t8C for ; Fri, 10 Nov 2017 04:33:02 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id B0B73B08; Thu, 9 Nov 2017 17:31:42 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 04F4BACC for ; Thu, 9 Nov 2017 17:31:42 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-qt0-f195.google.com (mail-qt0-f195.google.com [209.85.216.195]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id B05768A for ; Thu, 9 Nov 2017 17:31:41 +0000 (UTC) Received: by mail-qt0-f195.google.com with SMTP id z28so8597920qtz.13 for ; Thu, 09 Nov 2017 09:31:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FfpaADtsaVQKg0n4iz64cd7VDOIEMs76AO0ytllt2hg=; b=QQH1XJDgmA9dU/1Sq2YhldOM+3Xp/uNj2Yw2D/OXMKdKqbq3GsvXkInytaachnq3xo kIYo7L0KAxrjURBZocSbfUHZyqoXr8EEsnimUUqP/uOAQu0GwSo0gN/uGRQ/n7R+xx/C 3bIrWQBbDBsqmi1ZeaILQAw91Qt24PJZ/qBJfs+xjxNFo/DmQQKavk+UUEEbFkU2A70j MP6eujDneJFjYuowc+cs/3jai3bV6Uf2VImNsInS2M2KW0h46BTE05gb6SBoVajpOxy/ zhwaJC3gN8g+T3UoOvZp8N0I/dKnH41GCKGqn8SHnr6FUnfEWyWxE6gZm1d0FS343yOv fpYw== X-Gm-Message-State: AJaThX5J9I/sb1/99qdnaTE9xHpPTUxkYoutGzaAICeHWwTlq/53M1+7 v+pU7ljWRFJw45wUXzp641e6xGJZtao= X-Google-Smtp-Source: AGs4zMYu3cZ4zCQOMmkmRiWhvUv6/PPhkH9EKV1GtOH4JcXreI5zmtndhiQEWPaE5xIN36jqdIJCGw== X-Received: by 10.200.25.171 with SMTP id u40mr2090098qtj.297.1510248700712; Thu, 09 Nov 2017 09:31:40 -0800 (PST) Received: from localhost ([2804:7f4:5087:e653:e8e6:b1:eeb0:fb4]) by smtp.gmail.com with ESMTPSA id c4sm4865361qth.6.2017.11.09.09.31.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2017 09:31:40 -0800 (PST) From: Flavio Leitner To: dev@openvswitch.org Date: Thu, 9 Nov 2017 15:31:01 -0200 Message-Id: <20171109173107.26256-3-fbl@redhat.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171109173107.26256-1-fbl@redhat.com> References: <20171109173107.26256-1-fbl@redhat.com> X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Flavio Leitner , Jiri Benc Subject: [ovs-dev] [PATCH v2 2/8] netdev-linux: initialize netns as invalid. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org A port might be already in another netns, so initialize it as invalid to force a state update. Signed-off-by: Flavio Leitner --- lib/netdev-linux.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index 184822816..c31334ce1 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -788,6 +788,10 @@ netdev_linux_common_construct(struct netdev *netdev_) return EINVAL; } + /* The device could be in the same network namespace or in another one. + * Initialize as invalid to force a state update. */ + netns_set_invalid(&netdev->netns); + ovs_mutex_init(&netdev->mutex); return 0; } From patchwork Thu Nov 9 17:31:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Leitner X-Patchwork-Id: 836449 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXqzH1R1sz9t8Z for ; Fri, 10 Nov 2017 04:33:50 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id D2E9EBC2; Thu, 9 Nov 2017 17:31:45 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 5F97DB0B for ; Thu, 9 Nov 2017 17:31:45 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-qt0-f196.google.com (mail-qt0-f196.google.com [209.85.216.196]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 0AB758A for ; Thu, 9 Nov 2017 17:31:44 +0000 (UTC) Received: by mail-qt0-f196.google.com with SMTP id q33so4171408qta.2 for ; Thu, 09 Nov 2017 09:31:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=5LeFrR9/3shDLvv44C6djmggr2l1sMAE/6u8E9VFHsI=; b=i3Qa189iVbgDj6vGri91tDiJ4GXqqIa9T4SkHmjBzhpGBbAGLeQ5XH4kPg7679CEmR yYGj00twufC39LDRyiMeMy3fn7cg3o2iuYaCLPxEo3/s2KMKUS0vN4VKNfpbg1GLFR9L +oroWNmKUHmioNbIGQYC7SLlHiduhVUby6scVyVStDPMEfEEm3fM4Yi0nxlp9rPY2CUj EjpCKlElGiwJhXGGsMpRuPJaEeYaYjzq+w2UvE0vg+dmHGxcPSZTZE0Gcal7ai+mgZMC D9W8cfpBIi1sI7d4Lbf+K9YMQILtPUdHnAJ2RU5OtXVP5ySX0yOamvTMM/i1dFd8xwi2 O4mg== X-Gm-Message-State: AJaThX7pIUtC5vZk5KFxj9JRGbor4PFZMZ/O3bP7NmTNQyLbic+kKKNT U6uuu8SHzeVESAw1a9RvUfuzJmcyj/Q= X-Google-Smtp-Source: AGs4zMaKRJ8vCbbBTf0hS1k8CKZFtK5j6TBSw7lxJJ2OHKv3vNsFFiwvMq78LZqyBcX4+QA+fF8D4A== X-Received: by 10.200.39.20 with SMTP id g20mr1916548qtg.125.1510248704056; Thu, 09 Nov 2017 09:31:44 -0800 (PST) Received: from localhost ([2804:7f4:5087:e653:e8e6:b1:eeb0:fb4]) by smtp.gmail.com with ESMTPSA id g47sm5017627qtc.1.2017.11.09.09.31.43 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2017 09:31:43 -0800 (PST) From: Flavio Leitner To: dev@openvswitch.org Date: Thu, 9 Nov 2017 15:31:02 -0200 Message-Id: <20171109173107.26256-4-fbl@redhat.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171109173107.26256-1-fbl@redhat.com> References: <20171109173107.26256-1-fbl@redhat.com> X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Flavio Leitner , Jiri Benc Subject: [ovs-dev] [PATCH v2 3/8] vport: retrieve the netnsid if available. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Recent kernels provide the network namespace ID of a port, so use that to discover where the port currently is. Signed-off-by: Flavio Leitner --- datapath/linux/compat/include/linux/openvswitch.h | 2 ++ lib/dpif-netlink.c | 6 ++++++ lib/dpif-netlink.h | 2 ++ lib/netdev-linux.c | 20 ++++++++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/datapath/linux/compat/include/linux/openvswitch.h b/datapath/linux/compat/include/linux/openvswitch.h index bc6c94b8d..d2712d05f 100644 --- a/datapath/linux/compat/include/linux/openvswitch.h +++ b/datapath/linux/compat/include/linux/openvswitch.h @@ -283,6 +283,8 @@ enum ovs_vport_attr { /* receiving upcalls */ OVS_VPORT_ATTR_STATS, /* struct ovs_vport_stats */ OVS_VPORT_ATTR_PAD, + OVS_VPORT_ATTR_IFINDEX, + OVS_VPORT_ATTR_NETNSID, __OVS_VPORT_ATTR_MAX }; diff --git a/lib/dpif-netlink.c b/lib/dpif-netlink.c index fd333094d..b85e74c50 100644 --- a/lib/dpif-netlink.c +++ b/lib/dpif-netlink.c @@ -3065,6 +3065,7 @@ dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport, [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats), .optional = true }, [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true }, + [OVS_VPORT_ATTR_NETNSID] = { .type = NL_A_U32, .optional = true }, }; dpif_netlink_vport_init(vport); @@ -3100,6 +3101,11 @@ dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport, vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]); vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]); } + if (a[OVS_VPORT_ATTR_NETNSID]) { + netns_set_id(&vport->netns, nl_attr_get_u32(a[OVS_VPORT_ATTR_NETNSID])); + } else { + netns_set_local(&vport->netns); + } return 0; } diff --git a/lib/dpif-netlink.h b/lib/dpif-netlink.h index 568b81441..680a74c2f 100644 --- a/lib/dpif-netlink.h +++ b/lib/dpif-netlink.h @@ -21,6 +21,7 @@ #include #include #include "odp-netlink.h" +#include "netns.h" #include "flow.h" @@ -32,6 +33,7 @@ struct dpif_netlink_vport { /* ovs_vport header. */ int dp_ifindex; + struct netns netns; odp_port_t port_no; /* ODPP_NONE if unknown. */ enum ovs_vport_type type; diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index c31334ce1..fd181272b 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -476,6 +476,7 @@ struct netdev_linux { long long int miimon_interval; /* Miimon Poll rate. Disabled if <= 0. */ struct timer miimon_timer; + struct netns netns; /* network namespace. */ /* The following are figured out "on demand" only. They are only valid * when the corresponding VALID_* bit in 'cache_valid' is set. */ int ifindex; @@ -571,6 +572,25 @@ netdev_rxq_linux_cast(const struct netdev_rxq *rx) return CONTAINER_OF(rx, struct netdev_rxq_linux, up); } +static int +netdev_linux_netns_update(struct netdev_linux *netdev) +{ + struct netns *dev_netns = &netdev->netns; + struct dpif_netlink_vport reply; + struct ofpbuf *buf; + int error; + + error = dpif_netlink_vport_get(netdev_get_name(&netdev->up), &reply, &buf); + if (error) { + netns_set_invalid(dev_netns); + return error; + } + + netns_copy(dev_netns, &reply.netns); + ofpbuf_delete(buf); + return 0; +} + static void netdev_linux_update(struct netdev_linux *netdev, const struct rtnetlink_change *) OVS_REQUIRES(netdev->mutex); From patchwork Thu Nov 9 17:31:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Leitner X-Patchwork-Id: 836450 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXqzL5yyRz9t8c for ; Fri, 10 Nov 2017 04:34:22 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id C4AD5BD8; Thu, 9 Nov 2017 17:31:50 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id E54BFBC1 for ; Thu, 9 Nov 2017 17:31:49 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-qk0-f195.google.com (mail-qk0-f195.google.com [209.85.220.195]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 816A31CE for ; Thu, 9 Nov 2017 17:31:49 +0000 (UTC) Received: by mail-qk0-f195.google.com with SMTP id v137so8681683qkb.1 for ; Thu, 09 Nov 2017 09:31:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=DJbdvp6g0RUcU+0umgq/G3KBdCVGVstnL5YbrQ7m+nE=; b=Bg5uDkjhSAhByKzlnuOY7xQJcVCo8pEdmYwcMdEutVDJqoBbmOuLNCulzoN3TineE7 WOtvVKTLH8Dn/QoUEX4f9kGIaxjFva88soPi+vw9LEqxCQaCJaskGbphZZWQgAe1myez MGi7nkXYQ3c+wJ9Xx5xrxUL7xsiM7fGellgEnAQRoY4HlIXtqcOWtLY/i/K0H9mIpdSq 2qRU2SlepyPmNDTEQCcH7Y726dLDg6F9Na8oq2rZzJrSk74U29wo5+aPx4LSXCeV4rzY zsBtlGFQCFaJg3XTEBLd5urG65T/1T7EwA/amU7YQmDvMshQx6mlaWASjnw4+8TT3sQb G7BQ== X-Gm-Message-State: AJaThX4PbsEo7punJF0SCvLrCfivaRcuIJ6I31pDfgaKwKJ3a/La0cQj v0R7BEJV8BpDEZIihb9/QWG8JcH+G1g= X-Google-Smtp-Source: AGs4zMYsK8V9D2LOpgWZqa53nDnsZr5VLD3RVOrG3Bt8qTfIIGsdGHhyUPMfltGYzQrBGZAWslJp2g== X-Received: by 10.55.99.214 with SMTP id x205mr2062267qkb.34.1510248708430; Thu, 09 Nov 2017 09:31:48 -0800 (PST) Received: from localhost ([2804:7f4:5087:e653:e8e6:b1:eeb0:fb4]) by smtp.gmail.com with ESMTPSA id y10sm4813633qkl.14.2017.11.09.09.31.47 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2017 09:31:47 -0800 (PST) From: Flavio Leitner To: dev@openvswitch.org Date: Thu, 9 Nov 2017 15:31:03 -0200 Message-Id: <20171109173107.26256-5-fbl@redhat.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171109173107.26256-1-fbl@redhat.com> References: <20171109173107.26256-1-fbl@redhat.com> X-Spam-Status: No, score=0.5 required=5.0 tests=RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Flavio Leitner , Jiri Benc Subject: [ovs-dev] [PATCH v2 4/8] netdev: update device info only if netns matches. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org A network device in another network namespace could have the same name, so once the socket starts listening to other network namespaces, it is necessary to confirm the netns id. Signed-off-by: Flavio Leitner --- lib/netdev-linux.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index fd181272b..7a6937cc8 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -573,7 +573,7 @@ netdev_rxq_linux_cast(const struct netdev_rxq *rx) } static int -netdev_linux_netns_update(struct netdev_linux *netdev) +netdev_linux_netns_update__(struct netdev_linux *netdev) { struct netns *dev_netns = &netdev->netns; struct dpif_netlink_vport reply; @@ -591,7 +591,31 @@ netdev_linux_netns_update(struct netdev_linux *netdev) return 0; } -static void netdev_linux_update(struct netdev_linux *netdev, +static int +netdev_linux_netns_update(struct netdev_linux *netdev) +{ + if (netns_is_invalid(&netdev->netns)) { + return netdev_linux_netns_update__(netdev); + } + + return 0; +} + +static bool +netdev_linux_netns_is_remote(struct netdev_linux *netdev) +{ + netdev_linux_netns_update(netdev); + return netns_is_remote(&netdev->netns); +} + +static bool +netdev_linux_netns_is_eq(struct netdev_linux *netdev, struct netns *ns) +{ + netdev_linux_netns_update(netdev); + return netns_eq(&netdev->netns, ns); +} + +static void netdev_linux_update(struct netdev_linux *netdev, struct netns *, const struct rtnetlink_change *) OVS_REQUIRES(netdev->mutex); static void netdev_linux_changed(struct netdev_linux *netdev, @@ -655,10 +679,11 @@ netdev_linux_run(const struct netdev_class *netdev_class OVS_UNUSED) do { static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); uint64_t buf_stub[4096 / 8]; + struct netns ns; struct ofpbuf buf; ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); - error = nl_sock_recv(sock, &buf, NULL, false); + error = nl_sock_recv(sock, &buf, &ns, false); if (!error) { struct rtnetlink_change change; @@ -677,7 +702,7 @@ netdev_linux_run(const struct netdev_class *netdev_class OVS_UNUSED) struct netdev_linux *netdev = netdev_linux_cast(netdev_); ovs_mutex_lock(&netdev->mutex); - netdev_linux_update(netdev, &change); + netdev_linux_update(netdev, &ns, &change); ovs_mutex_unlock(&netdev->mutex); } netdev_close(netdev_); @@ -744,11 +769,11 @@ netdev_linux_changed(struct netdev_linux *dev, } static void -netdev_linux_update(struct netdev_linux *dev, - const struct rtnetlink_change *change) +netdev_linux_update__(struct netdev_linux *dev, + const struct rtnetlink_change *change) OVS_REQUIRES(dev->mutex) { - if (rtnetlink_type_is_rtnlgrp_link(change->nlmsg_type)){ + if (rtnetlink_type_is_rtnlgrp_link(change->nlmsg_type)) { if (change->nlmsg_type == RTM_NEWLINK) { /* Keep drv-info, and ip addresses. */ netdev_linux_changed(dev, change->ifi_flags, @@ -772,6 +797,7 @@ netdev_linux_update(struct netdev_linux *dev, dev->get_ifindex_error = 0; } else { netdev_linux_changed(dev, change->ifi_flags, 0); + netns_set_invalid(&dev->netns); } } else if (rtnetlink_type_is_rtnlgrp_addr(change->nlmsg_type)) { /* Invalidates in4, in6. */ @@ -781,6 +807,16 @@ netdev_linux_update(struct netdev_linux *dev, } } +static void +netdev_linux_update(struct netdev_linux *dev, struct netns *ns, + const struct rtnetlink_change *change) + OVS_REQUIRES(dev->mutex) +{ + if (netdev_linux_netns_is_eq(dev, ns)) { + netdev_linux_update__(dev, change); + } +} + static struct netdev * netdev_linux_alloc(void) { From patchwork Thu Nov 9 17:31:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Leitner X-Patchwork-Id: 836451 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXqzy0vxPz9t8c for ; Fri, 10 Nov 2017 04:34:54 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id A6B02BD6; Thu, 9 Nov 2017 17:31:54 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 07822BCF for ; Thu, 9 Nov 2017 17:31:53 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-qk0-f193.google.com (mail-qk0-f193.google.com [209.85.220.193]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 878CA134 for ; Thu, 9 Nov 2017 17:31:52 +0000 (UTC) Received: by mail-qk0-f193.google.com with SMTP id o6so8682352qkh.3 for ; Thu, 09 Nov 2017 09:31:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tyc+tbHuxHYV8iGuWmqxYlmYxnX955e5i8+OTw4XP8c=; b=M2q62ldQgWpxdKDdIt4CYZ2DJnX87VP2nvRhnC6IG+wmAqU/8g3NIdId9Q0gKg4Ro7 t9Li9jJeE8BZ5uQYVN1VXPrRDDKzgzvPqv57Yr6ibVO59JtG9kMWnGzyMKhoeP3LE/eh JNsL0M216J+YLaT74KUjYtMjdUgooYtml3sv1SvH5VoNOm/nDcOvbZuzudx+JE8LZyzB QjEb8ic7g1uK4dNEKGvcdOumdbKjLJkCPcNMZAcibq6MKRy2jU1LrjkiQGDW7WSSM8tu zxf3CptkW5oFyWE2GO28k/u1riL/XCLKKQS0I2JOjoUX6/icueNdzTuqZd+Lw50JLJqH gBRw== X-Gm-Message-State: AJaThX5/sXhc2zjjk+Mq2VaRlTeB23P3OaWJs1kAh9hJOdaBbtbuYZsy OYHqAc3o2VA0UCE6bJpFI5iWIxiPWLc= X-Google-Smtp-Source: AGs4zMYciNd2x6EG0R4AMQBaV5I5kFdVoja1+CSdKpDTlE/L4DPk9vxjV8O2gwRD4GgRQyyNT12GSw== X-Received: by 10.55.151.4 with SMTP id z4mr2061996qkd.173.1510248711469; Thu, 09 Nov 2017 09:31:51 -0800 (PST) Received: from localhost ([2804:7f4:5087:e653:e8e6:b1:eeb0:fb4]) by smtp.gmail.com with ESMTPSA id k126sm4883602qkc.69.2017.11.09.09.31.50 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2017 09:31:51 -0800 (PST) From: Flavio Leitner To: dev@openvswitch.org Date: Thu, 9 Nov 2017 15:31:04 -0200 Message-Id: <20171109173107.26256-6-fbl@redhat.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171109173107.26256-1-fbl@redhat.com> References: <20171109173107.26256-1-fbl@redhat.com> X-Spam-Status: No, score=0.5 required=5.0 tests=RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Flavio Leitner , Jiri Benc Subject: [ovs-dev] [PATCH v2 5/8] netdev-linux: use netlink to update netdev. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org The ioctl interface doesn't support network namespaces, so try updating the netdev using netlink message instead. To provide backwards compatibility, fall back to the previous method if netlink isn't supported or fails. Signed-off-by: Flavio Leitner --- lib/netdev-linux.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 3 deletions(-) diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index 7a6937cc8..703e1baf9 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -86,6 +86,9 @@ COVERAGE_DEFINE(netdev_get_ethtool); COVERAGE_DEFINE(netdev_set_ethtool); +#ifndef IFLA_IF_NETNSID +#define IFLA_IF_NETNSID 0x45 +#endif /* These were introduced in Linux 2.6.14, so they might be missing if we have * old headers. */ #ifndef ADVERTISED_Pause @@ -615,6 +618,7 @@ netdev_linux_netns_is_eq(struct netdev_linux *netdev, struct netns *ns) return netns_eq(&netdev->netns, ns); } +static int netdev_linux_update_via_netlink(struct netdev_linux *); static void netdev_linux_update(struct netdev_linux *netdev, struct netns *, const struct rtnetlink_change *) OVS_REQUIRES(netdev->mutex); @@ -1436,6 +1440,11 @@ netdev_linux_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac) ovs_mutex_lock(&netdev->mutex); if (!(netdev->cache_valid & VALID_ETHERADDR)) { + netdev_linux_update_via_netlink(netdev); + } + + if (!(netdev->cache_valid & VALID_ETHERADDR)) { + /* Fall back to ioctl if netlink fails */ netdev->ether_addr_error = get_etheraddr(netdev_get_name(netdev_), &netdev->etheraddr); netdev->cache_valid |= VALID_ETHERADDR; @@ -1456,6 +1465,11 @@ netdev_linux_get_mtu__(struct netdev_linux *netdev, int *mtup) int error; if (!(netdev->cache_valid & VALID_MTU)) { + netdev_linux_update_via_netlink(netdev); + } + + if (!(netdev->cache_valid & VALID_MTU)) { + /* Fall back to ioctl if netlink fails */ struct ifreq ifr; netdev->netdev_mtu_error = af_inet_ifreq_ioctl( @@ -2868,12 +2882,21 @@ netdev_linux_update_flags(struct netdev *netdev_, enum netdev_flags off, enum netdev_flags on, enum netdev_flags *old_flagsp) { struct netdev_linux *netdev = netdev_linux_cast(netdev_); - int error; + int error = 0; ovs_mutex_lock(&netdev->mutex); - error = update_flags(netdev, off, on, old_flagsp); + if (on || off) { + /* Changing flags over netlink isn't support yet. */ + error = update_flags(netdev, off, on, old_flagsp); + } else { + /* Try reading flags over netlink, or fall back to ioctl. */ + if (!netdev_linux_update_via_netlink(netdev)) { + *old_flagsp = iff_to_nd_flags(netdev->ifi_flags); + } else { + error = update_flags(netdev, off, on, old_flagsp); + } + } ovs_mutex_unlock(&netdev->mutex); - return error; } @@ -5515,6 +5538,11 @@ get_ifindex(const struct netdev *netdev_, int *ifindexp) struct netdev_linux *netdev = netdev_linux_cast(netdev_); if (!(netdev->cache_valid & VALID_IFINDEX)) { + netdev_linux_update_via_netlink(netdev); + } + + if (!(netdev->cache_valid & VALID_IFINDEX)) { + /* Fall back to ioctl if netlink fails */ int ifindex = linux_get_ifindex(netdev_get_name(netdev_)); if (ifindex < 0) { @@ -5532,6 +5560,79 @@ get_ifindex(const struct netdev *netdev_, int *ifindexp) } static int +netdev_linux_update_via_netlink(struct netdev_linux *netdev) +{ + struct ofpbuf request; + struct ofpbuf *reply; + struct rtnetlink_change chg; + struct rtnetlink_change *change = &chg; + int error; + + ofpbuf_init(&request, 0); + nl_msg_put_nlmsghdr(&request, + sizeof(struct ifinfomsg) + NL_ATTR_SIZE(IFNAMSIZ), + RTM_GETLINK, NLM_F_REQUEST); + ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg)); + + /* The correct identifiers for a Linux device are netnsid and ifindex, + * but ifindex changes as the port is moved to another network namespace + * and the interface name statically stored in ovsdb. */ + nl_msg_put_string(&request, IFLA_IFNAME, netdev_get_name(&netdev->up)); + if (netdev_linux_netns_is_remote(netdev)) { + nl_msg_push_u32(&request, IFLA_IF_NETNSID, netdev->netns.id); + } + error = nl_transact(NETLINK_ROUTE, &request, &reply); + ofpbuf_uninit(&request); + if (error) { + ofpbuf_delete(reply); + return error; + } + + if (rtnetlink_parse(reply, change) + && change->nlmsg_type == RTM_NEWLINK) { + bool changed = false; + error = 0; + + /* Update netdev from rtnl msg and increment its seq if needed. */ + if ((change->ifi_flags ^ netdev->ifi_flags) & IFF_RUNNING) { + netdev->carrier_resets++; + changed = true; + } + if (change->ifi_flags != netdev->ifi_flags) { + netdev->ifi_flags = change->ifi_flags; + changed = true; + } + if (change->mtu && change->mtu != netdev->mtu) { + netdev->mtu = change->mtu; + netdev->cache_valid |= VALID_MTU; + netdev->netdev_mtu_error = 0; + changed = true; + } + if (!eth_addr_is_zero(change->mac) + && !eth_addr_equals(change->mac, netdev->etheraddr)) { + netdev->etheraddr = change->mac; + netdev->cache_valid |= VALID_ETHERADDR; + netdev->ether_addr_error = 0; + changed = true; + } + if (change->if_index != netdev->ifindex) { + netdev->ifindex = change->if_index; + netdev->cache_valid |= VALID_IFINDEX; + netdev->get_ifindex_error = 0; + changed = true; + } + if (changed) { + netdev_change_seq_changed(&netdev->up); + } + } else { + error = EINVAL; + } + + ofpbuf_delete(reply); + return error; +} + +static int get_etheraddr(const char *netdev_name, struct eth_addr *ea) { struct ifreq ifr; From patchwork Thu Nov 9 17:31:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Leitner X-Patchwork-Id: 836452 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXr0Y55Zkz9s71 for ; Fri, 10 Nov 2017 04:35:25 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 7902ABF0; Thu, 9 Nov 2017 17:31:58 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 85513C05 for ; Thu, 9 Nov 2017 17:31:56 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-qt0-f195.google.com (mail-qt0-f195.google.com [209.85.216.195]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id D81104CC for ; Thu, 9 Nov 2017 17:31:55 +0000 (UTC) Received: by mail-qt0-f195.google.com with SMTP id z28so8598919qtz.13 for ; Thu, 09 Nov 2017 09:31:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=dOeDTX+8KlFAcDAzPPqUKYVtsCdb52jWV/YzdnU+SOU=; b=sze6gAcZGrbUk/buGYbupT54+dSg4JIB8fEeEsXHAWg6ocbvlAW2y6eFGymWM4BTCS n7wmJlja95rAOQb4sZgj6l0540wqECArIuw8tuqkTBLzHceOtFrb7hzOAO2CG69sVLQx an2CcqEIhQlcujqkvDDfFNVnDLthWHLt/gz9PRd+Doax/tqVZY89L/km2hykRG+g1Ygk lRSiR/erFyOrDwrtHK/ElAe6gDFwbvxtSsU60u9fDPRoe6f8+MUquYiwaY4gQS+vKZ+K OGkq1GH8+2xtOItUkVF7ZpvTrZWPKWj8uXZtNJKSR2wzCXis7KkdprYZEr8QvoC+GC9A fBng== X-Gm-Message-State: AJaThX7a/6Va8dyZnO54NKxo/TuTSJDjoev59i3MNK5Ueh6OSm1145MH zCWLVWNfn3oqCbsb2AM+MmlOrIefo6w= X-Google-Smtp-Source: AGs4zMZPNZVlgHtV1f+HwZPrLZGAi4tEJ1QQWHfegt6EdgE5ZQXd2GE9xLKULYxrqdyp7zdNvKi4uA== X-Received: by 10.200.43.26 with SMTP id 26mr2212191qtu.51.1510248714756; Thu, 09 Nov 2017 09:31:54 -0800 (PST) Received: from localhost ([2804:7f4:5087:e653:e8e6:b1:eeb0:fb4]) by smtp.gmail.com with ESMTPSA id n44sm5029297qtb.41.2017.11.09.09.31.53 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2017 09:31:54 -0800 (PST) From: Flavio Leitner To: dev@openvswitch.org Date: Thu, 9 Nov 2017 15:31:05 -0200 Message-Id: <20171109173107.26256-7-fbl@redhat.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171109173107.26256-1-fbl@redhat.com> References: <20171109173107.26256-1-fbl@redhat.com> X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Flavio Leitner , Jiri Benc Subject: [ovs-dev] [PATCH v2 6/8] netlink linux: enable listening to all nsids X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Internal ports may be moved to another network namespace and when that happens, the vswitch stops receiving netlink notifications. This patch enables the vswitch to listen to all network namespaces that have a nsid assigned into the network namespace where the socket has been opened. It requires kernel 4.2 or newer. Signed-off-by: Flavio Leitner --- lib/daemon-unix.c | 3 ++- lib/daemon.man | 6 +++--- lib/daemon.xml | 8 ++++---- lib/netdev-linux.c | 1 + lib/netlink-protocol.h | 6 ++++++ lib/netlink-socket.c | 27 +++++++++++++++++++++++++++ lib/netlink-socket.h | 2 ++ tests/ofproto-macros.at | 1 + tests/ovn-controller-vtep.at | 1 + 9 files changed, 47 insertions(+), 8 deletions(-) diff --git a/lib/daemon-unix.c b/lib/daemon-unix.c index 967a28432..be6d29cbe 100644 --- a/lib/daemon-unix.c +++ b/lib/daemon-unix.c @@ -818,7 +818,8 @@ daemon_become_new_user_linux(bool access_datapath OVS_UNUSED) if (access_datapath && !ret) { ret = capng_update(CAPNG_ADD, cap_sets, CAP_NET_ADMIN) - || capng_update(CAPNG_ADD, cap_sets, CAP_NET_RAW); + || capng_update(CAPNG_ADD, cap_sets, CAP_NET_RAW) + || capng_update(CAPNG_ADD, cap_sets, CAP_NET_BROADCAST); } } else { ret = -1; diff --git a/lib/daemon.man b/lib/daemon.man index 820a09903..68c0a312d 100644 --- a/lib/daemon.man +++ b/lib/daemon.man @@ -76,9 +76,9 @@ started by the root user accepts this argument. .IP On Linux, daemons will be granted CAP_IPC_LOCK and CAP_NET_BIND_SERVICES before dropping root privileges. Daemons that interact with a datapath, -such as \fBovs\-vswitchd\fR, will be granted two additional capabilities, namely -CAP_NET_ADMIN and CAP_NET_RAW. The capability change will apply even if -new user is "root". +such as \fBovs\-vswitchd\fR, will be granted three additional capabilities, +namely CAP_NET_ADMIN, CAP_NET_BROADCAST and CAP_NET_RAW. The capability +change will apply even if the new user is root. .IP On Windows, this option is not currently supported. For security reasons, specifying this option will cause the daemon process not to start. diff --git a/lib/daemon.xml b/lib/daemon.xml index 5cb447c49..1b5e8acae 100644 --- a/lib/daemon.xml +++ b/lib/daemon.xml @@ -107,10 +107,10 @@ On Linux, daemons will be granted CAP_IPC_LOCK and CAP_NET_BIND_SERVICES before dropping root privileges. Daemons that interact with a datapath, such as - ovs-vswitchd, will be granted two additional - capabilities, namely CAP_NET_ADMIN and - CAP_NET_RAW. The capability change will apply even - if the new user is root. + ovs-vswitchd, will be granted three additional + capabilities, namely CAP_NET_ADMIN, + CAP_NET_BROADCAST and CAP_NET_RAW. The + capability change will apply even if the new user is root.

diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index 703e1baf9..ce8df4c6b 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -653,6 +653,7 @@ netdev_linux_notify_sock(void) } } } + nl_sock_listen_all_nsid(sock, true); ovsthread_once_done(&once); } diff --git a/lib/netlink-protocol.h b/lib/netlink-protocol.h index a7b9a65fa..c0617dfad 100644 --- a/lib/netlink-protocol.h +++ b/lib/netlink-protocol.h @@ -158,6 +158,12 @@ enum { #define NETLINK_DROP_MEMBERSHIP 2 #endif +/* This was introduced in v4.2. (We want our programs to support the newer + * kernel features even if compiled with older headers.) */ +#ifndef NETLINK_LISTEN_ALL_NSID +#define NETLINK_LISTEN_ALL_NSID 8 +#endif + /* These were introduced all together in 2.6.23. (We want our programs to * support the newer kernel features even if compiled with older headers.) */ #ifndef CTRL_ATTR_MCAST_GRP_MAX diff --git a/lib/netlink-socket.c b/lib/netlink-socket.c index 4e0317fbe..022b53f2d 100644 --- a/lib/netlink-socket.c +++ b/lib/netlink-socket.c @@ -441,6 +441,33 @@ nl_sock_join_mcgroup(struct nl_sock *sock, unsigned int multicast_group) return 0; } +/* When 'enable' is true, it tries to enable 'sock' to receive netlink + * notifications form all network namespaces that have an nsid assigned + * into the network namespace where the socket has been opened. The + * running kernel needs to provide support for that. When 'enable' is + * false, it will receive netlink notifications only from the network + * namespace where the socket has been opened. + * + * Returns 0 if successful, otherwise a positive errno. */ +int +nl_sock_listen_all_nsid(struct nl_sock *sock, bool enable) +{ + int error; + int val = enable ? 1 : 0; + +#ifndef _WIN32 + if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &val, + sizeof val) < 0) { + error = errno; + VLOG_INFO("netlink: could not %s listening to all nsid (%s)", + enable ? "enable" : "disable", ovs_strerror(error)); + return errno; + } +#endif + + return 0; +} + #ifdef _WIN32 int nl_sock_subscribe_packet__(struct nl_sock *sock, bool subscribe) diff --git a/lib/netlink-socket.h b/lib/netlink-socket.h index 348483fad..deac8d244 100644 --- a/lib/netlink-socket.h +++ b/lib/netlink-socket.h @@ -214,6 +214,8 @@ void nl_sock_destroy(struct nl_sock *); int nl_sock_join_mcgroup(struct nl_sock *, unsigned int multicast_group); int nl_sock_leave_mcgroup(struct nl_sock *, unsigned int multicast_group); +int nl_sock_listen_all_nsid(struct nl_sock *, bool enable); + #ifdef _WIN32 int nl_sock_subscribe_packets(struct nl_sock *sock); int nl_sock_unsubscribe_packets(struct nl_sock *sock); diff --git a/tests/ofproto-macros.at b/tests/ofproto-macros.at index 38449db32..5657f0f87 100644 --- a/tests/ofproto-macros.at +++ b/tests/ofproto-macros.at @@ -341,6 +341,7 @@ m4_define([_OVS_VSWITCHD_START], /netdev_linux|INFO|.*device has unknown hardware address family/d /ofproto|INFO|datapath ID changed to fedcba9876543210/d /dpdk|INFO|DPDK Disabled - Use other_config:dpdk-init to enable/d +/netlink_socket|INFO|netlink: could not enable listening to all nsid/d /netdev: Flow API/d /tc: Using policy/d']]) ]) diff --git a/tests/ovn-controller-vtep.at b/tests/ovn-controller-vtep.at index 0d2711e3a..f435ff874 100644 --- a/tests/ovn-controller-vtep.at +++ b/tests/ovn-controller-vtep.at @@ -43,6 +43,7 @@ m4_define([OVN_CONTROLLER_VTEP_START], /vswitchd|INFO|ovs-vswitchd (Open vSwitch)/d /reconnect|INFO|/d /ofproto|INFO|using datapath ID/d +/netlink_socket|INFO|netlink: could not enable listening to all nsid/d /ofproto|INFO|datapath ID changed to fedcba9876543210/d']]) AT_CHECK([ovs-vsctl -- add-br br-vtep \ -- set bridge br-vtep datapath-type=dummy other-config:datapath-id=fedcba9876543210 other-config:hwaddr=aa:55:aa:55:00:00 protocols=[[OpenFlow10,OpenFlow11,OpenFlow12,OpenFlow13,OpenFlow14,OpenFlow15]] fail-mode=secure \ From patchwork Thu Nov 9 17:31:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Leitner X-Patchwork-Id: 836453 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXr1L07f4z9s7C for ; Fri, 10 Nov 2017 04:36:05 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id B7150C0F; Thu, 9 Nov 2017 17:32:00 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 3F49EC05 for ; Thu, 9 Nov 2017 17:31:59 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-qk0-f195.google.com (mail-qk0-f195.google.com [209.85.220.195]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 002141CE for ; Thu, 9 Nov 2017 17:31:58 +0000 (UTC) Received: by mail-qk0-f195.google.com with SMTP id n66so8654859qki.8 for ; Thu, 09 Nov 2017 09:31:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=9DwYNIn5smPNWc/LohuCPTk1LLswCH0x3G09nzOA744=; b=elNq2nirjshrNk4mwSaNLVktZm5EJodnAaTRTsBp47VgOxWbN0yDbsFNDoDNuoO9V1 lSx6OdG056pVCqyLY6gMmwlxDDuYZ6+BCEIE7EG+ZtLaxRlraMpS93eSs2uJUzuFtwnG eVhN0XAhJjqXrcTGMa/RaBEbSRqECMXwaHnpXgbfCeKUjSSz510T5yAwFZLUj6Fd+lKh 15bDcRMnKeaO63qkuL9B9uSmtbEuBNW4b/vpXUStcvNAlI3dsHreDSyNpW/LQ7tz675Q dvUKwx5unCtv/STHfyCQGvOfR8yWyhhbPr0FucHq3QV6ATwjjJFOtpwWLqYwHzksuegy mvkw== X-Gm-Message-State: AJaThX5ZG3mtlL1fiGqwzlH6Pt8ywLlQWiaHVXaS0F1OlkgmkDASArHc BgTfCeS/0MmYnNgbwqG4MQNiJo7jVgw= X-Google-Smtp-Source: AGs4zMY7qbepCZ2Niasxh9FSIxVUX2yK6nZGyuLVhNSD1oWqoC+AfgEQxfV2bNVNAJ7sY0jDwnz6oA== X-Received: by 10.55.136.67 with SMTP id k64mr2066253qkd.26.1510248717991; Thu, 09 Nov 2017 09:31:57 -0800 (PST) Received: from localhost ([2804:7f4:5087:e653:e8e6:b1:eeb0:fb4]) by smtp.gmail.com with ESMTPSA id j48sm5049633qta.96.2017.11.09.09.31.56 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2017 09:31:57 -0800 (PST) From: Flavio Leitner To: dev@openvswitch.org Date: Thu, 9 Nov 2017 15:31:06 -0200 Message-Id: <20171109173107.26256-8-fbl@redhat.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171109173107.26256-1-fbl@redhat.com> References: <20171109173107.26256-1-fbl@redhat.com> X-Spam-Status: No, score=0.5 required=5.0 tests=RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Flavio Leitner , Jiri Benc Subject: [ovs-dev] [PATCH v2 7/8] nlmon: added netns support. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Signed-off-by: Flavio Leitner --- utilities/nlmon.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/utilities/nlmon.c b/utilities/nlmon.c index d38a70b6f..8d9d20f97 100644 --- a/utilities/nlmon.c +++ b/utilities/nlmon.c @@ -41,6 +41,7 @@ main(int argc OVS_UNUSED, char *argv[]) { uint64_t buf_stub[4096 / 64]; struct nl_sock *sock; + struct netns ns; struct ofpbuf buf; int error; @@ -57,9 +58,10 @@ main(int argc OVS_UNUSED, char *argv[]) ovs_fatal(error, "could not join RTNLGRP_LINK multicast group"); } + nl_sock_listen_all_nsid(sock, true); ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); for (;;) { - error = nl_sock_recv(sock, &buf, NULL, false); + error = nl_sock_recv(sock, &buf, &ns, false); if (error == EAGAIN) { /* Nothing to do. */ } else if (error == ENOBUFS) { @@ -123,6 +125,11 @@ main(int argc OVS_UNUSED, char *argv[]) } } printf("\n"); + if (netns_is_remote(&ns)) { + printf("\tnetns id: %d\n", ns.id); + } else { + printf("\tnetns id: local\n"); + } if (attrs[IFLA_MASTER]) { uint32_t idx = nl_attr_get_u32(attrs[IFLA_MASTER]); char ifname[IFNAMSIZ]; From patchwork Thu Nov 9 17:31:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Flavio Leitner X-Patchwork-Id: 836454 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXr1x5YVmz9s7C for ; Fri, 10 Nov 2017 04:36:36 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 8BE04C27; Thu, 9 Nov 2017 17:32:04 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id E7AE8C12 for ; Thu, 9 Nov 2017 17:32:02 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-qt0-f196.google.com (mail-qt0-f196.google.com [209.85.216.196]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 3D1378A for ; Thu, 9 Nov 2017 17:32:02 +0000 (UTC) Received: by mail-qt0-f196.google.com with SMTP id 1so8617153qtn.3 for ; Thu, 09 Nov 2017 09:32:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=llUuDeorXg+IrYY7mwnggYbDIXo7AVTlCbg7SQxuZQg=; b=PX/l4bXg8wSh8e2e7FTuzxptMwdMFOEWCKtngjA8AiE4fmsYyh02kLO3LZplJJirEl Xl/1QqmdGd/DNRKcOTCgQ6kAnKfsA4yxlMCtEAJK6i74AD3dQiwUu6ubBKuFSpe3d26Y mM9BPxX0tUcCIctsHPdgNtWJBvC6nniyalZhmo3UKRyxEwbs+AbkxMkdi82TP8Axhn1Q Uxngl876qKg8o1a6NsOvCjN3I+3DCs4ONRFA1FKChyOCf98bDNyw5ww7k4dtD81yI8rm fxNK5843xgkA0LDi5tj2Mp2RerSI+Ioy66/+/k1pqyGjUS9EaUuAGSkra5Z1auCdpiwp WSVw== X-Gm-Message-State: AJaThX7LqQL22q51yW4rKMzm4+IKkfiz9CY7vmjOzaytICMPr1uVNATu tboQx8ytPsljFnsARcjURA3G92oDD8Y= X-Google-Smtp-Source: AGs4zMZquXqwaf+Qd3YLjlAgSRPBckXQKil+FUxfpIk6yieosDZfWJpE2J2CDivdg4MjpU4MgiS0Qg== X-Received: by 10.200.42.219 with SMTP id c27mr2167578qta.28.1510248721150; Thu, 09 Nov 2017 09:32:01 -0800 (PST) Received: from localhost ([2804:7f4:5087:e653:e8e6:b1:eeb0:fb4]) by smtp.gmail.com with ESMTPSA id 17sm4971471qkg.24.2017.11.09.09.32.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 09 Nov 2017 09:32:00 -0800 (PST) From: Flavio Leitner To: dev@openvswitch.org Date: Thu, 9 Nov 2017 15:31:07 -0200 Message-Id: <20171109173107.26256-9-fbl@redhat.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171109173107.26256-1-fbl@redhat.com> References: <20171109173107.26256-1-fbl@redhat.com> X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE, T_FILL_THIS_FORM_SHORT autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Flavio Leitner , Jiri Benc Subject: [ovs-dev] [PATCH v2 8/8] netdev-linux: fail ops not supporting remote netns. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org When the netdev is in another namespace and the operation doesn't support network namespaces, return the correct error. Signed-off-by: Flavio Leitner --- lib/netdev-linux.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 120 insertions(+), 14 deletions(-) diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index ce8df4c6b..2aee6c226 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -1342,6 +1342,11 @@ netdev_linux_send(struct netdev *netdev_, int qid OVS_UNUSED, int sock = 0; if (!is_tap_netdev(netdev_)) { + if (netdev_linux_netns_is_remote(netdev_linux_cast(netdev_))) { + error = EOPNOTSUPP; + goto free_batch; + } + sock = af_packet_sock(); if (sock < 0) { error = -sock; @@ -1401,6 +1406,10 @@ netdev_linux_set_etheraddr(struct netdev *netdev_, const struct eth_addr mac) int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } if (netdev->cache_valid & VALID_ETHERADDR) { error = netdev->ether_addr_error; @@ -1514,6 +1523,11 @@ netdev_linux_set_mtu(struct netdev *netdev_, int mtu) int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + if (netdev->cache_valid & VALID_MTU) { error = netdev->netdev_mtu_error; if (error || netdev->mtu == mtu) { @@ -1543,9 +1557,14 @@ netdev_linux_get_ifindex(const struct netdev *netdev_) int ifindex, error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } error = get_ifindex(netdev_, &ifindex); - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error ? -error : ifindex; } @@ -2086,6 +2105,11 @@ netdev_linux_get_features(const struct netdev *netdev_, int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + netdev_linux_read_features(netdev); if (!netdev->get_features_error) { *current = netdev->current; @@ -2094,8 +2118,9 @@ netdev_linux_get_features(const struct netdev *netdev_, *peer = 0; /* XXX */ } error = netdev->get_features_error; - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2111,6 +2136,12 @@ netdev_linux_set_advertisements(struct netdev *netdev_, ovs_mutex_lock(&netdev->mutex); COVERAGE_INC(netdev_get_ethtool); + + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + memset(&ecmd, 0, sizeof ecmd); error = netdev_linux_do_ethtool(netdev_get_name(netdev_), &ecmd, ETHTOOL_GSET, "ETHTOOL_GSET"); @@ -2188,6 +2219,11 @@ netdev_linux_set_policing(struct netdev *netdev_, : kbits_burst); /* Stick with user-specified value. */ ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto out; + } + if (netdev->cache_valid & VALID_POLICING) { error = netdev->netdev_policing_error; if (error || (netdev->kbits_rate == kbits_rate && @@ -2324,6 +2360,11 @@ netdev_linux_get_qos(const struct netdev *netdev_, int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = tc_query_qdisc(netdev_); if (!error) { *typep = netdev->tc->ops->ovs_name; @@ -2331,8 +2372,9 @@ netdev_linux_get_qos(const struct netdev *netdev_, ? netdev->tc->ops->qdisc_get(netdev_, details) : 0); } - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2354,6 +2396,11 @@ netdev_linux_set_qos(struct netdev *netdev_, } ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = tc_query_qdisc(netdev_); if (error) { goto exit; @@ -2387,6 +2434,11 @@ netdev_linux_get_queue(const struct netdev *netdev_, int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = tc_query_qdisc(netdev_); if (!error) { struct tc_queue *queue = tc_find_queue(netdev_, queue_id); @@ -2394,8 +2446,9 @@ netdev_linux_get_queue(const struct netdev *netdev_, ? netdev->tc->ops->class_get(netdev_, queue, details) : ENOENT); } - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2407,6 +2460,11 @@ netdev_linux_set_queue(struct netdev *netdev_, int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = tc_query_qdisc(netdev_); if (!error) { error = (queue_id < netdev->tc->ops->n_queues @@ -2414,8 +2472,9 @@ netdev_linux_set_queue(struct netdev *netdev_, ? netdev->tc->ops->class_set(netdev_, queue_id, details) : EINVAL); } - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2426,6 +2485,11 @@ netdev_linux_delete_queue(struct netdev *netdev_, unsigned int queue_id) int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = tc_query_qdisc(netdev_); if (!error) { if (netdev->tc->ops->class_delete) { @@ -2437,8 +2501,9 @@ netdev_linux_delete_queue(struct netdev *netdev_, unsigned int queue_id) error = EINVAL; } } - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2451,6 +2516,11 @@ netdev_linux_get_queue_stats(const struct netdev *netdev_, int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = tc_query_qdisc(netdev_); if (!error) { if (netdev->tc->ops->class_get_stats) { @@ -2466,8 +2536,9 @@ netdev_linux_get_queue_stats(const struct netdev *netdev_, error = EOPNOTSUPP; } } - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2510,10 +2581,15 @@ struct netdev_linux_queue_state { static int netdev_linux_queue_dump_start(const struct netdev *netdev_, void **statep) { - const struct netdev_linux *netdev = netdev_linux_cast(netdev_); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = tc_query_qdisc(netdev_); if (!error) { if (netdev->tc->ops->class_get) { @@ -2534,8 +2610,9 @@ netdev_linux_queue_dump_start(const struct netdev *netdev_, void **statep) error = EOPNOTSUPP; } } - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2543,11 +2620,16 @@ static int netdev_linux_queue_dump_next(const struct netdev *netdev_, void *state_, unsigned int *queue_idp, struct smap *details) { - const struct netdev_linux *netdev = netdev_linux_cast(netdev_); + struct netdev_linux *netdev = netdev_linux_cast(netdev_); struct netdev_linux_queue_state *state = state_; int error = EOF; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + while (state->cur_queue < state->n_queues) { unsigned int queue_id = state->queues[state->cur_queue++]; struct tc_queue *queue = tc_find_queue(netdev_, queue_id); @@ -2558,8 +2640,9 @@ netdev_linux_queue_dump_next(const struct netdev *netdev_, void *state_, break; } } - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2582,6 +2665,11 @@ netdev_linux_dump_queue_stats(const struct netdev *netdev_, int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = tc_query_qdisc(netdev_); if (!error) { struct queue_dump_state state; @@ -2608,8 +2696,9 @@ netdev_linux_dump_queue_stats(const struct netdev *netdev_, } } } - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2621,6 +2710,11 @@ netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address, int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address); if (!error) { if (address.s_addr != INADDR_ANY) { @@ -2629,8 +2723,8 @@ netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address, } } +exit: ovs_mutex_unlock(&netdev->mutex); - return error; } @@ -2645,9 +2739,15 @@ netdev_linux_get_addr_list(const struct netdev *netdev_, int error; ovs_mutex_lock(&netdev->mutex); + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } + error = netdev_get_addrs(netdev_get_name(netdev_), addr, mask, n_cnt); - ovs_mutex_unlock(&netdev->mutex); +exit: + ovs_mutex_unlock(&netdev->mutex); return error; } @@ -2888,6 +2988,10 @@ netdev_linux_update_flags(struct netdev *netdev_, enum netdev_flags off, ovs_mutex_lock(&netdev->mutex); if (on || off) { /* Changing flags over netlink isn't support yet. */ + if (netdev_linux_netns_is_remote(netdev)) { + error = EOPNOTSUPP; + goto exit; + } error = update_flags(netdev, off, on, old_flagsp); } else { /* Try reading flags over netlink, or fall back to ioctl. */ @@ -2897,6 +3001,8 @@ netdev_linux_update_flags(struct netdev *netdev_, enum netdev_flags off, error = update_flags(netdev, off, on, old_flagsp); } } + +exit: ovs_mutex_unlock(&netdev->mutex); return error; }