Skip to contentSkip to conversation

kni: fix device address set

PATCH Superseded Min Hu (Connor) 4 participants 18 messages Apr 6, 2022 – Jun 3, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kni: fix device address set

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")
---
 kernel/linux/kni/kni_misc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +8 -2
index 780187d8bf..046de0311f 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 	struct kni_net *knet = net_generic(net, kni_net_id);
 	int ret;
 	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
 	struct net_device *net_dev = NULL;
 	struct kni_dev *kni, *dev, *n;
 
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
 	/* if user has provided a valid mac address */
 	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		memcpy(mac_addr, dev_info.mac_addr, ETH_ALEN);
 	else
 		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE
+	memcpy(net_dev->dev_addr, mac_addr, ETH_ALEN);
+#else
+	dev_addr_set(net_dev, mac_addr);
+#endif
 
 	if (dev_info.mtu)
 		net_dev->mtu = dev_info.mtu;
Signature · 1 line
2.33.0
#1 lore raw

kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 407

Quoted 4 lines · On Wed, 6 Apr 2022 16:22:13 +0800 "Min Hu (Connor)" <[email protected]> wrote:
kernel/linux/kni/kni_misc.c +1 -1
 	/* if user has provided a valid mac address */
 	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		memcpy(mac_addr, dev_info.mac_addr, ETH_ALEN);

Minor enhancement. Could this use ether_addr_copy instead?

		ether_addr_copy(mac_addr, dev_info.mac_addr);
#2 lore raw

Hi, Stephen,
I think this is a good option, but the macro definition is like:
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) +#define ether_addr_copy(dst, src) memcpy(dst, src, ETH_ALEN) +#endif

@Ferry, why is it limited for "LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)" ?

Quoted 12 lines · Stephen Hemminger

kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 407

Quoted 4 lines · On Wed, 6 Apr 2022 16:22:13 +0800 "Min Hu (Connor)" <[email protected]> wrote:

/* if user has provided a valid mac address */ if (is_valid_ether_addr(dev_info.mac_addr))

kernel/linux/kni/kni_misc.c +1 -1
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		memcpy(mac_addr, dev_info.mac_addr, ETH_ALEN);

Minor enhancement. Could this use ether_addr_copy instead?

		ether_addr_copy(mac_addr, dev_info.mac_addr);

.

#3 lore raw

Review

Quoted 5 lines · On Thu, 7 Apr 2022 08:44:23 +0800 "Min Hu (Connor)" <[email protected]> wrote:

Hi, Stephen,
I think this is a good option, but the macro definition is like:

(quoted diff) +3 -0
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)
+#define ether_addr_copy(dst, src) memcpy(dst, src, ETH_ALEN)
+#endif

Minimal supported DPDK version is 4.4 now. So this is not a problem.

Note: 4.4 kernel reached end of support window in Febrary 2022. It is supported by the SLTS project but it would be unwise to use later DPDK on a kernel that is stuck being supported until 2036.

Apparently my patch to update it to current LTS 4.9 is sitting stuck in patchwork.

#4 lore raw

Hi,

Quoted 10 lines · Stephen Hemminger

Review

Quoted 5 lines · On Thu, 7 Apr 2022 08:44:23 +0800 "Min Hu (Connor)" <[email protected]> wrote:

Hi, Stephen,
I think this is a good option, but the macro definition is like:

(quoted diff) +3 -0
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)
+#define ether_addr_copy(dst, src) memcpy(dst, src, ETH_ALEN)
+#endif

Minimal supported DPDK version is 4.4 now. So this is not a problem.

I did not catch your meaning. I think this is a problem.
Acording to current definition, we can only use "ether_addr_copy" on linux platform of version 3.13.0(or below).

How about the situaction on other linux version, like 3.14.0(or upper)?

Quoted 9 lines

Note: 4.4 kernel reached end of support window in Febrary 2022. It is supported by the SLTS project but it would be unwise to use later DPDK on a kernel that is stuck being supported until 2036.

Apparently my patch to update it to current LTS 4.9 is sitting stuck in patchwork.
.

#5 lore raw
In reply to Min Hu (Connor)

07/04/2022 02:44, Min Hu (Connor):

Quoted 8 lines

Hi, Stephen,
I think this is a good option, but the macro definition is like:

