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