Skip to contentSkip to conversation

virtio-net: Allow setting the MAC address via set_config

RFC PATCH In review Alex Williamson 2 participants 3 messages Jan 7, 2009 – Jan 9, 2009 KVM

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/5 virtio-net: Allow setting the MAC address via set_config

virtio-net: Allow setting the MAC address via set_config

Rename get_config for simplicity

---

 hw/virtio-net.c |   21 +++++++++++++++++++--
 1 files changed, 19 insertions(+), 2 deletions(-)
hw/virtio-net.c +0 -1
index 2c41b3e..bfb7510 100644
@@ -38,7 +38,7 @@ static VirtIONet *to_virtio_net(VirtIODevice *vdev)
     return (VirtIONet *)vdev;
 }
 
-static void virtio_net_update_config(VirtIODevice *vdev, uint8_t

*config)
+static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)

 {
     VirtIONet *n = to_virtio_net(vdev);
     struct virtio_net_config netcfg;
@@ -48,6 +48,22 @@ static void virtio_net_update_config(VirtIODevice
*vdev, uint8_t *config)
     memcpy(config, &netcfg, sizeof(netcfg));

}

+static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
+{
+ VirtIONet *n = to_virtio_net(vdev);
+ struct virtio_net_config netcfg;
+
+ memcpy(&netcfg, config, sizeof(netcfg));
+
+ if (memcmp(netcfg.mac, n->mac, 6)) {
+ memcpy(n->mac, netcfg.mac, 6);
+ snprintf(n->vc->info_str, sizeof(n->vc->info_str),

+                 "virtio macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
+                 n->mac[0], n->mac[1], n->mac[2],
+                 n->mac[3], n->mac[4], n->mac[5]);

+ }
+}
+
static void virtio_net_set_link_status(VLANClientState *vc)

 {
     VirtIONet *n = vc->opaque;
@@ -326,7 +342,8 @@ PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd,
int devfn)
     if (!n)
         return NULL;
-    n->vdev.get_config = virtio_net_update_config;
+    n->vdev.get_config = virtio_net_get_config;
+    n->vdev.set_config = virtio_net_set_config;
     n->vdev.get_features = virtio_net_get_features;
     n->vdev.set_features = virtio_net_set_features;
     n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
#1 lore raw
Quoted 1 line · Alex Williamson

virtio-net: Allow setting the MAC address via set_config

This will basically never happen with QEMU, right?

We always set the MAC address - even if not supplied on the command line
- and the guest will never override that.

Quoted 13 lines

Rename get_config for simplicity

---

 hw/virtio-net.c |   21 +++++++++++++++++++--
 1 files changed, 19 insertions(+), 2 deletions(-)
hw/virtio-net.c +0 -0
index 2c41b3e..bfb7510 100644

hw/virtio-net.c static VirtIONet *to_virtio_net(VirtIODevice *vdev) · line 42

Quoted 6 lines
hw/virtio-net.c +1 -1
@@ -38,7 +38,7 @@ static VirtIONet *to_virtio_net(VirtIODevice *vdev)
     return (VirtIONet *)vdev;
 }
 
-static void virtio_net_update_config(VirtIODevice *vdev, uint8_t
 *config)
+static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)

Yay :-)

Quoted 12 lines
hw/virtio-net.c +0 -0
 {
     VirtIONet *n = to_virtio_net(vdev);
     struct virtio_net_config netcfg;
hw/virtio-net.c +3 -0
@@ -48,6 +48,22 @@ static void virtio_net_update_config(VirtIODevice
 *vdev, uint8_t *config)
     memcpy(config, &netcfg, sizeof(netcfg));
 }
 
+static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t
 *config)
+{
+    VirtIONet *n = to_virtio_net(vdev);

hw/virtio-net.c static void virtio_net_update_config(VirtIODevice · line 65

Quoted 10 lines
hw/virtio-net.c +10 -0
+    struct virtio_net_config netcfg;
+
+    memcpy(&netcfg, config, sizeof(netcfg));
+
+    if (memcmp(netcfg.mac, n->mac, 6)) {
+        memcpy(n->mac, netcfg.mac, 6);
+        snprintf(n->vc->info_str, sizeof(n->vc->info_str),
+                 "virtio macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
+                 n->mac[0], n->mac[1], n->mac[2],
+                 n->mac[3], n->mac[4], n->mac[5]);

There's qemu_format_nic_info_str() now.

Cheers,
Mark.

#2 lore raw
Quoted 7 lines · Mark McLoughlin
Quoted 1 line · Alex Williamson

virtio-net: Allow setting the MAC address via set_config

This will basically never happen with QEMU, right?

We always set the MAC address - even if not supplied on the command line
- and the guest will never override that.

Right, we always give the guest a MAC, but why will the guest never override it? That seems like a big assumption.

Quoted 8 lines
Quoted 8 lines

+static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)

(quoted diff) +6 -0
+{
+    VirtIONet *n = to_virtio_net(vdev);
+    struct virtio_net_config netcfg;
+
+    memcpy(&netcfg, config, sizeof(netcfg));
+
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+    if (memcmp(netcfg.mac, n->mac, 6)) {
+        memcpy(n->mac, netcfg.mac, 6);
+        snprintf(n->vc->info_str, sizeof(n->vc->info_str),
+                 "virtio macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
+                 n->mac[0], n->mac[1], n->mac[2],
+                 n->mac[3], n->mac[4], n->mac[5]);

There's qemu_format_nic_info_str() now.

Thanks, that will clean it up nicely. Thanks,

Alex

Signature · 1 line
Alex Williamson                             HP Open Source & Linux Org.