From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <helin.zhang@intel.com>
Received: from mga02.intel.com (mga02.intel.com [134.134.136.20])
 by dpdk.org (Postfix) with ESMTP id D8B765699
 for <dev@dpdk.org>; Thu, 22 Oct 2015 05:28:19 +0200 (CEST)
Received: from fmsmga001.fm.intel.com ([10.253.24.23])
 by orsmga101.jf.intel.com with ESMTP; 21 Oct 2015 20:28:18 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.20,180,1444719600"; d="scan'208";a="816673481"
Received: from fmsmsx103.amr.corp.intel.com ([10.18.124.201])
 by fmsmga001.fm.intel.com with ESMTP; 21 Oct 2015 20:28:15 -0700
Received: from fmsmsx113.amr.corp.intel.com (10.18.116.7) by
 FMSMSX103.amr.corp.intel.com (10.18.124.201) with Microsoft SMTP Server (TLS)
 id 14.3.248.2; Wed, 21 Oct 2015 20:28:14 -0700
Received: from shsmsx151.ccr.corp.intel.com (10.239.6.50) by
 FMSMSX113.amr.corp.intel.com (10.18.116.7) with Microsoft SMTP Server (TLS)
 id 14.3.248.2; Wed, 21 Oct 2015 20:28:14 -0700
Received: from shsmsx104.ccr.corp.intel.com ([169.254.5.194]) by
 SHSMSX151.ccr.corp.intel.com ([169.254.3.47]) with mapi id 14.03.0248.002;
 Thu, 22 Oct 2015 11:28:12 +0800
From: "Zhang, Helin" <helin.zhang@intel.com>
To: Dex Chen <dex.chen@ruckuswireless.com>
Thread-Topic: [dpdk-dev] [PATCH] kni: allow per-net instances
Thread-Index: AQHQtK+nihcwYQZZ30WWK7L1fBgcC553g9qw
Date: Thu, 22 Oct 2015 03:28:11 +0000
Message-ID: <F35DEAC7BCE34641BA9FAC6BCA4A12E70A91AB4A@SHSMSX104.ccr.corp.intel.com>
References: <1435831933-13339-1-git-send-email-dex.chen@ruckuswireless.com>
In-Reply-To: <1435831933-13339-1-git-send-email-dex.chen@ruckuswireless.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
x-originating-ip: [10.239.127.40]
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Cc: "dev@dpdk.org" <dev@dpdk.org>
Subject: Re: [dpdk-dev] [PATCH] kni: allow per-net instances
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches and discussions about DPDK <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Thu, 22 Oct 2015 03:28:20 -0000

Hi Dex

Two comments inlined. Thank you very much for the really good contribution =
to KNI!

Regards,
Helin

> -----Original Message-----
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Dex Chen
> Sent: Thursday, July 2, 2015 6:12 PM
> To: dev@dpdk.org
> Subject: [dpdk-dev] [PATCH] kni: allow per-net instances
>=20
> There is a global variable 'device_in_use' which is used to make sure onl=
y one
> instance is using /dev/kni device. If you were using LXC, you will find t=
here is only
> one instance of KNI example could be run even differnt namespaces were
> created.
>=20
> In order to have /dev/kni used simultaneously in different namespaces, ma=
king
> all of global variables as per network namespace variables.
>=20
> With regard to single kernel thread mode, there will be one kernel thread=
 for
> each of network namespace.
>=20
> Signed-off-by: Dex Chen <dex.chen@ruckuswireless.com>
> ---
>  lib/librte_eal/linuxapp/kni/kni_misc.c | 129
> ++++++++++++++++++++++-----------
>  1 file changed, 85 insertions(+), 44 deletions(-)
>=20
> diff --git a/lib/librte_eal/linuxapp/kni/kni_misc.c
> b/lib/librte_eal/linuxapp/kni/kni_misc.c
> index 2e9fa89..5ba8ab8 100644
> --- a/lib/librte_eal/linuxapp/kni/kni_misc.c
> +++ b/lib/librte_eal/linuxapp/kni/kni_misc.c
> @@ -28,6 +28,9 @@
>  #include <linux/pci.h>
>  #include <linux/kthread.h>
>  #include <linux/rwsem.h>
> +#include <linux/nsproxy.h>
> +#include <net/net_namespace.h>
> +#include <net/netns/generic.h>
>=20
>  #include <exec-env/rte_kni_common.h>
>  #include "kni_dev.h"
> @@ -90,18 +93,48 @@ static unsigned multiple_kthread_on =3D 0;
>=20
>  #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
>=20
> -static volatile unsigned long device_in_use; /* device in use flag */ -s=
tatic struct
> task_struct *kni_kthread;
> +static int kni_net_id;
>=20
> -/* kni list lock */
> -static DECLARE_RWSEM(kni_list_lock);
> +struct kni_net {
> +	volatile unsigned long device_in_use; /* device in use flag */
> +	struct task_struct *kni_kthread;
> +	struct rw_semaphore kni_list_lock;
> +	struct list_head kni_list_head;
> +};
> +
> +static __net_init int kni_init_net(struct net *net) {
> +	struct kni_net *knet =3D net_generic(net, kni_net_id);
>=20
> -/* kni list */
> -static struct list_head kni_list_head =3D LIST_HEAD_INIT(kni_list_head);
> +	/* Clear the bit of device in use */
> +	clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
> +
> +	init_rwsem(&knet->kni_list_lock);
> +	INIT_LIST_HEAD(&knet->kni_list_head);
> +
> +	return 0;
> +}
> +
> +static __net_exit void kni_exit_net(struct net *net) {
> +	/*
> +	 * Nothing to do here.
> +	 * Assuming all cleanup jobs were done in kni_release().
> +	 */
> +}
Agree with Stephen, kernel should handle it well.

> +
> +static struct pernet_operations kni_net_ops =3D {
> +	.init =3D kni_init_net,
> +	.exit =3D kni_exit_net,
> +	.id   =3D &kni_net_id,
> +	.size =3D sizeof(struct kni_net),
> +};
>=20
>  static int __init
>  kni_init(void)
>  {
> +	int rc;
> +
>  	KNI_PRINT("######## DPDK kni module loading ########\n");
>=20
>  	if (kni_parse_kthread_mode() < 0) {
> @@ -114,8 +147,9 @@ kni_init(void)
>  		return -EPERM;
>  	}
>=20
> -	/* Clear the bit of device in use */
> -	clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
> +	rc =3D register_pernet_subsys(&kni_net_ops);
> +	if (rc)
> +		goto out;
>=20
>  	/* Configure the lo mode according to the input parameter */
>  	kni_net_config_lo_mode(lo_mode);
> @@ -123,11 +157,16 @@ kni_init(void)
>  	KNI_PRINT("######## DPDK kni module loaded  ########\n");
>=20
>  	return 0;
> +
> +out:
> +	misc_deregister(&kni_misc);
> +	return rc;
>  }
>=20
>  static void __exit
>  kni_exit(void)
>  {
> +	unregister_pernet_subsys(&kni_net_ops);
Should above 'unregsiter' be moved after 'misc_deregister()'?

>  	misc_deregister(&kni_misc);
>  	KNI_PRINT("####### DPDK kni module unloaded  #######\n");  } @@
> -151,19 +190,22 @@ kni_parse_kthread_mode(void)  static int