Skip to content

Commit 6804d23

Browse files
author
Izabela Bakollari
committed
net: create a dummy net_device allocator
JIRA: https://issues.redhat.com/browse/RHEL-59092 commit c661050 Author: Breno Leitao <leitao@debian.org> Date: Mon Apr 22 05:38:56 2024 -0700 net: create a dummy net_device allocator It is impossible to use init_dummy_netdev together with alloc_netdev() as the 'setup' argument. This is because alloc_netdev() initializes some fields in the net_device structure, and later init_dummy_netdev() memzero them all. This causes some problems as reported here: https://lore.kernel.org/all/20240322082336.49f110cc@kernel.org/ Split the init_dummy_netdev() function in two. Create a new function called init_dummy_netdev_core() that does not memzero the net_device structure. Then have init_dummy_netdev() memzero-ing and calling init_dummy_netdev_core(), keeping the old behaviour. init_dummy_netdev_core() is the new function that could be called as an argument for alloc_netdev(). Also, create a helper to allocate and initialize dummy net devices, leveraging init_dummy_netdev_core() as the setup argument. This function basically simplify the allocation of dummy devices, by allocating and initializing it. Freeing the device continue to be done through free_netdev() Suggested-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Breno Leitao <leitao@debian.org> Reviewed-by: Ido Schimmel <idosch@nvidia.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Izabela Bakollari <ibakolla@redhat.com>
1 parent b5e1871 commit 6804d23

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

include/linux/netdevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4580,6 +4580,9 @@ static inline void netif_addr_unlock_bh(struct net_device *dev)
45804580

45814581
void ether_setup(struct net_device *dev);
45824582

4583+
/* Allocate dummy net_device */
4584+
struct net_device *alloc_netdev_dummy(int sizeof_priv);
4585+
45834586
/* Support for loadable net-drivers */
45844587
struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
45854588
unsigned char name_assign_type,

net/core/dev.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10149,25 +10149,12 @@ int register_netdevice(struct net_device *dev)
1014910149
}
1015010150
EXPORT_SYMBOL(register_netdevice);
1015110151

10152-
/**
10153-
* init_dummy_netdev - init a dummy network device for NAPI
10154-
* @dev: device to init
10155-
*
10156-
* This takes a network device structure and initializes the minimum
10157-
* amount of fields so it can be used to schedule NAPI polls without
10158-
* registering a full blown interface. This is to be used by drivers
10159-
* that need to tie several hardware interfaces to a single NAPI
10160-
* poll scheduler due to HW limitations.
10152+
/* Initialize the core of a dummy net device.
10153+
* This is useful if you are calling this function after alloc_netdev(),
10154+
* since it does not memset the net_device fields.
1016110155
*/
10162-
void init_dummy_netdev(struct net_device *dev)
10156+
static void init_dummy_netdev_core(struct net_device *dev)
1016310157
{
10164-
/* Clear everything. Note we don't initialize spinlocks
10165-
* as they aren't supposed to be taken by any of the
10166-
* NAPI code and this dummy netdev is supposed to be
10167-
* only ever used for NAPI polls
10168-
*/
10169-
memset(dev, 0, sizeof(struct net_device));
10170-
1017110158
/* make sure we BUG if trying to hit standard
1017210159
* register/unregister code path
1017310160
*/
@@ -10188,8 +10175,28 @@ void init_dummy_netdev(struct net_device *dev)
1018810175
* its refcount.
1018910176
*/
1019010177
}
10191-
EXPORT_SYMBOL_GPL(init_dummy_netdev);
1019210178

10179+
/**
10180+
* init_dummy_netdev - init a dummy network device for NAPI
10181+
* @dev: device to init
10182+
*
10183+
* This takes a network device structure and initializes the minimum
10184+
* amount of fields so it can be used to schedule NAPI polls without
10185+
* registering a full blown interface. This is to be used by drivers
10186+
* that need to tie several hardware interfaces to a single NAPI
10187+
* poll scheduler due to HW limitations.
10188+
*/
10189+
void init_dummy_netdev(struct net_device *dev)
10190+
{
10191+
/* Clear everything. Note we don't initialize spinlocks
10192+
* as they aren't supposed to be taken by any of the
10193+
* NAPI code and this dummy netdev is supposed to be
10194+
* only ever used for NAPI polls
10195+
*/
10196+
memset(dev, 0, sizeof(struct net_device));
10197+
init_dummy_netdev_core(dev);
10198+
}
10199+
EXPORT_SYMBOL_GPL(init_dummy_netdev);
1019310200

1019410201
/**
1019510202
* register_netdev - register a network device
@@ -10779,6 +10786,19 @@ void free_netdev(struct net_device *dev)
1077910786
}
1078010787
EXPORT_SYMBOL(free_netdev);
1078110788

10789+
/**
10790+
* alloc_netdev_dummy - Allocate and initialize a dummy net device.
10791+
* @sizeof_priv: size of private data to allocate space for
10792+
*
10793+
* Return: the allocated net_device on success, NULL otherwise
10794+
*/
10795+
struct net_device *alloc_netdev_dummy(int sizeof_priv)
10796+
{
10797+
return alloc_netdev(sizeof_priv, "dummy#", NET_NAME_UNKNOWN,
10798+
init_dummy_netdev_core);
10799+
}
10800+
EXPORT_SYMBOL_GPL(alloc_netdev_dummy);
10801+
1078210802
/**
1078310803
* synchronize_net - Synchronize with packet receive processing
1078410804
*

0 commit comments

Comments
 (0)