Rename utility functions to avoid partner namespace conflicts.
[openvswitch] / secchan / secchan.c
index e21454029e1ea353e585e7deb50aff9fbda18f24..9508369aa0b3bcabc4d6c66af8c31dc75f43dd2c 100644 (file)
 #include <time.h>
 #include <unistd.h>
 
-#include "buffer.h"
 #include "command-line.h"
 #include "compiler.h"
 #include "daemon.h"
-#include "dhcp.h"
 #include "dhcp-client.h"
+#include "dhcp.h"
 #include "dynamic-string.h"
 #include "fault.h"
 #include "flow.h"
@@ -58,6 +57,7 @@
 #include "list.h"
 #include "mac-learning.h"
 #include "netdev.h"
+#include "ofpbuf.h"
 #include "openflow.h"
 #include "packets.h"
 #include "poll-loop.h"
@@ -111,7 +111,7 @@ struct settings {
 
 struct half {
     struct rconn *rconn;
-    struct buffer *rxbuf;
+    struct ofpbuf *rxbuf;
     int n_txq;                  /* No. of packets queued for tx on 'rconn'. */
 };
 
@@ -219,10 +219,10 @@ main(int argc, char *argv[])
         struct vconn *listener;
         retval = vconn_open(name, &listener);
         if (retval && retval != EAGAIN) {
-            fatal(retval, "opening %s", name);
+            ofp_fatal(retval, "opening %s", name);
         }
         if (!vconn_is_passive(listener)) {
-            fatal(0, "%s is not a passive vconn", name);
+            ofp_fatal(0, "%s is not a passive vconn", name);
         }
         listeners[n_listeners++] = listener;
     }
@@ -236,9 +236,10 @@ main(int argc, char *argv[])
     /* Start listening for vlogconf requests. */
     retval = vlog_server_listen(NULL, NULL);
     if (retval) {
-        fatal(retval, "Could not listen for vlog connections");
+        ofp_fatal(retval, "Could not listen for vlog connections");
     }
 
+    die_if_already_running();
     daemonize();
 
     VLOG_WARN("OpenFlow reference implementation version %s", VERSION);
