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