From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM03-CO1-obe.outbound.protection.outlook.com (mail-co1nam03on0056.outbound.protection.outlook.com [104.47.40.56]) by dpdk.org (Postfix) with ESMTP id 316DF1B243 for ; Tue, 26 Dec 2017 11:37:54 +0100 (CET) Received: from CY1PR03CA0002.namprd03.prod.outlook.com (2603:10b6:600::12) by SN2PR03MB2367.namprd03.prod.outlook.com (2603:10b6:804:e::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.366.8; Tue, 26 Dec 2017 10:37:52 +0000 Received: from BN1AFFO11FD028.protection.gbl (2a01:111:f400:7c10::163) by CY1PR03CA0002.outlook.office365.com (2603:10b6:600::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.366.8 via Frontend Transport; Tue, 26 Dec 2017 10:37:52 +0000 Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=nxp.com; intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=fail action=none header.from=nxp.com; Received-SPF: Fail (protection.outlook.com: domain of nxp.com does not designate 192.88.168.50 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.168.50; helo=tx30smr01.am.freescale.net; Received: from tx30smr01.am.freescale.net (192.88.168.50) by BN1AFFO11FD028.mail.protection.outlook.com (10.58.52.88) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.302.6 via Frontend Transport; Tue, 26 Dec 2017 10:37:33 +0000 Received: from bf-netperf1.ap.freescale.net (bf-netperf1.ap.freescale.net [10.232.134.28]) by tx30smr01.am.freescale.net (8.14.3/8.14.0) with ESMTP id vBQAbmDx009888; Tue, 26 Dec 2017 03:37:49 -0700 From: Hemant Agrawal To: CC: Date: Tue, 26 Dec 2017 16:06:46 +0530 Message-ID: <1514284608-9263-1-git-send-email-hemant.agrawal@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1512042367-6361-1-git-send-email-hemant.agrawal@nxp.com> References: <1512042367-6361-1-git-send-email-hemant.agrawal@nxp.com> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131587582533542605; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.168.50; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(336005)(346002)(376002)(39380400002)(39860400002)(396003)(2980300002)(1109001)(1110001)(3190300001)(339900001)(189003)(199004)(104016004)(2906002)(16586007)(316002)(6666003)(4326008)(53936002)(77096006)(86362001)(47776003)(85426001)(105606002)(50466002)(305945005)(356003)(48376002)(51416003)(2950100002)(6916009)(76176011)(498600001)(50226002)(68736007)(8936002)(97736004)(36756003)(106466001)(2351001)(81166006)(59450400001)(5660300001)(81156014)(8676002); DIR:OUT; SFP:1101; SCL:1; SRVR:SN2PR03MB2367; H:tx30smr01.am.freescale.net; FPR:; SPF:Fail; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BN1AFFO11FD028; 1:CSN+FeXLWuLb+e/jElYk54ehAleqexufnTigWPmmOfAP1qEw8NL/qPuo6zD9UvJWfAw29MMz/WQn5r/yAcU1yJaWOmXvoFRoQ1FAj60ECnIpEGuc6LPPTyBf++Xs5a6X MIME-Version: 1.0 Content-Type: text/plain X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 15ccfc9e-d1ec-4909-a2cd-08d54c4cac3a X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(4534020)(4628075)(201703131517081)(5600026)(4604075)(2017052603307); SRVR:SN2PR03MB2367; X-Microsoft-Exchange-Diagnostics: 1; SN2PR03MB2367; 3:FoBFRJnEBMo12tnZDFTunm8ODyZ8Nd5FCmg7sLWIbIDpZZ/WT37CQzIsT+Bcqs4otIObTzQ1IWX5YWjyxvV9mvcKe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jR70rE+4zLfLU3M/U4kMV7jnf928I3lpq7B4J8J3AtDkYc/wWUOcPM/vkMSQWHO6wDftbNae0d5b+A6hIlbOOhGLYcMulC+hFOpFBAUHkai+bAyi6MsW6ow9Cx/SUbD9VM3JtrEvij4J1NfEOtL0jg2cQwZStPPgS2X5yR1ptfAQ7ROgLV5+A1zpQtGIXTWN5xJO X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6095135)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(3231023)(944510075)(944921075)(946801075)(946901075)(10201501046)(6055026)(6096035)(20161123563025)(201703131430075)(201703131433075)(201703131448075)(201703161259150)(201703151042153)(20161123559100)(20161123561025)(20161123556025)(20161123565025)(201708071742011); SRVR:SN2PR03MB2367; BCL:0; PCL:0; RULEID:(100000803101)(100110400095)(400006); SRVR:SN2PR03MB2367; X-Forefront-PRVS: 053315510E X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; SN2PR03MB2367; 23:z6JH476MVvLkA4M1TujehYqR3N6SEI5XC8fw3d9ar?= =?us-ascii?Q?kKfq+J58ISR1+yhzHfqlZu1N5yupa/ab8c0KifGglPzYH8rnpkF1Rf3BOdDa?= =?us-ascii?Q?6YUEgkwIOeap4t1/E5SEGns3+4FXknaun9Tl5+fw3I88SiqnHROf+VpW4STZ?= =?us-ascii?Q?Md3sAEkxh5miIB3cqRmcMLgPw5k3Q0SfFjNbFE2C/ccpYrfB8HAbQ89OTMWw?= =?us-ascii?Q?qL8/XigA36JalzJuhfG4/r8mHGYh8YNZziyqb+f95hDwurk8FUNv/1zlMhI1?= =?us-ascii?Q?kSW6Te98kvXYm3ghYg/flIMqTHgJPODAiKdn/0ZVZzIWf0TemRLFmvT/T0PP?= =?us-ascii?Q?3GRlP7qIJeeH9iSNJSHePDyExJrWGzJJPqhhp5YqLzyXDDMubGAJNkZIPsSE?= =?us-ascii?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EvMLpXHKeo7tglSbw6pvukVooOmPVYUWT8eqq6RSlgOY6XLQ+FitInbAvbuFWM6cPwMqWny7o+y5Mn4GqopL3ltYH5c/kLTCL+2MrCqGpuzvwA7YxB1Hj3nlYfRP7h8TTzb6hTzl1ERHVX8J9AGy4g8ZNLv8i6JsABtcdNjdrdCLY4g7Q50AdkwmHNY37wByRGpZzGU4=; 5:ji+8yM+5L9VHhniS7I3ATskowky3lL35QohOjJpezXSvBnN66L5JoD5ikSO0SBK/dW6qWKL8khlqQifKwvxQSc0UQi9cvS3S5mevKVPKeB3485u6YwDHOiLqbSmYS54jok7fjBFjv0pmvGiuYSSpH6+IqkNMeL9UI6soo8IMvo8=; 24:/eYP2LXJRDnK3njOI+hoCnySPenJ5P14fGO8GYZTJjh5tWZGLS7NcgU2ltvH8e0yKYBXy9ZcAXV67kmWK2IfxAfFWP0YYh4TikLpBXlGPyQ=; 7:LvuoSvOXyqZlFjeVJGu0qkcARoaEVN3BwyaSLlyLDc2IcW0TmRpPTHb9iIXJpumHQvS4W4/gNJcSbqojq/6PK91WZUcUMTeFZxNagd9EYLh/ww0f+wNZIb0kSHAS7iGc2k5Jq5dWpmvylzTtClgF5B2zXhz6zTanm8NY5c1oRQ0YMIgzMzqZ/DtjmFfxEnVxA0TUwxdTO83yQ+zEp0TO9/2005NTHQNTB2FDjf7ySDsb4DJDFGiflP+cSDqD/gqH SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Dec 2017 10:37:33.1982 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 15ccfc9e-d1ec-4909-a2cd-08d54c4cac3a X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.168.50]; Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN2PR03MB2367 Subject: [dpdk-dev] [PATCH v2 1/3] kni: support for MAC addr change X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Dec 2017 10:37:55 -0000 This patch adds following: 1. Option to configure the mac address during create. Generate random address only if the user has not provided any valid address. 2. Inform usespace, if mac address is being changed in linux. 3. Implement default handling of mac address change in the corresponding ethernet device. Signed-off-by: Hemant Agrawal --- v2: * fix ABI breakage * reducing the code in documentation. * initialize ops.portid to UNIT16_MAX for vdev case doc/guides/sample_app_ug/kernel_nic_interface.rst | 26 +++++++++++++- examples/kni/main.c | 36 +++++++++++++++++++ .../linuxapp/eal/include/exec-env/rte_kni_common.h | 3 ++ lib/librte_eal/linuxapp/kni/kni_misc.c | 17 +++++---- lib/librte_eal/linuxapp/kni/kni_net.c | 14 +++++++- lib/librte_kni/rte_kni.c | 42 ++++++++++++++++++++-- lib/librte_kni/rte_kni.h | 5 +++ test/test/test_kni.c | 2 ++ 8 files changed, 134 insertions(+), 11 deletions(-) diff --git a/doc/guides/sample_app_ug/kernel_nic_interface.rst b/doc/guides/sample_app_ug/kernel_nic_interface.rst index e1ac415..2c143da 100644 --- a/doc/guides/sample_app_ug/kernel_nic_interface.rst +++ b/doc/guides/sample_app_ug/kernel_nic_interface.rst @@ -209,6 +209,12 @@ Dumping the network traffic: #tcpdump -i vEth0_0 +Change the MAC address: + +.. code-block:: console + + #ifconfig vEth0_0 hw ether 0C:01:02:03:04:08 + When the DPDK userspace application is closed, all the KNI devices are deleted from Linux*. Explanation @@ -269,11 +275,15 @@ The code for allocating the kernel NIC interfaces for a specific port is as foll conf.addr = dev_info.pci_dev->addr; conf.id = dev_info.pci_dev->id; + /* Get the interface default mac address */ + rte_eth_macaddr_get(port_id, (struct ether_addr *)&conf.mac_addr); + memset(&ops, 0, sizeof(ops)); ops.port_id = port_id; ops.change_mtu = kni_change_mtu; ops.config_network_if = kni_config_network_interface; + ops.config_mac_address = kni_config_mac_address; kni = rte_kni_alloc(pktmbuf_pool, &conf, &ops); } else @@ -502,13 +512,19 @@ Callbacks for Kernel Requests To execute specific PMD operations in user space requested by some Linux* commands, callbacks must be implemented and filled in the struct rte_kni_ops structure. -Currently, setting a new MTU and configuring the network interface (up/ down) are supported. +Currently, setting a new MTU, change in mac address and configuring the network interface(up/ down) +are supported. +Default implementation for following is available in rte_kni library. Application +may choose to not implement follwoing callbacks: + ``config_mac_address`` + .. code-block:: c static struct rte_kni_ops kni_ops = { .change_mtu = kni_change_mtu, .config_network_if = kni_config_network_interface, + .config_mac_address = kni_config_mac_address, }; /* Callback for request of changing MTU */ @@ -587,3 +603,11 @@ Currently, setting a new MTU and configuring the network interface (up/ down) ar RTE_LOG(ERR, APP, "Failed to start port %d\n", port_id); return ret; } + + /* Callback for request of configuring device mac address */ + + static int + kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[]) + { + ..... + } diff --git a/examples/kni/main.c b/examples/kni/main.c index 3f17385..1c251c2 100644 --- a/examples/kni/main.c +++ b/examples/kni/main.c @@ -163,6 +163,7 @@ static struct kni_interface_stats kni_stats[RTE_MAX_ETHPORTS]; static int kni_change_mtu(uint16_t port_id, unsigned int new_mtu); static int kni_config_network_interface(uint16_t port_id, uint8_t if_up); +static int kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[]); static rte_atomic32_t kni_stop = RTE_ATOMIC32_INIT(0); @@ -766,6 +767,37 @@ kni_config_network_interface(uint16_t port_id, uint8_t if_up) return ret; } +static void +print_ethaddr(const char *name, struct ether_addr *mac_addr) +{ + char buf[ETHER_ADDR_FMT_SIZE]; + ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, mac_addr); + RTE_LOG(INFO, APP, "\t%s%s\n", name, buf); +} + +/* Callback for request of configuring mac address */ +static int +kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[]) +{ + int ret = 0; + + if (port_id >= rte_eth_dev_count() || port_id >= RTE_MAX_ETHPORTS) { + RTE_LOG(ERR, APP, "Invalid port id %d\n", port_id); + return -EINVAL; + } + + RTE_LOG(INFO, APP, "Configure mac address of %d\n", port_id); + print_ethaddr("Address:", (struct ether_addr *)mac_addr); + + ret = rte_eth_dev_default_mac_addr_set(port_id, + (struct ether_addr *)mac_addr); + if (ret < 0) + RTE_LOG(ERR, APP, "Failed to config mac_addr for port %d\n", + port_id); + + return ret; +} + static int kni_alloc(uint16_t port_id) { @@ -809,11 +841,15 @@ kni_alloc(uint16_t port_id) conf.addr = dev_info.pci_dev->addr; conf.id = dev_info.pci_dev->id; } + /* Get the interface default mac address */ + rte_eth_macaddr_get(port_id, + (struct ether_addr *)&conf.mac_addr); memset(&ops, 0, sizeof(ops)); ops.port_id = port_id; ops.change_mtu = kni_change_mtu; ops.config_network_if = kni_config_network_interface; + ops.config_mac_address = kni_config_mac_address; kni = rte_kni_alloc(pktmbuf_pool, &conf, &ops); } else diff --git a/lib/librte_eal/linuxapp/eal/include/exec-env/rte_kni_common.h b/lib/librte_eal/linuxapp/eal/include/exec-env/rte_kni_common.h index 2ac879f..1a760de 100644 --- a/lib/librte_eal/linuxapp/eal/include/exec-env/rte_kni_common.h +++ b/lib/librte_eal/linuxapp/eal/include/exec-env/rte_kni_common.h @@ -80,6 +80,7 @@ enum rte_kni_req_id { RTE_KNI_REQ_UNKNOWN = 0, RTE_KNI_REQ_CHANGE_MTU, RTE_KNI_REQ_CFG_NETWORK_IF, + RTE_KNI_REQ_CHANGE_MAC_ADDR, RTE_KNI_REQ_MAX, }; @@ -92,6 +93,7 @@ struct rte_kni_request { union { uint32_t new_mtu; /**< New MTU */ uint8_t if_up; /**< 1: interface up, 0: interface down */ + uint8_t mac_addr[6]; /**< MAC address for interface */ }; int32_t result; /**< Result for processing request */ } __attribute__((__packed__)); @@ -168,6 +170,7 @@ struct rte_kni_device_info { /* mbuf size */ unsigned mbuf_size; + char mac_addr[6]; }; #define KNI_DEVICE "kni" diff --git a/lib/librte_eal/linuxapp/kni/kni_misc.c b/lib/librte_eal/linuxapp/kni/kni_misc.c index 7590f1f..b524d30 100644 --- a/lib/librte_eal/linuxapp/kni/kni_misc.c +++ b/lib/librte_eal/linuxapp/kni/kni_misc.c @@ -458,12 +458,17 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num, if (kni->lad_dev) ether_addr_copy(net_dev->dev_addr, kni->lad_dev->dev_addr); - else - /* - * Generate random mac address. eth_random_addr() is the newer - * version of generating mac address in linux kernel. - */ - random_ether_addr(net_dev->dev_addr); + else { + /* if user has provided a valid mac address */ + if (is_valid_ether_addr((unsigned char *)(dev_info.mac_addr))) + memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN); + else + /* + * Generate random mac address. eth_random_addr() is the + * newer version of generating mac address in kernel. + */ + random_ether_addr(net_dev->dev_addr); + } ret = register_netdev(net_dev); if (ret) { diff --git a/lib/librte_eal/linuxapp/kni/kni_net.c b/lib/librte_eal/linuxapp/kni/kni_net.c index db9f489..3e02ea1 100644 --- a/lib/librte_eal/linuxapp/kni/kni_net.c +++ b/lib/librte_eal/linuxapp/kni/kni_net.c @@ -668,12 +668,24 @@ kni_net_rebuild_header(struct sk_buff *skb) static int kni_net_set_mac(struct net_device *netdev, void *p) { + int ret; + struct rte_kni_request req; + struct kni_dev *kni; struct sockaddr *addr = p; + memset(&req, 0, sizeof(req)); + req.req_id = RTE_KNI_REQ_CHANGE_MAC_ADDR; + if (!is_valid_ether_addr((unsigned char *)(addr->sa_data))) return -EADDRNOTAVAIL; + + memcpy(req.mac_addr, addr->sa_data, netdev->addr_len); memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); - return 0; + + kni = netdev_priv(netdev); + ret = kni_net_process_request(kni, &req); + + return (ret == 0 ? req.result : ret); } #ifdef HAVE_CHANGE_CARRIER_CB diff --git a/lib/librte_kni/rte_kni.c b/lib/librte_kni/rte_kni.c index 8eca8c0..a5ee210 100644 --- a/lib/librte_kni/rte_kni.c +++ b/lib/librte_kni/rte_kni.c @@ -356,6 +356,8 @@ rte_kni_alloc(struct rte_mempool *pktmbuf_pool, memset(ctx, 0, sizeof(struct rte_kni)); if (ops) memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops)); + else + ctx->ops.port_id = UINT16_MAX; memset(&dev_info, 0, sizeof(dev_info)); dev_info.bus = conf->addr.bus; @@ -368,6 +370,8 @@ rte_kni_alloc(struct rte_mempool *pktmbuf_pool, dev_info.group_id = conf->group_id; dev_info.mbuf_size = conf->mbuf_size; + memcpy(dev_info.mac_addr, conf->mac_addr, ETHER_ADDR_LEN); + snprintf(ctx->name, RTE_KNI_NAMESIZE, "%s", intf_name); snprintf(dev_info.name, RTE_KNI_NAMESIZE, "%s", intf_name); @@ -528,6 +532,28 @@ rte_kni_release(struct rte_kni *kni) return 0; } +/* default callback for request of configuring device mac address */ +static int +kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[]) +{ + int ret = 0; + + if (port_id >= rte_eth_dev_count() || port_id >= RTE_MAX_ETHPORTS) { + RTE_LOG(ERR, KNI, "Invalid port id %d\n", port_id); + return -EINVAL; + } + + RTE_LOG(INFO, KNI, "Configure mac address of %d", port_id); + + ret = rte_eth_dev_default_mac_addr_set(port_id, + (struct ether_addr *)mac_addr); + if (ret < 0) + RTE_LOG(ERR, KNI, "Failed to config mac_addr for port %d\n", + port_id); + + return ret; +} + int rte_kni_handle_request(struct rte_kni *kni) { @@ -559,6 +585,14 @@ rte_kni_handle_request(struct rte_kni *kni) req->result = kni->ops.config_network_if(\ kni->ops.port_id, req->if_up); break; + case RTE_KNI_REQ_CHANGE_MAC_ADDR: /* Change MAC Address */ + if (kni->ops.config_mac_address) + req->result = kni->ops.config_mac_address( + kni->ops.port_id, req->mac_addr); + else if (kni->ops.port_id != UNIT16_MAX) + req->result = kni_config_mac_address( + kni->ops.port_id, req->mac_addr); + break; default: RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id); req->result = -EINVAL; @@ -707,7 +741,9 @@ kni_check_request_register(struct rte_kni_ops *ops) if( NULL == ops ) return KNI_REQ_NO_REGISTER; - if((NULL == ops->change_mtu) && (NULL == ops->config_network_if)) + if ((NULL == ops->change_mtu) + && (NULL == ops->config_network_if) + && (NULL == ops->config_mac_address)) return KNI_REQ_NO_REGISTER; return KNI_REQ_REGISTERED; @@ -746,8 +782,8 @@ rte_kni_unregister_handlers(struct rte_kni *kni) return -1; } - kni->ops.change_mtu = NULL; - kni->ops.config_network_if = NULL; + memset(&kni->ops, 0, sizeof(struct rte_kni_ops)); + return 0; } void diff --git a/lib/librte_kni/rte_kni.h b/lib/librte_kni/rte_kni.h index d43b5b2..9bdc9f3 100644 --- a/lib/librte_kni/rte_kni.h +++ b/lib/librte_kni/rte_kni.h @@ -49,6 +49,7 @@ #include #include #include +#include #include @@ -70,6 +71,9 @@ struct rte_kni_ops { /* Pointer to function of configuring network interface */ int (*config_network_if)(uint16_t port_id, uint8_t if_up); + + /* Pointer to function of configuring mac address */ + int (*config_mac_address)(uint16_t port_id, uint8_t mac_addr[]); }; /** @@ -90,6 +94,7 @@ struct rte_kni_conf { __extension__ uint8_t force_bind : 1; /* Flag to bind kernel thread */ + char mac_addr[ETHER_ADDR_LEN]; /* MAC address assigned to KNI */ }; /** diff --git a/test/test/test_kni.c b/test/test/test_kni.c index b956727..06448c9 100644 --- a/test/test/test_kni.c +++ b/test/test/test_kni.c @@ -103,6 +103,7 @@ static const struct rte_eth_conf port_conf = { static struct rte_kni_ops kni_ops = { .change_mtu = NULL, .config_network_if = NULL, + .config_mac_address = NULL, }; static unsigned lcore_master, lcore_ingress, lcore_egress; @@ -260,6 +261,7 @@ test_kni_register_handler_mp(void) struct rte_kni_ops ops = { .change_mtu = kni_change_mtu, .config_network_if = NULL, + .config_mac_address = NULL, }; if (!kni) { -- 2.7.4