Skip to content

Commit 1138b98

Browse files
paravmellanoxmstsirkin
authored andcommitted
vdpa_sim_net: Enable user to set mac address and mtu
Enable user to set the mac address and mtu so that each vdpa device can have its own user specified mac address and mtu. Now that user is enabled to set the mac address, remove the module parameter for same. And example of setting mac addr and mtu and view the configuration: $ vdpa mgmtdev show vdpasim_net: supported_classes net $ vdpa dev add name bar mgmtdev vdpasim_net mac 00:11:22:33:44:55 mtu 9000 $ vdpa dev config show bar: mac 00:11:22:33:44:55 link up link_announce false mtu 9000 Signed-off-by: Parav Pandit <[email protected]> Reviewed-by: Eli Cohen <[email protected]> Reviewed-by: Stefano Garzarella <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent d8ca2fa commit 1138b98

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

drivers/vdpa/vdpa_sim/vdpa_sim_net.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/vringh.h>
1717
#include <linux/vdpa.h>
1818
#include <uapi/linux/virtio_net.h>
19+
#include <uapi/linux/vdpa.h>
1920

2021
#include "vdpa_sim.h"
2122

@@ -29,12 +30,6 @@
2930

3031
#define VDPASIM_NET_VQ_NUM 2
3132

32-
static char *macaddr;
33-
module_param(macaddr, charp, 0);
34-
MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
35-
36-
static u8 macaddr_buf[ETH_ALEN];
37-
3833
static void vdpasim_net_work(struct work_struct *work)
3934
{
4035
struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
@@ -112,9 +107,21 @@ static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
112107
{
113108
struct virtio_net_config *net_config = config;
114109

115-
net_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
116110
net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
117-
memcpy(net_config->mac, macaddr_buf, ETH_ALEN);
111+
}
112+
113+
static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
114+
const struct vdpa_dev_set_config *config)
115+
{
116+
struct virtio_net_config *vio_config = vdpasim->config;
117+
118+
if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR))
119+
memcpy(vio_config->mac, config->net.mac, ETH_ALEN);
120+
if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MTU))
121+
vio_config->mtu = cpu_to_vdpasim16(vdpasim, config->net.mtu);
122+
else
123+
/* Setup default MTU to be 1500 */
124+
vio_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
118125
}
119126

120127
static void vdpasim_net_mgmtdev_release(struct device *dev)
@@ -147,6 +154,8 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
147154
if (IS_ERR(simdev))
148155
return PTR_ERR(simdev);
149156

157+
vdpasim_net_setup_config(simdev, config);
158+
150159
ret = _vdpa_register_device(&simdev->vdpa, VDPASIM_NET_VQ_NUM);
151160
if (ret)
152161
goto reg_err;
@@ -180,20 +189,14 @@ static struct vdpa_mgmt_dev mgmt_dev = {
180189
.device = &vdpasim_net_mgmtdev,
181190
.id_table = id_table,
182191
.ops = &vdpasim_net_mgmtdev_ops,
192+
.config_attr_mask = (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR |
193+
1 << VDPA_ATTR_DEV_NET_CFG_MTU),
183194
};
184195

185196
static int __init vdpasim_net_init(void)
186197
{
187198
int ret;
188199

189-
if (macaddr) {
190-
mac_pton(macaddr, macaddr_buf);
191-
if (!is_valid_ether_addr(macaddr_buf))
192-
return -EADDRNOTAVAIL;
193-
} else {
194-
eth_random_addr(macaddr_buf);
195-
}
196-
197200
ret = device_register(&vdpasim_net_mgmtdev);
198201
if (ret)
199202
return ret;

0 commit comments

Comments
 (0)