ovsdb-server: Refactoring and clean up remote status reporting.
[cascardo/ovs.git] / ofproto / in-band.c
index 1a08fcc..ea7e315 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@
 #include "packets.h"
 #include "poll-loop.h"
 #include "timeval.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(in_band);
 
@@ -67,23 +67,23 @@ enum {
 
 /* Track one remote IP and next hop information. */
 struct in_band_remote {
-    struct sockaddr_in remote_addr; /* IP address, in network byte order. */
-    uint8_t remote_mac[ETH_ADDR_LEN]; /* Next-hop MAC, all-zeros if unknown. */
-    uint8_t last_remote_mac[ETH_ADDR_LEN]; /* Previous nonzero next-hop MAC. */
-    struct netdev *remote_netdev; /* Device to send to next-hop MAC. */
+    struct sockaddr_in remote_addr;  /* IP address, in network byte order. */
+    struct eth_addr remote_mac;      /* Next-hop MAC, all-zeros if unknown. */
+    struct eth_addr last_remote_mac; /* Previous nonzero next-hop MAC. */
+    struct netdev *remote_netdev;    /* Device to send to next-hop MAC. */
 };
 
 /* What to do to an in_band_rule. */
 enum in_band_op {
     ADD,                       /* Add the rule to ofproto's flow table. */
-    DELETE                     /* Delete the rule from ofproto's flow table. */
+    DEL                        /* Delete the rule from ofproto's flow table. */
 };
 
 /* A rule to add to or delete from ofproto's flow table.  */
 struct in_band_rule {
     struct hmap_node hmap_node; /* In struct in_band's "rules" hmap. */
     struct match match;
-    unsigned int priority;
+    int priority;
     enum in_band_op op;
 };
 
@@ -98,7 +98,7 @@ struct in_band {
 
     /* Local information. */
     time_t next_local_refresh;       /* Refresh timer. */
-    uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
+    struct eth_addr local_mac;       /* Current MAC. */
     struct netdev *local_netdev;     /* Local port's network device. */
 
     /* Flow tracking. */
@@ -115,12 +115,13 @@ refresh_remote(struct in_band *ib, struct in_band_remote *r)
     int retval;
 
     /* Find the next-hop IP address. */
-    memset(r->remote_mac, 0, sizeof r->remote_mac);
+    r->remote_mac = eth_addr_zero;
     retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr,
                                  &next_hop_inaddr, &next_hop_dev);
     if (retval) {
-        VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
-                  IP_ARGS(r->remote_addr.sin_addr.s_addr), strerror(retval));
+        VLOG_WARN("%s: cannot find route for controller ("IP_FMT"): %s",
+                  ib->ofproto->name, IP_ARGS(r->remote_addr.sin_addr.s_addr),
+                  ovs_strerror(retval));
         return 1;
     }
     if (!next_hop_inaddr.s_addr) {
@@ -135,10 +136,11 @@ refresh_remote(struct in_band *ib, struct in_band_remote *r)
 
         retval = netdev_open(next_hop_dev, "system", &r->remote_netdev);
         if (retval) {
-            VLOG_WARN_RL(&rl, "cannot open netdev %s (next hop "
+            VLOG_WARN_RL(&rl, "%s: cannot open netdev %s (next hop "
                          "to controller "IP_FMT"): %s",
-                         next_hop_dev, IP_ARGS(r->remote_addr.sin_addr.s_addr),
-                         strerror(retval));
+                         ib->ofproto->name, next_hop_dev,
+                         IP_ARGS(r->remote_addr.sin_addr.s_addr),
+                         ovs_strerror(retval));
             free(next_hop_dev);
             return 1;
         }
@@ -147,10 +149,11 @@ refresh_remote(struct in_band *ib, struct in_band_remote *r)
 
     /* Look up the MAC address of the next-hop IP address. */
     retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr,
-                               r->remote_mac);
+                               &r->remote_mac);
     if (retval) {
-        VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
-                    IP_ARGS(next_hop_inaddr.s_addr), strerror(retval));
+        VLOG_DBG_RL(&rl, "%s: cannot look up remote MAC address ("IP_FMT"): %s",
+                    ib->ofproto->name, IP_ARGS(next_hop_inaddr.s_addr),
+                    ovs_strerror(retval));
     }
 
     /* If we don't have a MAC address, then refresh quickly, since we probably
@@ -172,11 +175,11 @@ refresh_remotes(struct in_band *ib)
     any_changes = false;
     ib->next_remote_refresh = TIME_MAX;
     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
-        uint8_t old_remote_mac[ETH_ADDR_LEN];
+        struct eth_addr old_remote_mac;
         time_t next_refresh;
 
         /* Save old MAC. */
