netdev-dpdk: Properly support non pmd threads.
[cascardo/ovs.git] / lib / dpctl.c
index 4c2614b..e95483e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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.
 #include "dirs.h"
 #include "dpctl.h"
 #include "dpif.h"
-#include "dpif-netdev.h"
 #include "dynamic-string.h"
 #include "flow.h"
 #include "match.h"
 #include "netdev.h"
+#include "netdev-dpdk.h"
 #include "netlink.h"
 #include "odp-util.h"
 #include "ofp-parse.h"
@@ -60,6 +60,11 @@ struct dpctl_command {
     dpctl_command_handler *handler;
 };
 static const struct dpctl_command *get_all_dpctl_commands(void);
+static void dpctl_print(struct dpctl_params *dpctl_p, const char *fmt, ...)
+    OVS_PRINTF_FORMAT(2, 3);
+static void dpctl_error(struct dpctl_params* dpctl_p, int err_no,
+                        const char *fmt, ...)
+    OVS_PRINTF_FORMAT(3, 4);
 
 static void
 dpctl_puts(struct dpctl_params *dpctl_p, bool error, const char *string)
@@ -382,12 +387,14 @@ dpctl_set_if(int argc, const char *argv[], struct dpctl_params *dpctl_p)
                                 "%s: can't change type from %s to %s",
                                  name, type, value);
                     error = EINVAL;
+                    goto next_destroy_args;
                 }
             } else if (!strcmp(key, "port_no")) {
                 if (port_no != u32_to_odp(atoi(value))) {
                     dpctl_error(dpctl_p, 0, "%s: can't change port number from"
                               " %"PRIu32" to %d", name, port_no, atoi(value));
                     error = EINVAL;
+                    goto next_destroy_args;
                 }
             } else if (value[0] == '\0') {
                 smap_remove(&args, key);
@@ -397,7 +404,13 @@ dpctl_set_if(int argc, const char *argv[], struct dpctl_params *dpctl_p)
         }
 
         /* Update configuration. */
-        error = netdev_set_config(netdev, &args, NULL);
+        char *err_s = NULL;
+        error = netdev_set_config(netdev, &args, &err_s);
+        if (err_s || error) {
+            dpctl_error(dpctl_p, error, "%s",
+                        err_s ? err_s : "Error updating configuration");
+            free(err_s);
+        }
         if (error) {
             goto next_destroy_args;
         }
@@ -599,7 +612,57 @@ show_dpif(struct dpif *dpif, struct dpctl_params *dpctl_p)
             }
         }
     }
-    dpif_close(dpif);
+}
+
+typedef void (*dps_for_each_cb)(struct dpif *, struct dpctl_params *);
+
+static int
+dps_for_each(struct dpctl_params *dpctl_p, dps_for_each_cb cb)
+{
+    struct sset dpif_names = SSET_INITIALIZER(&dpif_names),
+                dpif_types = SSET_INITIALIZER(&dpif_types);
+    int error, openerror = 0, enumerror = 0;
+    const char *type, *name;
+    bool at_least_one = false;
+
+    dp_enumerate_types(&dpif_types);
+
+    SSET_FOR_EACH (type, &dpif_types) {
+        error = dp_enumerate_names(type, &dpif_names);
+        if (error) {
+            enumerror = error;
+        }
+
+        SSET_FOR_EACH (name, &dpif_names) {
+            struct dpif *dpif;
+
+            at_least_one = true;
+            error = dpif_open(name, type, &dpif);
+            if (!error) {
+                cb(dpif, dpctl_p);
+                dpif_close(dpif);
+            } else {
+                openerror = error;
+                dpctl_error(dpctl_p, error, "opening datapath %s failed",
+                            name);
+            }
+        }
+    }
+
+    sset_destroy(&dpif_names);
+    sset_destroy(&dpif_types);
+
+    /* If there has been an error while opening a datapath it should be
+     * reported.  Otherwise, we want to ignore the errors generated by
+     * dp_enumerate_names() if at least one datapath has been discovered,
+     * because they're not interesting for the user.  This happens, for
+     * example, if OVS is using a userspace datapath and the kernel module
+     * is not loaded. */
+    if (openerror) {
+        return openerror;
+    } else {
+        return at_least_one ? 0 : enumerror;
+    }
 }
 
 static int
