]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
ce4a6b76 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "in-band.h" | |
19 | #include <arpa/inet.h> | |
20 | #include <errno.h> | |
21 | #include <inttypes.h> | |
9d82ec47 | 22 | #include <sys/socket.h> |
064af421 BP |
23 | #include <net/if.h> |
24 | #include <string.h> | |
26d9fe3b | 25 | #include <stdlib.h> |
cf3fad8a | 26 | #include "classifier.h" |
0ad9b732 | 27 | #include "dhcp.h" |
064af421 | 28 | #include "flow.h" |
064af421 | 29 | #include "netdev.h" |
cdee00fd | 30 | #include "netlink.h" |
064af421 | 31 | #include "odp-util.h" |
f25d0cf3 | 32 | #include "ofp-actions.h" |
064af421 BP |
33 | #include "ofproto.h" |
34 | #include "ofpbuf.h" | |
5bee6e26 | 35 | #include "ofproto-provider.h" |
064af421 BP |
36 | #include "openflow/openflow.h" |
37 | #include "packets.h" | |
38 | #include "poll-loop.h" | |
064af421 | 39 | #include "timeval.h" |
064af421 BP |
40 | #include "vlog.h" |
41 | ||
d98e6007 | 42 | VLOG_DEFINE_THIS_MODULE(in_band); |
5136ce49 | 43 | |
0ade584e BP |
44 | /* Priorities used in classifier for in-band rules. These values are higher |
45 | * than any that may be set with OpenFlow, and "18" kind of looks like "IB". | |
46 | * The ordering of priorities is not important because all of the rules set up | |
47 | * by in-band control have the same action. The only reason to use more than | |
48 | * one priority is to make the kind of flow easier to see during debugging. */ | |
064af421 | 49 | enum { |
d2ede7bc | 50 | /* One set per bridge. */ |
0ade584e | 51 | IBR_FROM_LOCAL_DHCP = 180000, /* (a) From local port, DHCP. */ |
85088747 JP |
52 | IBR_TO_LOCAL_ARP, /* (b) To local port, ARP. */ |
53 | IBR_FROM_LOCAL_ARP, /* (c) From local port, ARP. */ | |
d2ede7bc BP |
54 | |
55 | /* One set per unique next-hop MAC. */ | |
56 | IBR_TO_NEXT_HOP_ARP, /* (d) To remote MAC, ARP. */ | |
57 | IBR_FROM_NEXT_HOP_ARP, /* (e) From remote MAC, ARP. */ | |
58 | ||
59 | /* One set per unique remote IP address. */ | |
60 | IBR_TO_REMOTE_ARP, /* (f) To remote IP, ARP. */ | |
61 | IBR_FROM_REMOTE_ARP, /* (g) From remote IP, ARP. */ | |
62 | ||
63 | /* One set per unique remote (IP,port) pair. */ | |
64 | IBR_TO_REMOTE_TCP, /* (h) To remote IP, TCP port. */ | |
65 | IBR_FROM_REMOTE_TCP /* (i) From remote IP, TCP port. */ | |
064af421 BP |
66 | }; |
67 | ||
0ade584e BP |
68 | /* Track one remote IP and next hop information. */ |
69 | struct in_band_remote { | |
d2ede7bc | 70 | struct sockaddr_in remote_addr; /* IP address, in network byte order. */ |
0ade584e BP |
71 | uint8_t remote_mac[ETH_ADDR_LEN]; /* Next-hop MAC, all-zeros if unknown. */ |
72 | uint8_t last_remote_mac[ETH_ADDR_LEN]; /* Previous nonzero next-hop MAC. */ | |
73 | struct netdev *remote_netdev; /* Device to send to next-hop MAC. */ | |
74 | }; | |
75 | ||
7ee20df1 BP |
76 | /* What to do to an in_band_rule. */ |
77 | enum in_band_op { | |
78 | ADD, /* Add the rule to ofproto's flow table. */ | |
f72e3073 | 79 | DEL /* Delete the rule from ofproto's flow table. */ |
7ee20df1 BP |
80 | }; |
81 | ||
82 | /* A rule to add to or delete from ofproto's flow table. */ | |
83 | struct in_band_rule { | |
81a76618 BP |
84 | struct hmap_node hmap_node; /* In struct in_band's "rules" hmap. */ |
85 | struct match match; | |
86 | unsigned int priority; | |
7ee20df1 BP |
87 | enum in_band_op op; |
88 | }; | |
89 | ||
064af421 BP |
90 | struct in_band { |
91 | struct ofproto *ofproto; | |
7ee20df1 | 92 | int queue_id; |
064af421 | 93 | |
0ade584e BP |
94 | /* Remote information. */ |
95 | time_t next_remote_refresh; /* Refresh timer. */ | |
96 | struct in_band_remote *remotes; | |
97 | size_t n_remotes; | |
98 | ||
99 | /* Local information. */ | |
100 | time_t next_local_refresh; /* Refresh timer. */ | |
101 | uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */ | |
102 | struct netdev *local_netdev; /* Local port's network device. */ | |
103 | ||
7ee20df1 BP |
104 | /* Flow tracking. */ |
105 | struct hmap rules; /* Contains "struct in_band_rule"s. */ | |
064af421 BP |
106 | }; |
107 | ||
108 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60); | |
109 | ||
0ade584e BP |
110 | static int |
111 | refresh_remote(struct in_band *ib, struct in_band_remote *r) | |
064af421 | 112 | { |
0ade584e | 113 | struct in_addr next_hop_inaddr; |
f1acd62b | 114 | char *next_hop_dev; |
0ade584e | 115 | int retval; |
064af421 | 116 | |
0ade584e | 117 | /* Find the next-hop IP address. */ |
d2ede7bc BP |
118 | memset(r->remote_mac, 0, sizeof r->remote_mac); |
119 | retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr, | |
0ade584e BP |
120 | &next_hop_inaddr, &next_hop_dev); |
121 | if (retval) { | |
122 | VLOG_WARN("cannot find route for controller ("IP_FMT"): %s", | |
10a89ef0 BP |
123 | IP_ARGS(r->remote_addr.sin_addr.s_addr), |
124 | ovs_strerror(retval)); | |
0ade584e BP |
125 | return 1; |
126 | } | |
127 | if (!next_hop_inaddr.s_addr) { | |
d2ede7bc | 128 | next_hop_inaddr = r->remote_addr.sin_addr; |
0ade584e | 129 | } |
c752217a | 130 | |
d2ede7bc | 131 | /* Open the next-hop network device. */ |
0ade584e BP |
132 | if (!r->remote_netdev |
133 | || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev)) | |
134 | { | |
135 | netdev_close(r->remote_netdev); | |
064af421 | 136 | |
18812dff | 137 | retval = netdev_open(next_hop_dev, "system", &r->remote_netdev); |
0ad9b732 | 138 | if (retval) { |
0ade584e BP |
139 | VLOG_WARN_RL(&rl, "cannot open netdev %s (next hop " |
140 | "to controller "IP_FMT"): %s", | |
ed36537e | 141 | next_hop_dev, IP_ARGS(r->remote_addr.sin_addr.s_addr), |
10a89ef0 | 142 | ovs_strerror(retval)); |
0ade584e BP |
143 | free(next_hop_dev); |
144 | return 1; | |
064af421 | 145 | } |
0ade584e BP |
146 | } |
147 | free(next_hop_dev); | |
148 | ||
149 | /* Look up the MAC address of the next-hop IP address. */ | |
150 | retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr, | |
151 | r->remote_mac); | |
152 | if (retval) { | |
153 | VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s", | |
10a89ef0 | 154 | IP_ARGS(next_hop_inaddr.s_addr), ovs_strerror(retval)); |
064af421 | 155 | } |
0ad9b732 | 156 | |
6dee2066 BP |
157 | /* If we don't have a MAC address, then refresh quickly, since we probably |
158 | * will get a MAC address soon (via ARP). Otherwise, we can afford to wait | |
159 | * a little while. */ | |
160 | return eth_addr_is_zero(r->remote_mac) ? 1 : 10; | |
064af421 BP |
161 | } |
162 | ||
0ade584e BP |
163 | static bool |
164 | refresh_remotes(struct in_band *ib) | |
064af421 | 165 | { |
0ade584e BP |
166 | struct in_band_remote *r; |
167 | bool any_changes; | |
0ade584e BP |
168 | |
169 | if (time_now() < ib->next_remote_refresh) { | |
170 | return false; | |
171 | } | |
172 | ||
173 | any_changes = false; | |
5dbdfff7 | 174 | ib->next_remote_refresh = TIME_MAX; |
0ade584e BP |
175 | for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) { |
176 | uint8_t old_remote_mac[ETH_ADDR_LEN]; | |
5dbdfff7 | 177 | time_t next_refresh; |
0ade584e | 178 | |
6dee2066 | 179 | /* Save old MAC. */ |
0ade584e BP |
180 | memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN); |
181 | ||
182 | /* Refresh remote information. */ | |
5dbdfff7 BP |
183 | next_refresh = refresh_remote(ib, r) + time_now(); |
184 | ib->next_remote_refresh = MIN(ib->next_remote_refresh, next_refresh); | |
0ade584e | 185 | |
6dee2066 | 186 | /* If the MAC changed, log the changes. */ |
0ade584e BP |
187 | if (!eth_addr_equals(r->remote_mac, old_remote_mac)) { |
188 | any_changes = true; | |
189 | if (!eth_addr_is_zero(r->remote_mac) | |
190 | && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) { | |
191 | VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT | |
192 | " to "ETH_ADDR_FMT, | |
193 | ETH_ADDR_ARGS(r->last_remote_mac), | |
194 | ETH_ADDR_ARGS(r->remote_mac)); | |
195 | memcpy(r->last_remote_mac, r->remote_mac, ETH_ADDR_LEN); | |
196 | } | |
064af421 | 197 | } |
064af421 | 198 | } |
0ade584e BP |
199 | |
200 | return any_changes; | |
064af421 BP |
201 | } |
202 | ||
0ade584e BP |
203 | /* Refreshes the MAC address of the local port into ib->local_mac, if it is due |
204 | * for a refresh. Returns true if anything changed, otherwise false. */ | |
205 | static bool | |
206 | refresh_local(struct in_band *ib) | |
064af421 | 207 | { |
0ade584e BP |
208 | uint8_t ea[ETH_ADDR_LEN]; |
209 | time_t now; | |
064af421 | 210 | |
0ade584e BP |
211 | now = time_now(); |
212 | if (now < ib->next_local_refresh) { | |
213 | return false; | |
064af421 | 214 | } |
0ade584e | 215 | ib->next_local_refresh = now + 1; |
064af421 | 216 | |
0ade584e BP |
217 | if (netdev_get_etheraddr(ib->local_netdev, ea) |
218 | || eth_addr_equals(ea, ib->local_mac)) { | |
219 | return false; | |
064af421 | 220 | } |
064af421 | 221 | |
0ade584e BP |
222 | memcpy(ib->local_mac, ea, ETH_ADDR_LEN); |
223 | return true; | |
064af421 BP |
224 | } |
225 | ||
ce4a6b76 BP |
226 | /* Returns true if packets in 'flow' should be directed to the local port. |
227 | * (This keeps the flow table from preventing DHCP replies from being seen by | |
228 | * the local port.) */ | |
0ad9b732 | 229 | bool |
ce4a6b76 | 230 | in_band_must_output_to_local_port(const struct flow *flow) |
0ad9b732 | 231 | { |
ce4a6b76 | 232 | return (flow->dl_type == htons(ETH_TYPE_IP) |
6767a2cc | 233 | && flow->nw_proto == IPPROTO_UDP |
d295e8e9 | 234 | && flow->tp_src == htons(DHCP_SERVER_PORT) |
ce4a6b76 | 235 | && flow->tp_dst == htons(DHCP_CLIENT_PORT)); |
0ad9b732 JP |
236 | } |
237 | ||
0ade584e | 238 | static void |
81a76618 | 239 | add_rule(struct in_band *ib, const struct match *match, unsigned int priority) |
0ade584e | 240 | { |
81a76618 | 241 | uint32_t hash = match_hash(match, 0); |
7ee20df1 BP |
242 | struct in_band_rule *rule; |
243 | ||
81a76618 BP |
244 | HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &ib->rules) { |
245 | if (match_equal(&rule->match, match)) { | |
7ee20df1 BP |
246 | rule->op = ADD; |
247 | return; | |
248 | } | |
249 | } | |
250 | ||
251 | rule = xmalloc(sizeof *rule); | |
81a76618 BP |
252 | rule->match = *match; |
253 | rule->priority = priority; | |
7ee20df1 | 254 | rule->op = ADD; |
81a76618 | 255 | hmap_insert(&ib->rules, &rule->hmap_node, hash); |
7ee20df1 BP |
256 | } |
257 | ||
258 | static void | |
259 | update_rules(struct in_band *ib) | |
260 | { | |
261 | struct in_band_rule *ib_rule; | |
262 | struct in_band_remote *r; | |
81a76618 | 263 | struct match match; |
0ade584e | 264 | |
7ee20df1 BP |
265 | /* Mark all the existing rules for deletion. (Afterward we will re-add any |
266 | * rules that are still valid.) */ | |
81a76618 | 267 | HMAP_FOR_EACH (ib_rule, hmap_node, &ib->rules) { |
f72e3073 | 268 | ib_rule->op = DEL; |
7ee20df1 BP |
269 | } |
270 | ||
6da1e809 | 271 | if (ib->n_remotes && !eth_addr_is_zero(ib->local_mac)) { |
d2ede7bc | 272 | /* (a) Allow DHCP requests sent from the local port. */ |
81a76618 BP |
273 | match_init_catchall(&match); |
274 | match_set_in_port(&match, OFPP_LOCAL); | |
275 | match_set_dl_type(&match, htons(ETH_TYPE_IP)); | |
276 | match_set_dl_src(&match, ib->local_mac); | |
277 | match_set_nw_proto(&match, IPPROTO_UDP); | |
278 | match_set_tp_src(&match, htons(DHCP_CLIENT_PORT)); | |
279 | match_set_tp_dst(&match, htons(DHCP_SERVER_PORT)); | |
280 | add_rule(ib, &match, IBR_FROM_LOCAL_DHCP); | |
0ad9b732 | 281 | |
d2ede7bc | 282 | /* (b) Allow ARP replies to the local port's MAC address. */ |
81a76618 BP |
283 | match_init_catchall(&match); |
284 | match_set_dl_type(&match, htons(ETH_TYPE_ARP)); | |
285 | match_set_dl_dst(&match, ib->local_mac); | |
286 | match_set_nw_proto(&match, ARP_OP_REPLY); | |
287 | add_rule(ib, &match, IBR_TO_LOCAL_ARP); | |
26d9fe3b | 288 | |
d2ede7bc | 289 | /* (c) Allow ARP requests from the local port's MAC address. */ |
81a76618 BP |
290 | match_init_catchall(&match); |
291 | match_set_dl_type(&match, htons(ETH_TYPE_ARP)); | |
292 | match_set_dl_src(&match, ib->local_mac); | |
293 | match_set_nw_proto(&match, ARP_OP_REQUEST); | |
294 | add_rule(ib, &match, IBR_FROM_LOCAL_ARP); | |
0ad9b732 | 295 | } |
a5f37a2d | 296 | |
7ee20df1 BP |
297 | for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) { |
298 | const uint8_t *remote_mac = r->remote_mac; | |
0ade584e | 299 | |
7ee20df1 BP |
300 | if (eth_addr_is_zero(remote_mac)) { |
301 | continue; | |
0ade584e BP |
302 | } |
303 | ||
d2ede7bc | 304 | /* (d) Allow ARP replies to the next hop's MAC address. */ |
81a76618 BP |
305 | match_init_catchall(&match); |
306 | match_set_dl_type(&match, htons(ETH_TYPE_ARP)); | |
307 | match_set_dl_dst(&match, remote_mac); | |
308 | match_set_nw_proto(&match, ARP_OP_REPLY); | |
309 | add_rule(ib, &match, IBR_TO_NEXT_HOP_ARP); | |
0ade584e | 310 | |
d2ede7bc | 311 | /* (e) Allow ARP requests from the next hop's MAC address. */ |
81a76618 BP |
312 | match_init_catchall(&match); |
313 | match_set_dl_type(&match, htons(ETH_TYPE_ARP)); | |
314 | match_set_dl_src(&match, remote_mac); | |
315 | match_set_nw_proto(&match, ARP_OP_REQUEST); | |
316 | add_rule(ib, &match, IBR_FROM_NEXT_HOP_ARP); | |
064af421 BP |
317 | } |
318 | ||
7ee20df1 BP |
319 | for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) { |
320 | const struct sockaddr_in *a = &r->remote_addr; | |
0ade584e | 321 | |
7ee20df1 BP |
322 | /* (f) Allow ARP replies containing the remote's IP address as a |
323 | * target. */ | |
81a76618 BP |
324 | match_init_catchall(&match); |
325 | match_set_dl_type(&match, htons(ETH_TYPE_ARP)); | |
326 | match_set_nw_proto(&match, ARP_OP_REPLY); | |
327 | match_set_nw_dst(&match, a->sin_addr.s_addr); | |
328 | add_rule(ib, &match, IBR_TO_REMOTE_ARP); | |
c16e55cf | 329 | |
7ee20df1 BP |
330 | /* (g) Allow ARP requests containing the remote's IP address as a |
331 | * source. */ | |
81a76618 BP |
332 | match_init_catchall(&match); |
333 | match_set_dl_type(&match, htons(ETH_TYPE_ARP)); | |
334 | match_set_nw_proto(&match, ARP_OP_REQUEST); | |
335 | match_set_nw_src(&match, a->sin_addr.s_addr); | |
336 | add_rule(ib, &match, IBR_FROM_REMOTE_ARP); | |
c16e55cf | 337 | |
7ee20df1 | 338 | /* (h) Allow TCP traffic to the remote's IP and port. */ |
81a76618 BP |
339 | match_init_catchall(&match); |
340 | match_set_dl_type(&match, htons(ETH_TYPE_IP)); | |
341 | match_set_nw_proto(&match, IPPROTO_TCP); | |
342 | match_set_nw_dst(&match, a->sin_addr.s_addr); | |
343 | match_set_tp_dst(&match, a->sin_port); | |
344 | add_rule(ib, &match, IBR_TO_REMOTE_TCP); | |
c16e55cf | 345 | |
7ee20df1 | 346 | /* (i) Allow TCP traffic from the remote's IP and port. */ |
81a76618 BP |
347 | match_init_catchall(&match); |
348 | match_set_dl_type(&match, htons(ETH_TYPE_IP)); | |
349 | match_set_nw_proto(&match, IPPROTO_TCP); | |
350 | match_set_nw_src(&match, a->sin_addr.s_addr); | |
351 | match_set_tp_src(&match, a->sin_port); | |
352 | add_rule(ib, &match, IBR_FROM_REMOTE_TCP); | |
7ee20df1 | 353 | } |
0ade584e BP |
354 | } |
355 | ||
6da1e809 BP |
356 | /* Updates the OpenFlow flow table for the current state of in-band control. |
357 | * Returns true ordinarily. Returns false if no remotes are configured on 'ib' | |
358 | * and 'ib' doesn't have any rules left to remove from the OpenFlow flow | |
359 | * table. Thus, a false return value means that the caller can destroy 'ib' | |
360 | * without leaving extra flows hanging around in the flow table. */ | |
361 | bool | |
7ee20df1 | 362 | in_band_run(struct in_band *ib) |
0ade584e | 363 | { |
f25d0cf3 BP |
364 | uint64_t ofpacts_stub[128 / 8]; |
365 | struct ofpbuf ofpacts; | |
b1da6250 | 366 | |
7ee20df1 | 367 | struct in_band_rule *rule, *next; |
b1da6250 | 368 | |
f25d0cf3 BP |
369 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
370 | ||
371 | if (ib->queue_id >= 0) { | |
372 | ofpact_put_SET_QUEUE(&ofpacts)->queue_id = ib->queue_id; | |
d2ede7bc | 373 | } |
f25d0cf3 | 374 | ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL; |
0ade584e | 375 | |
7ee20df1 BP |
376 | refresh_local(ib); |
377 | refresh_remotes(ib); | |
0ade584e | 378 | |
7ee20df1 | 379 | update_rules(ib); |
0ade584e | 380 | |
81a76618 | 381 | HMAP_FOR_EACH_SAFE (rule, next, hmap_node, &ib->rules) { |
7ee20df1 BP |
382 | switch (rule->op) { |
383 | case ADD: | |
81a76618 | 384 | ofproto_add_flow(ib->ofproto, &rule->match, rule->priority, |
1f317cb5 | 385 | ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts)); |
7ee20df1 | 386 | break; |
0ade584e | 387 | |
f72e3073 | 388 | case DEL: |
81a76618 BP |
389 | if (ofproto_delete_flow(ib->ofproto, |
390 | &rule->match, rule->priority)) { | |
7ee20df1 BP |
391 | /* ofproto doesn't have the rule anymore so there's no reason |
392 | * for us to track it any longer. */ | |
81a76618 | 393 | hmap_remove(&ib->rules, &rule->hmap_node); |
7ee20df1 BP |
394 | free(rule); |
395 | } | |
396 | break; | |
0ade584e BP |
397 | } |
398 | } | |
6da1e809 | 399 | |
f25d0cf3 BP |
400 | ofpbuf_uninit(&ofpacts); |
401 | ||
6da1e809 | 402 | return ib->n_remotes || !hmap_is_empty(&ib->rules); |
0ade584e BP |
403 | } |
404 | ||
064af421 BP |
405 | void |
406 | in_band_wait(struct in_band *in_band) | |
407 | { | |
7cf8b266 | 408 | long long int wakeup |
0ad9b732 | 409 | = MIN(in_band->next_remote_refresh, in_band->next_local_refresh); |
7cf8b266 | 410 | poll_timer_wait_until(wakeup * 1000); |
064af421 BP |
411 | } |
412 | ||
f1acd62b | 413 | int |
19a87e36 | 414 | in_band_create(struct ofproto *ofproto, const char *local_name, |
9b45d7f5 | 415 | struct in_band **in_bandp) |
064af421 BP |
416 | { |
417 | struct in_band *in_band; | |
f1acd62b | 418 | struct netdev *local_netdev; |
0ad9b732 | 419 | int error; |
064af421 | 420 | |
928ef386 | 421 | *in_bandp = NULL; |
89e3f9cc | 422 | error = netdev_open(local_name, "internal", &local_netdev); |
f1acd62b BP |
423 | if (error) { |
424 | VLOG_ERR("failed to initialize in-band control: cannot open " | |
10a89ef0 BP |
425 | "datapath local port %s (%s)", |
426 | local_name, ovs_strerror(error)); | |
f1acd62b BP |
427 | return error; |
428 | } | |
064af421 | 429 | |
ec6fde61 | 430 | in_band = xzalloc(sizeof *in_band); |
064af421 | 431 | in_band->ofproto = ofproto; |
7ee20df1 | 432 | in_band->queue_id = -1; |
f1acd62b | 433 | in_band->next_remote_refresh = TIME_MIN; |
0ade584e BP |
434 | in_band->next_local_refresh = TIME_MIN; |
435 | in_band->local_netdev = local_netdev; | |
7ee20df1 | 436 | hmap_init(&in_band->rules); |
064af421 BP |
437 | |
438 | *in_bandp = in_band; | |
f1acd62b BP |
439 | |
440 | return 0; | |
064af421 BP |
441 | } |
442 | ||
443 | void | |
0ade584e | 444 | in_band_destroy(struct in_band *ib) |
064af421 | 445 | { |
0ade584e | 446 | if (ib) { |
7ee20df1 BP |
447 | struct in_band_rule *rule, *next; |
448 | ||
81a76618 BP |
449 | HMAP_FOR_EACH_SAFE (rule, next, hmap_node, &ib->rules) { |
450 | hmap_remove(&ib->rules, &rule->hmap_node); | |
7ee20df1 BP |
451 | free(rule); |
452 | } | |
453 | hmap_destroy(&ib->rules); | |
0ade584e | 454 | in_band_set_remotes(ib, NULL, 0); |
0ade584e BP |
455 | netdev_close(ib->local_netdev); |
456 | free(ib); | |
457 | } | |
458 | } | |
f7de2cdf | 459 | |
a3c5ac70 | 460 | static bool |
d2ede7bc BP |
461 | any_addresses_changed(struct in_band *ib, |
462 | const struct sockaddr_in *addresses, size_t n) | |
a3c5ac70 BP |
463 | { |
464 | size_t i; | |
465 | ||
466 | if (n != ib->n_remotes) { | |
467 | return true; | |
468 | } | |
469 | ||
470 | for (i = 0; i < n; i++) { | |
d2ede7bc BP |
471 | const struct sockaddr_in *old = &ib->remotes[i].remote_addr; |
472 | const struct sockaddr_in *new = &addresses[i]; | |
473 | ||
474 | if (old->sin_addr.s_addr != new->sin_addr.s_addr || | |
475 | old->sin_port != new->sin_port) { | |
a3c5ac70 BP |
476 | return true; |
477 | } | |
478 | } | |
479 | ||
480 | return false; | |
481 | } | |
482 | ||
0ade584e | 483 | void |
d2ede7bc BP |
484 | in_band_set_remotes(struct in_band *ib, |
485 | const struct sockaddr_in *addresses, size_t n) | |
0ade584e BP |
486 | { |
487 | size_t i; | |
488 | ||
d2ede7bc | 489 | if (!any_addresses_changed(ib, addresses, n)) { |
0ade584e | 490 | return; |
0ade584e BP |
491 | } |
492 | ||
a3c5ac70 | 493 | /* Clear old remotes. */ |
0ade584e | 494 | for (i = 0; i < ib->n_remotes; i++) { |
0ade584e | 495 | netdev_close(ib->remotes[i].remote_netdev); |
064af421 | 496 | } |
0ade584e | 497 | free(ib->remotes); |
064af421 | 498 | |
a3c5ac70 | 499 | /* Set up new remotes. */ |
bad0c371 | 500 | ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL; |
0ade584e BP |
501 | ib->n_remotes = n; |
502 | for (i = 0; i < n; i++) { | |
d2ede7bc | 503 | ib->remotes[i].remote_addr = addresses[i]; |
0ade584e | 504 | } |
a3c5ac70 BP |
505 | |
506 | /* Force refresh in next call to in_band_run(). */ | |
507 | ib->next_remote_refresh = TIME_MIN; | |
0ade584e | 508 | } |
b1da6250 BP |
509 | |
510 | /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'. If | |
511 | * 'queue_id' is negative, 'ib' will not set any queue (which is also the | |
512 | * default). */ | |
513 | void | |
514 | in_band_set_queue(struct in_band *ib, int queue_id) | |
515 | { | |
516 | ib->queue_id = queue_id; | |
517 | } | |
518 |