X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=ofproto%2Ffail-open.c;h=a28acefdc9d0feaf4009a3dd7ac178db608cd8af;hb=a2b53dec11a6d001fd024ba8729de5c63bd2b6a1;hp=2a8fb955f53ef16a1d9a372594f37361dd18d6d1;hpb=ae412e7dd89489911a32c6723d4109c6d1de3a38;p=cascardo%2Fovs.git diff --git a/ofproto/fail-open.c b/ofproto/fail-open.c index 2a8fb955f..a28acefdc 100644 --- a/ofproto/fail-open.c +++ b/ofproto/fail-open.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * 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. @@ -18,21 +18,25 @@ #include "fail-open.h" #include #include +#include "classifier.h" +#include "connmgr.h" #include "flow.h" #include "mac-learning.h" #include "odp-util.h" -#include "ofp-util.h" #include "ofpbuf.h" +#include "ofp-actions.h" +#include "ofp-util.h" #include "ofproto.h" +#include "ofproto-provider.h" #include "pktbuf.h" +#include "dp-packet.h" #include "poll-loop.h" #include "rconn.h" -#include "status.h" #include "timeval.h" -#include "vconn.h" -#include "vlog.h" +#include "openvswitch/vconn.h" +#include "openvswitch/vlog.h" -VLOG_DEFINE_THIS_MODULE(fail_open) +VLOG_DEFINE_THIS_MODULE(fail_open); /* * Fail-open mode. @@ -69,12 +73,11 @@ VLOG_DEFINE_THIS_MODULE(fail_open) struct fail_open { struct ofproto *ofproto; - struct rconn **controllers; - size_t n_controllers; + struct connmgr *connmgr; int last_disconn_secs; - struct status_category *ss_cat; 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 *); @@ -84,7 +87,7 @@ static void fail_open_recover(struct fail_open *); static int trigger_duration(const struct fail_open *fo) { - if (!fo->n_controllers) { + if (!connmgr_has_controllers(fo->connmgr)) { /* Shouldn't ever arrive here, but if we do, never fail open. */ return INT_MAX; } else { @@ -101,38 +104,10 @@ trigger_duration(const struct fail_open *fo) * - The third interval is the time allowed to reconnect after no * response is received. */ - int max_probe_interval; - size_t i; - - max_probe_interval = 0; - for (i = 0; i < fo->n_controllers; i++) { - int probe_interval = rconn_get_probe_interval(fo->controllers[i]); - max_probe_interval = MAX(max_probe_interval, probe_interval); - } - return max_probe_interval * 3; + return connmgr_get_max_probe_interval(fo->connmgr) * 3; } } -/* Returns the number of seconds for which all controllers have been - * disconnected. */ -static int -failure_duration(const struct fail_open *fo) -{ - int min_failure_duration; - size_t i; - - if (!fo->n_controllers) { - return 0; - } - - min_failure_duration = INT_MAX; - for (i = 0; i < fo->n_controllers; i++) { - int failure_duration = rconn_failure_duration(fo->controllers[i]); - min_failure_duration = MIN(min_failure_duration, failure_duration); - } - return min_failure_duration; -} - /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */ bool fail_open_is_active(const struct fail_open *fo) @@ -140,72 +115,42 @@ fail_open_is_active(const struct fail_open *fo) return fo->last_disconn_secs != 0; } -/* Returns true if at least one controller is connected (regardless of whether - * those controllers are believed to have authenticated and accepted this - * switch), false if none of them are connected. */ -static bool -any_controller_is_connected(const struct fail_open *fo) -{ - size_t i; - - for (i = 0; i < fo->n_controllers; i++) { - if (rconn_is_connected(fo->controllers[i])) { - return true; - } - } - return false; -} - -/* Returns true if at least one controller is believed to have authenticated - * and accepted this switch, false otherwise. */ -static bool -any_controller_is_admitted(const struct fail_open *fo) -{ - size_t i; - - for (i = 0; i < fo->n_controllers; i++) { - if (rconn_is_admitted(fo->controllers[i])) { - return true; - } - } - return false; -} - -static void -send_bogus_packet_in(struct fail_open *fo, struct rconn *rconn) -{ - uint8_t mac[ETH_ADDR_LEN]; - struct ofpbuf *opi; - struct ofpbuf b; - - /* Compose ofp_packet_in. */ - ofpbuf_init(&b, 128); - eth_addr_nicira_random(mac); - compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac); - opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64); - ofpbuf_uninit(&b); - - /* Send. */ - rconn_send_with_limit(rconn, opi, fo->bogus_packet_counter, 1); -} - static void send_bogus_packet_ins(struct fail_open *fo) { - size_t i; - - for (i = 0; i < fo->n_controllers; i++) { - if (rconn_is_connected(fo->controllers[i])) { - send_bogus_packet_in(fo, fo->controllers[i]); + struct eth_addr mac; + struct dp_packet b; + + dp_packet_init(&b, 128); + eth_addr_nicira_random(&mac); + compose_rarp(&b, mac); + + struct ofproto_async_msg am = { + .oam = OAM_PACKET_IN, + .pin = { + .up = { + .packet = dp_packet_data(&b), + .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); + + dp_packet_uninit(&b); } /* Enter fail-open mode if we should be in it. */ void fail_open_run(struct fail_open *fo) { - int disconn_secs = failure_duration(fo); + int disconn_secs = connmgr_failure_duration(fo->connmgr); /* Enter fail-open mode if 'fo' is not in it but should be. */ if (disconn_secs >= trigger_duration(fo)) { @@ -228,7 +173,7 @@ fail_open_run(struct fail_open *fo) /* Schedule a bogus packet-in if we're connected and in fail-open. */ if (fail_open_is_active(fo)) { - if (any_controller_is_connected(fo)) { + if (connmgr_is_any_controller_connected(fo->connmgr)) { bool expired = time_msec() >= fo->next_bogus_packet_in; if (expired) { send_bogus_packet_ins(fo); @@ -247,25 +192,26 @@ 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 (any_controller_is_admitted(fo)) { + if (fail_open_is_active(fo) + && connmgr_is_any_controller_admitted(fo->connmgr)) { fail_open_recover(fo); } } static void fail_open_recover(struct fail_open *fo) + OVS_EXCLUDED(ofproto_mutex) { - if (fail_open_is_active(fo)) { - struct flow flow; + struct match match; - VLOG_WARN("No longer in fail-open mode"); - fo->last_disconn_secs = 0; - fo->next_bogus_packet_in = LLONG_MAX; + VLOG_WARN("No longer in fail-open mode"); + fo->last_disconn_secs = 0; + fo->next_bogus_packet_in = LLONG_MAX; - memset(&flow, 0, sizeof flow); - ofproto_delete_flow(fo->ofproto, &flow, OVSFW_ALL, FAIL_OPEN_PRIORITY); - } + match_init_catchall(&match); + ofproto_delete_flow(fo->ofproto, &match, FAIL_OPEN_PRIORITY); } void @@ -278,83 +224,60 @@ fail_open_wait(struct fail_open *fo) void fail_open_flushed(struct fail_open *fo) + OVS_EXCLUDED(ofproto_mutex) { - int disconn_secs = failure_duration(fo); + int disconn_secs = connmgr_failure_duration(fo->connmgr); bool open = disconn_secs >= trigger_duration(fo); if (open) { - union ofp_action action; - struct flow flow; + struct ofpbuf ofpacts; + struct match match; /* Set up a flow that matches every packet and directs them to * OFPP_NORMAL. */ - memset(&action, 0, sizeof action); - action.type = htons(OFPAT_OUTPUT); - action.output.len = htons(sizeof action); - action.output.port = htons(OFPP_NORMAL); - memset(&flow, 0, sizeof flow); - ofproto_add_flow(fo->ofproto, &flow, OVSFW_ALL, FAIL_OPEN_PRIORITY, - &action, 1, 0); + ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE); + ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL; + + 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; } -static void -fail_open_status_cb(struct status_reply *sr, void *fo_) +/* Returns the number of fail-open rules currently installed in the flow + * table. */ +int +fail_open_count_rules(const struct fail_open *fo) { - struct fail_open *fo = fo_; - int cur_duration = failure_duration(fo); - int trigger = trigger_duration(fo); - - status_reply_put(sr, "trigger-duration=%d", trigger); - status_reply_put(sr, "current-duration=%d", cur_duration); - status_reply_put(sr, "triggered=%s", - cur_duration >= trigger ? "true" : "false"); + return fo->fail_open_active != 0; } -/* Creates and returns a new struct fail_open for 'ofproto', registering switch - * status with 'switch_status'. - * - * The caller should register its set of controllers with - * fail_open_set_controllers(). (There should be at least one controller, - * otherwise there isn't any point in having the struct fail_open around.) */ +/* Creates and returns a new struct fail_open for 'ofproto' and 'mgr'. */ struct fail_open * -fail_open_create(struct ofproto *ofproto, struct switch_status *switch_status) +fail_open_create(struct ofproto *ofproto, struct connmgr *mgr) { struct fail_open *fo = xmalloc(sizeof *fo); fo->ofproto = ofproto; - fo->controllers = NULL; - fo->n_controllers = 0; + fo->connmgr = mgr; fo->last_disconn_secs = 0; - fo->ss_cat = switch_status_register(switch_status, "fail-open", - fail_open_status_cb, fo); fo->next_bogus_packet_in = LLONG_MAX; fo->bogus_packet_counter = rconn_packet_counter_create(); + fo->fail_open_active = false; return fo; } -/* Registers the 'n' rconns in 'rconns' as connections to the controller for - * 'fo'. The caller must ensure that all of the rconns remain valid until 'fo' - * is destroyed or a new set is registered in a subsequent call. - * - * Takes ownership of the 'rconns' array, but not of the rconns that it points - * to (of which the caller retains ownership). */ -void -fail_open_set_controllers(struct fail_open *fo, - struct rconn **rconns, size_t n) -{ - free(fo->controllers); - fo->controllers = rconns; - fo->n_controllers = n; -} - /* Destroys 'fo'. */ void fail_open_destroy(struct fail_open *fo) + OVS_EXCLUDED(ofproto_mutex) { if (fo) { - fail_open_recover(fo); - free(fo->controllers); - /* We don't own the rconns behind fo->controllers. */ - switch_status_unregister(fo->ss_cat); + if (fail_open_is_active(fo)) { + fail_open_recover(fo); + } + /* We don't own fo->connmgr. */ rconn_packet_counter_destroy(fo->bogus_packet_counter); free(fo); }