-        memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
+        old_remote_mac = r->remote_mac;
 
         /* Refresh remote information. */
         next_refresh = refresh_remote(ib, r) + time_now();
@@ -187,11 +190,12 @@ refresh_remotes(struct in_band *ib)
             any_changes = true;
             if (!eth_addr_is_zero(r->remote_mac)
                 && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) {
-                VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT
+                VLOG_DBG("%s: remote MAC address changed from "ETH_ADDR_FMT
                          " to "ETH_ADDR_FMT,
+                         ib->ofproto->name,
                          ETH_ADDR_ARGS(r->last_remote_mac),
                          ETH_ADDR_ARGS(r->remote_mac));
-                memcpy(r->last_remote_mac, r->remote_mac, ETH_ADDR_LEN);
+                r->last_remote_mac = r->remote_mac;
             }
         }
     }
@@ -204,7 +208,7 @@ refresh_remotes(struct in_band *ib)
 static bool
 refresh_local(struct in_band *ib)
 {
-    uint8_t ea[ETH_ADDR_LEN];
+    struct eth_addr ea;
     time_t now;
 
     now = time_now();
@@ -213,75 +217,37 @@ refresh_local(struct in_band *ib)
     }
     ib->next_local_refresh = now + 1;
 
-    if (netdev_get_etheraddr(ib->local_netdev, ea)
+    if (netdev_get_etheraddr(ib->local_netdev, &ea)
         || eth_addr_equals(ea, ib->local_mac)) {
         return false;
     }
 
-    memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
+    ib->local_mac = ea;
     return true;
 }
 
-/* Returns true if 'packet' should be sent to the local port regardless
- * of the flow table. */
+/* Returns true if packets in 'flow' should be directed to the local port.
+ * (This keeps the flow table from preventing DHCP replies from being seen by
+ * the local port.) */
 bool
-in_band_msg_in_hook(struct in_band *in_band, const struct flow *flow,
-                    const struct ofpbuf *packet)
+in_band_must_output_to_local_port(const struct flow *flow)
 {
-    /* Regardless of how the flow table is configured, we want to be
-     * able to see replies to our DHCP requests. */
-    if (flow->dl_type == htons(ETH_TYPE_IP)
+    return (flow->dl_type == htons(ETH_TYPE_IP)
             && flow->nw_proto == IPPROTO_UDP
             && flow->tp_src == htons(DHCP_SERVER_PORT)
-            && flow->tp_dst == htons(DHCP_CLIENT_PORT)
-            && packet->l7) {
-        struct dhcp_header *dhcp;
-
-        dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
-                         sizeof *dhcp);
-        if (!dhcp) {
-            return false;
-        }
-
-        refresh_local(in_band);
-        if (!eth_addr_is_zero(in_band->local_mac)
-            && eth_addr_equals(dhcp->chaddr, in_band->local_mac)) {
-            return true;
-        }
-    }
-
-    return false;
+            && flow->tp_dst == htons(DHCP_CLIENT_PORT));
 }
 
