datapath-windows: Update VXLAN header information
[cascardo/ovs.git] / lib / ovs-router.c
index ba51614..df55bb4 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 "packets.h"
 #include "seq.h"
 #include "ovs-router.h"
-#include "ovs-router-linux.h"
+#include "ovs-thread.h"
+#include "route-table.h"
 #include "unixctl.h"
 #include "util.h"
 
+static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
 static struct classifier cls;
 
 struct ovs_router_entry {
@@ -66,15 +68,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
@@ -108,14 +110,18 @@ 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));
@@ -142,12 +148,14 @@ rt_entry_delete(uint8_t priority, ovs_be32 ip_dst, uint8_t 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. */
+        ovs_mutex_lock(&mutex);
         cr = classifier_remove(&cls, cr);
-        if (cr) {
+        ovs_mutex_unlock(&mutex);
 
+        if (cr) {
             ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
             return true;
         }
@@ -253,22 +261,53 @@ 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);
+            if (classifier_remove(&cls, &rt->cr)) {
+                ovsrcu_postpone(rt_entry_free, rt);
+            }
         }
     }
+    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);
     unixctl_command_register("ovs/route/add", "ipv4_addr/prefix_len out_br_name gw", 2, 3,
@@ -276,4 +315,6 @@ ovs_router_unixctl_register(void)
     unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
     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);
 }