[dpdk-dev,v2] kni: create KNI interface in current network namespace

Message ID 1417486763-12883-1-git-send-email-takayuki@midokura.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Takayuki Usui Dec. 2, 2014, 2:19 a.m. UTC
  With this patch, KNI interface (e.g. vEth0) is created in the
network namespace where the DPDK application is running.
Otherwise, all interfaces are created in the default namespace
in the host.

put_net() is required, since get_net_ns_by_pid() increments
the reference counter of the network namespace with get_net().

Signed-off-by: Takayuki Usui <takayuki@midokura.com>
---
 lib/librte_eal/linuxapp/kni/kni_misc.c | 7 +++++++
 1 file changed, 7 insertions(+)
  

Comments

Nicolas Dichtel Dec. 2, 2014, 9:04 a.m. UTC | #1
Le 02/12/2014 03:19, Takayuki Usui a écrit :
> With this patch, KNI interface (e.g. vEth0) is created in the
> network namespace where the DPDK application is running.
> Otherwise, all interfaces are created in the default namespace
> in the host.
>
> put_net() is required, since get_net_ns_by_pid() increments
> the reference counter of the network namespace with get_net().
>
> Signed-off-by: Takayuki Usui <takayuki@midokura.com>
> ---
>   lib/librte_eal/linuxapp/kni/kni_misc.c | 7 +++++++
>   1 file changed, 7 insertions(+)
>
> diff --git a/lib/librte_eal/linuxapp/kni/kni_misc.c b/lib/librte_eal/linuxapp/kni/kni_misc.c
> index ba77776..33c7a48 100644
> --- a/lib/librte_eal/linuxapp/kni/kni_misc.c
> +++ b/lib/librte_eal/linuxapp/kni/kni_misc.c
> @@ -311,6 +311,7 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
>   	struct net_device *net_dev = NULL;
>   	struct net_device *lad_dev = NULL;
>   	struct kni_dev *kni, *dev, *n;
> +	struct net *net;
>
>   	printk(KERN_INFO "KNI: Creating kni...\n");
>   	/* Check the buffer size, to avoid warning */
> @@ -354,6 +355,12 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
>   		return -EBUSY;
>   	}
>
> +	net = get_net_ns_by_pid(current->pid);
> +	if (IS_ERR(net))
In case of error, you should call free_netdev(net_dev) to avoid a memory leak.

> +		return PTR_ERR(net);
> +	dev_net_set(net_dev, net);
> +	put_net(net);
> +
>   	kni = netdev_priv(net_dev);
>
>   	kni->net_dev = net_dev;
>
  

Patch

diff --git a/lib/librte_eal/linuxapp/kni/kni_misc.c b/lib/librte_eal/linuxapp/kni/kni_misc.c
index ba77776..33c7a48 100644
--- a/lib/librte_eal/linuxapp/kni/kni_misc.c
+++ b/lib/librte_eal/linuxapp/kni/kni_misc.c
@@ -311,6 +311,7 @@  kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
 	struct net_device *net_dev = NULL;
 	struct net_device *lad_dev = NULL;
 	struct kni_dev *kni, *dev, *n;
+	struct net *net;
 
 	printk(KERN_INFO "KNI: Creating kni...\n");
 	/* Check the buffer size, to avoid warning */
@@ -354,6 +355,12 @@  kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
 		return -EBUSY;
 	}
 
+	net = get_net_ns_by_pid(current->pid);
+	if (IS_ERR(net))
+		return PTR_ERR(net);
+	dev_net_set(net_dev, net);
+	put_net(net);
+
 	kni = netdev_priv(net_dev);
 
 	kni->net_dev = net_dev;