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