netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / ofproto / fail-open.c
index 495197e..a288054 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016 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 "flow.h"
 #include "mac-learning.h"
 #include "odp-util.h"
+#include "ofpbuf.h"
 #include "ofp-actions.h"
 #include "ofp-util.h"
-#include "ofpbuf.h"
 #include "ofproto.h"
 #include "ofproto-provider.h"
 #include "pktbuf.h"
+#include "dp-packet.h"
 #include "poll-loop.h"
 #include "rconn.h"
 #include "timeval.h"
-#include "vconn.h"
-#include "vlog.h"
+#include "openvswitch/vconn.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(fail_open);
 
@@ -76,6 +77,7 @@ struct fail_open {
     int last_disconn_secs;
     long long int next_bogus_packet_in;
     struct rconn_packet_counter *bogus_packet_counter;
+    bool fail_open_active;
 };
 
 static void fail_open_recover(struct fail_open *);
@@ -116,23 +118,34 @@ fail_open_is_active(const struct fail_open *fo)
 static void
 send_bogus_packet_ins(struct fail_open *fo)
 {
-    struct ofputil_packet_in pin;
-    uint8_t mac[ETH_ADDR_LEN];
-    struct ofpbuf b;
+    struct eth_addr mac;
+    struct dp_packet b;
 
-    ofpbuf_init(&b, 128);
-    eth_addr_nicira_random(mac);
+    dp_packet_init(&b, 128);
+    eth_addr_nicira_random(&mac);
     compose_rarp(&b, mac);
 
-    memset(&pin, 0, sizeof pin);
-    pin.packet = b.data;
-    pin.packet_len = b.size;
-    pin.reason = OFPR_NO_MATCH;
-    pin.send_len = b.size;
-    pin.fmd.in_port = OFPP_LOCAL;
-    connmgr_send_packet_in(fo->connmgr, &pin);
+    struct ofproto_async_msg am = {
+        .oam = OAM_PACKET_IN,
+        .pin = {
+            .up = {
+                .public = {
+                    .packet = dp_packet_data(&b),
+                    .packet_len = dp_packet_size(&b),
+                    .flow_metadata = MATCH_CATCHALL_INITIALIZER,
+                    .flow_metadata.flow.in_port.ofp_port = OFPP_LOCAL,
+                    .flow_metadata.wc.masks.in_port.ofp_port
+                    = u16_to_ofp(UINT16_MAX),
+                    .reason = OFPR_NO_MATCH,
+                    .cookie = OVS_BE64_MAX,
+                },
+            },
+            .max_len = UINT16_MAX,
+        }
+    };
+    connmgr_send_async_msg(fo->connmgr, &am);
 
-    ofpbuf_uninit(&b);
+    dp_packet_uninit(&b);
 }
 
 /* Enter fail-open mode if we should be in it. */
@@ -181,6 +194,7 @@ fail_open_run(struct fail_open *fo)
  * controller, exits fail open mode. */
 void
 fail_open_maybe_recover(struct fail_open *fo)
+    OVS_EXCLUDED(ofproto_mutex)
 {
     if (fail_open_is_active(fo)
         && connmgr_is_any_controller_admitted(fo->connmgr)) {
@@ -190,15 +204,16 @@ fail_open_maybe_recover(struct fail_open *fo)
 
 static void
 fail_open_recover(struct fail_open *fo)
+    OVS_EXCLUDED(ofproto_mutex)
 {
-    struct cls_rule rule;
+    struct match match;
 
     VLOG_WARN("No longer in fail-open mode");
     fo->last_disconn_secs = 0;
     fo->next_bogus_packet_in = LLONG_MAX;
 
-    cls_rule_init_catchall(&rule, FAIL_OPEN_PRIORITY);
-    ofproto_delete_flow(fo->ofproto, &rule);
+    match_init_catchall(&match);
+    ofproto_delete_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY);
 }
 
 void
@@ -211,24 +226,34 @@ fail_open_wait(struct fail_open *fo)
 
 void
 fail_open_flushed(struct fail_open *fo)
+    OVS_EXCLUDED(ofproto_mutex)
 {
     int disconn_secs = connmgr_failure_duration(fo->connmgr);
     bool open = disconn_secs >= trigger_duration(fo);
     if (open) {
         struct ofpbuf ofpacts;
-        struct cls_rule rule;
+        struct match match;
 
         /* Set up a flow that matches every packet and directs them to
          * OFPP_NORMAL. */
         ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
         ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
-        ofpact_pad(&ofpacts);
 
-        cls_rule_init_catchall(&rule, FAIL_OPEN_PRIORITY);
-        ofproto_add_flow(fo->ofproto, &rule, ofpacts.data, ofpacts.size);
+        match_init_catchall(&match);
+        ofproto_add_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY,
+                         ofpacts.data, ofpacts.size);
 
         ofpbuf_uninit(&ofpacts);
     }
+    fo->fail_open_active = open;
+}
+
+/* Returns the number of fail-open rules currently installed in the flow
+ * table. */
+int
+fail_open_count_rules(const struct fail_open *fo)
+{
+    return fo->fail_open_active != 0;
 }
 
 /* Creates and returns a new struct fail_open for 'ofproto' and 'mgr'. */
@@ -241,12 +266,14 @@ fail_open_create(struct ofproto *ofproto, struct connmgr *mgr)
     fo->last_disconn_secs = 0;
     fo->next_bogus_packet_in = LLONG_MAX;
     fo->bogus_packet_counter = rconn_packet_counter_create();
+    fo->fail_open_active = false;
     return fo;
 }
 
 /* Destroys 'fo'. */
 void
 fail_open_destroy(struct fail_open *fo)
+    OVS_EXCLUDED(ofproto_mutex)
 {
     if (fo) {
         if (fail_open_is_active(fo)) {