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