@@ -255,7 +256,7 @@ main(int argc, char *argv[])
     if (s.controller_name) {
         retval = rconn_connect(remote_rconn, s.controller_name);
         if (retval == EAFNOSUPPORT) {
-            fatal(0, "No support for %s vconn", s.controller_name);
+            ofp_fatal(0, "No support for %s vconn", s.controller_name);
         }
     }
     switch_status_register_category(switch_status, "remote",
@@ -429,7 +430,7 @@ relay_run(struct relay *r, const struct hook hooks[], size_t n_hooks)
                     const struct hook *h;
                     for (h = hooks; h < &hooks[n_hooks]; h++) {
                         if (h->packet_cb(r, i, h->aux)) {
-                            buffer_delete(this->rxbuf);
+                            ofpbuf_delete(this->rxbuf);
                             this->rxbuf = NULL;
                             progress = true;
                             break;
@@ -445,7 +446,7 @@ relay_run(struct relay *r, const struct hook hooks[], size_t n_hooks)
                     if (!retval) {
                         progress = true;
                     } else {
-                        buffer_delete(this->rxbuf);
+                        ofpbuf_delete(this->rxbuf);
                     }
                     this->rxbuf = NULL;
                 }
@@ -491,7 +492,7 @@ relay_destroy(struct relay *r)
     for (i = 0; i < 2; i++) {
         struct half *this = &r->halves[i];
         rconn_destroy(this->rconn);
-        buffer_delete(this->rxbuf);
+        ofpbuf_delete(this->rxbuf);
     }
     free(r);
 }
@@ -508,7 +509,7 @@ struct in_band_data {
 };
 
 static void
-queue_tx(struct rconn *rc, struct in_band_data *in_band, struct buffer *b)
+queue_tx(struct rconn *rc, struct in_band_data *in_band, struct ofpbuf *b)
 {
     rconn_send_with_limit(rc, b, &in_band->n_queued, 10);
 }
@@ -575,11 +576,11 @@ in_band_packet_cb(struct relay *r, int half, void *in_band_)
 {
     struct in_band_data *in_band = in_band_;
     struct rconn *rc = r->halves[HALF_LOCAL].rconn;
-    struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
+    struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
     struct ofp_packet_in *opi;
     struct ofp_header *oh;
     size_t pkt_ofs, pkt_len;
-    struct buffer pkt;
+    struct ofpbuf pkt;
     struct flow flow;
     uint16_t in_port, out_port;
     const uint8_t *controller_mac;
@@ -624,18 +625,30 @@ in_band_packet_cb(struct relay *r, int half, void *in_band_)
                && is_controller_mac(flow.dl_src, in_band)) {
         /* ARP sent by controller. */
         out_port = OFPP_FLOOD;
-    } else if (is_controller_mac(flow.dl_dst, in_band)
-               && in_port == mac_learning_lookup(in_band->ml,
-                                                 controller_mac)) {
-        /* Drop controller traffic that arrives on the controller port. */
-        queue_tx(rc, in_band, make_add_flow(&flow, ntohl(opi->buffer_id),
-                                            in_band->s->max_idle, 0));
-        return true;
+    } else if (is_controller_mac(flow.dl_dst, in_band)) {
+        if (mac_learning_learn(in_band->ml, flow.dl_src, in_port)) {
+            VLOG_DBG_RL(&vrl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
+                        ETH_ADDR_ARGS(flow.dl_src), in_port);
+        }
+
+        out_port = mac_learning_lookup(in_band->ml, controller_mac);
+        if (in_port != out_port) {
+            return false;
+        }
+
+        /* This is controller traffic that arrived on the controller port.
+         * It will get dropped below. */
+    } else if (is_controller_mac(flow.dl_src, in_band)) {
+        out_port = mac_learning_lookup(in_band->ml, flow.dl_dst);
     } else {
         return false;
     }
 
-    if (out_port != OFPP_FLOOD) {
+    if (in_port == out_port) {
+        /* The input and output port match.  Set up a flow to drop packets. */
+        queue_tx(rc, in_band, make_add_flow(&flow, ntohl(opi->buffer_id),
+                                          in_band->s->max_idle, 0));
+    } else if (out_port != OFPP_FLOOD) {
         /* The output port is known, so add a new flow. */
         queue_tx(rc, in_band,
                  make_add_simple_flow(&flow, ntohl(opi->buffer_id),
@@ -649,7 +662,7 @@ in_band_packet_cb(struct relay *r, int half, void *in_band_)
     } else {
         /* We don't know that MAC.  Send along the packet without setting up a
          * flow. */
-        struct buffer *b;
+        struct ofpbuf *b;
         if (ntohl(opi->buffer_id) == UINT32_MAX) {
             b = make_unbuffered_packet_out(&pkt, in_port, out_port);
         } else {
@@ -700,7 +713,7 @@ in_band_hook_create(const struct settings *s, struct switch_status *ss,
     retval = netdev_open(s->of_name, NETDEV_ETH_TYPE_NONE,
                          &in_band->of_device);
     if (retval) {
-        fatal(retval, "Could not open %s device", s->of_name);
+        ofp_fatal(retval, "Could not open %s device", s->of_name);
     }
     memcpy(in_band->mac, netdev_get_etheraddr(in_band->of_device),
            ETH_ADDR_LEN);
@@ -797,7 +810,7 @@ struct rate_limiter {
     struct rconn *remote_rconn;
 
     /* One queue per physical port. */
-    struct queue queues[OFPP_MAX];
+    struct ofp_queue queues[OFPP_MAX];
     int n_queued;               /* Sum over queues[*].n. */
     int next_tx_port;           /* Next port to check in round-robin. */
 
@@ -824,9 +837,9 @@ struct rate_limiter {
 static void
 drop_packet(struct rate_limiter *rl)
 {
-    struct queue *longest;      /* Queue currently selected as longest. */
+    struct ofp_queue *longest;  /* Queue currently selected as longest. */
     int n_longest;              /* # of queues of same length as 'longest'. */
-    struct queue *q;
+    struct ofp_queue *q;
 
     longest = &rl->queues[0];
     n_longest = 1;
@@ -846,19 +859,19 @@ drop_packet(struct rate_limiter *rl)
     }
 
     /* FIXME: do we want to pop the tail instead? */
-    buffer_delete(queue_pop_head(longest));
+    ofpbuf_delete(queue_pop_head(longest));
     rl->n_queued--;
 }
 
 /* Remove and return the next packet to transmit (in round-robin order). */
-static struct buffer *
+static struct ofpbuf *
 dequeue_packet(struct rate_limiter *rl)
 {
     unsigned int i;
 
     for (i = 0; i < OFPP_MAX; i++) {
         unsigned int port = (rl->next_tx_port + i) % OFPP_MAX;
-        struct queue *q = &rl->queues[port];
+        struct ofp_queue *q = &rl->queues[port];
         if (q->n) {
             rl->next_tx_port = (port + 1) % OFPP_MAX;
             rl->n_queued--;
@@ -900,7 +913,7 @@ rate_limit_packet_cb(struct relay *r, int half, void *rl_)
 {
     struct rate_limiter *rl = rl_;
     const struct settings *s = rl->s;
-    struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
+    struct ofpbuf *msg = r->halves[HALF_LOCAL].rxbuf;
     struct ofp_header *oh;
 
     if (half == HALF_REMOTE) {
@@ -929,7 +942,7 @@ rate_limit_packet_cb(struct relay *r, int half, void *rl_)
         if (rl->n_queued >= s->burst_limit) {
             drop_packet(rl);
         }
-        queue_push_tail(&rl->queues[port], buffer_clone(msg));
+        queue_push_tail(&rl->queues[port], ofpbuf_clone(msg));
         rl->n_queued++;
         rl->n_limited++;
         return true;
@@ -961,7 +974,7 @@ rate_limit_periodic_cb(void *rl_)
          * because the TCP connection is responsible for buffering and there is
          * no point in trying to transmit faster than the TCP connection can
          * handle. */
-        struct buffer *b = dequeue_packet(rl);
+        struct ofpbuf *b = dequeue_packet(rl);
         if (rconn_send_with_limit(rl->remote_rconn, b, &rl->n_txq, 10)) {
             rl->n_tx_dropped++;
         }
@@ -1031,13 +1044,13 @@ switch_status_packet_cb(struct relay *r, int half, void *ss_)
 {
     struct switch_status *ss = ss_;
     struct rconn *rc = r->halves[HALF_REMOTE].rconn;
-    struct buffer *msg = r->halves[HALF_REMOTE].rxbuf;
+    struct ofpbuf *msg = r->halves[HALF_REMOTE].rxbuf;
     struct switch_status_category *c;
     struct ofp_stats_request *osr;
     struct ofp_stats_reply *reply;
     struct status_reply sr;
     struct ofp_header *oh;
-    struct buffer *b;
+    struct ofpbuf *b;
     int retval;
 
     if (half == HALF_LOCAL) {
@@ -1091,6 +1104,7 @@ rconn_status_cb(struct status_reply *sr, void *rconn_)
 
     status_reply_put(sr, "name=%s", rconn_get_name(rconn));
     status_reply_put(sr, "state=%s", rconn_get_state(rconn));
+    status_reply_put(sr, "backoff=%d", rconn_get_backoff(rconn));
     status_reply_put(sr, "is-connected=%s",
                      rconn_is_connected(rconn) ? "true" : "false");
     status_reply_put(sr, "sent-msgs=%u", rconn_packets_sent(rconn));
@@ -1103,6 +1117,7 @@ rconn_status_cb(struct status_reply *sr, void *rconn_)
                      (long int) (now - rconn_get_last_connection(rconn)));
     status_reply_put(sr, "time-connected=%lu",
                      rconn_get_total_time_connected(rconn));
+    status_reply_put(sr, "state-elapsed=%u", rconn_get_state_elapsed(rconn));
 }
 
 static void
@@ -1201,11 +1216,10 @@ discovery_status_cb(struct status_reply *sr, void *d_)
 {
     struct discovery *d = d_;
 
-    status_reply_put(sr, "discovery.accept-remote=%s",
-                     d->s->accept_controller_re);
-    status_reply_put(sr, "discovery.n-changes=%d", d->n_changes);
-    status_reply_put(sr, "discovery.state=%s", dhclient_get_state(d->dhcp));
-    status_reply_put(sr, "discovery.state-elapsed=%u",
+    status_reply_put(sr, "accept-remote=%s", d->s->accept_controller_re);
+    status_reply_put(sr, "n-changes=%d", d->n_changes);
+    status_reply_put(sr, "state=%s", dhclient_get_state(d->dhcp));
+    status_reply_put(sr, "state-elapsed=%u",
                      dhclient_get_state_elapsed(d->dhcp));
     if (dhclient_is_bound(d->dhcp)) {
         uint32_t ip = dhclient_get_ip(d->dhcp);
@@ -1217,25 +1231,24 @@ discovery_status_cb(struct status_reply *sr, void *d_)
         char *domain_name;
         int i;
 
-        status_reply_put(sr, "discovery.ip="IP_FMT, IP_ARGS(&ip));
-        status_reply_put(sr, "discovery.netmask="IP_FMT, IP_ARGS(&netmask));
+        status_reply_put(sr, "ip="IP_FMT, IP_ARGS(&ip));
+        status_reply_put(sr, "netmask="IP_FMT, IP_ARGS(&netmask));
         if (router) {
-            status_reply_put(sr, "discovery.router="IP_FMT, IP_ARGS(&router));
+            status_reply_put(sr, "router="IP_FMT, IP_ARGS(&router));
         }
 
         for (i = 0; dhcp_msg_get_ip(cfg, DHCP_CODE_DNS_SERVER, i, &dns_server);
              i++) {
-            status_reply_put(sr, "discovery.dns%d="IP_FMT,
-                             i, IP_ARGS(&dns_server));
+            status_reply_put(sr, "dns%d="IP_FMT, i, IP_ARGS(&dns_server));
         }
 
         domain_name = dhcp_msg_get_string(cfg, DHCP_CODE_DOMAIN_NAME);
         if (domain_name) {
-            status_reply_put(sr, "discovery.domain=%s", domain_name);
+            status_reply_put(sr, "domain=%s", domain_name);
             free(domain_name);
         }
 
-        status_reply_put(sr, "discovery.lease-remaining=%u",
+        status_reply_put(sr, "lease-remaining=%u",
                          dhclient_get_lease_remaining(d->dhcp));
     }
 }
@@ -1251,11 +1264,11 @@ discovery_init(const struct settings *s, struct switch_status *ss)
     /* Bring ofX network device up. */
     retval = netdev_open(s->of_name, NETDEV_ETH_TYPE_NONE, &netdev);
     if (retval) {
-        fatal(retval, "Could not open %s device", s->of_name);
+        ofp_fatal(retval, "Could not open %s device", s->of_name);
     }
     retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
     if (retval) {
-        fatal(retval, "Could not bring %s device up", s->of_name);
+        ofp_fatal(retval, "Could not bring %s device up", s->of_name);
     }
     netdev_close(netdev);
 
@@ -1263,7 +1276,7 @@ discovery_init(const struct settings *s, struct switch_status *ss)
     retval = dhclient_create(s->of_name, modify_dhcp_request,
                              validate_dhcp_offer, (void *) s, &dhcp);
     if (retval) {
-        fatal(retval, "Failed to initialize DHCP client");
+        ofp_fatal(retval, "Failed to initialize DHCP client");
     }
     dhclient_init(dhcp, 0);
 
@@ -1361,7 +1374,7 @@ parse_options(int argc, char *argv[], struct settings *s)
     static struct option long_options[] = {
         {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
         {"no-resolv-conf", no_argument, 0, OPT_NO_RESOLV_CONF},
-        {"fail",        required_argument, 0, 'f'},
+        {"fail",        required_argument, 0, 'F'},
         {"inactivity-probe", required_argument, 0, OPT_INACTIVITY_PROBE},
         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
         {"max-backoff", required_argument, 0, OPT_MAX_BACKOFF},
@@ -1369,6 +1382,7 @@ parse_options(int argc, char *argv[], struct settings *s)
         {"rate-limit",  optional_argument, 0, OPT_RATE_LIMIT},
         {"burst-limit", required_argument, 0, OPT_BURST_LIMIT},
         {"detach",      no_argument, 0, 'D'},
+        {"force",       no_argument, 0, 'f'},
         {"pidfile",     optional_argument, 0, 'P'},
         {"verbose",     optional_argument, 0, 'v'},
         {"help",        no_argument, 0, 'h'},
@@ -1406,21 +1420,21 @@ parse_options(int argc, char *argv[], struct settings *s)
             s->update_resolv_conf = false;
             break;
 
-        case 'f':
+        case 'F':
             if (!strcmp(optarg, "open")) {
                 s->fail_mode = FAIL_OPEN;
             } else if (!strcmp(optarg, "closed")) {
                 s->fail_mode = FAIL_CLOSED;
             } else {
-                fatal(0,
-                      "-f or --fail argument must be \"open\" or \"closed\"");
+                ofp_fatal(0, "-f or --fail argument must be \"open\" "
+                          "or \"closed\"");
             }
             break;
 
         case OPT_INACTIVITY_PROBE:
             s->probe_interval = atoi(optarg);
             if (s->probe_interval < 5) {
-                fatal(0, "--inactivity-probe argument must be at least 5");
+                ofp_fatal(0, "--inactivity-probe argument must be at least 5");
             }
             break;
 
@@ -1430,8 +1444,8 @@ parse_options(int argc, char *argv[], struct settings *s)
             } else {
                 s->max_idle = atoi(optarg);
                 if (s->max_idle < 1 || s->max_idle > 65535) {
-                    fatal(0, "--max-idle argument must be between 1 and "
-                          "65535 or the word 'permanent'");
+                    ofp_fatal(0, "--max-idle argument must be between 1 and "
+                              "65535 or the word 'permanent'");
                 }
             }
             break;
@@ -1439,7 +1453,7 @@ parse_options(int argc, char *argv[], struct settings *s)
         case OPT_MAX_BACKOFF:
             s->max_backoff = atoi(optarg);
             if (s->max_backoff < 1) {
-                fatal(0, "--max-backoff argument must be at least 1");
+                ofp_fatal(0, "--max-backoff argument must be at least 1");
             } else if (s->max_backoff > 3600) {
                 s->max_backoff = 3600;
             }
@@ -1449,7 +1463,7 @@ parse_options(int argc, char *argv[], struct settings *s)
             if (optarg) {
                 s->rate_limit = atoi(optarg);
                 if (s->rate_limit < 1) {
-                    fatal(0, "--rate-limit argument must be at least 1");
+                    ofp_fatal(0, "--rate-limit argument must be at least 1");
                 }
             } else {
                 s->rate_limit = 1000;
@@ -1459,7 +1473,7 @@ parse_options(int argc, char *argv[], struct settings *s)
         case OPT_BURST_LIMIT:
             s->burst_limit = atoi(optarg);
             if (s->burst_limit < 1) {
-                fatal(0, "--burst-limit argument must be at least 1");
+                ofp_fatal(0, "--burst-limit argument must be at least 1");
             }
             break;
 
@@ -1471,10 +1485,15 @@ parse_options(int argc, char *argv[], struct settings *s)
             set_pidfile(optarg);
             break;
 
+        case 'f':
+            ignore_existing_pidfile();
+            break;
+
         case 'l':
             if (s->n_listeners >= MAX_MGMT) {
-                fatal(0, "-l or --listen may be specified at most %d times",
-                      MAX_MGMT);
+                ofp_fatal(0,
+                          "-l or --listen may be specified at most %d times",
+                          MAX_MGMT);
             }
             s->listener_names[s->n_listeners++] = optarg;
             break;
@@ -1504,7 +1523,8 @@ parse_options(int argc, char *argv[], struct settings *s)
     argc -= optind;
     argv += optind;
     if (argc < 1 || argc > 2) {
-        fatal(0, "need one or two non-option arguments; use --help for usage");
+        ofp_fatal(0, "need one or two non-option arguments; "
+                  "use --help for usage");
     }
 
     /* Local and remote vconns. */
@@ -1512,7 +1532,8 @@ parse_options(int argc, char *argv[], struct settings *s)
     if (strncmp(s->nl_name, "nl:", 3)
         || strlen(s->nl_name) < 4
         || s->nl_name[strspn(s->nl_name + 3, "0123456789") + 3]) {
-        fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"", s->nl_name);
+        ofp_fatal(0, "%s: argument is not of the form \"nl:DP_IDX\"",
+                  s->nl_name);
     }
     s->of_name = xasprintf("of%s", s->nl_name + 3);
     s->controller_name = argc > 1 ? xstrdup(argv[1]) : NULL;
@@ -1527,7 +1548,7 @@ parse_options(int argc, char *argv[], struct settings *s)
         size_t length = regerror(retval, &s->accept_controller_regex, NULL, 0);
         char *buffer = xmalloc(length);
         regerror(retval, &s->accept_controller_regex, buffer, length);
-        fatal(0, "%s: %s", accept_re, buffer);
+        ofp_fatal(0, "%s: %s", accept_re, buffer);
     }
     s->accept_controller_re = accept_re;
 
@@ -1541,12 +1562,12 @@ parse_options(int argc, char *argv[], struct settings *s)
 
         retval = netdev_open(s->of_name, NETDEV_ETH_TYPE_NONE, &netdev);
         if (retval) {
-            fatal(retval, "Could not open %s device", s->of_name);
+            ofp_fatal(retval, "Could not open %s device", s->of_name);
         }
 
         retval = netdev_get_flags(netdev, &flags);
         if (retval) {
-            fatal(retval, "Could not get flags for %s device", s->of_name);
+            ofp_fatal(retval, "Could not get flags for %s device", s->of_name);
         }
 
         s->in_band = (flags & NETDEV_UP) != 0;
@@ -1586,7 +1607,7 @@ usage(void)
            "  --accept-vconn=REGEX    accept matching discovered controllers\n"
            "  --no-resolv-conf        do not update /etc/resolv.conf\n"
            "\nNetworking options:\n"
-           "  -f, --fail=open|closed  when controller connection fails:\n"
+           "  -F, --fail=open|closed  when controller connection fails:\n"
            "                            closed: drop all packets\n"
            "                            open (default): act as learning switch\n"
            "  --inactivity-probe=SECS time between inactivity probes\n"
@@ -1601,6 +1622,7 @@ usage(void)
            "\nOther options:\n"
            "  -D, --detach            run in background as daemon\n"
            "  -P, --pidfile[=FILE]    create pidfile (default: %s/secchan.pid)\n"
+           "  -f, --force             with -P, start even if already running\n"
            "  -v, --verbose=MODULE[:FACILITY[:LEVEL]]  set logging levels\n"
            "  -v, --verbose           set maximum verbosity level\n"
            "  -h, --help              display this help message\n"