[ovs-discuss] [abstract-netdev v2 03/21] netdev: Change netdev_get_etheraddr() to return an error code.

Ben Pfaff blp at nicira.com
Tue Jul 28 21:13:29 UTC 2009


To make the netdev code more portable, it needs to support returning error
codes from functions that don't have them.  This commit changes
netdev_get_etheraddr() to return an error code and updates all of its
callers.

(Currently netdev_get_etheraddr() won't ever return an error, but other
future implementations might.)
---
 lib/dhcp-client.c |   13 ++++++++-----
 lib/netdev.c      |   12 +++++++-----
 lib/netdev.h      |    2 +-
 ofproto/ofproto.c |    2 +-
 4 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/lib/dhcp-client.c b/lib/dhcp-client.c
index 06937ec..561562b 100644
--- a/lib/dhcp-client.c
+++ b/lib/dhcp-client.c
@@ -882,7 +882,7 @@ dhclient_msg_init(struct dhclient *cli, enum dhcp_msg_type type,
     msg->xid = cli->xid;
     msg->secs = cli->secs;
     msg->type = type;
-    memcpy(msg->chaddr, netdev_get_etheraddr(cli->netdev), ETH_ADDR_LEN);
+    netdev_get_etheraddr(cli->netdev, msg->chaddr);
 }
 
 /* If time goes backward this returns a large number, which makes it look like
@@ -905,9 +905,13 @@ timeout(struct dhclient *cli, unsigned int secs)
 static bool
 do_receive_msg(struct dhclient *cli, struct dhcp_msg *msg)
 {
+    uint8_t cli_mac[ETH_ADDR_LEN];
     struct ofpbuf b;
+    int mtu;
 
-    ofpbuf_init(&b, netdev_get_mtu(cli->netdev) + VLAN_ETH_HEADER_LEN);
+    mtu = netdev_get_mtu(cli->netdev);
+    ofpbuf_init(&b, mtu + VLAN_ETH_HEADER_LEN);
+    netdev_get_etheraddr(cli->netdev, cli_mac);
     for (; cli->received < 50; cli->received++) {
         const struct ip_header *ip;
         const struct dhcp_header *dhcp;
@@ -925,8 +929,7 @@ do_receive_msg(struct dhclient *cli, struct dhcp_msg *msg)
             || flow.nw_proto != IP_TYPE_UDP
             || flow.tp_dst != htons(68)
             || !(eth_addr_is_broadcast(flow.dl_dst)
-                 || eth_addr_equals(flow.dl_dst,
-                                    netdev_get_etheraddr(cli->netdev)))) {
+                 || eth_addr_equals(flow.dl_dst, cli_mac))) {
             continue;
         }
 
@@ -977,7 +980,7 @@ do_send_msg(struct dhclient *cli, const struct dhcp_msg *msg)
 
     dhcp_assemble(msg, &b);
 
-    memcpy(eh.eth_src, netdev_get_etheraddr(cli->netdev), ETH_ADDR_LEN);
+    netdev_get_etheraddr(cli->netdev, eh.eth_src);
     memcpy(eh.eth_dst, eth_addr_broadcast, ETH_ADDR_LEN);
     eh.eth_type = htons(ETH_TYPE_IP);
 
diff --git a/lib/netdev.c b/lib/netdev.c
index 7982235..7b5a330 100644
--- a/lib/netdev.c
+++ b/lib/netdev.c
@@ -688,12 +688,14 @@ netdev_nodev_set_etheraddr(const char *name, const uint8_t mac[ETH_ADDR_LEN])
     return set_etheraddr(name, ARPHRD_ETHER, mac);
 }
 
-/* Returns a pointer to 'netdev''s MAC address.  The caller must not modify or
- * free the returned buffer. */
-const uint8_t *
-netdev_get_etheraddr(const struct netdev *netdev)
+/* Retrieves 'netdev''s MAC address.  If successful, returns 0 and copies the
+ * the MAC address into 'mac'.  On failure, returns a positive errno value and
+ * clears 'mac' to all-zeros. */
+int
+netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
 {
-    return netdev->etheraddr;
+    memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
+    return 0;
 }
 
 /* Returns the name of the network device that 'netdev' represents,
diff --git a/lib/netdev.h b/lib/netdev.h
index 2106f6e..fca86a1 100644
--- a/lib/netdev.h
+++ b/lib/netdev.h
@@ -84,7 +84,7 @@ int netdev_drain(struct netdev *);
 int netdev_send(struct netdev *, const struct ofpbuf *);
 void netdev_send_wait(struct netdev *);
 int netdev_set_etheraddr(struct netdev *, const uint8_t mac[6]);
-const uint8_t *netdev_get_etheraddr(const struct netdev *);
+int netdev_get_etheraddr(const struct netdev *, uint8_t mac[6]);
 const char *netdev_get_name(const struct netdev *);
 int netdev_get_mtu(const struct netdev *);
 int netdev_get_features(struct netdev *,
diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
index 9cf46cc..dfb9545 100644
--- a/ofproto/ofproto.c
+++ b/ofproto/ofproto.c
@@ -1121,7 +1121,7 @@ make_ofport(const struct odp_port *odp_port)
     ofport = xmalloc(sizeof *ofport);
     ofport->netdev = netdev;
     ofport->opp.port_no = odp_port_to_ofp_port(odp_port->port);
-    memcpy(ofport->opp.hw_addr, netdev_get_etheraddr(netdev), ETH_ALEN);
+    netdev_get_etheraddr(netdev, ofport->opp.hw_addr);
     memcpy(ofport->opp.name, odp_port->devname,
            MIN(sizeof ofport->opp.name, sizeof odp_port->devname));
     ofport->opp.name[sizeof ofport->opp.name - 1] = '\0';
-- 
1.6.3.3





More information about the discuss mailing list