Add support for deleting flow entries with "del-flows" command in "dpctl".
[sliver-openvswitch.git] / utilities / dpctl.c
index f440425..27eb43a 100644 (file)
@@ -172,6 +172,7 @@ usage(void)
            "  dump-flows SWITCH           print all flow entries\n"
            "  dump-flows SWITCH FLOW      print matching FLOWs\n"
            "  add-flows SWITCH FILE       add flows from FILE\n"
+           "  del-flows SWITCH FLOW       delete matching FLOWs\n"
            "where each SWITCH is an active OpenFlow connection method.\n",
            program_name, program_name);
     vconn_usage(true, false);
@@ -197,7 +198,7 @@ static void run(int retval, const char *message, ...)
         if (retval == EOF) {
             fputs(": unexpected end of file\n", stderr);
         } else {
-            vfprintf(stderr, ": %s\n", strerror(retval));
+            fprintf(stderr, ": %s\n", strerror(retval));
         }
 
         exit(EXIT_FAILURE);
@@ -399,7 +400,7 @@ do_show(int argc UNUSED, char *argv[])
 static void
 do_dump_tables(int argc, char *argv[])
 {
-    dump_transaction(argv[1], OFPT_TABLE_STAT_REQUEST);
+    dump_transaction(argv[1], OFPT_TABLE_STATS_REQUEST);
 }
 
 
@@ -455,7 +456,7 @@ str_to_action(const char *str, struct ofp_action *action)
 
 static void
 str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action,
-            uint16_t *table_idx)
+            uint8_t *table_idx, uint16_t *priority)
 {
     struct field {
         const char *name;
@@ -483,7 +484,7 @@ str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action,
     bool got_action = false;
 
     if (table_idx) {
-        *table_idx = htons(0xffff);
+        *table_idx = 0xff;
     }
     memset(match, 0, sizeof *match);
     wildcards = OFPFW_ALL;
@@ -501,7 +502,12 @@ str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action,
         }
 
         if (table_idx && !strcmp(name, "table")) {
-            *table_idx = htons(atoi(value));
+            *table_idx = atoi(value);
+            continue;
+        }
+
+        if (priority && !strcmp(name, "priority")) {
+            *priority = atoi(value);
             continue;
         }
 
@@ -553,11 +559,12 @@ static void do_dump_flows(int argc, char *argv[])
 {
     struct vconn *vconn;
     struct buffer *request, *reply;
-    struct ofp_flow_stat_request *fsr;
+    struct ofp_flow_stats_request *fsr;
 
     run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
-    fsr = alloc_openflow_buffer(sizeof *fsr, OFPT_FLOW_STAT_REQUEST, &request);
-    str_to_flow(argc > 2 ? argv[2] : "", &fsr->match, NULL, &fsr->table_id);
+    fsr = alloc_openflow_buffer(sizeof *fsr, OFPT_FLOW_STATS_REQUEST, &request);
+    str_to_flow(argc > 2 ? argv[2] : "", &fsr->match, NULL, &fsr->table_id, 
+            NULL);
     fsr->type = OFPFS_INDIV;
     fsr->pad = 0;
     reply = transact_openflow(vconn, request);
@@ -568,7 +575,6 @@ static void do_dump_flows(int argc, char *argv[])
 static void do_add_flows(int argc, char *argv[])
 {
     struct vconn *vconn;
-    char vconn_name[16];
 
     FILE *file;
     char line[1024];
@@ -578,10 +584,11 @@ static void do_add_flows(int argc, char *argv[])
         fatal(errno, "%s: open", argv[2]);
     }
 
-    run(vconn_open_block(vconn_name, &vconn), "connecting to %s", argv[1]);
+    run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
     while (fgets(line, sizeof line, file)) {
         struct buffer *buffer;
         struct ofp_flow_mod *ofm;
+        uint16_t priority=0;
         size_t size;
 
         char *comment;
@@ -604,17 +611,44 @@ static void do_add_flows(int argc, char *argv[])
         ofm->max_idle = htons(50);
         ofm->buffer_id = htonl(UINT32_MAX);
         ofm->group_id = htonl(0);
-        str_to_flow(line, &ofm->match, &ofm->actions[0], NULL);
-        run(vconn_send_block(vconn, buffer), "send OpenFlow packet");
+        str_to_flow(line, &ofm->match, &ofm->actions[0], NULL, &priority);
+        ofm->priority = htons(priority);
+
+        send_openflow_buffer(vconn, buffer);
     }
     vconn_close(vconn);
     fclose(file);
 }
 
+static void do_del_flows(int argc, char *argv[])
+{
+    struct vconn *vconn;
+
+    run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
+    struct buffer *buffer;
+    struct ofp_flow_mod *ofm;
+    size_t size;
+
+
+    /* Parse and send. */
+    size = sizeof *ofm;
+    ofm = alloc_openflow_buffer(size, OFPT_FLOW_MOD, &buffer);
+    ofm->command = htons(OFPFC_DELETE);
+    ofm->max_idle = 0;
+    ofm->buffer_id = htonl(UINT32_MAX);
+    ofm->group_id = 0;
+    ofm->priority = 0;
+    str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, NULL);
+
+    send_openflow_buffer(vconn, buffer);
+
+    vconn_close(vconn);
+}
+
 static void
 do_dump_ports(int argc, char *argv[])
 {
-    dump_transaction(argv[1], OFPT_PORT_STAT_REQUEST);
+    dump_transaction(argv[1], OFPT_PORT_STATS_REQUEST);
 }
 
 static void do_help(int argc UNUSED, char *argv[] UNUSED)
@@ -638,5 +672,6 @@ static struct command all_commands[] = {
     { "dump-tables", 1, 1, do_dump_tables },
     { "dump-flows", 1, 2, do_dump_flows },
     { "add-flows", 2, 2, do_add_flows },
+    { "del-flows", 1, 2, do_del_flows },
     { "dump-ports", 1, 1, do_dump_ports },
 };