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