-/* Returns true if the rule that would match 'flow' with 'actions' is
- * allowed to be set up in the datapath. */
-bool
-in_band_rule_check(const struct flow *flow, uint32_t local_odp_port,
-                   const struct nlattr *actions, size_t actions_len)
+/* Returns the number of in-band rules currently installed in the flow
+ * table. */
+int
+in_band_count_rules(const struct in_band *ib)
 {
-    /* Don't allow flows that would prevent DHCP replies from being seen
-     * by the local port. */
-    if (flow->dl_type == htons(ETH_TYPE_IP)
-            && flow->nw_proto == IPPROTO_UDP
-            && flow->tp_src == htons(DHCP_SERVER_PORT)
-            && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
-        const struct nlattr *a;
-        unsigned int left;
-
-        NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
-            if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
-                && nl_attr_get_u32(a) == local_odp_port) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    return true;
+    return hmap_count(&ib->rules);
 }
 
 static void
-add_rule(struct in_band *ib, const struct match *match, unsigned int priority)
+add_rule(struct in_band *ib, const struct match *match, int priority)
 {
     uint32_t hash = match_hash(match, 0);
     struct in_band_rule *rule;
@@ -310,7 +276,7 @@ update_rules(struct in_band *ib)
     /* Mark all the existing rules for deletion.  (Afterward we will re-add any
      * rules that are still valid.) */
     HMAP_FOR_EACH (ib_rule, hmap_node, &ib->rules) {
-        ib_rule->op = DELETE;
+        ib_rule->op = DEL;
     }
 
     if (ib->n_remotes && !eth_addr_is_zero(ib->local_mac)) {
@@ -340,23 +306,21 @@ update_rules(struct in_band *ib)
     }
 
     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
-        const uint8_t *remote_mac = r->remote_mac;
-
-        if (eth_addr_is_zero(remote_mac)) {
+        if (eth_addr_is_zero(r->remote_mac)) {
             continue;
         }
 
         /* (d) Allow ARP replies to the next hop's MAC address. */
         match_init_catchall(&match);
         match_set_dl_type(&match, htons(ETH_TYPE_ARP));
-        match_set_dl_dst(&match, remote_mac);
+        match_set_dl_dst(&match, r->remote_mac);
         match_set_nw_proto(&match, ARP_OP_REPLY);
         add_rule(ib, &match, IBR_TO_NEXT_HOP_ARP);
 
         /* (e) Allow ARP requests from the next hop's MAC address. */
         match_init_catchall(&match);
         match_set_dl_type(&match, htons(ETH_TYPE_ARP));
-        match_set_dl_src(&match, remote_mac);
+        match_set_dl_src(&match, r->remote_mac);
         match_set_nw_proto(&match, ARP_OP_REQUEST);
         add_rule(ib, &match, IBR_FROM_NEXT_HOP_ARP);
     }
@@ -430,14 +394,10 @@ in_band_run(struct in_band *ib)
                              ofpacts.data, ofpacts.size);
             break;
 
-        case DELETE:
-            if (ofproto_delete_flow(ib->ofproto,
-                                    &rule->match, rule->priority)) {
-                /* ofproto doesn't have the rule anymore so there's no reason
-                 * for us to track it any longer. */
-                hmap_remove(&ib->rules, &rule->hmap_node);
-                free(rule);
-            }
+        case DEL:
+            ofproto_delete_flow(ib->ofproto, &rule->match, rule->priority);
+            hmap_remove(&ib->rules, &rule->hmap_node);
+            free(rule);
             break;
         }
     }
@@ -466,8 +426,9 @@ in_band_create(struct ofproto *ofproto, const char *local_name,
     *in_bandp = NULL;
     error = netdev_open(local_name, "internal", &local_netdev);
     if (error) {
-        VLOG_ERR("failed to initialize in-band control: cannot open "
-                 "datapath local port %s (%s)", local_name, strerror(error));
+        VLOG_ERR("%s: failed to initialize in-band control: cannot open "
+                 "datapath local port %s (%s)", ofproto->name,
+                 local_name, ovs_strerror(error));
         return error;
     }