@@ -615,6 +678,7 @@ dpctl_show(int argc, const char *argv[], struct dpctl_params *dpctl_p)
             error = parsed_dpif_open(name, false, &dpif);
             if (!error) {
                 show_dpif(dpif, dpctl_p);
+                dpif_close(dpif);
             } else {
                 dpctl_error(dpctl_p, error, "opening datapath %s failed",
                             name);
@@ -622,73 +686,23 @@ dpctl_show(int argc, const char *argv[], struct dpctl_params *dpctl_p)
             }
         }
     } else {
-        struct sset types;
-        const char *type;
-
-        sset_init(&types);
-        dp_enumerate_types(&types);
-        SSET_FOR_EACH (type, &types) {
-            struct sset names;
-            const char *name;
-
-            sset_init(&names);
-            error = dp_enumerate_names(type, &names);
-            if (error) {
-                lasterror = error;
-                goto next;
-            }
-            SSET_FOR_EACH (name, &names) {
-                struct dpif *dpif;
-
-                error = dpif_open(name, type, &dpif);
-                if (!error) {
-                    show_dpif(dpif, dpctl_p);
-                } else {
-                    dpctl_error(dpctl_p, error, "opening datapath %s failed",
-                                name);
-                    lasterror = error;
-                }
-            }
-next:
-            sset_destroy(&names);
-        }
-        sset_destroy(&types);
+        lasterror = dps_for_each(dpctl_p, show_dpif);
     }
+
     return lasterror;
 }
 
+static void
+dump_cb(struct dpif *dpif, struct dpctl_params *dpctl_p)
+{
+    dpctl_print(dpctl_p, "%s\n", dpif_name(dpif));
+}
+
 static int
 dpctl_dump_dps(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
                struct dpctl_params *dpctl_p)
 {
-    struct sset dpif_names, dpif_types;
-    const char *type;
-    int error, lasterror = 0;
-
-    sset_init(&dpif_names);
-    sset_init(&dpif_types);
-    dp_enumerate_types(&dpif_types);
-
-    SSET_FOR_EACH (type, &dpif_types) {
-        const char *name;
-
-        error = dp_enumerate_names(type, &dpif_names);
-        if (error) {
-            lasterror = error;
-        }
-
-        SSET_FOR_EACH (name, &dpif_names) {
-            struct dpif *dpif;
-            if (!dpif_open(name, type, &dpif)) {
-                dpctl_print(dpctl_p, "%s\n", dpif_name(dpif));
-                dpif_close(dpif);
-            }
-        }
-    }
-
-    sset_destroy(&dpif_names);
-    sset_destroy(&dpif_types);
-    return lasterror;
+    return dps_for_each(dpctl_p, dump_cb);
 }
 
 static void
@@ -769,6 +783,12 @@ dpctl_dump_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p)
         }
     }
 
+    /* Make sure that these values are different. PMD_ID_NULL means that the
+     * pmd is unspecified (e.g. because the datapath doesn't have different
+     * pmd threads), while NON_PMD_CORE_ID refers to every non pmd threads
+     * in the userspace datapath */
+    BUILD_ASSERT(PMD_ID_NULL != NON_PMD_CORE_ID);
+
     ds_init(&ds);
     flow_dump = dpif_flow_dump_create(dpif, false);
     flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
