]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/connmgr.c
ofproto-dpif-xlate: Fix conntrack fields on NXT_RESUME
[mirror_ovs.git] / ofproto / connmgr.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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 #include <errno.h>
19 #include <stdlib.h>
20
21 #include "bundles.h"
22 #include "connmgr.h"
23 #include "coverage.h"
24 #include "fail-open.h"
25 #include "in-band.h"
26 #include "odp-util.h"
27 #include "ofproto-provider.h"
28 #include "openvswitch/dynamic-string.h"
29 #include "openvswitch/ofp-actions.h"
30 #include "openvswitch/ofp-msgs.h"
31 #include "openvswitch/ofp-monitor.h"
32 #include "openvswitch/ofpbuf.h"
33 #include "openvswitch/vconn.h"
34 #include "openvswitch/vlog.h"
35 #include "ovs-atomic.h"
36 #include "pinsched.h"
37 #include "openvswitch/poll-loop.h"
38 #include "openvswitch/rconn.h"
39 #include "openvswitch/shash.h"
40 #include "sat-math.h"
41 #include "simap.h"
42 #include "stream.h"
43 #include "timeval.h"
44 #include "util.h"
45
46 VLOG_DEFINE_THIS_MODULE(connmgr);
47 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
48
49 /* An OpenFlow connection.
50 *
51 *
52 * Thread-safety
53 * =============
54 *
55 * 'ofproto_mutex' must be held whenever an ofconn is created or destroyed or,
56 * more or less equivalently, whenever an ofconn is added to or removed from a
57 * connmgr. 'ofproto_mutex' doesn't protect the data inside the ofconn, except
58 * as specifically noted below. */
59 struct ofconn {
60 /* Configuration that persists from one connection to the next. */
61
62 struct ovs_list node; /* In struct connmgr's "all_conns" list. */
63 struct hmap_node hmap_node; /* In struct connmgr's "controllers" map. */
64
65 struct connmgr *connmgr; /* Connection's manager. */
66 struct rconn *rconn; /* OpenFlow connection. */
67 enum ofconn_type type; /* Type. */
68 enum ofproto_band band; /* In-band or out-of-band? */
69 bool enable_async_msgs; /* Initially enable async messages? */
70 bool want_packet_in_on_miss;
71
72 /* State that should be cleared from one connection to the next. */
73
74 /* OpenFlow state. */
75 enum ofp12_controller_role role; /* Role. */
76 enum ofputil_protocol protocol; /* Current protocol variant. */
77 enum ofputil_packet_in_format packet_in_format;
78
79 /* OFPT_PACKET_IN related data. */
80 struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
81 #define N_SCHEDULERS 2
82 struct pinsched *schedulers[N_SCHEDULERS];
83 int miss_send_len; /* Bytes to send of buffered packets. */
84 uint16_t controller_id; /* Connection controller ID. */
85
86 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
87 * requests, and the maximum number before we stop reading OpenFlow
88 * requests. */
89 #define OFCONN_REPLY_MAX 100
90 struct rconn_packet_counter *reply_counter;
91
92 /* Asynchronous message configuration in each possible role.
93 *
94 * A 1-bit enables sending an asynchronous message for one possible reason
95 * that the message might be generated, a 0-bit disables it. */
96 struct ofputil_async_cfg *async_cfg;
97
98 /* Flow table operation logging. */
99 int n_add, n_delete, n_modify; /* Number of unreported ops of each kind. */
100 long long int first_op, last_op; /* Range of times for unreported ops. */
101 long long int next_op_report; /* Time to report ops, or LLONG_MAX. */
102 long long int op_backoff; /* Earliest time to report ops again. */
103
104 /* Flow monitors (e.g. NXST_FLOW_MONITOR). */
105
106 /* Configuration. Contains "struct ofmonitor"s. */
107 struct hmap monitors OVS_GUARDED_BY(ofproto_mutex);
108
109 /* Flow control.
110 *
111 * When too many flow monitor notifications back up in the transmit buffer,
112 * we pause the transmission of further notifications. These members track
113 * the flow control state.
114 *
115 * When notifications are flowing, 'monitor_paused' is 0. When
116 * notifications are paused, 'monitor_paused' is the value of
117 * 'monitor_seqno' at the point we paused.
118 *
119 * 'monitor_counter' counts the OpenFlow messages and bytes currently in
120 * flight. This value growing too large triggers pausing. */
121 uint64_t monitor_paused OVS_GUARDED_BY(ofproto_mutex);
122 struct rconn_packet_counter *monitor_counter OVS_GUARDED_BY(ofproto_mutex);
123
124 /* State of monitors for a single ongoing flow_mod.
125 *
126 * 'updates' is a list of "struct ofpbuf"s that contain
127 * NXST_FLOW_MONITOR_REPLY messages representing the changes made by the
128 * current flow_mod.
129 *
130 * When 'updates' is nonempty, 'sent_abbrev_update' is true if 'updates'
131 * contains an update event of type NXFME_ABBREV and false otherwise.. */
132 struct ovs_list updates OVS_GUARDED_BY(ofproto_mutex);
133 bool sent_abbrev_update OVS_GUARDED_BY(ofproto_mutex);
134
135 /* Active bundles. Contains "struct ofp_bundle"s. */
136 struct hmap bundles;
137 long long int next_bundle_expiry_check;
138 };
139
140 /* vswitchd/ovs-vswitchd.8.in documents the value of BUNDLE_IDLE_LIFETIME in
141 * seconds. That documentation must be kept in sync with the value below. */
142 #define BUNDLE_EXPIRY_INTERVAL 1000 /* Check bundle expiry every 1 sec. */
143 #define BUNDLE_IDLE_TIMEOUT_DEFAULT 10000 /* Expire idle bundles after
144 * 10 seconds. */
145
146 static unsigned int bundle_idle_timeout = BUNDLE_IDLE_TIMEOUT_DEFAULT;
147
148 static struct ofconn *ofconn_create(struct connmgr *, struct rconn *,
149 enum ofconn_type, bool enable_async_msgs)
150 OVS_REQUIRES(ofproto_mutex);
151 static void ofconn_destroy(struct ofconn *) OVS_REQUIRES(ofproto_mutex);
152 static void ofconn_flush(struct ofconn *) OVS_REQUIRES(ofproto_mutex);
153
154 static void ofconn_reconfigure(struct ofconn *,
155 const struct ofproto_controller *);
156
157 static void ofconn_run(struct ofconn *,
158 void (*handle_openflow)(struct ofconn *,
159 const struct ofpbuf *ofp_msg));
160 static void ofconn_wait(struct ofconn *);
161
162 static void ofconn_log_flow_mods(struct ofconn *);
163
164 static const char *ofconn_get_target(const struct ofconn *);
165 static char *ofconn_make_name(const struct connmgr *, const char *target);
166
167 static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst);
168
169 static void ofconn_send(const struct ofconn *, struct ofpbuf *,
170 struct rconn_packet_counter *);
171
172 static void do_send_packet_ins(struct ofconn *, struct ovs_list *txq);
173
174 /* A listener for incoming OpenFlow "service" connections. */
175 struct ofservice {
176 struct hmap_node node; /* In struct connmgr's "services" hmap. */
177 struct pvconn *pvconn; /* OpenFlow connection listener. */
178
179 /* These are not used by ofservice directly. They are settings for
180 * accepted "struct ofconn"s from the pvconn. */
181 int probe_interval; /* Max idle time before probing, in seconds. */
182 int rate_limit; /* Max packet-in rate in packets per second. */
183 int burst_limit; /* Limit on accumulating packet credits. */
184 bool enable_async_msgs; /* Initially enable async messages? */
185 uint8_t dscp; /* DSCP Value for controller connection */
186 uint32_t allowed_versions; /* OpenFlow protocol versions that may
187 * be negotiated for a session. */
188 };
189
190 static void ofservice_reconfigure(struct ofservice *,
191 const struct ofproto_controller *);
192 static int ofservice_create(struct connmgr *mgr, const char *target,
193 uint32_t allowed_versions, uint8_t dscp);
194 static void ofservice_destroy(struct connmgr *, struct ofservice *);
195 static struct ofservice *ofservice_lookup(struct connmgr *,
196 const char *target);
197
198 /* Connection manager for an OpenFlow switch. */
199 struct connmgr {
200 struct ofproto *ofproto;
201 char *name;
202 char *local_port_name;
203
204 /* OpenFlow connections. */
205 struct hmap controllers; /* All OFCONN_PRIMARY controllers. */
206 struct ovs_list all_conns; /* All controllers. All modifications are
207 protected by ofproto_mutex, so that any
208 traversals from other threads can be made
209 safe by holding the ofproto_mutex. */
210 uint64_t master_election_id; /* monotonically increasing sequence number
211 * for master election */
212 bool master_election_id_defined;
213
214 /* OpenFlow listeners. */
215 struct hmap services; /* Contains "struct ofservice"s. */
216 struct pvconn **snoops;
217 size_t n_snoops;
218
219 /* Fail open. */
220 struct fail_open *fail_open;
221 enum ofproto_fail_mode fail_mode;
222
223 /* In-band control. */
224 struct in_band *in_band;
225 struct sockaddr_in *extra_in_band_remotes;
226 size_t n_extra_remotes;
227 int in_band_queue;
228
229 ATOMIC(int) want_packet_in_on_miss; /* Sum of ofconns' values. */
230 };
231
232 static void update_in_band_remotes(struct connmgr *);
233 static void add_snooper(struct connmgr *, struct vconn *);
234 static void ofmonitor_run(struct connmgr *);
235 static void ofmonitor_wait(struct connmgr *);
236
237 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
238 * a name for the ofproto suitable for using in log messages.
239 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
240 * 'ofproto'. */
241 struct connmgr *
242 connmgr_create(struct ofproto *ofproto,
243 const char *name, const char *local_port_name)
244 {
245 struct connmgr *mgr;
246
247 mgr = xmalloc(sizeof *mgr);
248 mgr->ofproto = ofproto;
249 mgr->name = xstrdup(name);
250 mgr->local_port_name = xstrdup(local_port_name);
251
252 hmap_init(&mgr->controllers);
253 ovs_list_init(&mgr->all_conns);
254 mgr->master_election_id = 0;
255 mgr->master_election_id_defined = false;
256
257 hmap_init(&mgr->services);
258 mgr->snoops = NULL;
259 mgr->n_snoops = 0;
260
261 mgr->fail_open = NULL;
262 mgr->fail_mode = OFPROTO_FAIL_SECURE;
263
264 mgr->in_band = NULL;
265 mgr->extra_in_band_remotes = NULL;
266 mgr->n_extra_remotes = 0;
267 mgr->in_band_queue = -1;
268
269 atomic_init(&mgr->want_packet_in_on_miss, 0);
270
271 return mgr;
272 }
273
274 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
275 * packet rather than to send the packet to the controller.
276 *
277 * This function maintains the count of pre-OpenFlow1.3 with controller_id 0,
278 * as we assume these are the controllers that should receive "table-miss"
279 * notifications. */
280 static void
281 update_want_packet_in_on_miss(struct ofconn *ofconn)
282 {
283 /* We want a packet-in on miss when controller_id is zero and OpenFlow is
284 * lower than version 1.3. */
285 enum ofputil_protocol p = ofconn->protocol;
286 int new_want = (ofconn->controller_id == 0 &&
287 (p == OFPUTIL_P_NONE ||
288 ofputil_protocol_to_ofp_version(p) < OFP13_VERSION));
289
290 /* Update the setting and the count if necessary. */
291 int old_want = ofconn->want_packet_in_on_miss;
292 if (old_want != new_want) {
293 atomic_int *dst = &ofconn->connmgr->want_packet_in_on_miss;
294 int count;
295 atomic_read_relaxed(dst, &count);
296 atomic_store_relaxed(dst, count - old_want + new_want);
297
298 ofconn->want_packet_in_on_miss = new_want;
299 }
300 }
301
302 /* Frees 'mgr' and all of its resources. */
303 void
304 connmgr_destroy(struct connmgr *mgr)
305 OVS_REQUIRES(ofproto_mutex)
306 {
307 struct ofservice *ofservice, *next_ofservice;
308 struct ofconn *ofconn, *next_ofconn;
309 size_t i;
310
311 if (!mgr) {
312 return;
313 }
314
315 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
316 ofconn_destroy(ofconn);
317 }
318
319 hmap_destroy(&mgr->controllers);
320
321 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
322 ofservice_destroy(mgr, ofservice);
323 }
324 hmap_destroy(&mgr->services);
325
326 for (i = 0; i < mgr->n_snoops; i++) {
327 pvconn_close(mgr->snoops[i]);
328 }
329 free(mgr->snoops);
330
331 fail_open_destroy(mgr->fail_open);
332 mgr->fail_open = NULL;
333
334 in_band_destroy(mgr->in_band);
335 mgr->in_band = NULL;
336 free(mgr->extra_in_band_remotes);
337 free(mgr->name);
338 free(mgr->local_port_name);
339
340 free(mgr);
341 }
342
343 /* Does all of the periodic maintenance required by 'mgr'. Calls
344 * 'handle_openflow' for each message received on an OpenFlow connection,
345 * passing along the OpenFlow connection itself and the message that was sent.
346 * 'handle_openflow' must not modify or free the message. */
347 void
348 connmgr_run(struct connmgr *mgr,
349 void (*handle_openflow)(struct ofconn *,
350 const struct ofpbuf *ofp_msg))
351 OVS_EXCLUDED(ofproto_mutex)
352 {
353 struct ofconn *ofconn, *next_ofconn;
354 struct ofservice *ofservice;
355 size_t i;
356
357 if (mgr->in_band) {
358 if (!in_band_run(mgr->in_band)) {
359 in_band_destroy(mgr->in_band);
360 mgr->in_band = NULL;
361 }
362 }
363
364 LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) {
365 ofconn_run(ofconn, handle_openflow);
366 }
367 ofmonitor_run(mgr);
368
369 /* Fail-open maintenance. Do this after processing the ofconns since
370 * fail-open checks the status of the controller rconn. */
371 if (mgr->fail_open) {
372 fail_open_run(mgr->fail_open);
373 }
374
375 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
376 struct vconn *vconn;
377 int retval;
378
379 retval = pvconn_accept(ofservice->pvconn, &vconn);
380 if (!retval) {
381 struct rconn *rconn;
382 char *name;
383
384 /* Passing default value for creation of the rconn */
385 rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp,
386 vconn_get_allowed_versions(vconn));
387 name = ofconn_make_name(mgr, vconn_get_name(vconn));
388 rconn_connect_unreliably(rconn, vconn, name);
389 free(name);
390
391 ovs_mutex_lock(&ofproto_mutex);
392 ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE,
393 ofservice->enable_async_msgs);
394 ovs_mutex_unlock(&ofproto_mutex);
395
396 ofconn_set_rate_limit(ofconn, ofservice->rate_limit,
397 ofservice->burst_limit);
398 } else if (retval != EAGAIN) {
399 VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval));
400 }
401 }
402
403 for (i = 0; i < mgr->n_snoops; i++) {
404 struct vconn *vconn;
405 int retval;
406
407 retval = pvconn_accept(mgr->snoops[i], &vconn);
408 if (!retval) {
409 add_snooper(mgr, vconn);
410 } else if (retval != EAGAIN) {
411 VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval));
412 }
413 }
414 }
415
416 /* Causes the poll loop to wake up when connmgr_run() needs to run. */
417 void
418 connmgr_wait(struct connmgr *mgr)
419 {
420 struct ofservice *ofservice;
421 struct ofconn *ofconn;
422 size_t i;
423
424 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
425 ofconn_wait(ofconn);
426 }
427 ofmonitor_wait(mgr);
428 if (mgr->in_band) {
429 in_band_wait(mgr->in_band);
430 }
431 if (mgr->fail_open) {
432 fail_open_wait(mgr->fail_open);
433 }
434 HMAP_FOR_EACH (ofservice, node, &mgr->services) {
435 pvconn_wait(ofservice->pvconn);
436 }
437 for (i = 0; i < mgr->n_snoops; i++) {
438 pvconn_wait(mgr->snoops[i]);
439 }
440 }
441
442 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
443 * memory_report(). */
444 void
445 connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage)
446 {
447 const struct ofconn *ofconn;
448 unsigned int packets = 0;
449 unsigned int ofconns = 0;
450
451 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
452 int i;
453
454 ofconns++;
455
456 packets += rconn_count_txqlen(ofconn->rconn);
457 for (i = 0; i < N_SCHEDULERS; i++) {
458 struct pinsched_stats stats;
459
460 pinsched_get_stats(ofconn->schedulers[i], &stats);
461 packets += stats.n_queued;
462 }
463 }
464 simap_increase(usage, "ofconns", ofconns);
465 simap_increase(usage, "packets", packets);
466 }
467
468 /* Returns the ofproto that owns 'ofconn''s connmgr. */
469 struct ofproto *
470 ofconn_get_ofproto(const struct ofconn *ofconn)
471 {
472 return ofconn->connmgr->ofproto;
473 }
474
475 /* Sets the bundle idle timeout to 'timeout' seconds, interpreting 0 as
476 * requesting the default timeout.
477 *
478 * The OpenFlow spec mandates the timeout to be at least one second; . */
479 void
480 connmgr_set_bundle_idle_timeout(unsigned timeout)
481 {
482 bundle_idle_timeout = (timeout
483 ? sat_mul(timeout, 1000)
484 : BUNDLE_IDLE_TIMEOUT_DEFAULT);
485 }
486 \f
487 /* OpenFlow configuration. */
488
489 static void add_controller(struct connmgr *, const char *target, uint8_t dscp,
490 uint32_t allowed_versions)
491 OVS_REQUIRES(ofproto_mutex);
492 static struct ofconn *find_controller_by_target(struct connmgr *,
493 const char *target);
494 static void update_fail_open(struct connmgr *) OVS_EXCLUDED(ofproto_mutex);
495 static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
496 const struct sset *);
497
498 /* Returns true if 'mgr' has any configured primary controllers.
499 *
500 * Service controllers do not count, but configured primary controllers do
501 * count whether or not they are currently connected. */
502 bool
503 connmgr_has_controllers(const struct connmgr *mgr)
504 {
505 return !hmap_is_empty(&mgr->controllers);
506 }
507
508 /* Initializes 'info' and populates it with information about each configured
509 * primary controller. The keys in 'info' are the controllers' targets; the
510 * data values are corresponding "struct ofproto_controller_info".
511 *
512 * The caller owns 'info' and everything in it and should free it when it is no
513 * longer needed. */
514 void
515 connmgr_get_controller_info(struct connmgr *mgr, struct shash *info)
516 {
517 const struct ofconn *ofconn;
518
519 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
520 const struct rconn *rconn = ofconn->rconn;
521 const char *target = rconn_get_target(rconn);
522
523 if (!shash_find(info, target)) {
524 struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo);
525 time_t now = time_now();
526 time_t last_connection = rconn_get_last_connection(rconn);
527 time_t last_disconnect = rconn_get_last_disconnect(rconn);
528 int last_error = rconn_get_last_error(rconn);
529 int i;
530
531 shash_add(info, target, cinfo);
532
533 cinfo->is_connected = rconn_is_connected(rconn);
534 cinfo->role = ofconn->role;
535
536 smap_init(&cinfo->pairs);
537 if (last_error) {
538 smap_add(&cinfo->pairs, "last_error",
539 ovs_retval_to_string(last_error));
540 }
541
542 smap_add(&cinfo->pairs, "state", rconn_get_state(rconn));
543
544 if (last_connection != TIME_MIN) {
545 smap_add_format(&cinfo->pairs, "sec_since_connect",
546 "%ld", (long int) (now - last_connection));
547 }
548
549 if (last_disconnect != TIME_MIN) {
550 smap_add_format(&cinfo->pairs, "sec_since_disconnect",
551 "%ld", (long int) (now - last_disconnect));
552 }
553
554 for (i = 0; i < N_SCHEDULERS; i++) {
555 if (ofconn->schedulers[i]) {
556 const char *name = i ? "miss" : "action";
557 struct pinsched_stats stats;
558
559 pinsched_get_stats(ofconn->schedulers[i], &stats);
560 smap_add_nocopy(&cinfo->pairs,
561 xasprintf("packet-in-%s-backlog", name),
562 xasprintf("%u", stats.n_queued));
563 smap_add_nocopy(&cinfo->pairs,
564 xasprintf("packet-in-%s-bypassed", name),
565 xasprintf("%llu", stats.n_normal));
566 smap_add_nocopy(&cinfo->pairs,
567 xasprintf("packet-in-%s-queued", name),
568 xasprintf("%llu", stats.n_limited));
569 smap_add_nocopy(&cinfo->pairs,
570 xasprintf("packet-in-%s-dropped", name),
571 xasprintf("%llu", stats.n_queue_dropped));
572 }
573 }
574 }
575 }
576 }
577
578 void
579 connmgr_free_controller_info(struct shash *info)
580 {
581 struct shash_node *node;
582
583 SHASH_FOR_EACH (node, info) {
584 struct ofproto_controller_info *cinfo = node->data;
585 smap_destroy(&cinfo->pairs);
586 free(cinfo);
587 }
588 shash_destroy(info);
589 }
590
591 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
592 * 'controllers'. */
593 void
594 connmgr_set_controllers(struct connmgr *mgr,
595 const struct ofproto_controller *controllers,
596 size_t n_controllers, uint32_t allowed_versions)
597 OVS_EXCLUDED(ofproto_mutex)
598 {
599 bool had_controllers = connmgr_has_controllers(mgr);
600 struct shash new_controllers;
601 struct ofconn *ofconn, *next_ofconn;
602 struct ofservice *ofservice, *next_ofservice;
603 size_t i;
604
605 /* Required to add and remove ofconns. This could probably be narrowed to
606 * cover a smaller amount of code, if that yielded some benefit. */
607 ovs_mutex_lock(&ofproto_mutex);
608
609 /* Create newly configured controllers and services.
610 * Create a name to ofproto_controller mapping in 'new_controllers'. */
611 shash_init(&new_controllers);
612 for (i = 0; i < n_controllers; i++) {
613 const struct ofproto_controller *c = &controllers[i];
614
615 if (!vconn_verify_name(c->target)) {
616 bool add = false;
617 ofconn = find_controller_by_target(mgr, c->target);
618 if (!ofconn) {
619 VLOG_INFO("%s: added primary controller \"%s\"",
620 mgr->name, c->target);
621 add = true;
622 } else if (rconn_get_allowed_versions(ofconn->rconn) !=
623 allowed_versions) {
624 VLOG_INFO("%s: re-added primary controller \"%s\"",
625 mgr->name, c->target);
626 add = true;
627 ofconn_destroy(ofconn);
628 }
629 if (add) {
630 add_controller(mgr, c->target, c->dscp, allowed_versions);
631 }
632 } else if (!pvconn_verify_name(c->target)) {
633 bool add = false;
634 ofservice = ofservice_lookup(mgr, c->target);
635 if (!ofservice) {
636 VLOG_INFO("%s: added service controller \"%s\"",
637 mgr->name, c->target);
638 add = true;
639 } else if (ofservice->allowed_versions != allowed_versions) {
640 VLOG_INFO("%s: re-added service controller \"%s\"",
641 mgr->name, c->target);
642 ofservice_destroy(mgr, ofservice);
643 add = true;
644 }
645 if (add) {
646 ofservice_create(mgr, c->target, allowed_versions, c->dscp);
647 }
648 } else {
649 VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
650 mgr->name, c->target);
651 continue;
652 }
653
654 shash_add_once(&new_controllers, c->target, &controllers[i]);
655 }
656
657 /* Delete controllers that are no longer configured.
658 * Update configuration of all now-existing controllers. */
659 HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) {
660 const char *target = ofconn_get_target(ofconn);
661 struct ofproto_controller *c;
662
663 c = shash_find_data(&new_controllers, target);
664 if (!c) {
665 VLOG_INFO("%s: removed primary controller \"%s\"",
666 mgr->name, target);
667 ofconn_destroy(ofconn);
668 } else {
669 ofconn_reconfigure(ofconn, c);
670 }
671 }
672
673 /* Delete services that are no longer configured.
674 * Update configuration of all now-existing services. */
675 HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) {
676 const char *target = pvconn_get_name(ofservice->pvconn);
677 struct ofproto_controller *c;
678
679 c = shash_find_data(&new_controllers, target);
680 if (!c) {
681 VLOG_INFO("%s: removed service controller \"%s\"",
682 mgr->name, target);
683 ofservice_destroy(mgr, ofservice);
684 } else {
685 ofservice_reconfigure(ofservice, c);
686 }
687 }
688
689 shash_destroy(&new_controllers);
690
691 ovs_mutex_unlock(&ofproto_mutex);
692
693 update_in_band_remotes(mgr);
694 update_fail_open(mgr);
695 if (had_controllers != connmgr_has_controllers(mgr)) {
696 ofproto_flush_flows(mgr->ofproto);
697 }
698 }
699
700 /* Drops the connections between 'mgr' and all of its primary and secondary
701 * controllers, forcing them to reconnect. */
702 void
703 connmgr_reconnect(const struct connmgr *mgr)
704 {
705 struct ofconn *ofconn;
706
707 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
708 rconn_reconnect(ofconn->rconn);
709 }
710 }
711
712 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
713 *
714 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
715 * important controller on 'mgr' is mirrored. */
716 int
717 connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops)
718 {
719 return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops);
720 }
721
722 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
723 void
724 connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops)
725 {
726 size_t i;
727
728 for (i = 0; i < mgr->n_snoops; i++) {
729 sset_add(snoops, pvconn_get_name(mgr->snoops[i]));
730 }
731 }
732
733 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
734 bool
735 connmgr_has_snoops(const struct connmgr *mgr)
736 {
737 return mgr->n_snoops > 0;
738 }
739
740 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
741 * to be called later to finish the new ofconn's configuration. */
742 static void
743 add_controller(struct connmgr *mgr, const char *target, uint8_t dscp,
744 uint32_t allowed_versions)
745 OVS_REQUIRES(ofproto_mutex)
746 {
747 char *name = ofconn_make_name(mgr, target);
748 struct ofconn *ofconn;
749
750 ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp, allowed_versions),
751 OFCONN_PRIMARY, true);
752 rconn_connect(ofconn->rconn, target, name);
753 hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0));
754
755 free(name);
756 }
757
758 static struct ofconn *
759 find_controller_by_target(struct connmgr *mgr, const char *target)
760 {
761 struct ofconn *ofconn;
762
763 HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
764 hash_string(target, 0), &mgr->controllers) {
765 if (!strcmp(ofconn_get_target(ofconn), target)) {
766 return ofconn;
767 }
768 }
769 return NULL;
770 }
771
772 static void
773 update_in_band_remotes(struct connmgr *mgr)
774 {
775 struct sockaddr_in *addrs;
776 size_t max_addrs, n_addrs;
777 struct ofconn *ofconn;
778 size_t i;
779
780 /* Allocate enough memory for as many remotes as we could possibly have. */
781 max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers);
782 addrs = xmalloc(max_addrs * sizeof *addrs);
783 n_addrs = 0;
784
785 /* Add all the remotes. */
786 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
787 const char *target = rconn_get_target(ofconn->rconn);
788 union {
789 struct sockaddr_storage ss;
790 struct sockaddr_in in;
791 } sa;
792
793 if (ofconn->band == OFPROTO_IN_BAND
794 && stream_parse_target_with_default_port(target, OFP_PORT, &sa.ss)
795 && sa.ss.ss_family == AF_INET) {
796 addrs[n_addrs++] = sa.in;
797 }
798 }
799 for (i = 0; i < mgr->n_extra_remotes; i++) {
800 addrs[n_addrs++] = mgr->extra_in_band_remotes[i];
801 }
802
803 /* Create or update or destroy in-band. */
804 if (n_addrs) {
805 if (!mgr->in_band) {
806 in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band);
807 }
808 } else {
809 /* in_band_run() needs a chance to delete any existing in-band flows.
810 * We will destroy mgr->in_band after it's done with that. */
811 }
812 if (mgr->in_band) {
813 in_band_set_queue(mgr->in_band, mgr->in_band_queue);
814 in_band_set_remotes(mgr->in_band, addrs, n_addrs);
815 }
816
817 /* Clean up. */
818 free(addrs);
819 }
820
821 static void
822 update_fail_open(struct connmgr *mgr)
823 OVS_EXCLUDED(ofproto_mutex)
824 {
825 if (connmgr_has_controllers(mgr)
826 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
827 if (!mgr->fail_open) {
828 mgr->fail_open = fail_open_create(mgr->ofproto, mgr);
829 }
830 } else {
831 ovs_mutex_lock(&ofproto_mutex);
832 fail_open_destroy(mgr->fail_open);
833 ovs_mutex_unlock(&ofproto_mutex);
834 mgr->fail_open = NULL;
835 }
836 }
837
838 static int
839 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
840 const struct sset *sset)
841 {
842 struct pvconn **pvconns = *pvconnsp;
843 size_t n_pvconns = *n_pvconnsp;
844 const char *name;
845 int retval = 0;
846 size_t i;
847
848 for (i = 0; i < n_pvconns; i++) {
849 pvconn_close(pvconns[i]);
850 }
851 free(pvconns);
852
853 pvconns = xmalloc(sset_count(sset) * sizeof *pvconns);
854 n_pvconns = 0;
855 SSET_FOR_EACH (name, sset) {
856 struct pvconn *pvconn;
857 int error;
858 error = pvconn_open(name, 0, 0, &pvconn);
859 if (!error) {
860 pvconns[n_pvconns++] = pvconn;
861 } else {
862 VLOG_ERR("failed to listen on %s: %s", name, ovs_strerror(error));
863 if (!retval) {
864 retval = error;
865 }
866 }
867 }
868
869 *pvconnsp = pvconns;
870 *n_pvconnsp = n_pvconns;
871
872 return retval;
873 }
874
875 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
876 * means that 'ofconn' is more interesting for monitoring than a lower return
877 * value. */
878 static int
879 snoop_preference(const struct ofconn *ofconn)
880 {
881 switch (ofconn->role) {
882 case OFPCR12_ROLE_MASTER:
883 return 3;
884 case OFPCR12_ROLE_EQUAL:
885 return 2;
886 case OFPCR12_ROLE_SLAVE:
887 return 1;
888 case OFPCR12_ROLE_NOCHANGE:
889 default:
890 /* Shouldn't happen. */
891 return 0;
892 }
893 }
894
895 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
896 * Connects this vconn to a controller. */
897 static void
898 add_snooper(struct connmgr *mgr, struct vconn *vconn)
899 {
900 struct ofconn *ofconn, *best;
901
902 /* Pick a controller for monitoring. */
903 best = NULL;
904 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
905 if (ofconn->type == OFCONN_PRIMARY
906 && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
907 best = ofconn;
908 }
909 }
910
911 if (best) {
912 rconn_add_monitor(best->rconn, vconn);
913 } else {
914 VLOG_INFO_RL(&rl, "no controller connection to snoop");
915 vconn_close(vconn);
916 }
917 }
918 \f
919 /* Public ofconn functions. */
920
921 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
922 enum ofconn_type
923 ofconn_get_type(const struct ofconn *ofconn)
924 {
925 return ofconn->type;
926 }
927
928 /* If a master election id is defined, stores it into '*idp' and returns
929 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
930 bool
931 ofconn_get_master_election_id(const struct ofconn *ofconn, uint64_t *idp)
932 {
933 *idp = (ofconn->connmgr->master_election_id_defined
934 ? ofconn->connmgr->master_election_id
935 : UINT64_MAX);
936 return ofconn->connmgr->master_election_id_defined;
937 }
938
939 /* Sets the master election id.
940 *
941 * Returns true if successful, false if the id is stale
942 */
943 bool
944 ofconn_set_master_election_id(struct ofconn *ofconn, uint64_t id)
945 {
946 if (ofconn->connmgr->master_election_id_defined
947 &&
948 /* Unsigned difference interpreted as a two's complement signed
949 * value */
950 (int64_t)(id - ofconn->connmgr->master_election_id) < 0) {
951 return false;
952 }
953 ofconn->connmgr->master_election_id = id;
954 ofconn->connmgr->master_election_id_defined = true;
955
956 return true;
957 }
958
959 /* Returns the role configured for 'ofconn'.
960 *
961 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
962 enum ofp12_controller_role
963 ofconn_get_role(const struct ofconn *ofconn)
964 {
965 return ofconn->role;
966 }
967
968 void
969 ofconn_send_role_status(struct ofconn *ofconn, uint32_t role, uint8_t reason)
970 {
971 struct ofputil_role_status status;
972 struct ofpbuf *buf;
973
974 status.reason = reason;
975 status.role = role;
976 ofconn_get_master_election_id(ofconn, &status.generation_id);
977
978 buf = ofputil_encode_role_status(&status, ofconn_get_protocol(ofconn));
979 if (buf) {
980 ofconn_send(ofconn, buf, NULL);
981 }
982 }
983
984 /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
985 * any existing master is demoted to a slave. */
986 void
987 ofconn_set_role(struct ofconn *ofconn, enum ofp12_controller_role role)
988 {
989 if (role != ofconn->role && role == OFPCR12_ROLE_MASTER) {
990 struct ofconn *other;
991
992 LIST_FOR_EACH (other, node, &ofconn->connmgr->all_conns) {
993 if (other->role == OFPCR12_ROLE_MASTER) {
994 other->role = OFPCR12_ROLE_SLAVE;
995 ofconn_send_role_status(other, OFPCR12_ROLE_SLAVE, OFPCRR_MASTER_REQUEST);
996 }
997 }
998 }
999 ofconn->role = role;
1000 }
1001
1002 void
1003 ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable)
1004 {
1005 struct ofputil_async_cfg ac = ofconn_get_async_config(ofconn);
1006 uint32_t bit = 1u << OFPR_INVALID_TTL;
1007 if (enable) {
1008 ac.master[OAM_PACKET_IN] |= bit;
1009 } else {
1010 ac.master[OAM_PACKET_IN] &= ~bit;
1011 }
1012 ofconn_set_async_config(ofconn, &ac);
1013 }
1014
1015 bool
1016 ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn)
1017 {
1018 struct ofputil_async_cfg ac = ofconn_get_async_config(ofconn);
1019 uint32_t bit = 1u << OFPR_INVALID_TTL;
1020 return (ac.master[OAM_PACKET_IN] & bit) != 0;
1021 }
1022
1023 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
1024 *
1025 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
1026 * completed version negotiation. This can't happen if at least one OpenFlow
1027 * message, other than OFPT_HELLO, has been received on the connection (such as
1028 * in ofproto.c's message handling code), since version negotiation is a
1029 * prerequisite for starting to receive messages. This means that
1030 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
1031 enum ofputil_protocol
1032 ofconn_get_protocol(const struct ofconn *ofconn)
1033 {
1034 if (ofconn->protocol == OFPUTIL_P_NONE &&
1035 rconn_is_connected(ofconn->rconn)) {
1036 int version = rconn_get_version(ofconn->rconn);
1037 if (version > 0) {
1038 ofconn_set_protocol(CONST_CAST(struct ofconn *, ofconn),
1039 ofputil_protocol_from_ofp_version(version));
1040 }
1041 }
1042
1043 return ofconn->protocol;
1044 }
1045
1046 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
1047 *
1048 * (This doesn't actually send anything to accomplish this. Presumably the
1049 * caller already did that.) */
1050 void
1051 ofconn_set_protocol(struct ofconn *ofconn, enum ofputil_protocol protocol)
1052 {
1053 ofconn->protocol = protocol;
1054 update_want_packet_in_on_miss(ofconn);
1055 }
1056
1057 /* Returns the currently configured packet in format for 'ofconn', one of
1058 * NXPIF_*.
1059 *
1060 * The default, if no other format has been set, is NXPIF_STANDARD. */
1061 enum ofputil_packet_in_format
1062 ofconn_get_packet_in_format(struct ofconn *ofconn)
1063 {
1064 return ofconn->packet_in_format;
1065 }
1066
1067 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
1068 * NXPIF_*). */
1069 void
1070 ofconn_set_packet_in_format(struct ofconn *ofconn,
1071 enum ofputil_packet_in_format packet_in_format)
1072 {
1073 ofconn->packet_in_format = packet_in_format;
1074 }
1075
1076 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
1077 *
1078 * The connection controller ID is used for OFPP_CONTROLLER and
1079 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
1080 void
1081 ofconn_set_controller_id(struct ofconn *ofconn, uint16_t controller_id)
1082 {
1083 ofconn->controller_id = controller_id;
1084 update_want_packet_in_on_miss(ofconn);
1085 }
1086
1087 /* Returns the default miss send length for 'ofconn'. */
1088 int
1089 ofconn_get_miss_send_len(const struct ofconn *ofconn)
1090 {
1091 return ofconn->miss_send_len;
1092 }
1093
1094 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
1095 void
1096 ofconn_set_miss_send_len(struct ofconn *ofconn, int miss_send_len)
1097 {
1098 ofconn->miss_send_len = miss_send_len;
1099 }
1100
1101 void
1102 ofconn_set_async_config(struct ofconn *ofconn,
1103 const struct ofputil_async_cfg *ac)
1104 {
1105 if (!ofconn->async_cfg) {
1106 ofconn->async_cfg = xmalloc(sizeof *ofconn->async_cfg);
1107 }
1108 *ofconn->async_cfg = *ac;
1109
1110 if (ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn))
1111 < OFP14_VERSION) {
1112 if (ofconn->async_cfg->master[OAM_PACKET_IN] & (1u << OFPR_ACTION)) {
1113 ofconn->async_cfg->master[OAM_PACKET_IN] |= OFPR14_ACTION_BITS;
1114 }
1115 if (ofconn->async_cfg->slave[OAM_PACKET_IN] & (1u << OFPR_ACTION)) {
1116 ofconn->async_cfg->slave[OAM_PACKET_IN] |= OFPR14_ACTION_BITS;
1117 }
1118 }
1119 }
1120
1121 struct ofputil_async_cfg
1122 ofconn_get_async_config(const struct ofconn *ofconn)
1123 {
1124 if (ofconn->async_cfg) {
1125 return *ofconn->async_cfg;
1126 }
1127
1128 int version = rconn_get_version(ofconn->rconn);
1129 return (version < 0 || !ofconn->enable_async_msgs
1130 ? OFPUTIL_ASYNC_CFG_INIT
1131 : ofputil_async_cfg_default(version));
1132 }
1133
1134 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1135 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1136 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1137 * controller has accepted some of the replies.) */
1138 void
1139 ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg)
1140 {
1141 ofconn_send(ofconn, msg, ofconn->reply_counter);
1142 }
1143
1144 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
1145 * accounting them as replies. */
1146 void
1147 ofconn_send_replies(const struct ofconn *ofconn, struct ovs_list *replies)
1148 {
1149 struct ofpbuf *reply;
1150
1151 LIST_FOR_EACH_POP (reply, list_node, replies) {
1152 ofconn_send_reply(ofconn, reply);
1153 }
1154 }
1155
1156 /* Sends 'error' on 'ofconn', as a reply to 'request'. */
1157 void
1158 ofconn_send_error(const struct ofconn *ofconn,
1159 const struct ofp_header *request, enum ofperr error)
1160 {
1161 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1162 struct ofpbuf *reply;
1163
1164 reply = ofperr_encode_reply(error, request);
1165 if (!VLOG_DROP_INFO(&err_rl)) {
1166 const char *type_name;
1167 size_t request_len;
1168 enum ofpraw raw;
1169
1170 request_len = ntohs(request->length);
1171 type_name = (!ofpraw_decode_partial(&raw, request,
1172 MIN(64, request_len))
1173 ? ofpraw_get_name(raw)
1174 : "invalid");
1175
1176 VLOG_INFO("%s: sending %s error reply to %s message",
1177 rconn_get_name(ofconn->rconn), ofperr_to_string(error),
1178 type_name);
1179 }
1180 ofconn_send_reply(ofconn, reply);
1181 }
1182
1183 /* Reports that a flow_mod operation of the type specified by 'command' was
1184 * successfully executed by 'ofconn', so that the connmgr can log it. */
1185 void
1186 ofconn_report_flow_mod(struct ofconn *ofconn,
1187 enum ofp_flow_mod_command command)
1188 {
1189 long long int now;
1190
1191 switch (command) {
1192 case OFPFC_ADD:
1193 ofconn->n_add++;
1194 break;
1195
1196 case OFPFC_MODIFY:
1197 case OFPFC_MODIFY_STRICT:
1198 ofconn->n_modify++;
1199 break;
1200
1201 case OFPFC_DELETE:
1202 case OFPFC_DELETE_STRICT:
1203 ofconn->n_delete++;
1204 break;
1205 }
1206
1207 now = time_msec();
1208 if (ofconn->next_op_report == LLONG_MAX) {
1209 ofconn->first_op = now;
1210 ofconn->next_op_report = MAX(now + 10 * 1000, ofconn->op_backoff);
1211 ofconn->op_backoff = ofconn->next_op_report + 60 * 1000;
1212 }
1213 ofconn->last_op = now;
1214 }
1215 \f
1216 /* OpenFlow 1.4 bundles. */
1217
1218 static inline uint32_t
1219 bundle_hash(uint32_t id)
1220 {
1221 return hash_int(id, 0);
1222 }
1223
1224 struct ofp_bundle *
1225 ofconn_get_bundle(struct ofconn *ofconn, uint32_t id)
1226 {
1227 struct ofp_bundle *bundle;
1228
1229 HMAP_FOR_EACH_IN_BUCKET(bundle, node, bundle_hash(id), &ofconn->bundles) {
1230 if (bundle->id == id) {
1231 return bundle;
1232 }
1233 }
1234
1235 return NULL;
1236 }
1237
1238 void
1239 ofconn_insert_bundle(struct ofconn *ofconn, struct ofp_bundle *bundle)
1240 {
1241 hmap_insert(&ofconn->bundles, &bundle->node, bundle_hash(bundle->id));
1242 }
1243
1244 void
1245 ofconn_remove_bundle(struct ofconn *ofconn, struct ofp_bundle *bundle)
1246 {
1247 hmap_remove(&ofconn->bundles, &bundle->node);
1248 }
1249
1250 static void
1251 bundle_remove_all(struct ofconn *ofconn)
1252 {
1253 struct ofp_bundle *b, *next;
1254
1255 HMAP_FOR_EACH_SAFE (b, next, node, &ofconn->bundles) {
1256 ofp_bundle_remove__(ofconn, b);
1257 }
1258 }
1259
1260 static void
1261 bundle_remove_expired(struct ofconn *ofconn, long long int now)
1262 {
1263 struct ofp_bundle *b, *next;
1264 long long int limit = now - bundle_idle_timeout;
1265
1266 HMAP_FOR_EACH_SAFE (b, next, node, &ofconn->bundles) {
1267 if (b->used <= limit) {
1268 ofconn_send_error(ofconn, b->msg, OFPERR_OFPBFC_TIMEOUT);
1269 ofp_bundle_remove__(ofconn, b);
1270 }
1271 }
1272 }
1273 \f
1274 /* Private ofconn functions. */
1275
1276 static const char *
1277 ofconn_get_target(const struct ofconn *ofconn)
1278 {
1279 return rconn_get_target(ofconn->rconn);
1280 }
1281
1282 static struct ofconn *
1283 ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type,
1284 bool enable_async_msgs)
1285 OVS_REQUIRES(ofproto_mutex)
1286 {
1287 struct ofconn *ofconn;
1288
1289 ofconn = xzalloc(sizeof *ofconn);
1290 ofconn->connmgr = mgr;
1291 ovs_list_push_back(&mgr->all_conns, &ofconn->node);
1292 ofconn->rconn = rconn;
1293 ofconn->type = type;
1294 ofconn->enable_async_msgs = enable_async_msgs;
1295
1296 hmap_init(&ofconn->monitors);
1297 ovs_list_init(&ofconn->updates);
1298
1299 hmap_init(&ofconn->bundles);
1300 ofconn->next_bundle_expiry_check = time_msec() + BUNDLE_EXPIRY_INTERVAL;
1301
1302 ofconn_flush(ofconn);
1303
1304 return ofconn;
1305 }
1306
1307 /* Clears all of the state in 'ofconn' that should not persist from one
1308 * connection to the next. */
1309 static void
1310 ofconn_flush(struct ofconn *ofconn)
1311 OVS_REQUIRES(ofproto_mutex)
1312 {
1313 struct ofmonitor *monitor, *next_monitor;
1314 int i;
1315
1316 ofconn_log_flow_mods(ofconn);
1317
1318 ofconn->role = OFPCR12_ROLE_EQUAL;
1319 ofconn_set_protocol(ofconn, OFPUTIL_P_NONE);
1320 ofconn->packet_in_format = OFPUTIL_PACKET_IN_STD;
1321
1322 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1323 ofconn->packet_in_counter = rconn_packet_counter_create();
1324 for (i = 0; i < N_SCHEDULERS; i++) {
1325 if (ofconn->schedulers[i]) {
1326 int rate, burst;
1327
1328 pinsched_get_limits(ofconn->schedulers[i], &rate, &burst);
1329 pinsched_destroy(ofconn->schedulers[i]);
1330 ofconn->schedulers[i] = pinsched_create(rate, burst);
1331 }
1332 }
1333 ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY
1334 ? OFP_DEFAULT_MISS_SEND_LEN
1335 : 0);
1336 ofconn->controller_id = 0;
1337
1338 rconn_packet_counter_destroy(ofconn->reply_counter);
1339 ofconn->reply_counter = rconn_packet_counter_create();
1340
1341 free(ofconn->async_cfg);
1342 ofconn->async_cfg = NULL;
1343
1344 ofconn->n_add = ofconn->n_delete = ofconn->n_modify = 0;
1345 ofconn->first_op = ofconn->last_op = LLONG_MIN;
1346 ofconn->next_op_report = LLONG_MAX;
1347 ofconn->op_backoff = LLONG_MIN;
1348
1349 HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node,
1350 &ofconn->monitors) {
1351 ofmonitor_destroy(monitor);
1352 }
1353 rconn_packet_counter_destroy(ofconn->monitor_counter);
1354 ofconn->monitor_counter = rconn_packet_counter_create();
1355 ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */
1356 }
1357
1358 static void
1359 ofconn_destroy(struct ofconn *ofconn)
1360 OVS_REQUIRES(ofproto_mutex)
1361 {
1362 ofconn_flush(ofconn);
1363
1364 /* Force clearing of want_packet_in_on_miss to keep the global count
1365 * accurate. */
1366 ofconn->controller_id = 1;
1367 update_want_packet_in_on_miss(ofconn);
1368
1369 if (ofconn->type == OFCONN_PRIMARY) {
1370 hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node);
1371 }
1372
1373 bundle_remove_all(ofconn);
1374 hmap_destroy(&ofconn->bundles);
1375
1376 hmap_destroy(&ofconn->monitors);
1377 ovs_list_remove(&ofconn->node);
1378 rconn_destroy(ofconn->rconn);
1379 rconn_packet_counter_destroy(ofconn->packet_in_counter);
1380 rconn_packet_counter_destroy(ofconn->reply_counter);
1381 rconn_packet_counter_destroy(ofconn->monitor_counter);
1382 free(ofconn);
1383 }
1384
1385 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1386 * target. */
1387 static void
1388 ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c)
1389 {
1390 int probe_interval;
1391
1392 ofconn->band = c->band;
1393 ofconn->enable_async_msgs = c->enable_async_msgs;
1394
1395 rconn_set_max_backoff(ofconn->rconn, c->max_backoff);
1396
1397 probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0;
1398 rconn_set_probe_interval(ofconn->rconn, probe_interval);
1399
1400 ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit);
1401
1402 /* If dscp value changed reconnect. */
1403 if (c->dscp != rconn_get_dscp(ofconn->rconn)) {
1404 rconn_set_dscp(ofconn->rconn, c->dscp);
1405 rconn_reconnect(ofconn->rconn);
1406 }
1407 }
1408
1409 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1410 * messages. */
1411 static bool
1412 ofconn_may_recv(const struct ofconn *ofconn)
1413 {
1414 int count = rconn_packet_counter_n_packets(ofconn->reply_counter);
1415 return count < OFCONN_REPLY_MAX;
1416 }
1417
1418 static void
1419 ofconn_run(struct ofconn *ofconn,
1420 void (*handle_openflow)(struct ofconn *,
1421 const struct ofpbuf *ofp_msg))
1422 {
1423 struct connmgr *mgr = ofconn->connmgr;
1424 size_t i;
1425
1426 for (i = 0; i < N_SCHEDULERS; i++) {
1427 struct ovs_list txq;
1428
1429 pinsched_run(ofconn->schedulers[i], &txq);
1430 do_send_packet_ins(ofconn, &txq);
1431 }
1432
1433 rconn_run(ofconn->rconn);
1434
1435 /* Limit the number of iterations to avoid starving other tasks. */
1436 for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) {
1437 struct ofpbuf *of_msg = rconn_recv(ofconn->rconn);
1438 if (!of_msg) {
1439 break;
1440 }
1441
1442 if (mgr->fail_open) {
1443 fail_open_maybe_recover(mgr->fail_open);
1444 }
1445
1446 handle_openflow(ofconn, of_msg);
1447 ofpbuf_delete(of_msg);
1448 }
1449
1450 long long int now = time_msec();
1451
1452 if (now >= ofconn->next_bundle_expiry_check) {
1453 ofconn->next_bundle_expiry_check = now + BUNDLE_EXPIRY_INTERVAL;
1454 bundle_remove_expired(ofconn, now);
1455 }
1456
1457 if (now >= ofconn->next_op_report) {
1458 ofconn_log_flow_mods(ofconn);
1459 }
1460
1461 ovs_mutex_lock(&ofproto_mutex);
1462 if (!rconn_is_alive(ofconn->rconn)) {
1463 ofconn_destroy(ofconn);
1464 } else if (!rconn_is_connected(ofconn->rconn)) {
1465 ofconn_flush(ofconn);
1466 }
1467 ovs_mutex_unlock(&ofproto_mutex);
1468 }
1469
1470 static void
1471 ofconn_wait(struct ofconn *ofconn)
1472 {
1473 int i;
1474
1475 for (i = 0; i < N_SCHEDULERS; i++) {
1476 pinsched_wait(ofconn->schedulers[i]);
1477 }
1478 rconn_run_wait(ofconn->rconn);
1479 if (ofconn_may_recv(ofconn)) {
1480 rconn_recv_wait(ofconn->rconn);
1481 }
1482 if (ofconn->next_op_report != LLONG_MAX) {
1483 poll_timer_wait_until(ofconn->next_op_report);
1484 }
1485 }
1486
1487 static void
1488 ofconn_log_flow_mods(struct ofconn *ofconn)
1489 {
1490 int n_flow_mods = ofconn->n_add + ofconn->n_delete + ofconn->n_modify;
1491 if (n_flow_mods) {
1492 long long int ago = (time_msec() - ofconn->first_op) / 1000;
1493 long long int interval = (ofconn->last_op - ofconn->first_op) / 1000;
1494 struct ds s;
1495
1496 ds_init(&s);
1497 ds_put_format(&s, "%d flow_mods ", n_flow_mods);
1498 if (interval == ago) {
1499 ds_put_format(&s, "in the last %lld s", ago);
1500 } else if (interval) {
1501 ds_put_format(&s, "in the %lld s starting %lld s ago",
1502 interval, ago);
1503 } else {
1504 ds_put_format(&s, "%lld s ago", ago);
1505 }
1506
1507 ds_put_cstr(&s, " (");
1508 if (ofconn->n_add) {
1509 ds_put_format(&s, "%d adds, ", ofconn->n_add);
1510 }
1511 if (ofconn->n_delete) {
1512 ds_put_format(&s, "%d deletes, ", ofconn->n_delete);
1513 }
1514 if (ofconn->n_modify) {
1515 ds_put_format(&s, "%d modifications, ", ofconn->n_modify);
1516 }
1517 s.length -= 2;
1518 ds_put_char(&s, ')');
1519
1520 VLOG_INFO("%s: %s", rconn_get_name(ofconn->rconn), ds_cstr(&s));
1521 ds_destroy(&s);
1522
1523 ofconn->n_add = ofconn->n_delete = ofconn->n_modify = 0;
1524 }
1525 ofconn->next_op_report = LLONG_MAX;
1526 }
1527
1528 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1529 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1530 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1531 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1532 * 'ofconn'. */
1533 static bool
1534 ofconn_receives_async_msg(const struct ofconn *ofconn,
1535 enum ofputil_async_msg_type type,
1536 unsigned int reason)
1537 {
1538 ovs_assert(reason < 32);
1539 ovs_assert((unsigned int) type < OAM_N_TYPES);
1540
1541 /* Keep the following code in sync with the documentation in the
1542 * "Asynchronous Messages" section in 'topics/design' */
1543
1544 if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) {
1545 /* Service connections don't get asynchronous messages unless they have
1546 * explicitly asked for them by setting a nonzero miss send length. */
1547 return false;
1548 }
1549
1550 struct ofputil_async_cfg ac = ofconn_get_async_config(ofconn);
1551 uint32_t *masks = (ofconn->role == OFPCR12_ROLE_SLAVE
1552 ? ac.slave
1553 : ac.master);
1554 return (masks[type] & (1u << reason)) != 0;
1555 }
1556
1557 /* This function returns true to indicate that a packet_in message
1558 * for a "table-miss" should be sent to at least one controller.
1559 *
1560 * False otherwise. */
1561 bool
1562 connmgr_wants_packet_in_on_miss(struct connmgr *mgr)
1563 {
1564 int count;
1565
1566 atomic_read_relaxed(&mgr->want_packet_in_on_miss, &count);
1567 return count > 0;
1568 }
1569
1570 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1571 * 'target', suitable for use in log messages for identifying the connection.
1572 *
1573 * The name is dynamically allocated. The caller should free it (with free())
1574 * when it is no longer needed. */
1575 static char *
1576 ofconn_make_name(const struct connmgr *mgr, const char *target)
1577 {
1578 return xasprintf("%s<->%s", mgr->name, target);
1579 }
1580
1581 static void
1582 ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst)
1583 {
1584 int i;
1585
1586 for (i = 0; i < N_SCHEDULERS; i++) {
1587 struct pinsched **s = &ofconn->schedulers[i];
1588
1589 if (rate > 0) {
1590 if (!*s) {
1591 *s = pinsched_create(rate, burst);
1592 } else {
1593 pinsched_set_limits(*s, rate, burst);
1594 }
1595 } else {
1596 pinsched_destroy(*s);
1597 *s = NULL;
1598 }
1599 }
1600 }
1601
1602 static void
1603 ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg,
1604 struct rconn_packet_counter *counter)
1605 {
1606 ofpmsg_update_length(msg);
1607 rconn_send(ofconn->rconn, msg, counter);
1608 }
1609 \f
1610 /* Sending asynchronous messages. */
1611
1612 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1613 * controllers managed by 'mgr'. For messages caused by a controller
1614 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1615 * request; otherwise, specify 'source' as NULL. */
1616 void
1617 connmgr_send_port_status(struct connmgr *mgr, struct ofconn *source,
1618 const struct ofputil_phy_port *pp, uint8_t reason)
1619 {
1620 /* XXX Should limit the number of queued port status change messages. */
1621 struct ofputil_port_status ps;
1622 struct ofconn *ofconn;
1623
1624 ps.reason = reason;
1625 ps.desc = *pp;
1626 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1627 if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) {
1628 struct ofpbuf *msg;
1629
1630 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1631 * generate OFPT_PORT_STATUS messages. That requirement was a
1632 * relic of how OpenFlow originally supported a single controller,
1633 * so that one could expect the controller to already know the
1634 * changes it had made.
1635 *
1636 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1637 * OFPT_PORT_STATUS messages to every controller. This is
1638 * obviously more useful in the multi-controller case. We could
1639 * always implement it that way in OVS, but that would risk
1640 * confusing controllers that are intended for single-controller
1641 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1642 * in response to any OFPT_PORT_STATUS!)
1643 *
1644 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1645 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1646 * originating controller. In a single-controller environment, in
1647 * particular, this means that it will never generate
1648 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1649 if (ofconn == source
1650 && rconn_get_version(ofconn->rconn) < OFP15_VERSION) {
1651 continue;
1652 }
1653
1654 msg = ofputil_encode_port_status(&ps, ofconn_get_protocol(ofconn));
1655 ofconn_send(ofconn, msg, NULL);
1656 }
1657 }
1658 }
1659
1660 /* Sends an OFPT_REQUESTFORWARD message with 'request' and 'reason' to
1661 * appropriate controllers managed by 'mgr'. For messages caused by a
1662 * controller OFPT_GROUP_MOD and OFPT_METER_MOD, specify 'source' as the
1663 * controller connection that sent the request; otherwise, specify 'source'
1664 * as NULL. */
1665 void
1666 connmgr_send_requestforward(struct connmgr *mgr, const struct ofconn *source,
1667 const struct ofputil_requestforward *rf)
1668 {
1669 struct ofconn *ofconn;
1670
1671 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1672 if (ofconn_receives_async_msg(ofconn, OAM_REQUESTFORWARD, rf->reason)
1673 && rconn_get_version(ofconn->rconn) >= OFP14_VERSION
1674 && ofconn != source) {
1675 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1676 ofconn_send(ofconn, ofputil_encode_requestforward(rf, protocol),
1677 NULL);
1678 }
1679 }
1680 }
1681
1682 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1683 * appropriate controllers managed by 'mgr'.
1684 *
1685 * This may be called from the RCU thread. */
1686 void
1687 connmgr_send_flow_removed(struct connmgr *mgr,
1688 const struct ofputil_flow_removed *fr)
1689 OVS_REQUIRES(ofproto_mutex)
1690 {
1691 struct ofconn *ofconn;
1692
1693 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1694 if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) {
1695 struct ofpbuf *msg;
1696
1697 /* Account flow expirations as replies to OpenFlow requests. That
1698 * works because preventing OpenFlow requests from being processed
1699 * also prevents new flows from being added (and expiring). (It
1700 * also prevents processing OpenFlow requests that would not add
1701 * new flows, so it is imperfect.) */
1702 msg = ofputil_encode_flow_removed(fr, ofconn_get_protocol(ofconn));
1703 ofconn_send_reply(ofconn, msg);
1704 }
1705 }
1706 }
1707
1708 /* Sends an OFPT_TABLE_STATUS message with 'reason' to appropriate controllers
1709 * managed by 'mgr'. When the table state changes, the controller needs to be
1710 * informed with the OFPT_TABLE_STATUS message. The reason values
1711 * OFPTR_VACANCY_DOWN and OFPTR_VACANCY_UP identify a vacancy message. The
1712 * vacancy events are generated when the remaining space in the flow table
1713 * changes and crosses one of the vacancy thereshold specified by
1714 * OFPT_TABLE_MOD. */
1715 void
1716 connmgr_send_table_status(struct connmgr *mgr,
1717 const struct ofputil_table_desc *td,
1718 uint8_t reason)
1719 {
1720 struct ofputil_table_status ts;
1721 struct ofconn *ofconn;
1722
1723 ts.reason = reason;
1724 ts.desc = *td;
1725
1726 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1727 if (ofconn_receives_async_msg(ofconn, OAM_TABLE_STATUS, reason)) {
1728 struct ofpbuf *msg;
1729
1730 msg = ofputil_encode_table_status(&ts,
1731 ofconn_get_protocol(ofconn));
1732 if (msg) {
1733 ofconn_send(ofconn, msg, NULL);
1734 }
1735 }
1736 }
1737 }
1738
1739 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1740 * necessary according to their individual configurations. */
1741 void
1742 connmgr_send_async_msg(struct connmgr *mgr,
1743 const struct ofproto_async_msg *am)
1744 {
1745 struct ofconn *ofconn;
1746
1747 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
1748 enum ofputil_protocol protocol = ofconn_get_protocol(ofconn);
1749 if (protocol == OFPUTIL_P_NONE || !rconn_is_connected(ofconn->rconn)
1750 || ofconn->controller_id != am->controller_id
1751 || !ofconn_receives_async_msg(ofconn, am->oam,
1752 am->pin.up.base.reason)) {
1753 continue;
1754 }
1755
1756 struct ofpbuf *msg = ofputil_encode_packet_in_private(
1757 &am->pin.up, protocol, ofconn->packet_in_format);
1758
1759 struct ovs_list txq;
1760 bool is_miss = (am->pin.up.base.reason == OFPR_NO_MATCH ||
1761 am->pin.up.base.reason == OFPR_EXPLICIT_MISS ||
1762 am->pin.up.base.reason == OFPR_IMPLICIT_MISS);
1763 pinsched_send(ofconn->schedulers[is_miss],
1764 am->pin.up.base.flow_metadata.flow.in_port.ofp_port,
1765 msg, &txq);
1766 do_send_packet_ins(ofconn, &txq);
1767 }
1768 }
1769
1770 static void
1771 do_send_packet_ins(struct ofconn *ofconn, struct ovs_list *txq)
1772 {
1773 struct ofpbuf *pin;
1774
1775 LIST_FOR_EACH_POP (pin, list_node, txq) {
1776 if (rconn_send_with_limit(ofconn->rconn, pin,
1777 ofconn->packet_in_counter, 100) == EAGAIN) {
1778 static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(5, 5);
1779
1780 VLOG_INFO_RL(&rll, "%s: dropping packet-in due to queue overflow",
1781 rconn_get_name(ofconn->rconn));
1782 }
1783 }
1784 }
1785 \f
1786 /* Fail-open settings. */
1787
1788 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1789 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1790 enum ofproto_fail_mode
1791 connmgr_get_fail_mode(const struct connmgr *mgr)
1792 {
1793 return mgr->fail_mode;
1794 }
1795
1796 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1797 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1798 void
1799 connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode)
1800 {
1801 if (mgr->fail_mode != fail_mode) {
1802 mgr->fail_mode = fail_mode;
1803 update_fail_open(mgr);
1804 if (!connmgr_has_controllers(mgr)) {
1805 ofproto_flush_flows(mgr->ofproto);
1806 }
1807 }
1808 }
1809 \f
1810 /* Fail-open implementation. */
1811
1812 /* Returns the longest probe interval among the primary controllers configured
1813 * on 'mgr'. Returns 0 if there are no primary controllers. */
1814 int
1815 connmgr_get_max_probe_interval(const struct connmgr *mgr)
1816 {
1817 const struct ofconn *ofconn;
1818 int max_probe_interval;
1819
1820 max_probe_interval = 0;
1821 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1822 int probe_interval = rconn_get_probe_interval(ofconn->rconn);
1823 max_probe_interval = MAX(max_probe_interval, probe_interval);
1824 }
1825 return max_probe_interval;
1826 }
1827
1828 /* Returns the number of seconds for which all of 'mgr's primary controllers
1829 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1830 int
1831 connmgr_failure_duration(const struct connmgr *mgr)
1832 {
1833 const struct ofconn *ofconn;
1834 int min_failure_duration;
1835
1836 if (!connmgr_has_controllers(mgr)) {
1837 return 0;
1838 }
1839
1840 min_failure_duration = INT_MAX;
1841 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1842 int failure_duration = rconn_failure_duration(ofconn->rconn);
1843 min_failure_duration = MIN(min_failure_duration, failure_duration);
1844 }
1845 return min_failure_duration;
1846 }
1847
1848 /* Returns true if at least one primary controller is connected (regardless of
1849 * whether those controllers are believed to have authenticated and accepted
1850 * this switch), false if none of them are connected. */
1851 bool
1852 connmgr_is_any_controller_connected(const struct connmgr *mgr)
1853 {
1854 const struct ofconn *ofconn;
1855
1856 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1857 if (rconn_is_connected(ofconn->rconn)) {
1858 return true;
1859 }
1860 }
1861 return false;
1862 }
1863
1864 /* Returns true if at least one primary controller is believed to have
1865 * authenticated and accepted this switch, false otherwise. */
1866 bool
1867 connmgr_is_any_controller_admitted(const struct connmgr *mgr)
1868 {
1869 const struct ofconn *ofconn;
1870
1871 HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) {
1872 if (rconn_is_admitted(ofconn->rconn)) {
1873 return true;
1874 }
1875 }
1876 return false;
1877 }
1878 \f
1879 /* In-band configuration. */
1880
1881 static bool any_extras_changed(const struct connmgr *,
1882 const struct sockaddr_in *extras, size_t n);
1883
1884 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1885 * in-band control should guarantee access, in the same way that in-band
1886 * control guarantees access to OpenFlow controllers. */
1887 void
1888 connmgr_set_extra_in_band_remotes(struct connmgr *mgr,
1889 const struct sockaddr_in *extras, size_t n)
1890 {
1891 if (!any_extras_changed(mgr, extras, n)) {
1892 return;
1893 }
1894
1895 free(mgr->extra_in_band_remotes);
1896 mgr->n_extra_remotes = n;
1897 mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras);
1898
1899 update_in_band_remotes(mgr);
1900 }
1901
1902 /* Sets the OpenFlow queue used by flows set up by in-band control on
1903 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1904 * flows will use the default queue. */
1905 void
1906 connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id)
1907 {
1908 if (queue_id != mgr->in_band_queue) {
1909 mgr->in_band_queue = queue_id;
1910 update_in_band_remotes(mgr);
1911 }
1912 }
1913
1914 static bool
1915 any_extras_changed(const struct connmgr *mgr,
1916 const struct sockaddr_in *extras, size_t n)
1917 {
1918 size_t i;
1919
1920 if (n != mgr->n_extra_remotes) {
1921 return true;
1922 }
1923
1924 for (i = 0; i < n; i++) {
1925 const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i];
1926 const struct sockaddr_in *new = &extras[i];
1927
1928 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
1929 old->sin_port != new->sin_port) {
1930 return true;
1931 }
1932 }
1933
1934 return false;
1935 }
1936 \f
1937 /* In-band implementation. */
1938
1939 bool
1940 connmgr_has_in_band(struct connmgr *mgr)
1941 {
1942 return mgr->in_band != NULL;
1943 }
1944 \f
1945 /* Fail-open and in-band implementation. */
1946
1947 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1948 * and standalone mode to re-create their flows.
1949 *
1950 * In-band control has more sophisticated code that manages flows itself. */
1951 void
1952 connmgr_flushed(struct connmgr *mgr)
1953 OVS_EXCLUDED(ofproto_mutex)
1954 {
1955 if (mgr->fail_open) {
1956 fail_open_flushed(mgr->fail_open);
1957 }
1958
1959 /* If there are no controllers and we're in standalone mode, set up a flow
1960 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1961 * us). Otherwise, the switch is in secure mode and we won't pass any
1962 * traffic until a controller has been defined and it tells us to do so. */
1963 if (!connmgr_has_controllers(mgr)
1964 && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) {
1965 struct ofpbuf ofpacts;
1966 struct match match;
1967
1968 ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE);
1969 ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
1970
1971 match_init_catchall(&match);
1972 ofproto_add_flow(mgr->ofproto, &match, 0, ofpacts.data,
1973 ofpacts.size);
1974
1975 ofpbuf_uninit(&ofpacts);
1976 }
1977 }
1978
1979 /* Returns the number of hidden rules created by the in-band and fail-open
1980 * implementations in table 0. (Subtracting this count from the number of
1981 * rules in the table 0 classifier, as maintained in struct oftable, yields
1982 * the number of flows that OVS should report via OpenFlow for table 0.) */
1983 int
1984 connmgr_count_hidden_rules(const struct connmgr *mgr)
1985 {
1986 int n_hidden = 0;
1987 if (mgr->in_band) {
1988 n_hidden += in_band_count_rules(mgr->in_band);
1989 }
1990 if (mgr->fail_open) {
1991 n_hidden += fail_open_count_rules(mgr->fail_open);
1992 }
1993 return n_hidden;
1994 }
1995 \f
1996 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1997 * otherwise a positive errno value.
1998 *
1999 * ofservice_reconfigure() must be called to fully configure the new
2000 * ofservice. */
2001 static int
2002 ofservice_create(struct connmgr *mgr, const char *target,
2003 uint32_t allowed_versions, uint8_t dscp)
2004 {
2005 struct ofservice *ofservice;
2006 struct pvconn *pvconn;
2007 int error;
2008
2009 error = pvconn_open(target, allowed_versions, dscp, &pvconn);
2010 if (error) {
2011 return error;
2012 }
2013
2014 ofservice = xzalloc(sizeof *ofservice);
2015 hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0));
2016 ofservice->pvconn = pvconn;
2017 ofservice->allowed_versions = allowed_versions;
2018
2019 return 0;
2020 }
2021
2022 static void
2023 ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice)
2024 {
2025 hmap_remove(&mgr->services, &ofservice->node);
2026 pvconn_close(ofservice->pvconn);
2027 free(ofservice);
2028 }
2029
2030 static void
2031 ofservice_reconfigure(struct ofservice *ofservice,
2032 const struct ofproto_controller *c)
2033 {
2034 ofservice->probe_interval = c->probe_interval;
2035 ofservice->rate_limit = c->rate_limit;
2036 ofservice->burst_limit = c->burst_limit;
2037 ofservice->enable_async_msgs = c->enable_async_msgs;
2038 ofservice->dscp = c->dscp;
2039 }
2040
2041 /* Finds and returns the ofservice within 'mgr' that has the given
2042 * 'target', or a null pointer if none exists. */
2043 static struct ofservice *
2044 ofservice_lookup(struct connmgr *mgr, const char *target)
2045 {
2046 struct ofservice *ofservice;
2047
2048 HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
2049 &mgr->services) {
2050 if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
2051 return ofservice;
2052 }
2053 }
2054 return NULL;
2055 }
2056 \f
2057 /* Flow monitors (NXST_FLOW_MONITOR). */
2058
2059 /* A counter incremented when something significant happens to an OpenFlow
2060 * rule.
2061 *
2062 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
2063 * the current value (which is then incremented).
2064 *
2065 * - When a rule is modified, its 'modify_seqno' is set to the current
2066 * value (which is then incremented).
2067 *
2068 * Thus, by comparing an old value of monitor_seqno against a rule's
2069 * 'add_seqno', one can tell whether the rule was added before or after the old
2070 * value was read, and similarly for 'modify_seqno'.
2071 *
2072 * 32 bits should normally be sufficient (and would be nice, to save space in
2073 * each rule) but then we'd have to have some special cases for wraparound.
2074 *
2075 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
2076 * value. */
2077 static uint64_t monitor_seqno = 1;
2078
2079 COVERAGE_DEFINE(ofmonitor_pause);
2080 COVERAGE_DEFINE(ofmonitor_resume);
2081
2082 enum ofperr
2083 ofmonitor_create(const struct ofputil_flow_monitor_request *request,
2084 struct ofconn *ofconn, struct ofmonitor **monitorp)
2085 OVS_REQUIRES(ofproto_mutex)
2086 {
2087 struct ofmonitor *m;
2088
2089 *monitorp = NULL;
2090
2091 m = ofmonitor_lookup(ofconn, request->id);
2092 if (m) {
2093 return OFPERR_OFPMOFC_MONITOR_EXISTS;
2094 }
2095
2096 m = xmalloc(sizeof *m);
2097 m->ofconn = ofconn;
2098 hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0));
2099 m->id = request->id;
2100 m->flags = request->flags;
2101 m->out_port = request->out_port;
2102 m->table_id = request->table_id;
2103 minimatch_init(&m->match, &request->match);
2104
2105 *monitorp = m;
2106 return 0;
2107 }
2108
2109 struct ofmonitor *
2110 ofmonitor_lookup(struct ofconn *ofconn, uint32_t id)
2111 OVS_REQUIRES(ofproto_mutex)
2112 {
2113 struct ofmonitor *m;
2114
2115 HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0),
2116 &ofconn->monitors) {
2117 if (m->id == id) {
2118 return m;
2119 }
2120 }
2121 return NULL;
2122 }
2123
2124 void
2125 ofmonitor_destroy(struct ofmonitor *m)
2126 OVS_REQUIRES(ofproto_mutex)
2127 {
2128 if (m) {
2129 minimatch_destroy(&m->match);
2130 hmap_remove(&m->ofconn->monitors, &m->ofconn_node);
2131 free(m);
2132 }
2133 }
2134
2135 void
2136 ofmonitor_report(struct connmgr *mgr, struct rule *rule,
2137 enum nx_flow_update_event event,
2138 enum ofp_flow_removed_reason reason,
2139 const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid,
2140 const struct rule_actions *old_actions)
2141 OVS_REQUIRES(ofproto_mutex)
2142 {
2143 enum nx_flow_monitor_flags update;
2144 struct ofconn *ofconn;
2145
2146 if (rule_is_hidden(rule)) {
2147 return;
2148 }
2149
2150 switch (event) {
2151 case NXFME_ADDED:
2152 update = NXFMF_ADD;
2153 rule->add_seqno = rule->modify_seqno = monitor_seqno++;
2154 break;
2155
2156 case NXFME_DELETED:
2157 update = NXFMF_DELETE;
2158 break;
2159
2160 case NXFME_MODIFIED:
2161 update = NXFMF_MODIFY;
2162 rule->modify_seqno = monitor_seqno++;
2163 break;
2164
2165 default:
2166 case NXFME_ABBREV:
2167 OVS_NOT_REACHED();
2168 }
2169
2170 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2171 enum nx_flow_monitor_flags flags = 0;
2172 struct ofmonitor *m;
2173
2174 if (ofconn->monitor_paused) {
2175 /* Only send NXFME_DELETED notifications for flows that were added
2176 * before we paused. */
2177 if (event != NXFME_DELETED
2178 || rule->add_seqno > ofconn->monitor_paused) {
2179 continue;
2180 }
2181 }
2182
2183 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
2184 if (m->flags & update
2185 && (m->table_id == 0xff || m->table_id == rule->table_id)
2186 && (ofproto_rule_has_out_port(rule, m->out_port)
2187 || (old_actions
2188 && ofpacts_output_to_port(old_actions->ofpacts,
2189 old_actions->ofpacts_len,
2190 m->out_port)))
2191 && cls_rule_is_loose_match(&rule->cr, &m->match)) {
2192 flags |= m->flags;
2193 }
2194 }
2195
2196 if (flags) {
2197 if (ovs_list_is_empty(&ofconn->updates)) {
2198 ofputil_start_flow_update(&ofconn->updates);
2199 ofconn->sent_abbrev_update = false;
2200 }
2201
2202 if (flags & NXFMF_OWN || ofconn != abbrev_ofconn
2203 || ofconn->monitor_paused) {
2204 struct ofputil_flow_update fu;
2205
2206 fu.event = event;
2207 fu.reason = event == NXFME_DELETED ? reason : 0;
2208 fu.table_id = rule->table_id;
2209 fu.cookie = rule->flow_cookie;
2210 minimatch_expand(&rule->cr.match, &fu.match);
2211 fu.priority = rule->cr.priority;
2212
2213 ovs_mutex_lock(&rule->mutex);
2214 fu.idle_timeout = rule->idle_timeout;
2215 fu.hard_timeout = rule->hard_timeout;
2216 ovs_mutex_unlock(&rule->mutex);
2217
2218 if (flags & NXFMF_ACTIONS) {
2219 const struct rule_actions *actions = rule_get_actions(rule);
2220 fu.ofpacts = actions->ofpacts;
2221 fu.ofpacts_len = actions->ofpacts_len;
2222 } else {
2223 fu.ofpacts = NULL;
2224 fu.ofpacts_len = 0;
2225 }
2226 ofputil_append_flow_update(&fu, &ofconn->updates,
2227 ofproto_get_tun_tab(rule->ofproto));
2228 } else if (!ofconn->sent_abbrev_update) {
2229 struct ofputil_flow_update fu;
2230
2231 fu.event = NXFME_ABBREV;
2232 fu.xid = abbrev_xid;
2233 ofputil_append_flow_update(&fu, &ofconn->updates,
2234 ofproto_get_tun_tab(rule->ofproto));
2235
2236 ofconn->sent_abbrev_update = true;
2237 }
2238 }
2239 }
2240 }
2241
2242 void
2243 ofmonitor_flush(struct connmgr *mgr)
2244 OVS_REQUIRES(ofproto_mutex)
2245 {
2246 struct ofconn *ofconn;
2247
2248 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2249 struct rconn_packet_counter *counter = ofconn->monitor_counter;
2250
2251 struct ofpbuf *msg;
2252 LIST_FOR_EACH_POP (msg, list_node, &ofconn->updates) {
2253 ofconn_send(ofconn, msg, counter);
2254 }
2255
2256 if (!ofconn->monitor_paused
2257 && rconn_packet_counter_n_bytes(counter) > 128 * 1024) {
2258 struct ofpbuf *pause;
2259
2260 COVERAGE_INC(ofmonitor_pause);
2261 ofconn->monitor_paused = monitor_seqno++;
2262 pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED,
2263 OFP10_VERSION, htonl(0), 0);
2264 ofconn_send(ofconn, pause, counter);
2265 }
2266 }
2267 }
2268
2269 static void
2270 ofmonitor_resume(struct ofconn *ofconn)
2271 OVS_REQUIRES(ofproto_mutex)
2272 {
2273 struct rule_collection rules;
2274 struct ofpbuf *resumed;
2275 struct ofmonitor *m;
2276 struct ovs_list msgs;
2277
2278 rule_collection_init(&rules);
2279 HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) {
2280 ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules);
2281 }
2282
2283 ovs_list_init(&msgs);
2284 ofmonitor_compose_refresh_updates(&rules, &msgs);
2285
2286 resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION,
2287 htonl(0), 0);
2288 ovs_list_push_back(&msgs, &resumed->list_node);
2289 ofconn_send_replies(ofconn, &msgs);
2290
2291 ofconn->monitor_paused = 0;
2292 }
2293
2294 static bool
2295 ofmonitor_may_resume(const struct ofconn *ofconn)
2296 OVS_REQUIRES(ofproto_mutex)
2297 {
2298 return (ofconn->monitor_paused != 0
2299 && !rconn_packet_counter_n_packets(ofconn->monitor_counter));
2300 }
2301
2302 static void
2303 ofmonitor_run(struct connmgr *mgr)
2304 {
2305 struct ofconn *ofconn;
2306
2307 ovs_mutex_lock(&ofproto_mutex);
2308 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2309 if (ofmonitor_may_resume(ofconn)) {
2310 COVERAGE_INC(ofmonitor_resume);
2311 ofmonitor_resume(ofconn);
2312 }
2313 }
2314 ovs_mutex_unlock(&ofproto_mutex);
2315 }
2316
2317 static void
2318 ofmonitor_wait(struct connmgr *mgr)
2319 {
2320 struct ofconn *ofconn;
2321
2322 ovs_mutex_lock(&ofproto_mutex);
2323 LIST_FOR_EACH (ofconn, node, &mgr->all_conns) {
2324 if (ofmonitor_may_resume(ofconn)) {
2325 poll_immediate_wake();
2326 }
2327 }
2328 ovs_mutex_unlock(&ofproto_mutex);
2329 }
2330
2331 void
2332 ofproto_async_msg_free(struct ofproto_async_msg *am)
2333 {
2334 free(am->pin.up.base.packet);
2335 free(am->pin.up.base.userdata);
2336 free(am->pin.up.stack);
2337 free(am->pin.up.actions);
2338 free(am->pin.up.action_set);
2339 free(am);
2340 }