2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-17 14:28:02 +00:00

dpif-netdev: Make port numbers predictable for dummy dpif, for unit tests.

The unit tests feed a lot of flows through the ofproto-dpif "trace"
command, which means that they need to know the port numbers of the ports
that they create.  Until now, they've had to actually query those port
numbers from the database, which is a bit of unnecessary overhead for unit
tests.

This commit makes dummy dpif port numbers predictable: if the name of a
port contains a number, then the dummy dpif uses that number, if it is
valid and available, as the port number.

This commit also simplifies the unit tests that previously queried port
numbers to depend on the new behavior.

Signed-off-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
Ben Pfaff
2012-01-12 15:23:23 -08:00
parent a1893da130
commit 247527db0f
2 changed files with 179 additions and 257 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2010, 2011 Nicira Networks.
* Copyright (c) 2009, 2010, 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -370,6 +370,39 @@ do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
return 0;
}
static int
choose_port(struct dpif *dpif, struct netdev *netdev)
{
struct dp_netdev *dp = get_dp_netdev(dpif);
int port_no;
if (dpif->dpif_class == &dpif_dummy_class) {
/* If the port name contains a number, try to assign that port number.
* This can make writing unit tests easier because port numbers are
* predictable. */
const char *p;
for (p = netdev_get_name(netdev); *p != '\0'; p++) {
if (isdigit((unsigned char) *p)) {
port_no = strtol(p, NULL, 10);
if (port_no > 0 && port_no < MAX_PORTS
&& !dp->ports[port_no]) {
return port_no;
}
break;
}
}
}
for (port_no = 0; port_no < MAX_PORTS; port_no++) {
if (!dp->ports[port_no]) {
return port_no;
}
}
return -1;
}
static int
dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
uint16_t *port_nop)
@@ -377,12 +410,11 @@ dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
struct dp_netdev *dp = get_dp_netdev(dpif);
int port_no;
for (port_no = 0; port_no < MAX_PORTS; port_no++) {
if (!dp->ports[port_no]) {
*port_nop = port_no;
return do_add_port(dp, netdev_get_name(netdev),
netdev_get_type(netdev), port_no);
}
port_no = choose_port(dpif, netdev);
if (port_no >= 0) {
*port_nop = port_no;
return do_add_port(dp, netdev_get_name(netdev),
netdev_get_type(netdev), port_no);
}
return EFBIG;
}