@@ -933,10 +953,10 @@ dpctl_put_flow(int argc, const char *argv[], enum dpif_flow_put_flags flags,
             FOR_EACH_CORE_ON_NUMA (iter, dump) {
                 if (ovs_numa_core_is_pinned(iter->core_id)) {
                     error = dpif_flow_put(dpif, flags,
-                                          ofpbuf_data(&key), ofpbuf_size(&key),
-                                          ofpbuf_size(&mask) == 0 ? NULL : ofpbuf_data(&mask),
-                                          ofpbuf_size(&mask), ofpbuf_data(&actions),
-                                          ofpbuf_size(&actions), ufid_present ? &ufid : NULL,
+                                          key.data, key.size,
+                                          mask.size == 0 ? NULL : mask.data,
+                                          mask.size, actions.data,
+                                          actions.size, ufid_present ? &ufid : NULL,
                                           iter->core_id, dpctl_p->print_statistics ? &stats : NULL);
                 }
             }
@@ -946,10 +966,10 @@ dpctl_put_flow(int argc, const char *argv[], enum dpif_flow_put_flags flags,
         }
     } else {
         error = dpif_flow_put(dpif, flags,
-                              ofpbuf_data(&key), ofpbuf_size(&key),
-                              ofpbuf_size(&mask) == 0 ? NULL : ofpbuf_data(&mask),
-                              ofpbuf_size(&mask), ofpbuf_data(&actions),
-                              ofpbuf_size(&actions), ufid_present ? &ufid : NULL,
+                              key.data, key.size,
+                              mask.size == 0 ? NULL : mask.data,
+                              mask.size, actions.data,
+                              actions.size, ufid_present ? &ufid : NULL,
                               PMD_ID_NULL, dpctl_p->print_statistics ? &stats : NULL);
     }
     if (error) {
@@ -1123,8 +1143,8 @@ dpctl_del_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
 
             FOR_EACH_CORE_ON_NUMA (iter, dump) {
                 if (ovs_numa_core_is_pinned(iter->core_id)) {
-                    error = dpif_flow_del(dpif, ofpbuf_data(&key),
-                                          ofpbuf_size(&key), ufid_present ? &ufid : NULL,
+                    error = dpif_flow_del(dpif, key.data,
+                                          key.size, ufid_present ? &ufid : NULL,
                                           iter->core_id, dpctl_p->print_statistics ? &stats : NULL);
                 }
             }
@@ -1133,7 +1153,7 @@ dpctl_del_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
             error = EINVAL;
         }
     } else {
-        error = dpif_flow_del(dpif, ofpbuf_data(&key), ofpbuf_size(&key),
+        error = dpif_flow_del(dpif, key.data, key.size,
                               ufid_present ? &ufid : NULL, PMD_ID_NULL,
                               dpctl_p->print_statistics ? &stats : NULL);
     }
@@ -1246,7 +1266,7 @@ dpctl_parse_actions(int argc, const char *argv[], struct dpctl_params* dpctl_p)
         }
 
         ds_init(&s);
-        format_odp_actions(&s, ofpbuf_data(&actions), ofpbuf_size(&actions));
+        format_odp_actions(&s, actions.data, actions.size);
         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
         ds_destroy(&s);
 
@@ -1390,12 +1410,11 @@ dpctl_normalize_actions(int argc, const char *argv[],
     }
 
     ds_clear(&s);
