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