]>
Commit | Line | Data |
---|---|---|
064af421 BP |
1 | /* |
2 | * Copyright (c) 2008, 2009 Nicira Networks. | |
3 | * | |
4 | * Permission to use, copy, modify, and/or distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
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 "dpif.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 secure channel. */ | |
47 | IBR_TO_LOCAL_PORT, /* Sent to secure channel. */ | |
48 | IBR_ARP_FROM_CTL, /* ARP from the controller. */ | |
49 | IBR_TO_CTL_OFP_SRC, /* To controller, OpenFlow source port. */ | |
50 | IBR_TO_CTL_OFP_DST, /* To controller, OpenFlow dest port. */ | |
51 | IBR_FROM_CTL_OFP_SRC, /* From controller, OpenFlow source port. */ | |
52 | IBR_FROM_CTL_OFP_DST, /* From controller, OpenFlow dest port. */ | |
53 | #if OFP_TCP_PORT != OFP_SSL_PORT | |
54 | #error Need to support separate TCP and SSL flows. | |
55 | #endif | |
56 | N_IB_RULES | |
57 | }; | |
58 | ||
59 | struct ib_rule { | |
60 | bool installed; | |
61 | flow_t flow; | |
62 | uint32_t wildcards; | |
63 | unsigned int priority; | |
64 | }; | |
65 | ||
66 | struct in_band { | |
67 | struct ofproto *ofproto; | |
68 | struct netdev *netdev; | |
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 | time_t next_refresh; /* Next time to refresh MAC address. */ | |
78 | ||
79 | /* Keeping track of the local port's MAC address. */ | |
80 | uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */ | |
81 | time_t next_local_refresh; /* Next time to refresh MAC address. */ | |
82 | ||
83 | /* Rules that we set up. */ | |
84 | struct ib_rule rules[N_IB_RULES]; | |
85 | }; | |
86 | ||
87 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60); | |
88 | ||
89 | static const uint8_t * | |
90 | get_controller_mac(struct in_band *ib) | |
91 | { | |
92 | time_t now = time_now(); | |
93 | uint32_t ip; | |
94 | ||
95 | ip = rconn_get_ip(ib->controller); | |
96 | if (ip != ib->ip || now >= ib->next_refresh) { | |
97 | bool have_mac; | |
98 | ||
99 | ib->ip = ip; | |
100 | ||
101 | /* Look up MAC address. */ | |
102 | memset(ib->mac, 0, sizeof ib->mac); | |
103 | if (ib->ip) { | |
104 | int retval = netdev_arp_lookup(ib->netdev, ib->ip, ib->mac); | |
105 | if (retval) { | |
106 | VLOG_DBG_RL(&rl, "cannot look up controller hw address " | |
107 | "("IP_FMT"): %s", | |
108 | IP_ARGS(&ib->ip), strerror(retval)); | |
109 | } | |
110 | } | |
111 | have_mac = !eth_addr_is_zero(ib->mac); | |
112 | ||
113 | /* Log changes in IP, MAC addresses. */ | |
114 | if (ib->ip && ib->ip != ib->last_ip) { | |
115 | VLOG_DBG("controller IP address changed from "IP_FMT | |
116 | " to "IP_FMT, IP_ARGS(&ib->last_ip), IP_ARGS(&ib->ip)); | |
117 | ib->last_ip = ib->ip; | |
118 | } | |
119 | if (have_mac && memcmp(ib->last_mac, ib->mac, ETH_ADDR_LEN)) { | |
120 | VLOG_DBG("controller MAC address changed from "ETH_ADDR_FMT" to " | |
121 | ETH_ADDR_FMT, | |
122 | ETH_ADDR_ARGS(ib->last_mac), ETH_ADDR_ARGS(ib->mac)); | |
123 | memcpy(ib->last_mac, ib->mac, ETH_ADDR_LEN); | |
124 | } | |
125 | ||
126 | /* Schedule next refresh. | |
127 | * | |
128 | * If we have an IP address but not a MAC address, then refresh | |
129 | * quickly, since we probably will get a MAC address soon (via ARP). | |
130 | * Otherwise, we can afford to wait a little while. */ | |
131 | ib->next_refresh = now + (!ib->ip || have_mac ? 10 : 1); | |
132 | } | |
133 | return !eth_addr_is_zero(ib->mac) ? ib->mac : NULL; | |
134 | } | |
135 | ||
136 | static const uint8_t * | |
137 | get_local_mac(struct in_band *ib) | |
138 | { | |
139 | time_t now = time_now(); | |
140 | if (now >= ib->next_local_refresh) { | |
141 | uint8_t ea[ETH_ADDR_LEN]; | |
142 | if (!netdev_nodev_get_etheraddr(netdev_get_name(ib->netdev), ea)) { | |
143 | memcpy(ib->local_mac, ea, ETH_ADDR_LEN); | |
144 | } | |
145 | ib->next_local_refresh = now + 1; | |
146 | } | |
147 | return !eth_addr_is_zero(ib->local_mac) ? ib->local_mac : NULL; | |
148 | } | |
149 | ||
150 | static void | |
151 | in_band_status_cb(struct status_reply *sr, void *in_band_) | |
152 | { | |
153 | struct in_band *in_band = in_band_; | |
154 | struct in_addr local_ip; | |
155 | const uint8_t *local_mac; | |
156 | uint32_t controller_ip; | |
157 | const uint8_t *controller_mac; | |
158 | ||
159 | if (netdev_get_in4(in_band->netdev, &local_ip)) { | |
160 | status_reply_put(sr, "local-ip="IP_FMT, IP_ARGS(&local_ip.s_addr)); | |
161 | } | |
162 | local_mac = get_local_mac(in_band); | |
163 | if (local_mac) { | |
164 | status_reply_put(sr, "local-mac="ETH_ADDR_FMT, | |
165 | ETH_ADDR_ARGS(local_mac)); | |
166 | } | |
167 | ||
168 | controller_ip = rconn_get_ip(in_band->controller); | |
169 | if (controller_ip) { | |
170 | status_reply_put(sr, "controller-ip="IP_FMT, | |
171 | IP_ARGS(&controller_ip)); | |
172 | } | |
173 | controller_mac = get_controller_mac(in_band); | |
174 | if (controller_mac) { | |
175 | status_reply_put(sr, "controller-mac="ETH_ADDR_FMT, | |
176 | ETH_ADDR_ARGS(controller_mac)); | |
177 | } | |
178 | } | |
179 | ||
180 | static void | |
181 | drop_flow(struct in_band *in_band, int rule_idx) | |
182 | { | |
183 | struct ib_rule *rule = &in_band->rules[rule_idx]; | |
184 | ||
185 | if (rule->installed) { | |
186 | rule->installed = false; | |
187 | ofproto_delete_flow(in_band->ofproto, &rule->flow, rule->wildcards, | |
188 | rule->priority); | |
189 | } | |
190 | } | |
191 | ||
192 | /* out_port and fixed_fields are assumed never to change. */ | |
193 | static void | |
194 | setup_flow(struct in_band *in_band, int rule_idx, const flow_t *flow, | |
195 | uint32_t fixed_fields, uint16_t out_port) | |
196 | { | |
197 | struct ib_rule *rule = &in_band->rules[rule_idx]; | |
198 | ||
199 | if (!rule->installed || memcmp(flow, &rule->flow, sizeof *flow)) { | |
200 | union ofp_action action; | |
201 | ||
202 | drop_flow(in_band, rule_idx); | |
203 | ||
204 | rule->installed = true; | |
205 | rule->flow = *flow; | |
206 | rule->wildcards = OFPFW_ALL & ~fixed_fields; | |
207 | rule->priority = IB_BASE_PRIORITY + (N_IB_RULES - rule_idx); | |
208 | ||
209 | action.type = htons(OFPAT_OUTPUT); | |
210 | action.output.len = htons(sizeof action); | |
211 | action.output.port = htons(out_port); | |
212 | action.output.max_len = htons(0); | |
213 | ofproto_add_flow(in_band->ofproto, &rule->flow, rule->wildcards, | |
214 | rule->priority, &action, 1, 0); | |
215 | } | |
216 | } | |
217 | ||
218 | void | |
219 | in_band_run(struct in_band *in_band) | |
220 | { | |
221 | const uint8_t *controller_mac; | |
222 | const uint8_t *local_mac; | |
223 | flow_t flow; | |
224 | ||
225 | if (time_now() < MIN(in_band->next_refresh, in_band->next_local_refresh)) { | |
226 | return; | |
227 | } | |
228 | controller_mac = get_controller_mac(in_band); | |
229 | local_mac = get_local_mac(in_band); | |
230 | ||
231 | /* Switch traffic sent by the secure channel. */ | |
232 | memset(&flow, 0, sizeof flow); | |
233 | flow.in_port = ODPP_LOCAL; | |
234 | setup_flow(in_band, IBR_FROM_LOCAL_PORT, &flow, OFPFW_IN_PORT, | |
235 | OFPP_NORMAL); | |
236 | ||
237 | /* Deliver traffic sent to the secure channel to the local port. */ | |
238 | if (local_mac) { | |
239 | memset(&flow, 0, sizeof flow); | |
240 | memcpy(flow.dl_dst, local_mac, ETH_ADDR_LEN); | |
241 | setup_flow(in_band, IBR_TO_LOCAL_PORT, &flow, OFPFW_DL_DST, | |
242 | OFPP_NORMAL); | |
243 | } else { | |
244 | drop_flow(in_band, IBR_TO_LOCAL_PORT); | |
245 | } | |
246 | ||
247 | if (controller_mac) { | |
248 | /* Switch ARP requests sent by the controller. (OFPP_NORMAL will "do | |
249 | * the right thing" regarding VLANs here.) */ | |
250 | memset(&flow, 0, sizeof flow); | |
251 | flow.dl_type = htons(ETH_TYPE_ARP); | |
252 | memcpy(flow.dl_dst, eth_addr_broadcast, ETH_ADDR_LEN); | |
253 | memcpy(flow.dl_src, controller_mac, ETH_ADDR_LEN); | |
254 | setup_flow(in_band, IBR_ARP_FROM_CTL, &flow, | |
255 | OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_DL_SRC, | |
256 | OFPP_NORMAL); | |
257 | ||
258 | /* OpenFlow traffic to or from the controller. | |
259 | * | |
260 | * (A given field's value is completely ignored if it is wildcarded, | |
261 | * which is why we can get away with using a single 'flow' in each | |
262 | * case here.) */ | |
263 | memset(&flow, 0, sizeof flow); | |
264 | flow.dl_type = htons(ETH_TYPE_IP); | |
265 | memcpy(flow.dl_src, controller_mac, ETH_ADDR_LEN); | |
266 | memcpy(flow.dl_dst, controller_mac, ETH_ADDR_LEN); | |
267 | flow.nw_proto = IP_TYPE_TCP; | |
268 | flow.tp_src = htons(OFP_TCP_PORT); | |
269 | flow.tp_dst = htons(OFP_TCP_PORT); | |
270 | setup_flow(in_band, IBR_TO_CTL_OFP_SRC, &flow, | |
271 | (OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_NW_PROTO | |
272 | | OFPFW_TP_SRC), OFPP_NORMAL); | |
273 | setup_flow(in_band, IBR_TO_CTL_OFP_DST, &flow, | |
274 | (OFPFW_DL_TYPE | OFPFW_DL_DST | OFPFW_NW_PROTO | |
275 | | OFPFW_TP_DST), OFPP_NORMAL); | |
276 | setup_flow(in_band, IBR_FROM_CTL_OFP_SRC, &flow, | |
277 | (OFPFW_DL_TYPE | OFPFW_DL_SRC | OFPFW_NW_PROTO | |
278 | | OFPFW_TP_SRC), OFPP_NORMAL); | |
279 | setup_flow(in_band, IBR_FROM_CTL_OFP_DST, &flow, | |
280 | (OFPFW_DL_TYPE | OFPFW_DL_SRC | OFPFW_NW_PROTO | |
281 | | OFPFW_TP_DST), OFPP_NORMAL); | |
282 | } else { | |
283 | drop_flow(in_band, IBR_ARP_FROM_CTL); | |
284 | drop_flow(in_band, IBR_TO_CTL_OFP_DST); | |
285 | drop_flow(in_band, IBR_TO_CTL_OFP_SRC); | |
286 | drop_flow(in_band, IBR_FROM_CTL_OFP_DST); | |
287 | drop_flow(in_band, IBR_FROM_CTL_OFP_SRC); | |
288 | } | |
289 | } | |
290 | ||
291 | void | |
292 | in_band_wait(struct in_band *in_band) | |
293 | { | |
294 | time_t now = time_now(); | |
295 | time_t wakeup = MIN(in_band->next_refresh, in_band->next_local_refresh); | |
296 | if (wakeup > now) { | |
297 | poll_timer_wait((wakeup - now) * 1000); | |
298 | } else { | |
299 | poll_immediate_wake(); | |
300 | } | |
301 | } | |
302 | ||
303 | void | |
304 | in_band_flushed(struct in_band *in_band) | |
305 | { | |
306 | int i; | |
307 | ||
308 | for (i = 0; i < N_IB_RULES; i++) { | |
309 | in_band->rules[i].installed = false; | |
310 | } | |
311 | } | |
312 | ||
313 | int | |
314 | in_band_create(struct ofproto *ofproto, | |
315 | struct dpif *dpif, struct switch_status *ss, | |
316 | struct rconn *controller, struct in_band **in_bandp) | |
317 | { | |
318 | struct in_band *in_band; | |
319 | struct netdev *netdev; | |
320 | char local_name[IF_NAMESIZE]; | |
321 | int error; | |
322 | ||
323 | *in_bandp = NULL; | |
324 | error = dpif_get_name(dpif, local_name, sizeof local_name); | |
325 | if (error) { | |
326 | return error; | |
327 | } | |
328 | ||
329 | error = netdev_open(local_name, NETDEV_ETH_TYPE_NONE, &netdev); | |
330 | if (error) { | |
331 | VLOG_ERR("failed to open %s network device: %s", | |
332 | local_name, strerror(error)); | |
333 | return error; | |
334 | } | |
335 | ||
336 | in_band = xcalloc(1, sizeof *in_band); | |
337 | in_band->ofproto = ofproto; | |
338 | in_band->netdev = netdev; | |
339 | in_band->controller = controller; | |
340 | in_band->ss_cat = switch_status_register(ss, "in-band", | |
341 | in_band_status_cb, in_band); | |
342 | in_band->next_refresh = TIME_MIN; | |
343 | in_band->next_local_refresh = TIME_MIN; | |
344 | ||
345 | *in_bandp = in_band; | |
346 | return 0; | |
347 | } | |
348 | ||
349 | void | |
350 | in_band_destroy(struct in_band *in_band) | |
351 | { | |
352 | if (in_band) { | |
353 | netdev_close(in_band->netdev); | |
354 | switch_status_unregister(in_band->ss_cat); | |
355 | /* We don't own the rconn. */ | |
356 | } | |
357 | } | |
358 |