ofproto: Start work to enable datapaths with built-in wildcard support.
[sliver-openvswitch.git] / ofproto / fail-open.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "fail-open.h"
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include "flow.h"
22 #include "mac-learning.h"
23 #include "ofpbuf.h"
24 #include "ofproto.h"
25 #include "pktbuf.h"
26 #include "poll-loop.h"
27 #include "rconn.h"
28 #include "status.h"
29 #include "timeval.h"
30 #include "vconn.h"
31
32 #define THIS_MODULE VLM_fail_open
33 #include "vlog.h"
34
35 /*
36  * Fail-open mode.
37  *
38  * In fail-open mode, the switch detects when the controller cannot be
39  * contacted or when the controller is dropping switch connections because the
40  * switch does not pass its admission control policy.  In those situations the
41  * switch sets up flows itself using the "normal" action.
42  *
43  * There is a little subtlety to implementation, to properly handle the case
44  * where the controller allows switch connections but drops them a few seconds
45  * later for admission control reasons.  Because of this case, we don't want to
46  * just stop setting up flows when we connect to the controller: if we did,
47  * then new flow setup and existing flows would stop during the duration of
48  * connection to the controller, and thus the whole network would go down for
49  * that period of time.
50  *
51  * So, instead, we add some special caseswhen we are connected to a controller,
52  * but not yet sure that it has admitted us:
53  *
54  *     - We set up flows immediately ourselves, but simultaneously send out an
55  *       OFPT_PACKET_IN to the controller.  We put a special bogus buffer-id in
56  *       these OFPT_PACKET_IN messages so that duplicate packets don't get sent
57  *       out to the network when the controller replies.
58  *
59  *     - We also send out OFPT_PACKET_IN messages for totally bogus packets
60  *       every so often, in case no real new flows are arriving in the network.
61  *
62  *     - We don't flush the flow table at the time we connect, because this
63  *       could cause network stuttering in a switch with lots of flows or very
64  *       high-bandwidth flows by suddenly throwing lots of packets down to
65  *       userspace.
66  */
67
68 struct fail_open {
69     struct ofproto *ofproto;
70     struct rconn *controller;
71     int trigger_duration;
72     int last_disconn_secs;
73     struct status_category *ss_cat;
74     long long int next_bogus_packet_in;
75     struct rconn_packet_counter *bogus_packet_counter;
76 };
77
78 static void fail_open_recover(struct fail_open *);
79
80 /* Returns true if 'fo' should be in fail-open mode, otherwise false. */
81 static inline bool
82 should_fail_open(const struct fail_open *fo)
83 {
84     return rconn_failure_duration(fo->controller) >= fo->trigger_duration;
85 }
86
87 /* Returns true if 'fo' is currently in fail-open mode, otherwise false. */
88 bool
89 fail_open_is_active(const struct fail_open *fo)
90 {
91     return fo->last_disconn_secs != 0;
92 }
93
94 static void
95 send_bogus_packet_in(struct fail_open *fo)
96 {
97     uint8_t mac[ETH_ADDR_LEN];
98     struct ofpbuf *opi;
99     struct ofpbuf b;
100
101     /* Compose ofp_packet_in. */
102     ofpbuf_init(&b, 128);
103     eth_addr_nicira_random(mac);
104     compose_benign_packet(&b, "Open vSwitch Controller Probe", 0xa033, mac);
105     opi = make_packet_in(pktbuf_get_null(), OFPP_LOCAL, OFPR_NO_MATCH, &b, 64);
106     ofpbuf_uninit(&b);
107
108     /* Send. */
109     rconn_send_with_limit(fo->controller, opi, fo->bogus_packet_counter, 1);
110 }
111
112 /* Enter fail-open mode if we should be in it.  Handle reconnecting to a
113  * controller from fail-open mode. */
114 void
115 fail_open_run(struct fail_open *fo)
116 {
117     /* Enter fail-open mode if 'fo' is not in it but should be.  */
118     if (should_fail_open(fo)) {
119         int disconn_secs = rconn_failure_duration(fo->controller);
120         if (!fail_open_is_active(fo)) {
121             VLOG_WARN("Could not connect to controller (or switch failed "
122                       "controller's post-connection admission control "
123                       "policy) for %d seconds, failing open", disconn_secs);
124             fo->last_disconn_secs = disconn_secs;
125
126             /* Flush all OpenFlow and datapath flows.  We will set up our
127              * fail-open rule from fail_open_flushed() when
128              * ofproto_flush_flows() calls back to us. */
129             ofproto_flush_flows(fo->ofproto);
130         } else if (disconn_secs > fo->last_disconn_secs + 60) {
131             VLOG_INFO("Still in fail-open mode after %d seconds disconnected "
132                       "from controller", disconn_secs);
133             fo->last_disconn_secs = disconn_secs;
134         }
135     }
136
137     /* Schedule a bogus packet-in if we're connected and in fail-open. */
138     if (fail_open_is_active(fo)) {
139         if (rconn_is_connected(fo->controller)) {
140             bool expired = time_msec() >= fo->next_bogus_packet_in;
141             if (expired) {
142                 send_bogus_packet_in(fo);
143             }
144             if (expired || fo->next_bogus_packet_in == LLONG_MAX) {
145                 fo->next_bogus_packet_in = time_msec() + 2000;
146             }
147         } else {
148             fo->next_bogus_packet_in = LLONG_MAX;
149         }
150     }
151
152 }
153
154 /* If 'fo' is currently in fail-open mode and its rconn has connected to the
155  * controller, exits fail open mode. */
156 void
157 fail_open_maybe_recover(struct fail_open *fo)
158 {
159     if (rconn_is_admitted(fo->controller)) {
160         fail_open_recover(fo);
161     }
162 }
163
164 static void
165 fail_open_recover(struct fail_open *fo)
166 {
167     if (fail_open_is_active(fo)) {
168         flow_t flow;
169
170         VLOG_WARN("No longer in fail-open mode");
171         fo->last_disconn_secs = 0;
172         fo->next_bogus_packet_in = LLONG_MAX;
173
174         memset(&flow, 0, sizeof flow);
175         flow.wildcards = OFPFW_ALL;
176         flow.priority = FAIL_OPEN_PRIORITY;
177         ofproto_delete_flow(fo->ofproto, &flow);
178     }
179 }
180
181 void
182 fail_open_wait(struct fail_open *fo)
183 {
184     if (fo->next_bogus_packet_in != LLONG_MAX) {
185         poll_timer_wait(fo->next_bogus_packet_in - time_msec());
186     }
187 }
188
189 void
190 fail_open_flushed(struct fail_open *fo)
191 {
192     int disconn_secs = rconn_failure_duration(fo->controller);
193     bool open = disconn_secs >= fo->trigger_duration;
194     if (open) {
195         union ofp_action action;
196         flow_t flow;
197
198         /* Set up a flow that matches every packet and directs them to
199          * OFPP_NORMAL. */
200         memset(&action, 0, sizeof action);
201         action.type = htons(OFPAT_OUTPUT);
202         action.output.len = htons(sizeof action);
203         action.output.port = htons(OFPP_NORMAL);
204         memset(&flow, 0, sizeof flow);
205         flow.wildcards = OFPFW_ALL;
206         flow.priority = FAIL_OPEN_PRIORITY;
207         ofproto_add_flow(fo->ofproto, &flow, &action, 1, 0);
208     }
209 }
210
211 static void
212 fail_open_status_cb(struct status_reply *sr, void *fo_)
213 {
214     struct fail_open *fo = fo_;
215     int cur_duration = rconn_failure_duration(fo->controller);
216
217     status_reply_put(sr, "trigger-duration=%d", fo->trigger_duration);
218     status_reply_put(sr, "current-duration=%d", cur_duration);
219     status_reply_put(sr, "triggered=%s",
220                      cur_duration >= fo->trigger_duration ? "true" : "false");
221 }
222
223 struct fail_open *
224 fail_open_create(struct ofproto *ofproto,
225                  int trigger_duration, struct switch_status *switch_status,
226                  struct rconn *controller)
227 {
228     struct fail_open *fo = xmalloc(sizeof *fo);
229     fo->ofproto = ofproto;
230     fo->controller = controller;
231     fo->trigger_duration = trigger_duration;
232     fo->last_disconn_secs = 0;
233     fo->ss_cat = switch_status_register(switch_status, "fail-open",
234                                         fail_open_status_cb, fo);
235     fo->next_bogus_packet_in = LLONG_MAX;
236     fo->bogus_packet_counter = rconn_packet_counter_create();
237     return fo;
238 }
239
240 void
241 fail_open_set_trigger_duration(struct fail_open *fo, int trigger_duration)
242 {
243     fo->trigger_duration = trigger_duration;
244 }
245
246 void
247 fail_open_destroy(struct fail_open *fo)
248 {
249     if (fo) {
250         fail_open_recover(fo);
251         /* We don't own fo->controller. */
252         switch_status_unregister(fo->ss_cat);
253         rconn_packet_counter_destroy(fo->bogus_packet_counter);
254         free(fo);
255     }
256 }