tnl-ports: Add destination IP and MAC address to the match.
[cascardo/ovs.git] / lib / ovs-router.c
index c119c94..0316a38 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Nicira, Inc.
+ * Copyright (c) 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.
 #include "dynamic-string.h"
 #include "netdev.h"
 #include "packets.h"
+#include "seq.h"
 #include "ovs-router.h"
+#include "ovs-thread.h"
+#include "route-table.h"
+#include "tnl-ports.h"
 #include "unixctl.h"
 #include "util.h"
 
+static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
 static struct classifier cls;
 
 struct ovs_router_entry {
@@ -64,15 +69,15 @@ ovs_router_lookup(ovs_be32 ip_dst, char output_bridge[], ovs_be32 *gw)
     const struct cls_rule *cr;
     struct flow flow = {.nw_dst = ip_dst};
 
-    cr = classifier_lookup(&cls, &flow, NULL);
+    cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
     if (cr) {
         struct ovs_router_entry *p = ovs_router_entry_cast(cr);
 
-        strncpy(output_bridge, p->output_bridge, IFNAMSIZ);
+        ovs_strlcpy(output_bridge, p->output_bridge, IFNAMSIZ);
         *gw = p->gw;
         return true;
     }
-    return false;
+    return route_table_fallback_lookup(ip_dst, output_bridge, gw);
 }
 
 static void
@@ -86,7 +91,7 @@ static void rt_init_match(struct match *match, ovs_be32 ip_dst, uint8_t plen)
 {
     ovs_be32 mask;
 
-    mask = htonl(UINT32_MAX << (32 - plen));
+    mask = be32_prefix_mask(plen);
 
     ip_dst &= mask; /* Clear out insignificant bits. */
     memset(match, 0, sizeof *match);
@@ -106,18 +111,24 @@ ovs_router_insert__(uint8_t priority, ovs_be32 ip_dst, uint8_t plen,
     rt_init_match(&match, ip_dst, plen);
 
     p = xzalloc(sizeof *p);
-    strncpy(p->output_bridge, output_bridge, IFNAMSIZ);
+    ovs_strlcpy(p->output_bridge, output_bridge, sizeof p->output_bridge);
     p->gw = gw;
     p->nw_addr = match.flow.nw_dst;
     p->plen = plen;
     p->priority = priority;
-    cls_rule_init(&p->cr, &match, priority); /* Longest prefix matches first. */
+    /* Longest prefix matches first. */
+    cls_rule_init(&p->cr, &match, priority);
+
+    ovs_mutex_lock(&mutex);
+    cr = classifier_replace(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
+    ovs_mutex_unlock(&mutex);
 
-    cr = classifier_replace(&cls, &p->cr);
     if (cr) {
         /* An old rule with the same match was displaced. */
         ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
     }
+    tnl_port_map_insert_ipdev(output_bridge);
+    seq_change(tnl_conf_seq);
 }
 
 void
@@ -127,29 +138,42 @@ ovs_router_insert(ovs_be32 ip_dst, uint8_t plen, const char output_bridge[],
     ovs_router_insert__(plen, ip_dst, plen, output_bridge, gw);
 }
 
+
+static bool
+__rt_entry_delete(const struct cls_rule *cr)
+{
+    struct ovs_router_entry *p = ovs_router_entry_cast(cr);
+
+    tnl_port_map_delete_ipdev(p->output_bridge);
+    /* Remove it. */
+    cr = classifier_remove(&cls, cr);
+    if (cr) {
+        ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
+        return true;
+    }
+    return false;
+}
+
 static bool
 rt_entry_delete(uint8_t priority, ovs_be32 ip_dst, uint8_t plen)
 {
-    struct cls_rule *cr;
+    const struct cls_rule *cr;
     struct cls_rule rule;
     struct match match;
+    bool res = false;
 
     rt_init_match(&match, ip_dst, plen);
 
     cls_rule_init(&rule, &match, priority);
 
     /* Find the exact rule. */
-    cr = classifier_find_rule_exactly(&cls, &rule);
+    cr = classifier_find_rule_exactly(&cls, &rule, CLS_MAX_VERSION);
     if (cr) {
-        /* Remove it. */
-        cr = classifier_remove(&cls, cr);
-        if (cr) {
-
-            ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
-            return true;
-        }
+        ovs_mutex_lock(&mutex);
+        res = __rt_entry_delete(cr);
+        ovs_mutex_unlock(&mutex);
     }
-    return false;
+    return res;
 }
 
 static bool
@@ -159,7 +183,7 @@ scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
     int slen = strlen(s);
     uint8_t *ip = (uint8_t *)addr;
 
-    *addr = htons(0);
+    *addr = htonl(0);
     if (!ovs_scan(s, "%"SCNu8"%n", &ip[0], &n)) {
         return false;
     }
@@ -215,6 +239,7 @@ ovs_router_del(struct unixctl_conn *conn, int argc OVS_UNUSED,
 
         if (rt_entry_delete(plen + 32, ip, plen)) {
             unixctl_command_reply(conn, "OK");
+            seq_change(tnl_conf_seq);
         } else {
             unixctl_command_reply(conn, "Not found");
         }
@@ -249,27 +274,58 @@ ovs_router_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
     ds_destroy(&ds);
 }
 
+static void
+ovs_router_lookup_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
+                      const char *argv[], void *aux OVS_UNUSED)
+{
+    ovs_be32 ip;
+    unsigned int plen;
+
+    if (scan_ipv4_route(argv[1], &ip, &plen) && plen == 32) {
+        char iface[IFNAMSIZ];
+        ovs_be32 gw;
+
+        if (ovs_router_lookup(ip, iface, &gw)) {
+            struct ds ds = DS_EMPTY_INITIALIZER;
+
+            ds_put_format(&ds, "gateway " IP_FMT "\n", IP_ARGS(gw));
+            ds_put_format(&ds, "dev %s\n", iface);
+            unixctl_command_reply(conn, ds_cstr(&ds));
+        } else {
+            unixctl_command_reply(conn, "Not found");
+        }
+    } else {
+        unixctl_command_reply(conn, "Invalid parameters");
+    }
+}
+
 void
 ovs_router_flush(void)
 {
     struct ovs_router_entry *rt;
 
-    CLS_FOR_EACH_SAFE(rt, cr, &cls) {
+    ovs_mutex_lock(&mutex);
+    classifier_defer(&cls);
+    CLS_FOR_EACH(rt, cr, &cls) {
         if (rt->priority == rt->plen) {
-            classifier_remove(&cls, &rt->cr);
+            __rt_entry_delete(&rt->cr);
         }
     }
+    classifier_publish(&cls);
+    ovs_mutex_unlock(&mutex);
+    seq_change(tnl_conf_seq);
 }
 
 /* May not be called more than once. */
 void
-ovs_router_unixctl_register(void)
+ovs_router_init(void)
 {
     classifier_init(&cls, NULL);
-    /* XXX: Add documentation for these commands. */
-    unixctl_command_register("ovs/route/add", "ip mask dev gw", 2, 3,
+    unixctl_command_register("ovs/route/add", "ipv4_addr/prefix_len out_br_name gw", 2, 3,
                              ovs_router_add, NULL);
     unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
-    unixctl_command_register("ovs/route/del", "ip mask", 1, 1, ovs_router_del,
+    unixctl_command_register("ovs/route/del", "ipv4_addr/prefix_len", 1, 1, ovs_router_del,
                              NULL);
+    unixctl_command_register("ovs/route/lookup", "ipv4_addr", 1, 1,
+                             ovs_router_lookup_cmd, NULL);
 }