Commit 1a609520277c65a2b00bbbcca360f482e257d64d

Authored by Jan Kiszka
Committed by Anthony Liguori
1 parent 28432466

net: Provide VLAN client lookup helper

Introduce qemu_find_vlan_client_by_name for VLANClientState lookup based
on VLAN ID and client name. This is useful for monitor commands.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 3 changed files with 34 additions and 14 deletions
hw/xen_nic.c
... ... @@ -301,7 +301,7 @@ static int net_init(struct XenDevice *xendev)
301 301 if (netdev->mac == NULL)
302 302 return -1;
303 303  
304   - vlan = qemu_find_vlan(netdev->xendev.dev);
  304 + vlan = qemu_find_vlan(netdev->xendev.dev, 1);
305 305 netdev->vs = qemu_new_vlan_client(vlan, "xen", NULL,
306 306 net_rx_ok, net_rx_packet, NULL,
307 307 NULL, netdev);
... ...
... ... @@ -389,6 +389,32 @@ VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
389 389 return NULL;
390 390 }
391 391  
  392 +static VLANClientState *
  393 +qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
  394 + const char *client_str)
  395 +{
  396 + VLANState *vlan;
  397 + VLANClientState *vc;
  398 +
  399 + vlan = qemu_find_vlan(vlan_id, 0);
  400 + if (!vlan) {
  401 + monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
  402 + return NULL;
  403 + }
  404 +
  405 + for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
  406 + if (!strcmp(vc->name, client_str)) {
  407 + break;
  408 + }
  409 + }
  410 + if (!vc) {
  411 + monitor_printf(mon, "can't find device %s on VLAN %d\n",
  412 + client_str, vlan_id);
  413 + }
  414 +
  415 + return vc;
  416 +}
  417 +
392 418 int qemu_can_send_packet(VLANClientState *sender)
393 419 {
394 420 VLANState *vlan = sender->vlan;
... ... @@ -2255,13 +2281,16 @@ static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
2255 2281 }
2256 2282  
2257 2283 /* find or alloc a new VLAN */
2258   -VLANState *qemu_find_vlan(int id)
  2284 +VLANState *qemu_find_vlan(int id, int allocate)
2259 2285 {
2260 2286 VLANState **pvlan, *vlan;
2261 2287 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2262 2288 if (vlan->id == id)
2263 2289 return vlan;
2264 2290 }
  2291 + if (!allocate) {
  2292 + return NULL;
  2293 + }
2265 2294 vlan = qemu_mallocz(sizeof(VLANState));
2266 2295 vlan->id = id;
2267 2296 vlan->next = NULL;
... ... @@ -2327,7 +2356,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
2327 2356 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
2328 2357 vlan_id = strtol(buf, NULL, 0);
2329 2358 }
2330   - vlan = qemu_find_vlan(vlan_id);
  2359 + vlan = qemu_find_vlan(vlan_id, 1);
2331 2360  
2332 2361 if (get_param_value(buf, sizeof(buf), "name", p)) {
2333 2362 name = qemu_strdup(buf);
... ... @@ -2740,19 +2769,10 @@ void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2740 2769  
2741 2770 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
2742 2771 {
2743   - VLANState *vlan;
2744 2772 VLANClientState *vc;
2745 2773  
2746   - vlan = qemu_find_vlan(vlan_id);
2747   -
2748   - for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2749   - if (!strcmp(vc->name, device)) {
2750   - break;
2751   - }
2752   - }
2753   -
  2774 + vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
2754 2775 if (!vc) {
2755   - monitor_printf(mon, "can't find device %s\n", device);
2756 2776 return;
2757 2777 }
2758 2778 if (!net_host_check_device(vc->model)) {
... ...
... ... @@ -51,7 +51,7 @@ struct VLANState {
51 51 int delivering;
52 52 };
53 53  
54   -VLANState *qemu_find_vlan(int id);
  54 +VLANState *qemu_find_vlan(int id, int allocate);
55 55 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
56 56 const char *model,
57 57 const char *name,
... ...