]>
Commit | Line | Data |
---|---|---|
19a87e36 BP |
1 | /* |
2 | * Copyright (c) 2009, 2010, 2011 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 | ||
19 | #include "connmgr.h" | |
20 | ||
21 | #include <errno.h> | |
22 | #include <stdlib.h> | |
23 | ||
24 | #include "coverage.h" | |
19a87e36 BP |
25 | #include "fail-open.h" |
26 | #include "in-band.h" | |
27 | #include "odp-util.h" | |
28 | #include "ofp-util.h" | |
29 | #include "ofpbuf.h" | |
30 | #include "pinsched.h" | |
31 | #include "poll-loop.h" | |
32 | #include "pktbuf.h" | |
d08a2e92 | 33 | #include "private.h" |
19a87e36 BP |
34 | #include "rconn.h" |
35 | #include "shash.h" | |
36 | #include "timeval.h" | |
37 | #include "vconn.h" | |
38 | #include "vlog.h" | |
39 | ||
40 | VLOG_DEFINE_THIS_MODULE(connmgr); | |
41 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
42 | ||
19a87e36 BP |
43 | /* An OpenFlow connection. */ |
44 | struct ofconn { | |
45 | struct connmgr *connmgr; /* Connection's manager. */ | |
46 | struct list node; /* In struct connmgr's "all_conns" list. */ | |
47 | struct rconn *rconn; /* OpenFlow connection. */ | |
48 | enum ofconn_type type; /* Type. */ | |
49 | enum nx_flow_format flow_format; /* Currently selected flow format. */ | |
6c1491fb | 50 | bool flow_mod_table_id; /* NXT_FLOW_MOD_TABLE_ID enabled? */ |
19a87e36 | 51 | |
7ee20df1 BP |
52 | /* Asynchronous flow table operation support. */ |
53 | struct list opgroups; /* Contains pending "ofopgroups", if any. */ | |
54 | struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */ | |
55 | bool retry; /* True if 'blocked' is ready to try again. */ | |
56 | ||
19a87e36 BP |
57 | /* OFPT_PACKET_IN related data. */ |
58 | struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */ | |
59 | #define N_SCHEDULERS 2 | |
60 | struct pinsched *schedulers[N_SCHEDULERS]; | |
61 | struct pktbuf *pktbuf; /* OpenFlow packet buffers. */ | |
62 | int miss_send_len; /* Bytes to send of buffered packets. */ | |
63 | ||
64 | /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow | |
65 | * requests, and the maximum number before we stop reading OpenFlow | |
66 | * requests. */ | |
67 | #define OFCONN_REPLY_MAX 100 | |
68 | struct rconn_packet_counter *reply_counter; | |
69 | ||
70 | /* type == OFCONN_PRIMARY only. */ | |
71 | enum nx_role role; /* Role. */ | |
72 | struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */ | |
73 | enum ofproto_band band; /* In-band or out-of-band? */ | |
74 | }; | |
75 | ||
76 | static struct ofconn *ofconn_create(struct connmgr *, struct rconn *, | |
77 | enum ofconn_type); | |
78 | static void ofconn_destroy(struct ofconn *); | |
79 | ||
80 | static void ofconn_reconfigure(struct ofconn *, | |
81 | const struct ofproto_controller *); | |
82 | ||
83 | static void ofconn_run(struct ofconn *, | |
7ee20df1 | 84 | bool (*handle_openflow)(struct ofconn *, |
19a87e36 | 85 | struct ofpbuf *ofp_msg)); |
7ee20df1 | 86 | static void ofconn_wait(struct ofconn *, bool handling_openflow); |
19a87e36 BP |
87 | |
88 | static const char *ofconn_get_target(const struct ofconn *); | |
89 | static char *ofconn_make_name(const struct connmgr *, const char *target); | |
90 | ||
91 | static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst); | |
92 | ||
93 | static bool ofconn_receives_async_msgs(const struct ofconn *); | |
94 | ||
95 | static void ofconn_send(const struct ofconn *, struct ofpbuf *, | |
96 | struct rconn_packet_counter *); | |
97 | ||
98 | static void do_send_packet_in(struct ofpbuf *, void *ofconn_); | |
99 | ||
100 | /* A listener for incoming OpenFlow "service" connections. */ | |
101 | struct ofservice { | |
102 | struct hmap_node node; /* In struct connmgr's "services" hmap. */ | |
103 | struct pvconn *pvconn; /* OpenFlow connection listener. */ | |
104 | ||
105 | /* These are not used by ofservice directly. They are settings for | |
106 | * accepted "struct ofconn"s from the pvconn. */ | |
107 | int probe_interval; /* Max idle time before probing, in seconds. */ | |
108 | int rate_limit; /* Max packet-in rate in packets per second. */ | |
109 | int burst_limit; /* Limit on accumulating packet credits. */ | |
110 | }; | |
111 | ||
112 | static void ofservice_reconfigure(struct ofservice *, | |
113 | const struct ofproto_controller *); | |
114 | static int ofservice_create(struct connmgr *, const char *target); | |
115 | static void ofservice_destroy(struct connmgr *, struct ofservice *); | |
116 | static struct ofservice *ofservice_lookup(struct connmgr *, | |
117 | const char *target); | |
118 | ||
119 | /* Connection manager for an OpenFlow switch. */ | |
120 | struct connmgr { | |
121 | struct ofproto *ofproto; | |
122 | char *name; | |
123 | char *local_port_name; | |
124 | ||
125 | /* OpenFlow connections. */ | |
126 | struct hmap controllers; /* Controller "struct ofconn"s. */ | |
127 | struct list all_conns; /* Contains "struct ofconn"s. */ | |
128 | ||
129 | /* OpenFlow listeners. */ | |
130 | struct hmap services; /* Contains "struct ofservice"s. */ | |
131 | struct pvconn **snoops; | |
132 | size_t n_snoops; | |
133 | ||
134 | /* Fail open. */ | |
135 | struct fail_open *fail_open; | |
136 | enum ofproto_fail_mode fail_mode; | |
137 | ||
138 | /* In-band control. */ | |
139 | struct in_band *in_band; | |
140 | long long int next_in_band_update; | |
141 | struct sockaddr_in *extra_in_band_remotes; | |
142 | size_t n_extra_remotes; | |
143 | int in_band_queue; | |
144 | }; | |
145 | ||
146 | static void update_in_band_remotes(struct connmgr *); | |
147 | static void add_snooper(struct connmgr *, struct vconn *); | |
148 | ||
149 | /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is | |
150 | * a name for the ofproto suitable for using in log messages. | |
151 | * 'local_port_name' is the name of the local port (OFPP_LOCAL) within | |
152 | * 'ofproto'. */ | |
153 | struct connmgr * | |
154 | connmgr_create(struct ofproto *ofproto, | |
155 | const char *name, const char *local_port_name) | |
156 | { | |
157 | struct connmgr *mgr; | |
158 | ||
159 | mgr = xmalloc(sizeof *mgr); | |
160 | mgr->ofproto = ofproto; | |
161 | mgr->name = xstrdup(name); | |
162 | mgr->local_port_name = xstrdup(local_port_name); | |
163 | ||
164 | hmap_init(&mgr->controllers); | |
165 | list_init(&mgr->all_conns); | |
166 | ||
167 | hmap_init(&mgr->services); | |
168 | mgr->snoops = NULL; | |
169 | mgr->n_snoops = 0; | |
170 | ||
171 | mgr->fail_open = NULL; | |
172 | mgr->fail_mode = OFPROTO_FAIL_SECURE; | |
173 | ||
174 | mgr->in_band = NULL; | |
175 | mgr->next_in_band_update = LLONG_MAX; | |
176 | mgr->extra_in_band_remotes = NULL; | |
177 | mgr->n_extra_remotes = 0; | |
178 | mgr->in_band_queue = -1; | |
179 | ||
180 | return mgr; | |
181 | } | |
182 | ||
183 | /* Frees 'mgr' and all of its resources. */ | |
184 | void | |
185 | connmgr_destroy(struct connmgr *mgr) | |
186 | { | |
187 | struct ofservice *ofservice, *next_ofservice; | |
188 | struct ofconn *ofconn, *next_ofconn; | |
189 | size_t i; | |
190 | ||
191 | if (!mgr) { | |
192 | return; | |
193 | } | |
194 | ||
195 | LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) { | |
196 | ofconn_destroy(ofconn); | |
197 | } | |
198 | hmap_destroy(&mgr->controllers); | |
199 | ||
200 | HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) { | |
201 | ofservice_destroy(mgr, ofservice); | |
202 | } | |
203 | hmap_destroy(&mgr->services); | |
204 | ||
205 | for (i = 0; i < mgr->n_snoops; i++) { | |
206 | pvconn_close(mgr->snoops[i]); | |
207 | } | |
208 | free(mgr->snoops); | |
209 | ||
210 | fail_open_destroy(mgr->fail_open); | |
211 | mgr->fail_open = NULL; | |
212 | ||
213 | in_band_destroy(mgr->in_band); | |
214 | mgr->in_band = NULL; | |
215 | free(mgr->extra_in_band_remotes); | |
216 | free(mgr->name); | |
217 | free(mgr->local_port_name); | |
218 | ||
219 | free(mgr); | |
220 | } | |
221 | ||
7ee20df1 BP |
222 | /* Does all of the periodic maintenance required by 'mgr'. |
223 | * | |
224 | * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message | |
225 | * received on an OpenFlow connection, passing along the OpenFlow connection | |
226 | * itself and the message that was sent. If 'handle_openflow' returns true, | |
227 | * the message is considered to be fully processed. If 'handle_openflow' | |
228 | * returns false, the message is considered not to have been processed at all; | |
229 | * it will be stored and re-presented to 'handle_openflow' following the next | |
230 | * call to connmgr_retry(). 'handle_openflow' must not modify or free the | |
231 | * message. | |
232 | * | |
233 | * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and | |
234 | * other activities that could affect the flow table (in-band processing, | |
235 | * fail-open processing) are suppressed too. */ | |
19a87e36 BP |
236 | void |
237 | connmgr_run(struct connmgr *mgr, | |
7ee20df1 | 238 | bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg)) |
19a87e36 BP |
239 | { |
240 | struct ofconn *ofconn, *next_ofconn; | |
241 | struct ofservice *ofservice; | |
242 | size_t i; | |
243 | ||
7ee20df1 | 244 | if (handle_openflow && mgr->in_band) { |
19a87e36 BP |
245 | if (time_msec() >= mgr->next_in_band_update) { |
246 | update_in_band_remotes(mgr); | |
247 | } | |
248 | in_band_run(mgr->in_band); | |
249 | } | |
250 | ||
251 | LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) { | |
252 | ofconn_run(ofconn, handle_openflow); | |
253 | } | |
254 | ||
255 | /* Fail-open maintenance. Do this after processing the ofconns since | |
256 | * fail-open checks the status of the controller rconn. */ | |
7ee20df1 | 257 | if (handle_openflow && mgr->fail_open) { |
19a87e36 BP |
258 | fail_open_run(mgr->fail_open); |
259 | } | |
260 | ||
261 | HMAP_FOR_EACH (ofservice, node, &mgr->services) { | |
262 | struct vconn *vconn; | |
263 | int retval; | |
264 | ||
265 | retval = pvconn_accept(ofservice->pvconn, OFP_VERSION, &vconn); | |
266 | if (!retval) { | |
267 | struct rconn *rconn; | |
268 | char *name; | |
269 | ||
270 | rconn = rconn_create(ofservice->probe_interval, 0); | |
271 | name = ofconn_make_name(mgr, vconn_get_name(vconn)); | |
272 | rconn_connect_unreliably(rconn, vconn, name); | |
273 | free(name); | |
274 | ||
275 | ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE); | |
276 | ofconn_set_rate_limit(ofconn, ofservice->rate_limit, | |
277 | ofservice->burst_limit); | |
278 | } else if (retval != EAGAIN) { | |
279 | VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval)); | |
280 | } | |
281 | } | |
282 | ||
283 | for (i = 0; i < mgr->n_snoops; i++) { | |
284 | struct vconn *vconn; | |
285 | int retval; | |
286 | ||
287 | retval = pvconn_accept(mgr->snoops[i], OFP_VERSION, &vconn); | |
288 | if (!retval) { | |
289 | add_snooper(mgr, vconn); | |
290 | } else if (retval != EAGAIN) { | |
291 | VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval)); | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
7ee20df1 BP |
296 | /* Causes the poll loop to wake up when connmgr_run() needs to run. |
297 | * | |
298 | * If 'handling_openflow' is true, arriving OpenFlow messages and other | |
299 | * activities that affect the flow table will wake up the poll loop. If | |
300 | * 'handling_openflow' is false, they will not. */ | |
19a87e36 | 301 | void |
7ee20df1 | 302 | connmgr_wait(struct connmgr *mgr, bool handling_openflow) |
19a87e36 BP |
303 | { |
304 | struct ofservice *ofservice; | |
305 | struct ofconn *ofconn; | |
306 | size_t i; | |
307 | ||
308 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
7ee20df1 | 309 | ofconn_wait(ofconn, handling_openflow); |
19a87e36 | 310 | } |
7ee20df1 | 311 | if (handling_openflow && mgr->in_band) { |
19a87e36 BP |
312 | poll_timer_wait_until(mgr->next_in_band_update); |
313 | in_band_wait(mgr->in_band); | |
314 | } | |
7ee20df1 | 315 | if (handling_openflow && mgr->fail_open) { |
19a87e36 BP |
316 | fail_open_wait(mgr->fail_open); |
317 | } | |
318 | HMAP_FOR_EACH (ofservice, node, &mgr->services) { | |
319 | pvconn_wait(ofservice->pvconn); | |
320 | } | |
321 | for (i = 0; i < mgr->n_snoops; i++) { | |
322 | pvconn_wait(mgr->snoops[i]); | |
323 | } | |
324 | } | |
325 | ||
326 | /* Returns the ofproto that owns 'ofconn''s connmgr. */ | |
327 | struct ofproto * | |
328 | ofconn_get_ofproto(const struct ofconn *ofconn) | |
329 | { | |
330 | return ofconn->connmgr->ofproto; | |
331 | } | |
7ee20df1 BP |
332 | |
333 | /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by | |
334 | * returning false to the 'handle_openflow' callback to connmgr_run(), this | |
335 | * re-enables them. */ | |
336 | void | |
337 | connmgr_retry(struct connmgr *mgr) | |
338 | { | |
339 | struct ofconn *ofconn; | |
340 | ||
341 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
342 | ofconn->retry = true; | |
343 | } | |
344 | } | |
19a87e36 BP |
345 | \f |
346 | /* OpenFlow configuration. */ | |
347 | ||
348 | static void add_controller(struct connmgr *, const char *target); | |
349 | static struct ofconn *find_controller_by_target(struct connmgr *, | |
350 | const char *target); | |
351 | static void update_fail_open(struct connmgr *); | |
352 | static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp, | |
81e2083f | 353 | const struct sset *); |
19a87e36 BP |
354 | |
355 | /* Returns true if 'mgr' has any configured primary controllers. | |
356 | * | |
357 | * Service controllers do not count, but configured primary controllers do | |
358 | * count whether or not they are currently connected. */ | |
359 | bool | |
360 | connmgr_has_controllers(const struct connmgr *mgr) | |
361 | { | |
362 | return !hmap_is_empty(&mgr->controllers); | |
363 | } | |
364 | ||
365 | /* Initializes 'info' and populates it with information about each configured | |
366 | * primary controller. The keys in 'info' are the controllers' targets; the | |
367 | * data values are corresponding "struct ofproto_controller_info". | |
368 | * | |
369 | * The caller owns 'info' and everything in it and should free it when it is no | |
370 | * longer needed. */ | |
371 | void | |
372 | connmgr_get_controller_info(struct connmgr *mgr, struct shash *info) | |
373 | { | |
374 | const struct ofconn *ofconn; | |
375 | ||
19a87e36 BP |
376 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { |
377 | const struct rconn *rconn = ofconn->rconn; | |
5d279086 | 378 | const char *target = rconn_get_target(rconn); |
19a87e36 | 379 | |
5d279086 AE |
380 | if (!shash_find(info, target)) { |
381 | struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo); | |
382 | time_t now = time_now(); | |
383 | time_t last_connection = rconn_get_last_connection(rconn); | |
384 | time_t last_disconnect = rconn_get_last_disconnect(rconn); | |
385 | int last_error = rconn_get_last_error(rconn); | |
19a87e36 | 386 | |
5d279086 | 387 | shash_add(info, target, cinfo); |
19a87e36 | 388 | |
5d279086 AE |
389 | cinfo->is_connected = rconn_is_connected(rconn); |
390 | cinfo->role = ofconn->role; | |
19a87e36 | 391 | |
5d279086 | 392 | cinfo->pairs.n = 0; |
19a87e36 | 393 | |
5d279086 AE |
394 | if (last_error) { |
395 | cinfo->pairs.keys[cinfo->pairs.n] = "last_error"; | |
396 | cinfo->pairs.values[cinfo->pairs.n++] | |
397 | = xstrdup(ovs_retval_to_string(last_error)); | |
398 | } | |
19a87e36 | 399 | |
5d279086 | 400 | cinfo->pairs.keys[cinfo->pairs.n] = "state"; |
19a87e36 | 401 | cinfo->pairs.values[cinfo->pairs.n++] |
5d279086 AE |
402 | = xstrdup(rconn_get_state(rconn)); |
403 | ||
404 | if (last_connection != TIME_MIN) { | |
405 | cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect"; | |
406 | cinfo->pairs.values[cinfo->pairs.n++] | |
407 | = xasprintf("%ld", (long int) (now - last_connection)); | |
408 | } | |
409 | ||
410 | if (last_disconnect != TIME_MIN) { | |
411 | cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect"; | |
412 | cinfo->pairs.values[cinfo->pairs.n++] | |
413 | = xasprintf("%ld", (long int) (now - last_disconnect)); | |
414 | } | |
19a87e36 BP |
415 | } |
416 | } | |
417 | } | |
418 | ||
419 | /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in | |
420 | * 'controllers'. */ | |
421 | void | |
422 | connmgr_set_controllers(struct connmgr *mgr, | |
423 | const struct ofproto_controller *controllers, | |
424 | size_t n_controllers) | |
425 | { | |
08fd19f0 | 426 | bool had_controllers = connmgr_has_controllers(mgr); |
19a87e36 BP |
427 | struct shash new_controllers; |
428 | struct ofconn *ofconn, *next_ofconn; | |
429 | struct ofservice *ofservice, *next_ofservice; | |
19a87e36 BP |
430 | size_t i; |
431 | ||
432 | /* Create newly configured controllers and services. | |
433 | * Create a name to ofproto_controller mapping in 'new_controllers'. */ | |
434 | shash_init(&new_controllers); | |
435 | for (i = 0; i < n_controllers; i++) { | |
436 | const struct ofproto_controller *c = &controllers[i]; | |
437 | ||
438 | if (!vconn_verify_name(c->target)) { | |
439 | if (!find_controller_by_target(mgr, c->target)) { | |
440 | add_controller(mgr, c->target); | |
441 | } | |
442 | } else if (!pvconn_verify_name(c->target)) { | |
443 | if (!ofservice_lookup(mgr, c->target)) { | |
444 | ofservice_create(mgr, c->target); | |
445 | } | |
446 | } else { | |
447 | VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"", | |
448 | mgr->name, c->target); | |
449 | continue; | |
450 | } | |
451 | ||
452 | shash_add_once(&new_controllers, c->target, &controllers[i]); | |
453 | } | |
454 | ||
455 | /* Delete controllers that are no longer configured. | |
456 | * Update configuration of all now-existing controllers. */ | |
19a87e36 BP |
457 | HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) { |
458 | struct ofproto_controller *c; | |
459 | ||
460 | c = shash_find_data(&new_controllers, ofconn_get_target(ofconn)); | |
461 | if (!c) { | |
462 | ofconn_destroy(ofconn); | |
463 | } else { | |
464 | ofconn_reconfigure(ofconn, c); | |
465 | } | |
466 | } | |
467 | ||
468 | /* Delete services that are no longer configured. | |
469 | * Update configuration of all now-existing services. */ | |
470 | HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) { | |
471 | struct ofproto_controller *c; | |
472 | ||
473 | c = shash_find_data(&new_controllers, | |
474 | pvconn_get_name(ofservice->pvconn)); | |
475 | if (!c) { | |
476 | ofservice_destroy(mgr, ofservice); | |
477 | } else { | |
478 | ofservice_reconfigure(ofservice, c); | |
479 | } | |
480 | } | |
481 | ||
482 | shash_destroy(&new_controllers); | |
483 | ||
484 | update_in_band_remotes(mgr); | |
485 | update_fail_open(mgr); | |
08fd19f0 BP |
486 | if (had_controllers != connmgr_has_controllers(mgr)) { |
487 | ofproto_flush_flows(mgr->ofproto); | |
488 | } | |
19a87e36 BP |
489 | } |
490 | ||
491 | /* Drops the connections between 'mgr' and all of its primary and secondary | |
492 | * controllers, forcing them to reconnect. */ | |
493 | void | |
494 | connmgr_reconnect(const struct connmgr *mgr) | |
495 | { | |
496 | struct ofconn *ofconn; | |
497 | ||
498 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
499 | rconn_reconnect(ofconn->rconn); | |
500 | } | |
501 | } | |
502 | ||
503 | /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'. | |
504 | * | |
505 | * A "snoop" is a pvconn to which every OpenFlow message to or from the most | |
506 | * important controller on 'mgr' is mirrored. */ | |
507 | int | |
81e2083f | 508 | connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops) |
19a87e36 BP |
509 | { |
510 | return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops); | |
511 | } | |
512 | ||
513 | /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */ | |
514 | void | |
81e2083f | 515 | connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops) |
19a87e36 BP |
516 | { |
517 | size_t i; | |
518 | ||
519 | for (i = 0; i < mgr->n_snoops; i++) { | |
81e2083f | 520 | sset_add(snoops, pvconn_get_name(mgr->snoops[i])); |
19a87e36 BP |
521 | } |
522 | } | |
523 | ||
81e2083f BP |
524 | /* Returns true if 'mgr' has at least one snoop, false if it has none. */ |
525 | bool | |
526 | connmgr_has_snoops(const struct connmgr *mgr) | |
527 | { | |
528 | return mgr->n_snoops > 0; | |
529 | } | |
530 | ||
19a87e36 BP |
531 | /* Creates a new controller for 'target' in 'mgr'. update_controller() needs |
532 | * to be called later to finish the new ofconn's configuration. */ | |
533 | static void | |
534 | add_controller(struct connmgr *mgr, const char *target) | |
535 | { | |
536 | char *name = ofconn_make_name(mgr, target); | |
537 | struct ofconn *ofconn; | |
538 | ||
539 | ofconn = ofconn_create(mgr, rconn_create(5, 8), OFCONN_PRIMARY); | |
540 | ofconn->pktbuf = pktbuf_create(); | |
541 | ofconn->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN; | |
542 | rconn_connect(ofconn->rconn, target, name); | |
543 | hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0)); | |
544 | ||
545 | free(name); | |
546 | } | |
547 | ||
548 | static struct ofconn * | |
549 | find_controller_by_target(struct connmgr *mgr, const char *target) | |
550 | { | |
551 | struct ofconn *ofconn; | |
552 | ||
553 | HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node, | |
554 | hash_string(target, 0), &mgr->controllers) { | |
555 | if (!strcmp(ofconn_get_target(ofconn), target)) { | |
556 | return ofconn; | |
557 | } | |
558 | } | |
559 | return NULL; | |
560 | } | |
561 | ||
562 | static void | |
563 | update_in_band_remotes(struct connmgr *mgr) | |
564 | { | |
565 | struct sockaddr_in *addrs; | |
566 | size_t max_addrs, n_addrs; | |
567 | struct ofconn *ofconn; | |
568 | size_t i; | |
569 | ||
570 | /* Allocate enough memory for as many remotes as we could possibly have. */ | |
571 | max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers); | |
572 | addrs = xmalloc(max_addrs * sizeof *addrs); | |
573 | n_addrs = 0; | |
574 | ||
575 | /* Add all the remotes. */ | |
576 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
577 | struct sockaddr_in *sin = &addrs[n_addrs]; | |
578 | ||
579 | if (ofconn->band == OFPROTO_OUT_OF_BAND) { | |
580 | continue; | |
581 | } | |
582 | ||
583 | sin->sin_addr.s_addr = rconn_get_remote_ip(ofconn->rconn); | |
584 | if (sin->sin_addr.s_addr) { | |
585 | sin->sin_port = rconn_get_remote_port(ofconn->rconn); | |
586 | n_addrs++; | |
587 | } | |
588 | } | |
589 | for (i = 0; i < mgr->n_extra_remotes; i++) { | |
590 | addrs[n_addrs++] = mgr->extra_in_band_remotes[i]; | |
591 | } | |
592 | ||
593 | /* Create or update or destroy in-band. */ | |
594 | if (n_addrs) { | |
595 | if (!mgr->in_band) { | |
596 | in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band); | |
597 | } | |
598 | if (mgr->in_band) { | |
599 | in_band_set_remotes(mgr->in_band, addrs, n_addrs); | |
600 | } | |
601 | in_band_set_queue(mgr->in_band, mgr->in_band_queue); | |
602 | mgr->next_in_band_update = time_msec() + 1000; | |
603 | } else { | |
604 | in_band_destroy(mgr->in_band); | |
605 | mgr->in_band = NULL; | |
606 | } | |
607 | ||
608 | /* Clean up. */ | |
609 | free(addrs); | |
610 | } | |
611 | ||
612 | static void | |
613 | update_fail_open(struct connmgr *mgr) | |
614 | { | |
615 | if (connmgr_has_controllers(mgr) | |
616 | && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) { | |
617 | if (!mgr->fail_open) { | |
618 | mgr->fail_open = fail_open_create(mgr->ofproto, mgr); | |
619 | } | |
620 | } else { | |
621 | fail_open_destroy(mgr->fail_open); | |
622 | mgr->fail_open = NULL; | |
623 | } | |
624 | } | |
625 | ||
626 | static int | |
627 | set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp, | |
81e2083f | 628 | const struct sset *sset) |
19a87e36 BP |
629 | { |
630 | struct pvconn **pvconns = *pvconnsp; | |
631 | size_t n_pvconns = *n_pvconnsp; | |
81e2083f | 632 | const char *name; |
19a87e36 BP |
633 | int retval = 0; |
634 | size_t i; | |
635 | ||
636 | for (i = 0; i < n_pvconns; i++) { | |
637 | pvconn_close(pvconns[i]); | |
638 | } | |
639 | free(pvconns); | |
640 | ||
81e2083f | 641 | pvconns = xmalloc(sset_count(sset) * sizeof *pvconns); |
19a87e36 | 642 | n_pvconns = 0; |
81e2083f | 643 | SSET_FOR_EACH (name, sset) { |
19a87e36 BP |
644 | struct pvconn *pvconn; |
645 | int error; | |
646 | ||
647 | error = pvconn_open(name, &pvconn); | |
648 | if (!error) { | |
649 | pvconns[n_pvconns++] = pvconn; | |
650 | } else { | |
651 | VLOG_ERR("failed to listen on %s: %s", name, strerror(error)); | |
652 | if (!retval) { | |
653 | retval = error; | |
654 | } | |
655 | } | |
656 | } | |
657 | ||
658 | *pvconnsp = pvconns; | |
659 | *n_pvconnsp = n_pvconns; | |
660 | ||
661 | return retval; | |
662 | } | |
663 | ||
664 | /* Returns a "preference level" for snooping 'ofconn'. A higher return value | |
665 | * means that 'ofconn' is more interesting for monitoring than a lower return | |
666 | * value. */ | |
667 | static int | |
668 | snoop_preference(const struct ofconn *ofconn) | |
669 | { | |
670 | switch (ofconn->role) { | |
671 | case NX_ROLE_MASTER: | |
672 | return 3; | |
673 | case NX_ROLE_OTHER: | |
674 | return 2; | |
675 | case NX_ROLE_SLAVE: | |
676 | return 1; | |
677 | default: | |
678 | /* Shouldn't happen. */ | |
679 | return 0; | |
680 | } | |
681 | } | |
682 | ||
683 | /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'. | |
684 | * Connects this vconn to a controller. */ | |
685 | static void | |
686 | add_snooper(struct connmgr *mgr, struct vconn *vconn) | |
687 | { | |
688 | struct ofconn *ofconn, *best; | |
689 | ||
690 | /* Pick a controller for monitoring. */ | |
691 | best = NULL; | |
692 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
693 | if (ofconn->type == OFCONN_PRIMARY | |
694 | && (!best || snoop_preference(ofconn) > snoop_preference(best))) { | |
695 | best = ofconn; | |
696 | } | |
697 | } | |
698 | ||
699 | if (best) { | |
700 | rconn_add_monitor(best->rconn, vconn); | |
701 | } else { | |
702 | VLOG_INFO_RL(&rl, "no controller connection to snoop"); | |
703 | vconn_close(vconn); | |
704 | } | |
705 | } | |
706 | \f | |
707 | /* Public ofconn functions. */ | |
708 | ||
709 | /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */ | |
710 | enum ofconn_type | |
711 | ofconn_get_type(const struct ofconn *ofconn) | |
712 | { | |
713 | return ofconn->type; | |
714 | } | |
715 | ||
716 | /* Returns the role configured for 'ofconn'. | |
717 | * | |
718 | * The default role, if no other role has been set, is NX_ROLE_OTHER. */ | |
719 | enum nx_role | |
720 | ofconn_get_role(const struct ofconn *ofconn) | |
721 | { | |
722 | return ofconn->role; | |
723 | } | |
724 | ||
725 | /* Changes 'ofconn''s role to 'role'. If 'role' is NX_ROLE_MASTER then any | |
726 | * existing master is demoted to a slave. */ | |
727 | void | |
728 | ofconn_set_role(struct ofconn *ofconn, enum nx_role role) | |
729 | { | |
730 | if (role == NX_ROLE_MASTER) { | |
731 | struct ofconn *other; | |
732 | ||
733 | HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) { | |
734 | if (other->role == NX_ROLE_MASTER) { | |
735 | other->role = NX_ROLE_SLAVE; | |
736 | } | |
737 | } | |
738 | } | |
739 | ofconn->role = role; | |
740 | } | |
741 | ||
742 | /* Returns the currently configured flow format for 'ofconn', one of NXFF_*. | |
743 | * | |
744 | * The default, if no other format has been set, is NXFF_OPENFLOW10. */ | |
745 | enum nx_flow_format | |
746 | ofconn_get_flow_format(struct ofconn *ofconn) | |
747 | { | |
748 | return ofconn->flow_format; | |
749 | } | |
750 | ||
751 | /* Sets the flow format for 'ofconn' to 'flow_format' (one of NXFF_*). */ | |
752 | void | |
753 | ofconn_set_flow_format(struct ofconn *ofconn, enum nx_flow_format flow_format) | |
754 | { | |
755 | ofconn->flow_format = flow_format; | |
756 | } | |
757 | ||
6c1491fb BP |
758 | /* Returns true if the NXT_FLOW_MOD_TABLE_ID extension is enabled, false |
759 | * otherwise. | |
760 | * | |
761 | * By default the extension is not enabled. */ | |
762 | bool | |
763 | ofconn_get_flow_mod_table_id(const struct ofconn *ofconn) | |
764 | { | |
765 | return ofconn->flow_mod_table_id; | |
766 | } | |
767 | ||
768 | /* Enables or disables (according to 'enable') the NXT_FLOW_MOD_TABLE_ID | |
769 | * extension on 'ofconn'. */ | |
770 | void | |
771 | ofconn_set_flow_mod_table_id(struct ofconn *ofconn, bool enable) | |
772 | { | |
773 | ofconn->flow_mod_table_id = enable; | |
774 | } | |
775 | ||
19a87e36 BP |
776 | /* Returns the default miss send length for 'ofconn'. */ |
777 | int | |
778 | ofconn_get_miss_send_len(const struct ofconn *ofconn) | |
779 | { | |
780 | return ofconn->miss_send_len; | |
781 | } | |
782 | ||
783 | /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */ | |
784 | void | |
785 | ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len) | |
786 | { | |
787 | ofconn->miss_send_len = miss_send_len; | |
788 | } | |
789 | ||
790 | /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a | |
791 | * sufficient number of OpenFlow replies in-flight on a single ofconn, then the | |
792 | * connmgr will stop accepting new OpenFlow requests on that ofconn until the | |
793 | * controller has accepted some of the replies.) */ | |
794 | void | |
795 | ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg) | |
796 | { | |
797 | ofconn_send(ofconn, msg, ofconn->reply_counter); | |
798 | } | |
799 | ||
63f2140a BP |
800 | /* Sends each of the messages in list 'replies' on 'ofconn' in order, |
801 | * accounting them as replies. */ | |
802 | void | |
803 | ofconn_send_replies(const struct ofconn *ofconn, struct list *replies) | |
804 | { | |
805 | struct ofpbuf *reply, *next; | |
806 | ||
807 | LIST_FOR_EACH_SAFE (reply, next, list_node, replies) { | |
808 | list_remove(&reply->list_node); | |
809 | ofconn_send_reply(ofconn, reply); | |
810 | } | |
811 | } | |
812 | ||
1be5ff75 BP |
813 | /* Sends 'error', which should be an OpenFlow error created with |
814 | * e.g. ofp_mkerr(), on 'ofconn', as a reply to 'request'. Only at most the | |
815 | * first 64 bytes of 'request' are used. */ | |
816 | void | |
817 | ofconn_send_error(const struct ofconn *ofconn, | |
818 | const struct ofp_header *request, int error) | |
819 | { | |
820 | struct ofpbuf *msg = ofputil_encode_error_msg(error, request); | |
821 | if (msg) { | |
822 | ofconn_send_reply(ofconn, msg); | |
823 | } | |
824 | } | |
825 | ||
19a87e36 BP |
826 | /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */ |
827 | int | |
828 | ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id, | |
829 | struct ofpbuf **bufferp, uint16_t *in_port) | |
830 | { | |
831 | return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port); | |
832 | } | |
7ee20df1 BP |
833 | |
834 | /* Returns true if 'ofconn' has any pending opgroups. */ | |
835 | bool | |
836 | ofconn_has_pending_opgroups(const struct ofconn *ofconn) | |
837 | { | |
838 | return !list_is_empty(&ofconn->opgroups); | |
839 | } | |
840 | ||
841 | /* Returns the number of pending opgroups on 'ofconn'. */ | |
842 | size_t | |
843 | ofconn_n_pending_opgroups(const struct ofconn *ofconn) | |
844 | { | |
845 | return list_size(&ofconn->opgroups); | |
846 | } | |
847 | ||
848 | /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups. | |
849 | * | |
850 | * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove | |
851 | * 'ofconn_node' from the list and re-initialize it with list_init(). The | |
852 | * client may, therefore, use list_is_empty(ofconn_node) to determine whether | |
853 | * 'ofconn_node' is still associated with an active ofconn. | |
854 | * | |
855 | * The client may also remove ofconn_node from the list itself, with | |
856 | * list_remove(). */ | |
857 | void | |
858 | ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node) | |
859 | { | |
860 | list_push_back(&ofconn->opgroups, ofconn_node); | |
861 | } | |
19a87e36 BP |
862 | \f |
863 | /* Private ofconn functions. */ | |
864 | ||
865 | static const char * | |
866 | ofconn_get_target(const struct ofconn *ofconn) | |
867 | { | |
868 | return rconn_get_target(ofconn->rconn); | |
869 | } | |
870 | ||
871 | static struct ofconn * | |
872 | ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type) | |
873 | { | |
874 | struct ofconn *ofconn = xzalloc(sizeof *ofconn); | |
875 | ofconn->connmgr = mgr; | |
876 | list_push_back(&mgr->all_conns, &ofconn->node); | |
877 | ofconn->rconn = rconn; | |
878 | ofconn->type = type; | |
879 | ofconn->flow_format = NXFF_OPENFLOW10; | |
6c1491fb | 880 | ofconn->flow_mod_table_id = false; |
7ee20df1 | 881 | list_init(&ofconn->opgroups); |
19a87e36 BP |
882 | ofconn->role = NX_ROLE_OTHER; |
883 | ofconn->packet_in_counter = rconn_packet_counter_create (); | |
884 | ofconn->pktbuf = NULL; | |
885 | ofconn->miss_send_len = 0; | |
886 | ofconn->reply_counter = rconn_packet_counter_create (); | |
887 | return ofconn; | |
888 | } | |
889 | ||
7ee20df1 BP |
890 | /* Disassociates 'ofconn' from all of the ofopgroups that it initiated that |
891 | * have not yet completed. (Those ofopgroups will still run to completion in | |
892 | * the usual way, but any errors that they run into will not be reported on any | |
893 | * OpenFlow channel.) | |
894 | * | |
895 | * Also discards any blocked operation on 'ofconn'. */ | |
896 | static void | |
897 | ofconn_flush(struct ofconn *ofconn) | |
898 | { | |
899 | while (!list_is_empty(&ofconn->opgroups)) { | |
900 | list_init(list_pop_front(&ofconn->opgroups)); | |
901 | } | |
902 | ofpbuf_delete(ofconn->blocked); | |
903 | ofconn->blocked = NULL; | |
904 | } | |
905 | ||
19a87e36 BP |
906 | static void |
907 | ofconn_destroy(struct ofconn *ofconn) | |
908 | { | |
7ee20df1 BP |
909 | ofconn_flush(ofconn); |
910 | ||
19a87e36 BP |
911 | if (ofconn->type == OFCONN_PRIMARY) { |
912 | hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node); | |
913 | } | |
914 | ||
915 | list_remove(&ofconn->node); | |
916 | rconn_destroy(ofconn->rconn); | |
917 | rconn_packet_counter_destroy(ofconn->packet_in_counter); | |
918 | rconn_packet_counter_destroy(ofconn->reply_counter); | |
919 | pktbuf_destroy(ofconn->pktbuf); | |
920 | free(ofconn); | |
921 | } | |
922 | ||
923 | /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same | |
924 | * target. */ | |
925 | static void | |
926 | ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c) | |
927 | { | |
928 | int probe_interval; | |
929 | ||
930 | ofconn->band = c->band; | |
931 | ||
932 | rconn_set_max_backoff(ofconn->rconn, c->max_backoff); | |
933 | ||
934 | probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0; | |
935 | rconn_set_probe_interval(ofconn->rconn, probe_interval); | |
936 | ||
937 | ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit); | |
938 | } | |
939 | ||
7ee20df1 BP |
940 | /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow |
941 | * messages. */ | |
942 | static bool | |
943 | ofconn_may_recv(const struct ofconn *ofconn) | |
944 | { | |
945 | int count = rconn_packet_counter_read (ofconn->reply_counter); | |
946 | return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX; | |
947 | } | |
948 | ||
19a87e36 BP |
949 | static void |
950 | ofconn_run(struct ofconn *ofconn, | |
7ee20df1 | 951 | bool (*handle_openflow)(struct ofconn *, struct ofpbuf *ofp_msg)) |
19a87e36 BP |
952 | { |
953 | struct connmgr *mgr = ofconn->connmgr; | |
19a87e36 BP |
954 | size_t i; |
955 | ||
956 | for (i = 0; i < N_SCHEDULERS; i++) { | |
957 | pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn); | |
958 | } | |
959 | ||
960 | rconn_run(ofconn->rconn); | |
961 | ||
7ee20df1 BP |
962 | if (handle_openflow) { |
963 | /* Limit the number of iterations to avoid starving other tasks. */ | |
964 | for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) { | |
965 | struct ofpbuf *of_msg; | |
966 | ||
967 | of_msg = (ofconn->blocked | |
968 | ? ofconn->blocked | |
969 | : rconn_recv(ofconn->rconn)); | |
19a87e36 BP |
970 | if (!of_msg) { |
971 | break; | |
972 | } | |
973 | if (mgr->fail_open) { | |
974 | fail_open_maybe_recover(mgr->fail_open); | |
975 | } | |
7ee20df1 BP |
976 | |
977 | if (handle_openflow(ofconn, of_msg)) { | |
978 | ofpbuf_delete(of_msg); | |
979 | ofconn->blocked = NULL; | |
980 | } else { | |
981 | ofconn->blocked = of_msg; | |
982 | ofconn->retry = false; | |
983 | } | |
19a87e36 BP |
984 | } |
985 | } | |
986 | ||
987 | if (!rconn_is_alive(ofconn->rconn)) { | |
988 | ofconn_destroy(ofconn); | |
7ee20df1 BP |
989 | } else if (!rconn_is_connected(ofconn->rconn)) { |
990 | ofconn_flush(ofconn); | |
19a87e36 BP |
991 | } |
992 | } | |
993 | ||
994 | static void | |
7ee20df1 | 995 | ofconn_wait(struct ofconn *ofconn, bool handling_openflow) |
19a87e36 BP |
996 | { |
997 | int i; | |
998 | ||
999 | for (i = 0; i < N_SCHEDULERS; i++) { | |
1000 | pinsched_wait(ofconn->schedulers[i]); | |
1001 | } | |
1002 | rconn_run_wait(ofconn->rconn); | |
7ee20df1 | 1003 | if (handling_openflow && ofconn_may_recv(ofconn)) { |
19a87e36 | 1004 | rconn_recv_wait(ofconn->rconn); |
19a87e36 BP |
1005 | } |
1006 | } | |
1007 | ||
1008 | /* Returns true if 'ofconn' should receive asynchronous messages. */ | |
1009 | static bool | |
1010 | ofconn_receives_async_msgs(const struct ofconn *ofconn) | |
1011 | { | |
1012 | if (!rconn_is_connected(ofconn->rconn)) { | |
1013 | return false; | |
1014 | } else if (ofconn->type == OFCONN_PRIMARY) { | |
1015 | /* Primary controllers always get asynchronous messages unless they | |
1016 | * have configured themselves as "slaves". */ | |
1017 | return ofconn->role != NX_ROLE_SLAVE; | |
1018 | } else { | |
1019 | /* Service connections don't get asynchronous messages unless they have | |
1020 | * explicitly asked for them by setting a nonzero miss send length. */ | |
1021 | return ofconn->miss_send_len > 0; | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | /* Returns a human-readable name for an OpenFlow connection between 'mgr' and | |
1026 | * 'target', suitable for use in log messages for identifying the connection. | |
1027 | * | |
1028 | * The name is dynamically allocated. The caller should free it (with free()) | |
1029 | * when it is no longer needed. */ | |
1030 | static char * | |
1031 | ofconn_make_name(const struct connmgr *mgr, const char *target) | |
1032 | { | |
1033 | return xasprintf("%s<->%s", mgr->name, target); | |
1034 | } | |
1035 | ||
1036 | static void | |
1037 | ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst) | |
1038 | { | |
1039 | int i; | |
1040 | ||
1041 | for (i = 0; i < N_SCHEDULERS; i++) { | |
1042 | struct pinsched **s = &ofconn->schedulers[i]; | |
1043 | ||
1044 | if (rate > 0) { | |
1045 | if (!*s) { | |
1046 | *s = pinsched_create(rate, burst); | |
1047 | } else { | |
1048 | pinsched_set_limits(*s, rate, burst); | |
1049 | } | |
1050 | } else { | |
1051 | pinsched_destroy(*s); | |
1052 | *s = NULL; | |
1053 | } | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | static void | |
1058 | ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg, | |
1059 | struct rconn_packet_counter *counter) | |
1060 | { | |
1061 | update_openflow_length(msg); | |
1062 | if (rconn_send(ofconn->rconn, msg, counter)) { | |
1063 | ofpbuf_delete(msg); | |
1064 | } | |
1065 | } | |
1066 | \f | |
1067 | /* Sending asynchronous messages. */ | |
1068 | ||
78bd1cd0 | 1069 | static void schedule_packet_in(struct ofconn *, struct ofputil_packet_in, |
19a87e36 BP |
1070 | const struct flow *, struct ofpbuf *rw_packet); |
1071 | ||
1072 | /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate | |
9cfcdadf | 1073 | * controllers managed by 'mgr'. */ |
19a87e36 BP |
1074 | void |
1075 | connmgr_send_port_status(struct connmgr *mgr, const struct ofp_phy_port *opp, | |
1076 | uint8_t reason) | |
1077 | { | |
1078 | /* XXX Should limit the number of queued port status change messages. */ | |
1079 | struct ofconn *ofconn; | |
1080 | ||
1081 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
1082 | struct ofp_port_status *ops; | |
1083 | struct ofpbuf *b; | |
1084 | ||
1085 | /* Primary controllers, even slaves, should always get port status | |
1086 | updates. Otherwise obey ofconn_receives_async_msgs(). */ | |
1087 | if (ofconn->type != OFCONN_PRIMARY | |
1088 | && !ofconn_receives_async_msgs(ofconn)) { | |
1089 | continue; | |
1090 | } | |
1091 | ||
1092 | ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b); | |
1093 | ops->reason = reason; | |
1094 | ops->desc = *opp; | |
19a87e36 BP |
1095 | ofconn_send(ofconn, b, NULL); |
1096 | } | |
1097 | } | |
1098 | ||
1099 | /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to | |
1100 | * appropriate controllers managed by 'mgr'. */ | |
1101 | void | |
1102 | connmgr_send_flow_removed(struct connmgr *mgr, | |
1103 | const struct ofputil_flow_removed *fr) | |
1104 | { | |
1105 | struct ofconn *ofconn; | |
1106 | ||
1107 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
1108 | struct ofpbuf *msg; | |
1109 | ||
1110 | if (!ofconn_receives_async_msgs(ofconn)) { | |
1111 | continue; | |
1112 | } | |
1113 | ||
1114 | /* Account flow expirations as replies to OpenFlow requests. That | |
1115 | * works because preventing OpenFlow requests from being processed also | |
1116 | * prevents new flows from being added (and expiring). (It also | |
1117 | * prevents processing OpenFlow requests that would not add new flows, | |
1118 | * so it is imperfect.) */ | |
1119 | msg = ofputil_encode_flow_removed(fr, ofconn->flow_format); | |
1120 | ofconn_send_reply(ofconn, msg); | |
1121 | } | |
1122 | } | |
1123 | ||
78bd1cd0 BP |
1124 | /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as |
1125 | * necessary according to their individual configurations. | |
19a87e36 BP |
1126 | * |
1127 | * 'rw_packet' may be NULL. Otherwise, 'rw_packet' must contain the same data | |
78bd1cd0 BP |
1128 | * as pin->packet. (rw_packet == pin->packet is also valid.) Ownership of |
1129 | * 'rw_packet' is transferred to this function. */ | |
19a87e36 | 1130 | void |
78bd1cd0 BP |
1131 | connmgr_send_packet_in(struct connmgr *mgr, |
1132 | const struct ofputil_packet_in *pin, | |
19a87e36 BP |
1133 | const struct flow *flow, struct ofpbuf *rw_packet) |
1134 | { | |
1135 | struct ofconn *ofconn, *prev; | |
1136 | ||
1137 | prev = NULL; | |
1138 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
1139 | if (ofconn_receives_async_msgs(ofconn)) { | |
1140 | if (prev) { | |
78bd1cd0 | 1141 | schedule_packet_in(prev, *pin, flow, NULL); |
19a87e36 BP |
1142 | } |
1143 | prev = ofconn; | |
1144 | } | |
1145 | } | |
1146 | if (prev) { | |
78bd1cd0 | 1147 | schedule_packet_in(prev, *pin, flow, rw_packet); |
19a87e36 BP |
1148 | } else { |
1149 | ofpbuf_delete(rw_packet); | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */ | |
1154 | static void | |
1155 | do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_) | |
1156 | { | |
1157 | struct ofconn *ofconn = ofconn_; | |
1158 | ||
1159 | rconn_send_with_limit(ofconn->rconn, ofp_packet_in, | |
1160 | ofconn->packet_in_counter, 100); | |
1161 | } | |
1162 | ||
78bd1cd0 | 1163 | /* Takes 'pin', whose packet has the flow specified by 'flow', composes an |
19a87e36 BP |
1164 | * OpenFlow packet-in message from it, and passes it to 'ofconn''s packet |
1165 | * scheduler for sending. | |
1166 | * | |
1167 | * 'rw_packet' may be NULL. Otherwise, 'rw_packet' must contain the same data | |
78bd1cd0 BP |
1168 | * as pin->packet. (rw_packet == pin->packet is also valid.) Ownership of |
1169 | * 'rw_packet' is transferred to this function. */ | |
19a87e36 | 1170 | static void |
78bd1cd0 | 1171 | schedule_packet_in(struct ofconn *ofconn, struct ofputil_packet_in pin, |
19a87e36 BP |
1172 | const struct flow *flow, struct ofpbuf *rw_packet) |
1173 | { | |
1174 | struct connmgr *mgr = ofconn->connmgr; | |
19a87e36 BP |
1175 | |
1176 | /* Get OpenFlow buffer_id. */ | |
78bd1cd0 | 1177 | if (pin.reason == OFPR_ACTION) { |
19a87e36 BP |
1178 | pin.buffer_id = UINT32_MAX; |
1179 | } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) { | |
1180 | pin.buffer_id = pktbuf_get_null(); | |
1181 | } else if (!ofconn->pktbuf) { | |
1182 | pin.buffer_id = UINT32_MAX; | |
1183 | } else { | |
78bd1cd0 | 1184 | pin.buffer_id = pktbuf_save(ofconn->pktbuf, pin.packet, flow->in_port); |
19a87e36 BP |
1185 | } |
1186 | ||
1187 | /* Figure out how much of the packet to send. */ | |
78bd1cd0 BP |
1188 | if (pin.reason == OFPR_NO_MATCH) { |
1189 | pin.send_len = pin.packet->size; | |
1190 | } else { | |
1191 | /* Caller should have initialized 'send_len' to 'max_len' specified in | |
1192 | * struct ofp_action_output. */ | |
1193 | } | |
19a87e36 BP |
1194 | if (pin.buffer_id != UINT32_MAX) { |
1195 | pin.send_len = MIN(pin.send_len, ofconn->miss_send_len); | |
1196 | } | |
19a87e36 BP |
1197 | |
1198 | /* Make OFPT_PACKET_IN and hand over to packet scheduler. It might | |
1199 | * immediately call into do_send_packet_in() or it might buffer it for a | |
1200 | * while (until a later call to pinsched_run()). */ | |
78bd1cd0 | 1201 | pinsched_send(ofconn->schedulers[pin.reason == OFPR_NO_MATCH ? 0 : 1], |
19a87e36 BP |
1202 | flow->in_port, ofputil_encode_packet_in(&pin, rw_packet), |
1203 | do_send_packet_in, ofconn); | |
1204 | } | |
1205 | \f | |
1206 | /* Fail-open settings. */ | |
1207 | ||
1208 | /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or | |
1209 | * OFPROTO_FAIL_STANDALONE) for 'mgr'. */ | |
1210 | enum ofproto_fail_mode | |
1211 | connmgr_get_fail_mode(const struct connmgr *mgr) | |
1212 | { | |
1213 | return mgr->fail_mode; | |
1214 | } | |
1215 | ||
1216 | /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either | |
1217 | * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */ | |
1218 | void | |
1219 | connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode) | |
1220 | { | |
08fd19f0 BP |
1221 | if (mgr->fail_mode != fail_mode) { |
1222 | mgr->fail_mode = fail_mode; | |
1223 | update_fail_open(mgr); | |
1224 | if (!connmgr_has_controllers(mgr)) { | |
1225 | ofproto_flush_flows(mgr->ofproto); | |
1226 | } | |
1227 | } | |
19a87e36 BP |
1228 | } |
1229 | \f | |
1230 | /* Fail-open implementation. */ | |
1231 | ||
1232 | /* Returns the longest probe interval among the primary controllers configured | |
1233 | * on 'mgr'. Returns 0 if there are no primary controllers. */ | |
1234 | int | |
1235 | connmgr_get_max_probe_interval(const struct connmgr *mgr) | |
1236 | { | |
1237 | const struct ofconn *ofconn; | |
1238 | int max_probe_interval; | |
1239 | ||
1240 | max_probe_interval = 0; | |
1241 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
1242 | int probe_interval = rconn_get_probe_interval(ofconn->rconn); | |
1243 | max_probe_interval = MAX(max_probe_interval, probe_interval); | |
1244 | } | |
1245 | return max_probe_interval; | |
1246 | } | |
1247 | ||
1248 | /* Returns the number of seconds for which all of 'mgr's primary controllers | |
1249 | * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */ | |
1250 | int | |
1251 | connmgr_failure_duration(const struct connmgr *mgr) | |
1252 | { | |
1253 | const struct ofconn *ofconn; | |
1254 | int min_failure_duration; | |
1255 | ||
1256 | if (!connmgr_has_controllers(mgr)) { | |
1257 | return 0; | |
1258 | } | |
1259 | ||
1260 | min_failure_duration = INT_MAX; | |
1261 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
1262 | int failure_duration = rconn_failure_duration(ofconn->rconn); | |
1263 | min_failure_duration = MIN(min_failure_duration, failure_duration); | |
1264 | } | |
1265 | return min_failure_duration; | |
1266 | } | |
1267 | ||
1268 | /* Returns true if at least one primary controller is connected (regardless of | |
1269 | * whether those controllers are believed to have authenticated and accepted | |
1270 | * this switch), false if none of them are connected. */ | |
1271 | bool | |
1272 | connmgr_is_any_controller_connected(const struct connmgr *mgr) | |
1273 | { | |
1274 | const struct ofconn *ofconn; | |
1275 | ||
1276 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
1277 | if (rconn_is_connected(ofconn->rconn)) { | |
1278 | return true; | |
1279 | } | |
1280 | } | |
1281 | return false; | |
1282 | } | |
1283 | ||
1284 | /* Returns true if at least one primary controller is believed to have | |
1285 | * authenticated and accepted this switch, false otherwise. */ | |
1286 | bool | |
1287 | connmgr_is_any_controller_admitted(const struct connmgr *mgr) | |
1288 | { | |
1289 | const struct ofconn *ofconn; | |
1290 | ||
1291 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
1292 | if (rconn_is_admitted(ofconn->rconn)) { | |
1293 | return true; | |
1294 | } | |
1295 | } | |
1296 | return false; | |
1297 | } | |
1298 | ||
1299 | /* Sends 'packet' to each controller connected to 'mgr'. Takes ownership of | |
1300 | * 'packet'. */ | |
1301 | void | |
1302 | connmgr_broadcast(struct connmgr *mgr, struct ofpbuf *packet) | |
1303 | { | |
1304 | struct ofconn *ofconn, *prev; | |
1305 | ||
1306 | prev = NULL; | |
1307 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
1308 | if (prev) { | |
1309 | ofconn_send_reply(ofconn, ofpbuf_clone(packet)); | |
1310 | } | |
1311 | if (rconn_is_connected(ofconn->rconn)) { | |
1312 | prev = ofconn; | |
1313 | } | |
1314 | } | |
1315 | if (prev) { | |
2e2dd32b | 1316 | ofconn_send_reply(prev, packet); |
19a87e36 BP |
1317 | } else { |
1318 | ofpbuf_delete(packet); | |
1319 | } | |
1320 | } | |
1321 | \f | |
1322 | /* In-band configuration. */ | |
1323 | ||
1324 | static bool any_extras_changed(const struct connmgr *, | |
1325 | const struct sockaddr_in *extras, size_t n); | |
1326 | ||
1327 | /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s | |
1328 | * in-band control should guarantee access, in the same way that in-band | |
1329 | * control guarantees access to OpenFlow controllers. */ | |
1330 | void | |
1331 | connmgr_set_extra_in_band_remotes(struct connmgr *mgr, | |
1332 | const struct sockaddr_in *extras, size_t n) | |
1333 | { | |
1334 | if (!any_extras_changed(mgr, extras, n)) { | |
1335 | return; | |
1336 | } | |
1337 | ||
1338 | free(mgr->extra_in_band_remotes); | |
1339 | mgr->n_extra_remotes = n; | |
1340 | mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras); | |
1341 | ||
1342 | update_in_band_remotes(mgr); | |
1343 | } | |
1344 | ||
1345 | /* Sets the OpenFlow queue used by flows set up by in-band control on | |
1346 | * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control | |
1347 | * flows will use the default queue. */ | |
1348 | void | |
1349 | connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id) | |
1350 | { | |
1351 | if (queue_id != mgr->in_band_queue) { | |
1352 | mgr->in_band_queue = queue_id; | |
1353 | update_in_band_remotes(mgr); | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | static bool | |
1358 | any_extras_changed(const struct connmgr *mgr, | |
1359 | const struct sockaddr_in *extras, size_t n) | |
1360 | { | |
1361 | size_t i; | |
1362 | ||
1363 | if (n != mgr->n_extra_remotes) { | |
1364 | return true; | |
1365 | } | |
1366 | ||
1367 | for (i = 0; i < n; i++) { | |
1368 | const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i]; | |
1369 | const struct sockaddr_in *new = &extras[i]; | |
1370 | ||
1371 | if (old->sin_addr.s_addr != new->sin_addr.s_addr || | |
1372 | old->sin_port != new->sin_port) { | |
1373 | return true; | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | return false; | |
1378 | } | |
1379 | \f | |
1380 | /* In-band implementation. */ | |
1381 | ||
1382 | bool | |
1383 | connmgr_msg_in_hook(struct connmgr *mgr, const struct flow *flow, | |
1384 | const struct ofpbuf *packet) | |
1385 | { | |
1386 | return mgr->in_band && in_band_msg_in_hook(mgr->in_band, flow, packet); | |
1387 | } | |
1388 | ||
1389 | bool | |
1390 | connmgr_may_set_up_flow(struct connmgr *mgr, const struct flow *flow, | |
1391 | const struct nlattr *odp_actions, | |
1392 | size_t actions_len) | |
1393 | { | |
1394 | return !mgr->in_band || in_band_rule_check(flow, odp_actions, actions_len); | |
1395 | } | |
1396 | \f | |
1397 | /* Fail-open and in-band implementation. */ | |
1398 | ||
1399 | /* Called by 'ofproto' after all flows have been flushed, to allow fail-open | |
7ee20df1 BP |
1400 | * and standalone mode to re-create their flows. |
1401 | * | |
1402 | * In-band control has more sophisticated code that manages flows itself. */ | |
19a87e36 BP |
1403 | void |
1404 | connmgr_flushed(struct connmgr *mgr) | |
1405 | { | |
19a87e36 BP |
1406 | if (mgr->fail_open) { |
1407 | fail_open_flushed(mgr->fail_open); | |
1408 | } | |
08fd19f0 BP |
1409 | |
1410 | /* If there are no controllers and we're in standalone mode, set up a flow | |
1411 | * that matches every packet and directs them to OFPP_NORMAL (which goes to | |
1412 | * us). Otherwise, the switch is in secure mode and we won't pass any | |
1413 | * traffic until a controller has been defined and it tells us to do so. */ | |
1414 | if (!connmgr_has_controllers(mgr) | |
1415 | && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) { | |
1416 | union ofp_action action; | |
1417 | struct cls_rule rule; | |
1418 | ||
1419 | memset(&action, 0, sizeof action); | |
1420 | action.type = htons(OFPAT_OUTPUT); | |
1421 | action.output.len = htons(sizeof action); | |
1422 | action.output.port = htons(OFPP_NORMAL); | |
1423 | cls_rule_init_catchall(&rule, 0); | |
1424 | ofproto_add_flow(mgr->ofproto, &rule, &action, 1); | |
1425 | } | |
19a87e36 BP |
1426 | } |
1427 | \f | |
1428 | /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful, | |
1429 | * otherwise a positive errno value. | |
1430 | * | |
1431 | * ofservice_reconfigure() must be called to fully configure the new | |
1432 | * ofservice. */ | |
1433 | static int | |
1434 | ofservice_create(struct connmgr *mgr, const char *target) | |
1435 | { | |
1436 | struct ofservice *ofservice; | |
1437 | struct pvconn *pvconn; | |
1438 | int error; | |
1439 | ||
1440 | error = pvconn_open(target, &pvconn); | |
1441 | if (error) { | |
1442 | return error; | |
1443 | } | |
1444 | ||
1445 | ofservice = xzalloc(sizeof *ofservice); | |
1446 | hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0)); | |
1447 | ofservice->pvconn = pvconn; | |
1448 | ||
1449 | return 0; | |
1450 | } | |
1451 | ||
1452 | static void | |
1453 | ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice) | |
1454 | { | |
1455 | hmap_remove(&mgr->services, &ofservice->node); | |
1456 | pvconn_close(ofservice->pvconn); | |
1457 | free(ofservice); | |
1458 | } | |
1459 | ||
1460 | static void | |
1461 | ofservice_reconfigure(struct ofservice *ofservice, | |
1462 | const struct ofproto_controller *c) | |
1463 | { | |
1464 | ofservice->probe_interval = c->probe_interval; | |
1465 | ofservice->rate_limit = c->rate_limit; | |
1466 | ofservice->burst_limit = c->burst_limit; | |
1467 | } | |
1468 | ||
1469 | /* Finds and returns the ofservice within 'mgr' that has the given | |
1470 | * 'target', or a null pointer if none exists. */ | |
1471 | static struct ofservice * | |
1472 | ofservice_lookup(struct connmgr *mgr, const char *target) | |
1473 | { | |
1474 | struct ofservice *ofservice; | |
1475 | ||
1476 | HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0), | |
1477 | &mgr->services) { | |
1478 | if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) { | |
1479 | return ofservice; | |
1480 | } | |
1481 | } | |
1482 | return NULL; | |
1483 | } |