(quoted diff) +3 -0
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)
+#define ether_addr_copy(dst, src) memcpy(dst, src, ETH_ALEN)
+#endif

@Ferry, why is it limited for "LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)" ?

I guess that's because it is defined in "new kernels" so we need a definition in DPDK for old kernels.

#6 lore raw

OK, I see, thanks Thomas.

Quoted 17 lines · Thomas Monjalon

07/04/2022 02:44, Min Hu (Connor):

Quoted 8 lines

Hi, Stephen,
I think this is a good option, but the macro definition is like:

(quoted diff) +3 -0
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)
+#define ether_addr_copy(dst, src) memcpy(dst, src, ETH_ALEN)
+#endif

@Ferry, why is it limited for "LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)" ?

I guess that's because it is defined in "new kernels" so we need a definition in DPDK for old kernels.

.

#7 lore raw

PATCH v2 1/1 kni: fix device address set

In reply to Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.

---
 kernel/linux/kni/kni_misc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +8 -2
index 780187d8bf..17b6c21a36 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 	struct kni_net *knet = net_generic(net, kni_net_id);
 	int ret;
 	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
 	struct net_device *net_dev = NULL;
 	struct kni_dev *kni, *dev, *n;
 
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
 	/* if user has provided a valid mac address */
 	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		ether_addr_copy(mac_addr, dev_info.mac_addr)
 	else
 		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE
+	ether_addr_copy(net_dev->dev_addr, mac_addr)
+#else
+	dev_addr_set(net_dev, mac_addr);
+#endif
 
 	if (dev_info.mtu)
 		net_dev->mtu = dev_info.mtu;
Signature · 1 line
2.33.0
#8 lore raw

Hi, all,
any one has any comments for this patch?

Quoted 77 lines · Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.

---
  kernel/linux/kni/kni_misc.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +8 -2
index 780187d8bf..17b6c21a36 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
  	struct kni_net *knet = net_generic(net, kni_net_id);
  	int ret;
  	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
  	struct net_device *net_dev = NULL;
  	struct kni_dev *kni, *dev, *n;
 
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
  	/* if user has provided a valid mac address */
  	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		ether_addr_copy(mac_addr, dev_info.mac_addr)
  	else
  		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE
+	ether_addr_copy(net_dev->dev_addr, mac_addr)
+#else
+	dev_addr_set(net_dev, mac_addr);
+#endif
 
  	if (dev_info.mtu)
  		net_dev->mtu = dev_info.mtu;
#9 lore raw

PATCH v3 1/1 kni: fix device address set

In reply to Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v3:
* fix compiling errors.

v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.

---
 kernel/linux/kni/kni_misc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +8 -2
index 780187d8bf..9c5d8c5576 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 	struct kni_net *knet = net_generic(net, kni_net_id);
 	int ret;
 	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
 	struct net_device *net_dev = NULL;
 	struct kni_dev *kni, *dev, *n;
 
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
 	/* if user has provided a valid mac address */
 	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		ether_addr_copy(mac_addr, dev_info.mac_addr);
 	else
 		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE
+	ether_addr_copy(net_dev->dev_addr, mac_addr)
+#else
+	dev_addr_set(net_dev, mac_addr);
+#endif
 
 	if (dev_info.mtu)
 		net_dev->mtu = dev_info.mtu;
Signature · 1 line
2.33.0
#10 lore raw
Quoted 63 lines · Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v3:
* fix compiling errors.

v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.

---
  kernel/linux/kni/kni_misc.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +1 -0
index 780187d8bf..9c5d8c5576 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
  	struct kni_net *knet = net_generic(net, kni_net_id);
  	int ret;
  	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
  	struct net_device *net_dev = NULL;
  	struct kni_dev *kni, *dev, *n;
 
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 

kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 412

Quoted 10 lines
kernel/linux/kni/kni_misc.c +4 -2
  	/* if user has provided a valid mac address */
  	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		ether_addr_copy(mac_addr, dev_info.mac_addr);
  	else
  		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE
+	ether_addr_copy(net_dev->dev_addr, mac_addr)

Since semicolon is missing above, it breaks build. Please, fix and send v4.

Quoted 6 lines
kernel/linux/kni/kni_misc.c +3 -0
+#else
+	dev_addr_set(net_dev, mac_addr);
+#endif
 
  	if (dev_info.mtu)
  		net_dev->mtu = dev_info.mtu;
#11 lore raw

