datapath: Remove WARN_ON_ONCE(1) now that this code has been exercised.
[sliver-openvswitch.git] / ofproto / in-band.c
1 /*
2  * Copyright (c) 2008, 2009 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 "in-band.h"
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <net/if.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "flow.h"
26 #include "mac-learning.h"
27 #include "netdev.h"
28 #include "odp-util.h"
29 #include "ofp-print.h"
30 #include "ofproto.h"
31 #include "ofpbuf.h"
32 #include "openflow/openflow.h"
33 #include "packets.h"
34 #include "poll-loop.h"
35 #include "rconn.h"
36 #include "status.h"
37 #include "timeval.h"
38 #include "vconn.h"
39
40 #define THIS_MODULE VLM_in_band
41 #include "vlog.h"
42
43 #define IB_BASE_PRIORITY 18181800
44
45 enum {
46     IBR_FROM_LOCAL_PORT,        /* Sent by the local port. */
47     IBR_OFP_TO_LOCAL,           /* Sent to secure channel on local port. */
48     IBR_ARP_FROM_LOCAL,         /* ARP from the local port. */
49     IBR_ARP_FROM_CTL,           /* ARP from the controller. */
50     IBR_TO_CTL_OFP_SRC,         /* To controller, OpenFlow source port. */
51     IBR_TO_CTL_OFP_DST,         /* To controller, OpenFlow dest port. */
52     IBR_FROM_CTL_OFP_SRC,       /* From controller, OpenFlow source port. */
53     IBR_FROM_CTL_OFP_DST,       /* From controller, OpenFlow dest port. */
54 #if OFP_TCP_PORT != OFP_SSL_PORT
55 #error Need to support separate TCP and SSL flows.
56 #endif
57     N_IB_RULES
58 };
59
60 struct ib_rule {
61     bool installed;
62     flow_t flow;
63     uint32_t wildcards;
64     unsigned int priority;
65 };
66
67 struct in_band {
68     struct ofproto *ofproto;
69     struct rconn *controller;
70     struct status_category *ss_cat;
71
72     /* Keeping track of controller's MAC address. */
73     uint32_t ip;                /* Current IP, 0 if unknown. */
74     uint32_t last_ip;           /* Last known IP, 0 if never known. */
75     uint8_t mac[ETH_ADDR_LEN];  /* Current MAC, 0 if unknown. */
76     uint8_t last_mac[ETH_ADDR_LEN]; /* Last known MAC, 0 if never known */
77     struct netdev *netdev;
78     time_t next_refresh;        /* Next time to refresh MAC address. */
79
80     /* Keeping track of the local port's MAC address. */
81     uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
82     time_t next_local_refresh;  /* Next time to refresh MAC address. */
83
84     /* Rules that we set up. */
85     struct ib_rule rules[N_IB_RULES];
86 };
87
88 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
89
90 static const uint8_t *
91 get_controller_mac(struct in_band *ib)
92 {
93     time_t now = time_now();
94     uint32_t controller_ip;
95
96     controller_ip = rconn_get_remote_ip(ib->controller);
97     if (controller_ip != ib->ip || now >= ib->next_refresh) {
98         bool have_mac;
99
100         ib->ip = controller_ip;
101
102         /* Look up MAC address. */
103         memset(ib->mac, 0, sizeof ib->mac);
104         if (ib->ip) {
105             struct in_addr local_in4 = { rconn_get_local_ip(ib->controller) };
106             struct in_addr in4;
107             int retval;
108
109             /* Refresh device with IP address 'in4'. */
110             if (!ib->netdev
111                 || netdev_get_in4(ib->netdev, &in4)
112                 || in4.s_addr != local_in4.s_addr)
113             {
114                 netdev_close(ib->netdev);
115                 ib->netdev = netdev_find_dev_by_in4(&local_in4);
116             }
117
118             if (ib->netdev) {
119                 retval = netdev_arp_lookup(ib->netdev, ib->ip, ib->mac);
120                 if (retval) {
121                     VLOG_DBG_RL(&rl, "cannot look up controller MAC address "
122                                 "("IP_FMT"): %s",
123                                 IP_ARGS(&ib->ip), strerror(retval));
124                 }
125             } else {
126                 VLOG_DBG_RL(&rl, "cannot find device with IP address "IP_FMT,
127                     IP_ARGS(&local_in4.s_addr));
128             }
129         }
130         have_mac = !eth_addr_is_zero(ib->mac);
131
132         /* Log changes in IP, MAC addresses. */
133         if (ib->ip && ib->ip != ib->last_ip) {
134             VLOG_DBG("controller IP address changed from "IP_FMT
135                      " to "IP_FMT, IP_ARGS(&ib->last_ip), IP_ARGS(&ib->ip));
136             ib->last_ip = ib->ip;
137         }
138         if (have_mac && memcmp(ib->last_mac, ib->mac, ETH_ADDR_LEN)) {
139             VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to "
140                      ETH_ADDR_FMT,
141                      ETH_ADDR_ARGS(ib->last_mac), ETH_ADDR_ARGS(ib->mac));
142             memcpy(ib->last_mac, ib->mac, ETH_ADDR_LEN);
143         }
144
145         /* Schedule next refresh.
146          *
147          * If we have an IP address but not a MAC address, then refresh
148          * quickly, since we probably will get a MAC address soon (via ARP).
149          * Otherwise, we can afford to wait a little while. */
150         ib->next_refresh = now + (!ib->ip || have_mac ? 10 : 1);
151     }
152     return !eth_addr_is_zero(ib->mac) ? ib->mac : NULL;
153 }
154
155 static const uint8_t *
156 get_local_mac(struct in_band *ib)
157 {
158     time_t now = time_now();
159     if (now >= ib->next_local_refresh) {
160         uint8_t ea[ETH_ADDR_LEN];
161         if (ib->netdev && !netdev_get_etheraddr(ib->netdev, ea)) {
162             memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
163         }
164         ib->next_local_refresh = now + 1;
165     }
166     return !eth_addr_is_zero(ib->local_mac) ? ib->local_mac : NULL;
167 }
168
169 static void
170 in_band_status_cb(struct status_reply *sr, void *in_band_)
171 {
172     struct in_band *in_band = in_band_;
173     const uint8_t *local_mac;
174     const uint8_t *controller_mac;
175
176     local_mac = get_local_mac(in_band);
177     if (local_mac) {
178         status_reply_put(sr, "local-mac="ETH_ADDR_FMT,
179                          ETH_ADDR_ARGS(local_mac));
180     }
181
182     controller_mac = get_controller_mac(in_band);
183     if (controller_mac) {
184         status_reply_put(sr, "controller-mac="ETH_ADDR_FMT,
185                          ETH_ADDR_ARGS(controller_mac));
186     }
187 }
188
189 static void
190 drop_flow(struct in_band *in_band, int rule_idx)
191 {
192     struct ib_rule *rule = &in_band->rules[rule_idx];
193
194     if (rule->installed) {
195         rule->installed = false;
196         ofproto_delete_flow(in_band->ofproto, &rule->flow, rule->wildcards,
197                             rule->priority);
198     }
199 }
200
201 /* out_port and fixed_fields are assumed never to change. */
202 static void
203 setup_flow(struct in_band *in_band, int rule_idx, const flow_t *flow,
204            uint32_t fixed_fields, uint16_t out_port)
205 {
206     struct ib_rule *rule = &in_band->rules[rule_idx];
207
208     if (!rule->installed || memcmp(flow, &rule->flow, sizeof *flow)) {
209         union ofp_action action;
210
211         drop_flow(in_band, rule_idx);
212
213         rule->installed = true;
214         rule->flow = *flow;
215         rule->wildcards = OFPFW_ALL & ~fixed_fields;
216         rule->priority = IB_BASE_PRIORITY + (N_IB_RULES - rule_idx);
217
218         action.type = htons(OFPAT_OUTPUT);
219         action.output.len = htons(sizeof action);
220         action.output.port = htons(out_port);
221         action.output.max_len = htons(0);
222         ofproto_add_flow(in_band->ofproto, &rule->flow, rule->wildcards,
223                          rule->priority, &action, 1, 0);
224     }
225 }
226
227 void
228 in_band_run(struct in_band *in_band)
229 {
230     const uint8_t *controller_mac;
231     const uint8_t *local_mac;
232     flow_t flow;
233
234     if (time_now() < MIN(in_band->next_refresh, in_band->next_local_refresh)) {
235         return;
236     }
237     controller_mac = get_controller_mac(in_band);
238     local_mac = get_local_mac(in_band);
239
240     /* Switch traffic sent by the local port. */
241     memset(&flow, 0, sizeof flow);
242     flow.in_port = ODPP_LOCAL;
243     setup_flow(in_band, IBR_FROM_LOCAL_PORT, &flow, OFPFW_IN_PORT,
244                OFPP_NORMAL);
245
246     if (local_mac) {
247         /* Deliver traffic sent to the connection's interface. */
248         memset(&flow, 0, sizeof flow);
249         memcpy(flow.dl_dst, local_mac, ETH_ADDR_LEN);
250         setup_flow(in_band, IBR_OFP_TO_LOCAL, &flow, OFPFW_DL_DST,
251                     OFPP_NORMAL);
252
253         /* Allow the connection's interface to be the source of ARP traffic. */
254         memset(&flow, 0, sizeof flow);
255         flow.dl_type = htons(ETH_TYPE_ARP);
256         memcpy(flow.dl_src, local_mac, ETH_ADDR_LEN);
257         setup_flow(in_band, IBR_ARP_FROM_LOCAL, &flow,
258                    OFPFW_DL_TYPE | OFPFW_DL_SRC, OFPP_NORMAL);
259     } else {
260         drop_flow(in_band, IBR_OFP_TO_LOCAL);
261         drop_flow(in_band, IBR_ARP_FROM_LOCAL);
262     }
263
264     if (controller_mac) {
265         /* Switch ARP requests sent by the controller.  (OFPP_NORMAL will "do
266          * the right thing" regarding VLANs here.) */
267         memset(&flow, 0, sizeof flow);
268         flow.dl_type = htons(ETH_TYPE_ARP);
269         memcpy(flow.dl_dst, eth_addr_broadcast, ETH_ADDR_LEN);
270         memcpy(flow.dl_src, controller_mac, ETH_ADDR_LEN);
271         setup_flow(in_band, IBR_ARP_FROM_CTL, &flow,
272                    OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_DL_SRC,
273                    OFPP_NORMAL);
274
275         /* OpenFlow traffic to or from the controller.
276          *
277          * (A given field's value is completely ignored if it is wildcarded,
278          * which is why we can get away with using a single 'flow' in each
279          * case here.) */
280         memset(&flow, 0, sizeof flow);
281         flow.dl_type = htons(ETH_TYPE_IP);
282         memcpy(flow.dl_src, controller_mac, ETH_ADDR_LEN);
283         memcpy(flow.dl_dst, controller_mac, ETH_ADDR_LEN);
284         flow.nw_proto = IP_TYPE_TCP;
285         flow.tp_src = htons(OFP_TCP_PORT);
286         flow.tp_dst = htons(OFP_TCP_PORT);
287         setup_flow(in_band, IBR_TO_CTL_OFP_SRC, &flow,
288                    (OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_NW_PROTO
289                     | OFPFW_TP_SRC), OFPP_NORMAL);
290         setup_flow(in_band, IBR_TO_CTL_OFP_DST, &flow,
291                    (OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_NW_PROTO
292                     | OFPFW_TP_DST), OFPP_NORMAL);
293         setup_flow(in_band, IBR_FROM_CTL_OFP_SRC, &flow,
294                    (OFPFW_DL_TYPE | OFPFW_DL_SRC | OFPFW_NW_PROTO
295                     | OFPFW_TP_SRC), OFPP_NORMAL);
296         setup_flow(in_band, IBR_FROM_CTL_OFP_DST, &flow,
297                    (OFPFW_DL_TYPE | OFPFW_DL_SRC | OFPFW_NW_PROTO
298                     | OFPFW_TP_DST), OFPP_NORMAL);
299     } else {
300         drop_flow(in_band, IBR_ARP_FROM_CTL);
301         drop_flow(in_band, IBR_TO_CTL_OFP_DST);
302         drop_flow(in_band, IBR_TO_CTL_OFP_SRC);
303         drop_flow(in_band, IBR_FROM_CTL_OFP_DST);
304         drop_flow(in_band, IBR_FROM_CTL_OFP_SRC);
305     }
306 }
307
308 void
309 in_band_wait(struct in_band *in_band)
310 {
311     time_t now = time_now();
312     time_t wakeup = MIN(in_band->next_refresh, in_band->next_local_refresh);
313     if (wakeup > now) {
314         poll_timer_wait((wakeup - now) * 1000);
315     } else {
316         poll_immediate_wake();
317     }
318 }
319
320 void
321 in_band_flushed(struct in_band *in_band)
322 {
323     int i;
324
325     for (i = 0; i < N_IB_RULES; i++) {
326         in_band->rules[i].installed = false;
327     }
328 }
329
330 void
331 in_band_create(struct ofproto *ofproto, struct switch_status *ss,
332                struct rconn *controller, struct in_band **in_bandp)
333 {
334     struct in_band *in_band;
335
336     in_band = xcalloc(1, sizeof *in_band);
337     in_band->ofproto = ofproto;
338     in_band->controller = controller;
339     in_band->ss_cat = switch_status_register(ss, "in-band",
340                                              in_band_status_cb, in_band);
341     in_band->next_refresh = TIME_MIN;
342     in_band->next_local_refresh = TIME_MIN;
343     in_band->netdev = NULL;
344
345     *in_bandp = in_band;
346 }
347
348 void
349 in_band_destroy(struct in_band *in_band)
350 {
351     if (in_band) {
352         switch_status_unregister(in_band->ss_cat);
353         netdev_close(in_band->netdev);
354         /* We don't own the rconn. */
355     }
356 }
357