- /* Query the datapath for port information. */
- error = dpif_port_query_by_name(p->dpif, devname, &dpif_port);
-
- /* Find the old ofport. */
- old_ofport = shash_find_data(&p->port_by_name, devname);
- if (!error) {
- if (!old_ofport) {
- /* There's no port named 'devname' but there might be a port with
- * the same port number. This could happen if a port is deleted
- * and then a new one added in its place very quickly, or if a port
- * is renamed. In the former case we want to send an OFPPR_DELETE
- * and an OFPPR_ADD, and in the latter case we want to send a
- * single OFPPR_MODIFY. We can distinguish the cases by comparing
- * the old port's ifindex against the new port, or perhaps less
- * reliably but more portably by comparing the old port's MAC
- * against the new port's MAC. However, this code isn't that smart
- * and always sends an OFPPR_MODIFY (XXX). */
- old_ofport = get_port(p, dpif_port.port_no);
- }
- } else if (error != ENOENT && error != ENODEV) {
- VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error "
- "%s", strerror(error));
- goto exit;
- }
-
- /* Create a new ofport. */
- new_ofport = !error ? make_ofport(&dpif_port) : NULL;
-
- /* Eliminate a few pathological cases. */
- if (!old_ofport && !new_ofport) {
- goto exit;
- } else if (old_ofport && new_ofport) {
- /* Most of the 'config' bits are OpenFlow soft state, but
- * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the
- * OpenFlow bits from old_ofport. (make_ofport() only sets
- * OFPPC_PORT_DOWN and leaves the other bits 0.) */
- new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN;
-
- if (ofport_equal(old_ofport, new_ofport)) {
- /* False alarm--no change. */
- ofport_free(new_ofport);
- goto exit;
+ /* Fetch 'name''s location and properties from the datapath. */
+ netdev = (!dpif_port_query_by_name(ofproto->dpif, name, &dpif_port)
+ ? ofport_open(&dpif_port, &opp)
+ : NULL);
+ if (netdev) {
+ port = get_port(ofproto, dpif_port.port_no);
+ if (port && !strcmp(netdev_get_name(port->netdev), name)) {
+ /* 'name' hasn't changed location. Any properties changed? */
+ if (!ofport_equal(&port->opp, &opp)) {
+ ofport_modified(ofproto, port, netdev, &opp);
+ } else {
+ netdev_close(netdev);
+ }
+ } else {
+ /* If 'port' is nonnull then its name differs from 'name' and thus
+ * we should delete it. If we think there's a port named 'name'
+ * then its port number must be wrong now so delete it too. */
+ if (port) {
+ ofport_remove(ofproto, port);
+ }
+ ofport_remove_with_name(ofproto, name);
+ ofport_install(ofproto, netdev, &opp);