Hi, Andrew,
v4 has been sent, thanks.

Quoted 84 lines · Andrew Rybchenko
Quoted 66 lines · Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v3:
* fix compiling errors.

v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.
---
  kernel/linux/kni/kni_misc.c | 10 ++++++++--

  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +2 -1
index 780187d8bf..9c5d8c5576 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
       struct kni_net *knet = net_generic(net, kni_net_id);
       int ret;
       struct rte_kni_device_info dev_info;
+    unsigned char mac_addr[ETH_ALEN];
       struct net_device *net_dev = NULL;
       struct kni_dev *kni, *dev, *n;
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t 
 ioctl_num,
       /* if user has provided a valid mac address */
       if (is_valid_ether_addr(dev_info.mac_addr))
-        memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+        ether_addr_copy(mac_addr, dev_info.mac_addr);
Quoted 6 lines

      else
          /* Generate random MAC address. */ -        eth_random_addr(net_dev->dev_addr); +        eth_random_addr(mac_addr);
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE +    ether_addr_copy(net_dev->dev_addr, mac_addr)

Since semicolon is missing above, it breaks build. Please, fix and send v4.

Quoted 5 lines
kernel/linux/kni/kni_misc.c +3 -0
+#else
+    dev_addr_set(net_dev, mac_addr);
+#endif

      if (dev_info.mtu)
          net_dev->mtu = dev_info.mtu;

.

#12 lore raw

PATCH v4 1/1 kni: fix device address set

In reply to Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v3,v4:
* fix compiling errors.

v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.

---
 kernel/linux/kni/kni_misc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +8 -2
index 780187d8bf..9c5d8c5576 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 	struct kni_net *knet = net_generic(net, kni_net_id);
 	int ret;
 	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
 	struct net_device *net_dev = NULL;
 	struct kni_dev *kni, *dev, *n;
 
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
 	/* if user has provided a valid mac address */
 	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		ether_addr_copy(mac_addr, dev_info.mac_addr);
 	else
 		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE
+	ether_addr_copy(net_dev->dev_addr, mac_addr);
+#else
+	dev_addr_set(net_dev, mac_addr);
+#endif
 
 	if (dev_info.mtu)
 		net_dev->mtu = dev_info.mtu;
Signature · 1 line
2.33.0
#13 lore raw
Quoted 62 lines · Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v3,v4:
* fix compiling errors.

v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.

---
  kernel/linux/kni/kni_misc.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +1 -0
index 780187d8bf..9c5d8c5576 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
  	struct kni_net *knet = net_generic(net, kni_net_id);
  	int ret;
  	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
  	struct net_device *net_dev = NULL;
  	struct kni_dev *kni, *dev, *n;
 

kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 410

Quoted 9 lines
kernel/linux/kni/kni_misc.c +2 -2
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
  	/* if user has provided a valid mac address */
  	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		ether_addr_copy(mac_addr, dev_info.mac_addr);
  	else
  		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);

Isn't it better to use function provided by the kernel, if available? I mean eth_hw_addr_random() used in [1]

[1] https://patches.dpdk.org/project/dpdk/patch/[email protected]/

kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 411

Quoted 1 line
kernel/linux/kni/kni_misc.c +1 -0
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE

I think usage of HAVE_* defines like in [1] look much better. It is easier to read and will limit scope if condition is finally more complicated.

[1] https://patches.dpdk.org/project/dpdk/patch/[email protected]/

Quoted 7 lines
kernel/linux/kni/kni_misc.c +4 -0
+	ether_addr_copy(net_dev->dev_addr, mac_addr);
+#else
+	dev_addr_set(net_dev, mac_addr);
+#endif
 
  	if (dev_info.mtu)
  		net_dev->mtu = dev_info.mtu;
#14 lore raw

Hi, Andrew,

Quoted 66 lines · Andrew Rybchenko
Quoted 65 lines · Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v3,v4:
* fix compiling errors.

v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.
---
  kernel/linux/kni/kni_misc.c | 10 ++++++++--

  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +1 -1
index 780187d8bf..9c5d8c5576 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
       struct kni_net *knet = net_generic(net, kni_net_id);
       int ret;
       struct rte_kni_device_info dev_info;