-    odp_flow_format(ofpbuf_data(&keybuf), ofpbuf_size(&keybuf), NULL, 0, NULL,
+    odp_flow_format(keybuf.data, keybuf.size, NULL, 0, NULL,
                     &s, dpctl_p->verbosity);
     dpctl_print(dpctl_p, "input flow: %s\n", ds_cstr(&s));
 
-    error = odp_flow_key_to_flow(ofpbuf_data(&keybuf), ofpbuf_size(&keybuf),
-                                 &flow);
+    error = odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow);
     if (error) {
         dpctl_error(dpctl_p, error, "odp_flow_key_to_flow");
         goto out_freekeybuf;
@@ -1411,14 +1430,12 @@ dpctl_normalize_actions(int argc, const char *argv[],
 
     if (dpctl_p->verbosity) {
         ds_clear(&s);
-        format_odp_actions(&s, ofpbuf_data(&odp_actions),
-                           ofpbuf_size(&odp_actions));
+        format_odp_actions(&s, odp_actions.data, odp_actions.size);
         dpctl_print(dpctl_p, "input actions: %s\n", ds_cstr(&s));
     }
 
     hmap_init(&actions_per_flow);
-    NL_ATTR_FOR_EACH (a, left, ofpbuf_data(&odp_actions),
-                      ofpbuf_size(&odp_actions)) {
+    NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
         const struct ovs_action_push_vlan *push;
         switch(nl_attr_type(a)) {
         case OVS_ACTION_ATTR_POP_VLAN:
@@ -1451,8 +1468,7 @@ dpctl_normalize_actions(int argc, const char *argv[],
     for (i = 0; i < n_afs; i++) {
         struct actions_for_flow *af = afs[i];
 
-        sort_output_actions(ofpbuf_data(&af->actions),
-                            ofpbuf_size(&af->actions));
+        sort_output_actions(af->actions.data, af->actions.size);
 
         if (af->flow.vlan_tci != htons(0)) {
             dpctl_print(dpctl_p, "vlan(vid=%"PRIu16",pcp=%d): ",
@@ -1472,9 +1488,8 @@ dpctl_normalize_actions(int argc, const char *argv[],
         }
 
         ds_clear(&s);
-        format_odp_actions(&s, ofpbuf_data(&af->actions),
-                           ofpbuf_size(&af->actions));
-        dpctl_print(dpctl_p, ds_cstr(&s));
+        format_odp_actions(&s, af->actions.data, af->actions.size);
+        dpctl_puts(dpctl_p, false, ds_cstr(&s));
 
         ofpbuf_uninit(&af->actions);
         free(af);
@@ -1572,7 +1587,7 @@ dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
 {
     struct ds ds = DS_EMPTY_INITIALIZER;
     struct dpctl_params dpctl_p;
-    bool opt_parse_err = false;
+    bool error = false;
 
     dpctl_command_handler *handler = (dpctl_command_handler *) aux;
 
@@ -1584,7 +1599,7 @@ dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
     /* Parse options (like getopt). Unfortunately it does
      * not seem a good idea to call getopt_long() here, since it uses global
      * variables */
-    while (argc > 1 && !opt_parse_err) {
+    while (argc > 1 && !error) {
         const char *arg = argv[1];
         if (!strncmp(arg, "--", 2)) {
             /* Long option */
@@ -1598,13 +1613,13 @@ dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
                 dpctl_p.verbosity++;
             } else {
                 ds_put_format(&ds, "Unrecognized option %s", argv[1]);
-                opt_parse_err = true;
+                error = true;
             }
         } else if (arg[0] == '-' && arg[1] != '\0') {
             /* Short option[s] */
             const char *opt = &arg[1];
 
-            while (*opt && !opt_parse_err) {
+            while (*opt && !error) {
                 switch (*opt) {
                 case 'm':
                     dpctl_p.verbosity++;
@@ -1614,7 +1629,7 @@ dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
                     break;
                 default:
                     ds_put_format(&ds, "Unrecognized option -%c", *opt);
-                    opt_parse_err = true;
+                    error = true;
                     break;
                 }
                 opt++;
@@ -1624,22 +1639,26 @@ dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
             break;
         }
 
-        if (opt_parse_err) {
+        if (error) {
             break;
         }
         argv++;
         argc--;
     }
 
-    if (!opt_parse_err) {
+    if (!error) {
         dpctl_p.is_appctl = true;
         dpctl_p.output = dpctl_unixctl_print;
         dpctl_p.aux = &ds;
 
-        handler(argc, argv, &dpctl_p);
+        error = handler(argc, argv, &dpctl_p) != 0;
     }
 
-    unixctl_command_reply(conn, ds_cstr(&ds));
+    if (error) {
+        unixctl_command_reply_error(conn, ds_cstr(&ds));
+    } else {
+        unixctl_command_reply(conn, ds_cstr(&ds));
+    }
 
     ds_destroy(&ds);
 }