+    unsigned char mac_addr[ETH_ALEN];
       struct net_device *net_dev = NULL;
       struct kni_dev *kni, *dev, *n;
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t 
 ioctl_num,
       /* if user has provided a valid mac address */
       if (is_valid_ether_addr(dev_info.mac_addr))
-        memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
Quoted 8 lines
Quoted 5 lines
kernel/linux/kni/kni_misc.c +1 -0
+        ether_addr_copy(mac_addr, dev_info.mac_addr);

      else
          /* Generate random MAC address. */
-        eth_random_addr(net_dev->dev_addr);
+        eth_random_addr(mac_addr);

Isn't it better to use function provided by the kernel, if available? I mean eth_hw_addr_random() used in [1]

Well, here 'mac_addr' is my object, this object will be used in 'dev_addr_set' in the next code.
using eth_hw_addr_random will directly operate net-dev->dev_addr.

Quoted 9 lines

[1] https://patches.dpdk.org/project/dpdk/patch/[email protected]/

Quoted 1 line

+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE

I think usage of HAVE_* defines like in [1] look much better. It is easier to read and will limit scope if condition is finally more complicated.

Yes, fixed in v5.

Quoted 12 lines

[1] https://patches.dpdk.org/project/dpdk/patch/[email protected]/

Quoted 6 lines

+    ether_addr_copy(net_dev->dev_addr, mac_addr);

kernel/linux/kni/kni_misc.c +3 -0
+#else
+    dev_addr_set(net_dev, mac_addr);
+#endif

      if (dev_info.mtu)
          net_dev->mtu = dev_info.mtu;

.

#15 lore raw

PATCH v5 1/1 kni: fix device address set

In reply to Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v5:
* define 'HAVE_DEV_ADDR_SHADOW'.

v3,v4:
* fix compiling errors.

v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.

---
 kernel/linux/kni/compat.h   |  4 ++++
 kernel/linux/kni/kni_misc.c | 10 ++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)
kernel/linux/kni/compat.h +4 -0
index 664785674f..7ffca63bd4 100644
@@ -141,3 +141,7 @@
 #if KERNEL_VERSION(5, 9, 0) > LINUX_VERSION_CODE
 #define HAVE_TSK_IN_GUP
 #endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)
+#define HAVE_DEV_ADDR_SHADOW
+#endif
kernel/linux/kni/kni_misc.c +8 -2
index 780187d8bf..0470d349c5 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 	struct kni_net *knet = net_generic(net, kni_net_id);
 	int ret;
 	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
 	struct net_device *net_dev = NULL;
 	struct kni_dev *kni, *dev, *n;
 
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
 	/* if user has provided a valid mac address */
 	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		ether_addr_copy(mac_addr, dev_info.mac_addr);
 	else
 		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);
+#ifdef HAVE_DEV_ADDR_SHADOW
+	dev_addr_set(net_dev, mac_addr);
+#else
+	ether_addr_copy(net_dev->dev_addr, mac_addr);
+#endif
 
 	if (dev_info.mtu)
 		net_dev->mtu = dev_info.mtu;
Signature · 1 line
2.33.0
#16 lore raw
Quoted 78 lines · Min Hu (Connor)

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")

---
v5:
* define 'HAVE_DEV_ADDR_SHADOW'.

v3,v4:
* fix compiling errors.

v2:
* change 'memcpy' to 'ether_addr_copy' to copy device addr.

---
  kernel/linux/kni/compat.h   |  4 ++++
  kernel/linux/kni/kni_misc.c | 10 ++++++++--
  2 files changed, 12 insertions(+), 2 deletions(-)
kernel/linux/kni/compat.h +4 -0
index 664785674f..7ffca63bd4 100644
@@ -141,3 +141,7 @@
  #if KERNEL_VERSION(5, 9, 0) > LINUX_VERSION_CODE
  #define HAVE_TSK_IN_GUP
  #endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)
+#define HAVE_DEV_ADDR_SHADOW
+#endif
kernel/linux/kni/kni_misc.c +1 -0
index 780187d8bf..0470d349c5 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
  	struct kni_net *knet = net_generic(net, kni_net_id);
  	int ret;
  	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
  	struct net_device *net_dev = NULL;
  	struct kni_dev *kni, *dev, *n;
 

kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 410

Quoted 9 lines
kernel/linux/kni/kni_misc.c +2 -2
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
  	/* if user has provided a valid mac address */
  	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		ether_addr_copy(mac_addr, dev_info.mac_addr);
  	else
  		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);

The following point from Stephen is still not processed:

<quote>
This is still not the best way to handle this.
If you use eth_hw_addr_random() it will set the address assign type. Right now KNI is not doing this correctly. </quote>

Quoted 8 lines
kernel/linux/kni/kni_misc.c +5 -0
+#ifdef HAVE_DEV_ADDR_SHADOW
+	dev_addr_set(net_dev, mac_addr);
+#else
+	ether_addr_copy(net_dev->dev_addr, mac_addr);
+#endif
 
  	if (dev_info.mtu)
  		net_dev->mtu = dev_info.mtu;
#17 lore raw
In reply to Min Hu (Connor)
Quoted 60 lines · On Wed, 6 Apr 2022 16:22:13 +0800 "Min Hu (Connor)" <[email protected]> wrote:

Currently, run KNI APP When Kernel version is 5.17. When quit the APP, dmesg buffer get calltrace, info like:

[ 5965.847401] rte_kni: Creating kni...
[ 6225.627205] vEth0 (unregistered): Current addr:  70 fd 45 d0 72 a7 00..
[ 6225.640113] vEth0 (unregistered): Expected addr: 00 00 00 00 00 00 00..
[ 6225.653010] ------------[ cut here ]------------
[ 6225.657609] netdevice: vEth0 (unregistered): Incorrect netdev->dev_addr
[ 6225.832647] Call trace:
[ 6225.835083]  dev_addr_check+0xa0/0x144
[ 6225.838816]  dev_addr_flush+0x30/0x9c
[ 6225.842462]  free_netdev+0x8c/0x1e0
[ 6225.845939]  kni_release+0xc0/0x1d0 [rte_kni]
[ 6225.850281]  __fput+0x78/0x220
[ 6225.853327]  ____fput+0x1c/0x30
[ 6225.856455]  task_work_run+0x88/0xc0
[ 6225.860017]  do_exit+0x2fc/0x940
[ 6225.863232]  do_group_exit+0x40/0xac
[ 6225.866791]  get_signal+0x190/0x960
[ 6225.870265]  do_notify_resume+0x26c/0x1360
[ 6225.874346]  el0_interrupt+0x60/0xe0
[ 6225.877910]  __el0_irq_handler_common+0x18/0x24
[ 6225.882420]  el0t_64_irq_handler+0x14/0x20
[ 6225.886499]  el0t_64_irq+0x1a0/0x1a4
[ 6225.890059] ---[ end trace 0000000000000000 ]---
[ 6245.598157] rte_kni: Creating kni...

The reason is that 5.17 kernel introduce 'dev_addr_shadow' in function 'dev_addr_set' to ensure that netdev->dev_addr should only be modified via helpers('dev_addr_set'). 'dev_addr_check' will check if netdev->dev_addr is modified by other ways, like 'memcpy'.

More info could get by referring to kernel patch:
https://patchwork.kernel.org/project/netdevbpf/patch/
[email protected]/
https://www.spinics.net/lists/netdev/msg764992.html

Fixes ea6b39b5b847 ("kni: remove ethtool support")
---
 kernel/linux/kni/kni_misc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +1 -1
index 780187d8bf..046de0311f 100644
@@ -299,6 +299,7 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 	struct kni_net *knet = net_generic(net, kni_net_id);
 	int ret;
 	struct rte_kni_device_info dev_info;
+	unsigned char mac_addr[ETH_ALEN];
 	struct net_device *net_dev = NULL;
 	struct kni_dev *kni, *dev, *n;
 
@@ -403,10 +404,15 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
 	/* if user has provided a valid mac address */
 	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);

kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 415

Quoted 10 lines
kernel/linux/kni/kni_misc.c +7 -1
+		memcpy(mac_addr, dev_info.mac_addr, ETH_ALEN);
 	else
 		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr(mac_addr);
+#if KERNEL_VERSION(5, 17, 0) > LINUX_VERSION_CODE
+	memcpy(net_dev->dev_addr, mac_addr, ETH_ALEN);
+#else
+	dev_addr_set(net_dev, mac_addr);
+#endif

This is still not the best way to handle this.
If you use eth_hw_addr_random() it will set the address assign type. Right now KNI is not doing this correctly.

Also, the code is more readable if all the kernel compatibility stuff is handled in compat.h. There should be no kernel version checks in the functions.