2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2019 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "fail-open.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"
37 #include "openvswitch/poll-loop.h"
38 #include "openvswitch/rconn.h"
39 #include "openvswitch/shash.h"
46 VLOG_DEFINE_THIS_MODULE(connmgr
);
47 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
49 /* An OpenFlow connection.
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. */
60 struct connmgr
*connmgr
; /* Connection's manager. */
61 struct ovs_list connmgr_node
; /* In connmgr->conns. */
63 struct ofservice
*ofservice
; /* Connection's service. */
64 struct ovs_list ofservice_node
; /* In service->conns. */
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
;
72 enum ofp12_controller_role role
; /* Role. */
73 enum ofputil_protocol protocol
; /* Current protocol variant. */
74 enum ofputil_packet_in_format packet_in_format
;
76 /* OFPT_PACKET_IN related data. */
77 struct rconn_packet_counter
*packet_in_counter
; /* # queued on 'rconn'. */
78 #define N_SCHEDULERS 2
79 struct pinsched
*schedulers
[N_SCHEDULERS
];
80 int miss_send_len
; /* Bytes to send of buffered packets. */
81 uint16_t controller_id
; /* Connection controller ID. */
83 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
84 * requests, and the maximum number before we stop reading OpenFlow
86 #define OFCONN_REPLY_MAX 100
87 struct rconn_packet_counter
*reply_counter
;
89 /* Asynchronous message configuration in each possible role.
91 * A 1-bit enables sending an asynchronous message for one possible reason
92 * that the message might be generated, a 0-bit disables it. */
93 struct ofputil_async_cfg
*async_cfg
;
95 /* Flow table operation logging. */
96 int n_add
, n_delete
, n_modify
; /* Number of unreported ops of each kind. */
97 long long int first_op
, last_op
; /* Range of times for unreported ops. */
98 long long int next_op_report
; /* Time to report ops, or LLONG_MAX. */
99 long long int op_backoff
; /* Earliest time to report ops again. */
101 /* Reassembly of multipart requests. */
102 struct hmap assembler
;
104 /* Flow monitors (e.g. NXST_FLOW_MONITOR). */
106 /* Configuration. Contains "struct ofmonitor"s. */
107 struct hmap monitors
OVS_GUARDED_BY(ofproto_mutex
);
111 * When too many flow monitor notifications back up in the transmit buffer,
112 * we pause the transmission of further notifications. These members track
113 * the flow control state.
115 * When notifications are flowing, 'monitor_paused' is 0. When
116 * notifications are paused, 'monitor_paused' is the value of
117 * 'monitor_seqno' at the point we paused.
119 * 'monitor_counter' counts the OpenFlow messages and bytes currently in
120 * flight. This value growing too large triggers pausing. */
121 uint64_t monitor_paused
OVS_GUARDED_BY(ofproto_mutex
);
122 struct rconn_packet_counter
*monitor_counter
OVS_GUARDED_BY(ofproto_mutex
);
124 /* State of monitors for a single ongoing flow_mod.
126 * 'updates' is a list of "struct ofpbuf"s that contain
127 * NXST_FLOW_MONITOR_REPLY messages representing the changes made by the
130 * When 'updates' is nonempty, 'sent_abbrev_update' is true if 'updates'
131 * contains an update event of type NXFME_ABBREV and false otherwise.. */
132 struct ovs_list updates
OVS_GUARDED_BY(ofproto_mutex
);
133 bool sent_abbrev_update
OVS_GUARDED_BY(ofproto_mutex
);
135 /* Active bundles. Contains "struct ofp_bundle"s. */
137 long long int next_bundle_expiry_check
;
140 /* vswitchd/ovs-vswitchd.8.in documents the value of BUNDLE_IDLE_LIFETIME in
141 * seconds. That documentation must be kept in sync with the value below. */
142 #define BUNDLE_EXPIRY_INTERVAL 1000 /* Check bundle expiry every 1 sec. */
143 #define BUNDLE_IDLE_TIMEOUT_DEFAULT 10000 /* Expire idle bundles after
146 static unsigned int bundle_idle_timeout
= BUNDLE_IDLE_TIMEOUT_DEFAULT
;
148 static void ofconn_create(struct ofservice
*, struct rconn
*,
149 const struct ofproto_controller
*settings
)
150 OVS_EXCLUDED(ofproto_mutex
);
151 static void ofconn_destroy(struct ofconn
*) OVS_REQUIRES(ofproto_mutex
);
153 static void ofconn_reconfigure(struct ofconn
*,
154 const struct ofproto_controller
*);
156 static void ofconn_run(struct ofconn
*,
157 void (*handle_openflow
)(struct ofconn
*,
158 const struct ovs_list
*msgs
));
159 static void ofconn_wait(struct ofconn
*);
161 static void ofconn_log_flow_mods(struct ofconn
*);
163 static char *ofconn_make_name(const struct connmgr
*, const char *target
);
165 static void ofconn_set_rate_limit(struct ofconn
*, int rate
, int burst
);
167 static void ofconn_send(const struct ofconn
*, struct ofpbuf
*,
168 struct rconn_packet_counter
*);
170 static void do_send_packet_ins(struct ofconn
*, struct ovs_list
*txq
);
172 /* A listener for incoming OpenFlow connections or for establishing an
173 * outgoing connection. */
175 struct hmap_node hmap_node
; /* In connmgr->services, by target. */
176 struct connmgr
*connmgr
;
178 char *target
; /* e.g. "tcp:..." or "pssl:...". */
179 struct ovs_list conns
; /* "ofconn"s generated by this service. */
180 enum ofconn_type type
; /* OFCONN_PRIMARY or OFCONN_SERVICE. */
182 /* Source of connections. */
183 struct rconn
*rconn
; /* Active connection only. */
184 struct pvconn
*pvconn
; /* Passive listener only. */
186 /* Settings for "struct ofconn"s established by this service. */
187 struct ofproto_controller s
;
190 static void ofservice_run(struct ofservice
*);
191 static void ofservice_wait(struct ofservice
*);
192 static void ofservice_reconfigure(struct ofservice
*,
193 const struct ofproto_controller
*)
194 OVS_REQUIRES(ofproto_mutex
);
195 static void ofservice_create(struct connmgr
*mgr
, const char *target
,
196 const struct ofproto_controller
*)
197 OVS_REQUIRES(ofproto_mutex
);
198 static void ofservice_destroy(struct ofservice
*) OVS_REQUIRES(ofproto_mutex
);
199 static struct ofservice
*ofservice_lookup(struct connmgr
*,
202 /* Connection manager for an OpenFlow switch. */
204 struct ofproto
*ofproto
;
206 char *local_port_name
;
208 /* OpenFlow connections.
210 * All modifications to 'conns' protected by ofproto_mutex, so that any
211 * traversals from other threads can be made safe by holding the
213 struct ovs_list conns
; /* All ofconns. */
214 uint64_t master_election_id
; /* monotonically increasing sequence number
215 * for master election */
216 bool master_election_id_defined
;
218 /* OpenFlow connection establishment. */
219 struct hmap services
; /* Contains "struct ofservice"s. */
220 struct pvconn
**snoops
;
224 struct fail_open
*fail_open
;
225 enum ofproto_fail_mode fail_mode
;
227 /* In-band control. */
228 struct in_band
*in_band
;
229 struct sockaddr_in
*extra_in_band_remotes
;
230 size_t n_extra_remotes
;
233 ATOMIC(int) want_packet_in_on_miss
; /* Sum of ofconns' values. */
236 static void update_in_band_remotes(struct connmgr
*);
237 static void add_snooper(struct connmgr
*, struct vconn
*);
238 static void ofmonitor_run(struct connmgr
*);
239 static void ofmonitor_wait(struct connmgr
*);
241 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
242 * a name for the ofproto suitable for using in log messages.
243 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
246 connmgr_create(struct ofproto
*ofproto
,
247 const char *name
, const char *local_port_name
)
249 struct connmgr
*mgr
= xmalloc(sizeof *mgr
);
250 mgr
->ofproto
= ofproto
;
251 mgr
->name
= xstrdup(name
);
252 mgr
->local_port_name
= xstrdup(local_port_name
);
254 ovs_list_init(&mgr
->conns
);
255 mgr
->master_election_id
= 0;
256 mgr
->master_election_id_defined
= false;
258 hmap_init(&mgr
->services
);
262 mgr
->fail_open
= NULL
;
263 mgr
->fail_mode
= OFPROTO_FAIL_SECURE
;
266 mgr
->extra_in_band_remotes
= NULL
;
267 mgr
->n_extra_remotes
= 0;
268 mgr
->in_band_queue
= -1;
270 atomic_init(&mgr
->want_packet_in_on_miss
, 0);
275 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
276 * packet rather than to send the packet to the controller.
278 * This function maintains the count of pre-OpenFlow1.3 with controller_id 0,
279 * as we assume these are the controllers that should receive "table-miss"
282 update_want_packet_in_on_miss(struct ofconn
*ofconn
)
284 /* We want a packet-in on miss when controller_id is zero and OpenFlow is
285 * lower than version 1.3. */
286 enum ofputil_protocol p
= ofconn
->protocol
;
287 int new_want
= (ofconn
->controller_id
== 0 &&
288 (p
== OFPUTIL_P_NONE
||
289 ofputil_protocol_to_ofp_version(p
) < OFP13_VERSION
));
291 /* Update the setting and the count if necessary. */
292 int old_want
= ofconn
->want_packet_in_on_miss
;
293 if (old_want
!= new_want
) {
294 atomic_int
*dst
= &ofconn
->connmgr
->want_packet_in_on_miss
;
296 atomic_read_relaxed(dst
, &count
);
297 atomic_store_relaxed(dst
, count
- old_want
+ new_want
);
299 ofconn
->want_packet_in_on_miss
= new_want
;
303 /* Frees 'mgr' and all of its resources. */
305 connmgr_destroy(struct connmgr
*mgr
)
306 OVS_REQUIRES(ofproto_mutex
)
312 struct ofservice
*ofservice
, *next_ofservice
;
313 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, hmap_node
, &mgr
->services
) {
314 ofservice_destroy(ofservice
);
316 hmap_destroy(&mgr
->services
);
317 ovs_assert(ovs_list_is_empty(&mgr
->conns
));
319 for (size_t i
= 0; i
< mgr
->n_snoops
; i
++) {
320 pvconn_close(mgr
->snoops
[i
]);
324 fail_open_destroy(mgr
->fail_open
);
325 mgr
->fail_open
= NULL
;
327 in_band_destroy(mgr
->in_band
);
329 free(mgr
->extra_in_band_remotes
);
331 free(mgr
->local_port_name
);
336 /* Does all of the periodic maintenance required by 'mgr'. Calls
337 * 'handle_openflow' for each message received on an OpenFlow connection,
338 * passing along the OpenFlow connection itself and the message that was sent.
339 * 'handle_openflow' must not modify or free the message. */
341 connmgr_run(struct connmgr
*mgr
,
342 void (*handle_openflow
)(struct ofconn
*,
343 const struct ovs_list
*msgs
))
344 OVS_EXCLUDED(ofproto_mutex
)
347 if (!in_band_run(mgr
->in_band
)) {
348 in_band_destroy(mgr
->in_band
);
353 struct ofconn
*ofconn
, *next_ofconn
;
354 LIST_FOR_EACH_SAFE (ofconn
, next_ofconn
, connmgr_node
, &mgr
->conns
) {
355 ofconn_run(ofconn
, handle_openflow
);
359 /* Fail-open maintenance. Do this after processing the ofconns since
360 * fail-open checks the status of the controller rconn. */
361 if (mgr
->fail_open
) {
362 fail_open_run(mgr
->fail_open
);
365 struct ofservice
*ofservice
;
366 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
367 ofservice_run(ofservice
);
370 for (size_t i
= 0; i
< mgr
->n_snoops
; i
++) {
372 int retval
= pvconn_accept(mgr
->snoops
[i
], &vconn
);
374 add_snooper(mgr
, vconn
);
375 } else if (retval
!= EAGAIN
) {
376 VLOG_WARN_RL(&rl
, "accept failed (%s)", ovs_strerror(retval
));
381 /* Causes the poll loop to wake up when connmgr_run() needs to run. */
383 connmgr_wait(struct connmgr
*mgr
)
385 struct ofconn
*ofconn
;
386 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
393 in_band_wait(mgr
->in_band
);
396 if (mgr
->fail_open
) {
397 fail_open_wait(mgr
->fail_open
);
400 struct ofservice
*ofservice
;
401 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
402 ofservice_wait(ofservice
);
405 for (size_t i
= 0; i
< mgr
->n_snoops
; i
++) {
406 pvconn_wait(mgr
->snoops
[i
]);
410 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
411 * memory_report(). */
413 connmgr_get_memory_usage(const struct connmgr
*mgr
, struct simap
*usage
)
415 unsigned int packets
= 0;
416 unsigned int ofconns
= 0;
418 struct ofconn
*ofconn
;
419 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
422 packets
+= rconn_count_txqlen(ofconn
->rconn
);
423 for (int i
= 0; i
< N_SCHEDULERS
; i
++) {
424 struct pinsched_stats stats
;
426 pinsched_get_stats(ofconn
->schedulers
[i
], &stats
);
427 packets
+= stats
.n_queued
;
430 simap_increase(usage
, "ofconns", ofconns
);
431 simap_increase(usage
, "packets", packets
);
434 /* Returns the ofproto that owns 'ofconn''s connmgr. */
436 ofconn_get_ofproto(const struct ofconn
*ofconn
)
438 return ofconn
->connmgr
->ofproto
;
441 /* Sets the bundle idle timeout to 'timeout' seconds, interpreting 0 as
442 * requesting the default timeout.
444 * The OpenFlow spec mandates the timeout to be at least one second; . */
446 connmgr_set_bundle_idle_timeout(unsigned timeout
)
448 bundle_idle_timeout
= (timeout
449 ? sat_mul(timeout
, 1000)
450 : BUNDLE_IDLE_TIMEOUT_DEFAULT
);
453 /* OpenFlow configuration. */
455 static void update_fail_open(struct connmgr
*) OVS_EXCLUDED(ofproto_mutex
);
456 static int set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
457 const struct sset
*);
459 /* Returns true if 'mgr' has any configured primary controllers.
461 * Service controllers do not count, but configured primary controllers do
462 * count whether or not they are currently connected. */
464 connmgr_has_controllers(const struct connmgr
*mgr
)
466 struct ofservice
*ofservice
;
467 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
468 if (ofservice
->type
== OFCONN_PRIMARY
) {
475 static struct ofconn
*
476 ofservice_first_conn(const struct ofservice
*ofservice
)
478 return (ovs_list_is_empty(&ofservice
->conns
)
480 : CONTAINER_OF(ofservice
->conns
.next
,
481 struct ofconn
, ofservice_node
));
484 /* Initializes 'info' and populates it with information about each configured
485 * primary controller. The keys in 'info' are the controllers' targets; the
486 * data values are corresponding "struct ofproto_controller_info".
488 * The caller owns 'info' and everything in it and should free it when it is no
491 connmgr_get_controller_info(struct connmgr
*mgr
, struct shash
*info
)
493 struct ofservice
*ofservice
;
494 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
495 const struct rconn
*rconn
= ofservice
->rconn
;
499 const char *target
= rconn_get_target(rconn
);
501 if (!shash_find(info
, target
)) {
502 struct ofconn
*ofconn
= ofservice_first_conn(ofservice
);
503 struct ofproto_controller_info
*cinfo
= xmalloc(sizeof *cinfo
);
504 long long int now
= time_msec();
505 long long int last_connection
= rconn_get_last_connection(rconn
);
506 long long int last_disconnect
= rconn_get_last_disconnect(rconn
);
507 int last_error
= rconn_get_last_error(rconn
);
510 shash_add(info
, target
, cinfo
);
512 cinfo
->is_connected
= rconn_is_connected(rconn
);
513 cinfo
->role
= ofconn
? ofconn
->role
: OFPCR12_ROLE_NOCHANGE
;
515 smap_init(&cinfo
->pairs
);
517 smap_add(&cinfo
->pairs
, "last_error",
518 ovs_retval_to_string(last_error
));
521 smap_add(&cinfo
->pairs
, "state", rconn_get_state(rconn
));
523 if (last_connection
!= LLONG_MIN
) {
524 smap_add_format(&cinfo
->pairs
, "sec_since_connect",
525 "%lld", (now
- last_connection
) / 1000);
528 if (last_disconnect
!= LLONG_MIN
) {
529 smap_add_format(&cinfo
->pairs
, "sec_since_disconnect",
530 "%lld", (now
- last_disconnect
) / 1000);
533 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
534 if (ofconn
&& ofconn
->schedulers
[i
]) {
535 const char *name
= i
? "miss" : "action";
536 struct pinsched_stats stats
;
538 pinsched_get_stats(ofconn
->schedulers
[i
], &stats
);
539 smap_add_nocopy(&cinfo
->pairs
,
540 xasprintf("packet-in-%s-backlog", name
),
541 xasprintf("%u", stats
.n_queued
));
542 smap_add_nocopy(&cinfo
->pairs
,
543 xasprintf("packet-in-%s-bypassed", name
),
544 xasprintf("%llu", stats
.n_normal
));
545 smap_add_nocopy(&cinfo
->pairs
,
546 xasprintf("packet-in-%s-queued", name
),
547 xasprintf("%llu", stats
.n_limited
));
548 smap_add_nocopy(&cinfo
->pairs
,
549 xasprintf("packet-in-%s-dropped", name
),
550 xasprintf("%llu", stats
.n_queue_dropped
));
558 connmgr_free_controller_info(struct shash
*info
)
560 struct shash_node
*node
;
562 SHASH_FOR_EACH (node
, info
) {
563 struct ofproto_controller_info
*cinfo
= node
->data
;
564 smap_destroy(&cinfo
->pairs
);
570 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
573 connmgr_set_controllers(struct connmgr
*mgr
, struct shash
*controllers
)
574 OVS_EXCLUDED(ofproto_mutex
)
576 bool had_controllers
= connmgr_has_controllers(mgr
);
578 /* Required to add and remove ofconns. This could probably be narrowed to
579 * cover a smaller amount of code, if that yielded some benefit. */
580 ovs_mutex_lock(&ofproto_mutex
);
582 /* Create newly configured services. */
583 struct shash_node
*node
;
584 SHASH_FOR_EACH (node
, controllers
) {
585 const char *target
= node
->name
;
586 const struct ofproto_controller
*c
= node
->data
;
587 if (!ofservice_lookup(mgr
, target
)) {
588 ofservice_create(mgr
, target
, c
);
592 /* Delete services that are no longer configured.
593 * Update configuration of all now-existing services. */
594 struct ofservice
*ofservice
, *next_ofservice
;
595 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, hmap_node
, &mgr
->services
) {
596 const char *target
= ofservice
->target
;
597 struct ofproto_controller
*c
= shash_find_data(controllers
, target
);
599 VLOG_INFO("%s: removed %s controller \"%s\"",
600 mgr
->name
, ofconn_type_to_string(ofservice
->type
),
602 ofservice_destroy(ofservice
);
604 ofservice_reconfigure(ofservice
, c
);
608 ovs_mutex_unlock(&ofproto_mutex
);
610 update_in_band_remotes(mgr
);
611 update_fail_open(mgr
);
612 if (had_controllers
!= connmgr_has_controllers(mgr
)) {
613 ofproto_flush_flows(mgr
->ofproto
);
617 /* Drops the connections between 'mgr' and all of its primary and secondary
618 * controllers, forcing them to reconnect. */
620 connmgr_reconnect(const struct connmgr
*mgr
)
622 struct ofconn
*ofconn
;
624 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
625 rconn_reconnect(ofconn
->rconn
);
629 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
631 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
632 * important controller on 'mgr' is mirrored. */
634 connmgr_set_snoops(struct connmgr
*mgr
, const struct sset
*snoops
)
636 return set_pvconns(&mgr
->snoops
, &mgr
->n_snoops
, snoops
);
639 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
641 connmgr_get_snoops(const struct connmgr
*mgr
, struct sset
*snoops
)
643 for (size_t i
= 0; i
< mgr
->n_snoops
; i
++) {
644 sset_add(snoops
, pvconn_get_name(mgr
->snoops
[i
]));
648 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
650 connmgr_has_snoops(const struct connmgr
*mgr
)
652 return mgr
->n_snoops
> 0;
656 update_in_band_remotes(struct connmgr
*mgr
)
658 /* Allocate enough memory for as many remotes as we could possibly have. */
659 size_t max_addrs
= mgr
->n_extra_remotes
+ hmap_count(&mgr
->services
);
660 struct sockaddr_in
*addrs
= xmalloc(max_addrs
* sizeof *addrs
);
663 /* Add all the remotes. */
664 struct ofservice
*ofservice
;
665 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
666 const char *target
= ofservice
->target
;
668 struct sockaddr_storage ss
;
669 struct sockaddr_in in
;
672 if (ofservice
->s
.band
== OFPROTO_IN_BAND
673 && stream_parse_target_with_default_port(target
, OFP_PORT
, &sa
.ss
)
674 && sa
.ss
.ss_family
== AF_INET
) {
675 addrs
[n_addrs
++] = sa
.in
;
678 for (size_t i
= 0; i
< mgr
->n_extra_remotes
; i
++) {
679 addrs
[n_addrs
++] = mgr
->extra_in_band_remotes
[i
];
682 /* Create or update or destroy in-band. */
685 in_band_create(mgr
->ofproto
, mgr
->local_port_name
, &mgr
->in_band
);
688 /* in_band_run() needs a chance to delete any existing in-band flows.
689 * We will destroy mgr->in_band after it's done with that. */
692 in_band_set_queue(mgr
->in_band
, mgr
->in_band_queue
);
693 in_band_set_remotes(mgr
->in_band
, addrs
, n_addrs
);
701 update_fail_open(struct connmgr
*mgr
)
702 OVS_EXCLUDED(ofproto_mutex
)
704 if (connmgr_has_controllers(mgr
)
705 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
706 if (!mgr
->fail_open
) {
707 mgr
->fail_open
= fail_open_create(mgr
->ofproto
, mgr
);
710 ovs_mutex_lock(&ofproto_mutex
);
711 fail_open_destroy(mgr
->fail_open
);
712 ovs_mutex_unlock(&ofproto_mutex
);
713 mgr
->fail_open
= NULL
;
718 set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
719 const struct sset
*sset
)
721 /* Free the old pvconns. */
722 struct pvconn
**old_pvconns
= *pvconnsp
;
723 size_t old_n_pvconns
= *n_pvconnsp
;
724 for (size_t i
= 0; i
< old_n_pvconns
; i
++) {
725 pvconn_close(old_pvconns
[i
]);
729 /* Populate the new pvconns. */
730 struct pvconn
**new_pvconns
= xmalloc(sset_count(sset
)
731 * sizeof *new_pvconns
);
732 size_t new_n_pvconns
= 0;
736 SSET_FOR_EACH (name
, sset
) {
737 struct pvconn
*pvconn
;
738 int error
= pvconn_open(name
, 0, 0, &pvconn
);
740 new_pvconns
[new_n_pvconns
++] = pvconn
;
742 VLOG_ERR("failed to listen on %s: %s", name
, ovs_strerror(error
));
749 *pvconnsp
= new_pvconns
;
750 *n_pvconnsp
= new_n_pvconns
;
755 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
756 * means that 'ofconn' is more interesting for monitoring than a lower return
759 snoop_preference(const struct ofservice
*ofservice
)
761 struct ofconn
*ofconn
= ofservice_first_conn(ofservice
);
766 switch (ofconn
->role
) {
767 case OFPCR12_ROLE_MASTER
:
769 case OFPCR12_ROLE_EQUAL
:
771 case OFPCR12_ROLE_SLAVE
:
773 case OFPCR12_ROLE_NOCHANGE
:
775 /* Shouldn't happen. */
780 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
781 * Connects this vconn to a controller. */
783 add_snooper(struct connmgr
*mgr
, struct vconn
*vconn
)
785 /* Pick a controller for monitoring. */
786 struct ofservice
*best
= NULL
;
787 struct ofservice
*ofservice
;
788 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
789 if (ofservice
->rconn
&&
790 (!best
|| snoop_preference(ofservice
) > snoop_preference(best
))) {
796 rconn_add_monitor(best
->rconn
, vconn
);
798 VLOG_INFO_RL(&rl
, "no controller connection to snoop");
803 /* Public ofconn functions. */
805 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
807 ofconn_get_type(const struct ofconn
*ofconn
)
812 /* If a master election id is defined, stores it into '*idp' and returns
813 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
815 ofconn_get_master_election_id(const struct ofconn
*ofconn
, uint64_t *idp
)
817 *idp
= (ofconn
->connmgr
->master_election_id_defined
818 ? ofconn
->connmgr
->master_election_id
820 return ofconn
->connmgr
->master_election_id_defined
;
823 /* Sets the master election id.
825 * Returns true if successful, false if the id is stale
828 ofconn_set_master_election_id(struct ofconn
*ofconn
, uint64_t id
)
830 if (ofconn
->connmgr
->master_election_id_defined
832 /* Unsigned difference interpreted as a two's complement signed
834 (int64_t)(id
- ofconn
->connmgr
->master_election_id
) < 0) {
837 ofconn
->connmgr
->master_election_id
= id
;
838 ofconn
->connmgr
->master_election_id_defined
= true;
843 /* Returns the role configured for 'ofconn'.
845 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
846 enum ofp12_controller_role
847 ofconn_get_role(const struct ofconn
*ofconn
)
853 ofconn_send_role_status(struct ofconn
*ofconn
, uint32_t role
, uint8_t reason
)
855 struct ofputil_role_status status
;
856 status
.reason
= reason
;
858 ofconn_get_master_election_id(ofconn
, &status
.generation_id
);
861 = ofputil_encode_role_status(&status
, ofconn_get_protocol(ofconn
));
863 ofconn_send(ofconn
, buf
, NULL
);
867 /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
868 * any existing master is demoted to a slave. */
870 ofconn_set_role(struct ofconn
*ofconn
, enum ofp12_controller_role role
)
872 if (role
!= ofconn
->role
&& role
== OFPCR12_ROLE_MASTER
) {
873 struct ofconn
*other
;
875 LIST_FOR_EACH (other
, connmgr_node
, &ofconn
->connmgr
->conns
) {
876 if (other
->role
== OFPCR12_ROLE_MASTER
) {
877 other
->role
= OFPCR12_ROLE_SLAVE
;
878 ofconn_send_role_status(other
, OFPCR12_ROLE_SLAVE
,
879 OFPCRR_MASTER_REQUEST
);
887 ofconn_set_invalid_ttl_to_controller(struct ofconn
*ofconn
, bool enable
)
889 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
890 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
892 ac
.master
[OAM_PACKET_IN
] |= bit
;
894 ac
.master
[OAM_PACKET_IN
] &= ~bit
;
896 ofconn_set_async_config(ofconn
, &ac
);
900 ofconn_get_invalid_ttl_to_controller(struct ofconn
*ofconn
)
902 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
903 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
904 return (ac
.master
[OAM_PACKET_IN
] & bit
) != 0;
907 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
909 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
910 * completed version negotiation. This can't happen if at least one OpenFlow
911 * message, other than OFPT_HELLO, has been received on the connection (such as
912 * in ofproto.c's message handling code), since version negotiation is a
913 * prerequisite for starting to receive messages. This means that
914 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
915 enum ofputil_protocol
916 ofconn_get_protocol(const struct ofconn
*ofconn
)
918 if (ofconn
->protocol
== OFPUTIL_P_NONE
&&
919 rconn_is_connected(ofconn
->rconn
)) {
920 int version
= rconn_get_version(ofconn
->rconn
);
922 ofconn_set_protocol(CONST_CAST(struct ofconn
*, ofconn
),
923 ofputil_protocol_from_ofp_version(version
));
927 return ofconn
->protocol
;
930 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
932 * (This doesn't actually send anything to accomplish this. Presumably the
933 * caller already did that.) */
935 ofconn_set_protocol(struct ofconn
*ofconn
, enum ofputil_protocol protocol
)
937 ofconn
->protocol
= protocol
;
938 update_want_packet_in_on_miss(ofconn
);
941 /* Returns the currently configured packet in format for 'ofconn', one of
944 * The default, if no other format has been set, is NXPIF_STANDARD. */
945 enum ofputil_packet_in_format
946 ofconn_get_packet_in_format(struct ofconn
*ofconn
)
948 return ofconn
->packet_in_format
;
951 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
954 ofconn_set_packet_in_format(struct ofconn
*ofconn
,
955 enum ofputil_packet_in_format packet_in_format
)
957 ofconn
->packet_in_format
= packet_in_format
;
960 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
962 * The connection controller ID is used for OFPP_CONTROLLER and
963 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
965 ofconn_set_controller_id(struct ofconn
*ofconn
, uint16_t controller_id
)
967 ofconn
->controller_id
= controller_id
;
968 update_want_packet_in_on_miss(ofconn
);
971 /* Returns the default miss send length for 'ofconn'. */
973 ofconn_get_miss_send_len(const struct ofconn
*ofconn
)
975 return ofconn
->miss_send_len
;
978 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
980 ofconn_set_miss_send_len(struct ofconn
*ofconn
, int miss_send_len
)
982 ofconn
->miss_send_len
= miss_send_len
;
986 ofconn_set_async_config(struct ofconn
*ofconn
,
987 const struct ofputil_async_cfg
*ac
)
989 if (!ofconn
->async_cfg
) {
990 ofconn
->async_cfg
= xmalloc(sizeof *ofconn
->async_cfg
);
992 *ofconn
->async_cfg
= *ac
;
994 if (ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn
))
996 if (ofconn
->async_cfg
->master
[OAM_PACKET_IN
] & (1u << OFPR_ACTION
)) {
997 ofconn
->async_cfg
->master
[OAM_PACKET_IN
] |= OFPR14_ACTION_BITS
;
999 if (ofconn
->async_cfg
->slave
[OAM_PACKET_IN
] & (1u << OFPR_ACTION
)) {
1000 ofconn
->async_cfg
->slave
[OAM_PACKET_IN
] |= OFPR14_ACTION_BITS
;
1005 struct ofputil_async_cfg
1006 ofconn_get_async_config(const struct ofconn
*ofconn
)
1008 if (ofconn
->async_cfg
) {
1009 return *ofconn
->async_cfg
;
1012 int version
= rconn_get_version(ofconn
->rconn
);
1013 return (version
< 0 || !ofconn
->ofservice
->s
.enable_async_msgs
1014 ? OFPUTIL_ASYNC_CFG_INIT
1015 : ofputil_async_cfg_default(version
));
1018 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1019 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1020 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1021 * controller has accepted some of the replies.) */
1023 ofconn_send_reply(const struct ofconn
*ofconn
, struct ofpbuf
*msg
)
1025 ofconn_send(ofconn
, msg
, ofconn
->reply_counter
);
1028 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
1029 * accounting them as replies. */
1031 ofconn_send_replies(const struct ofconn
*ofconn
, struct ovs_list
*replies
)
1033 struct ofpbuf
*reply
;
1035 LIST_FOR_EACH_POP (reply
, list_node
, replies
) {
1036 ofconn_send_reply(ofconn
, reply
);
1040 /* Sends 'error' on 'ofconn', as a reply to 'request'. */
1042 ofconn_send_error(const struct ofconn
*ofconn
,
1043 const struct ofp_header
*request
, enum ofperr error
)
1045 static struct vlog_rate_limit err_rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1046 struct ofpbuf
*reply
= ofperr_encode_reply(error
, request
);
1047 if (!VLOG_DROP_INFO(&err_rl
)) {
1048 size_t request_len
= ntohs(request
->length
);
1051 const char *type_name
= (!ofpraw_decode_partial(&raw
, request
,
1052 MIN(64, request_len
))
1053 ? ofpraw_get_name(raw
)
1056 VLOG_INFO("%s: sending %s error reply to %s message",
1057 rconn_get_name(ofconn
->rconn
), ofperr_to_string(error
),
1060 ofconn_send_reply(ofconn
, reply
);
1063 /* Reports that a flow_mod operation of the type specified by 'command' was
1064 * successfully executed by 'ofconn', so that the connmgr can log it. */
1066 ofconn_report_flow_mod(struct ofconn
*ofconn
,
1067 enum ofp_flow_mod_command command
)
1075 case OFPFC_MODIFY_STRICT
:
1080 case OFPFC_DELETE_STRICT
:
1085 long long int now
= time_msec();
1086 if (ofconn
->next_op_report
== LLONG_MAX
) {
1087 ofconn
->first_op
= now
;
1088 ofconn
->next_op_report
= MAX(now
+ 10 * 1000, ofconn
->op_backoff
);
1089 ofconn
->op_backoff
= ofconn
->next_op_report
+ 60 * 1000;
1091 ofconn
->last_op
= now
;
1094 /* OpenFlow 1.4 bundles. */
1096 static inline uint32_t
1097 bundle_hash(uint32_t id
)
1099 return hash_int(id
, 0);
1103 ofconn_get_bundle(struct ofconn
*ofconn
, uint32_t id
)
1105 struct ofp_bundle
*bundle
;
1107 HMAP_FOR_EACH_IN_BUCKET(bundle
, node
, bundle_hash(id
), &ofconn
->bundles
) {
1108 if (bundle
->id
== id
) {
1117 ofconn_insert_bundle(struct ofconn
*ofconn
, struct ofp_bundle
*bundle
)
1119 hmap_insert(&ofconn
->bundles
, &bundle
->node
, bundle_hash(bundle
->id
));
1123 ofconn_remove_bundle(struct ofconn
*ofconn
, struct ofp_bundle
*bundle
)
1125 hmap_remove(&ofconn
->bundles
, &bundle
->node
);
1129 bundle_remove_all(struct ofconn
*ofconn
)
1131 struct ofp_bundle
*b
, *next
;
1133 HMAP_FOR_EACH_SAFE (b
, next
, node
, &ofconn
->bundles
) {
1134 ofp_bundle_remove__(ofconn
, b
);
1139 bundle_remove_expired(struct ofconn
*ofconn
, long long int now
)
1141 long long int limit
= now
- bundle_idle_timeout
;
1143 struct ofp_bundle
*b
, *next
;
1144 HMAP_FOR_EACH_SAFE (b
, next
, node
, &ofconn
->bundles
) {
1145 if (b
->used
<= limit
) {
1146 ofconn_send_error(ofconn
, b
->msg
, OFPERR_OFPBFC_TIMEOUT
);
1147 ofp_bundle_remove__(ofconn
, b
);
1152 /* Private ofconn functions. */
1155 ofconn_create(struct ofservice
*ofservice
, struct rconn
*rconn
,
1156 const struct ofproto_controller
*settings
)
1157 OVS_EXCLUDED(ofproto_mutex
)
1159 ovs_mutex_lock(&ofproto_mutex
);
1161 struct ofconn
*ofconn
= xzalloc(sizeof *ofconn
);
1163 ofconn
->connmgr
= ofservice
->connmgr
;
1164 ovs_list_push_back(&ofservice
->connmgr
->conns
, &ofconn
->connmgr_node
);
1166 hmap_init(&ofconn
->assembler
);
1168 ofconn
->ofservice
= ofservice
;
1169 ovs_list_push_back(&ofservice
->conns
, &ofconn
->ofservice_node
);
1171 ofconn
->rconn
= rconn
;
1172 ofconn
->type
= settings
->type
;
1173 ofconn
->band
= settings
->band
;
1175 ofconn
->role
= OFPCR12_ROLE_EQUAL
;
1176 ofconn_set_protocol(ofconn
, OFPUTIL_P_NONE
);
1177 ofconn
->packet_in_format
= OFPUTIL_PACKET_IN_STD
;
1179 ofconn
->packet_in_counter
= rconn_packet_counter_create();
1180 ofconn
->miss_send_len
= (ofconn
->type
== OFCONN_PRIMARY
1181 ? OFP_DEFAULT_MISS_SEND_LEN
1183 ofconn
->controller_id
= 0;
1185 ofconn
->reply_counter
= rconn_packet_counter_create();
1187 ofconn
->async_cfg
= NULL
;
1189 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1190 ofconn
->first_op
= ofconn
->last_op
= LLONG_MIN
;
1191 ofconn
->next_op_report
= LLONG_MAX
;
1192 ofconn
->op_backoff
= LLONG_MIN
;
1194 hmap_init(&ofconn
->monitors
);
1195 ofconn
->monitor_counter
= rconn_packet_counter_create();
1197 ovs_list_init(&ofconn
->updates
);
1199 hmap_init(&ofconn
->bundles
);
1200 ofconn
->next_bundle_expiry_check
= time_msec() + BUNDLE_EXPIRY_INTERVAL
;
1202 ofconn_set_rate_limit(ofconn
, settings
->rate_limit
, settings
->burst_limit
);
1204 ovs_mutex_unlock(&ofproto_mutex
);
1208 ofconn_destroy(struct ofconn
*ofconn
)
1209 OVS_REQUIRES(ofproto_mutex
)
1215 ofconn_log_flow_mods(ofconn
);
1217 ovs_list_remove(&ofconn
->connmgr_node
);
1218 ovs_list_remove(&ofconn
->ofservice_node
);
1220 if (ofconn
->rconn
!= ofconn
->ofservice
->rconn
) {
1221 rconn_destroy(ofconn
->rconn
);
1224 /* Force clearing of want_packet_in_on_miss to keep the global count
1226 ofconn
->controller_id
= 1;
1227 update_want_packet_in_on_miss(ofconn
);
1229 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1230 for (int i
= 0; i
< N_SCHEDULERS
; i
++) {
1231 if (ofconn
->schedulers
[i
]) {
1232 pinsched_destroy(ofconn
->schedulers
[i
]);
1236 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1238 free(ofconn
->async_cfg
);
1240 struct ofmonitor
*monitor
, *next_monitor
;
1241 HMAP_FOR_EACH_SAFE (monitor
, next_monitor
, ofconn_node
,
1242 &ofconn
->monitors
) {
1243 ofmonitor_destroy(monitor
);
1245 hmap_destroy(&ofconn
->monitors
);
1246 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1248 ofpbuf_list_delete(&ofconn
->updates
); /* ...but it should be empty. */
1250 bundle_remove_all(ofconn
);
1251 hmap_destroy(&ofconn
->bundles
);
1256 /* Reconfigures 'ofconn' to match 'c'. */
1258 ofconn_reconfigure(struct ofconn
*ofconn
, const struct ofproto_controller
*c
)
1260 rconn_set_max_backoff(ofconn
->rconn
, c
->max_backoff
);
1262 int probe_interval
= c
->probe_interval
? MAX(c
->probe_interval
, 5) : 0;
1263 rconn_set_probe_interval(ofconn
->rconn
, probe_interval
);
1265 ofconn
->band
= c
->band
;
1267 ofconn_set_rate_limit(ofconn
, c
->rate_limit
, c
->burst_limit
);
1269 if (c
->dscp
!= rconn_get_dscp(ofconn
->rconn
)) {
1270 rconn_set_dscp(ofconn
->rconn
, c
->dscp
);
1271 rconn_reconnect(ofconn
->rconn
);
1275 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1278 ofconn_may_recv(const struct ofconn
*ofconn
)
1280 int count
= rconn_packet_counter_n_packets(ofconn
->reply_counter
);
1281 return count
< OFCONN_REPLY_MAX
;
1285 ofconn_run(struct ofconn
*ofconn
,
1286 void (*handle_openflow
)(struct ofconn
*,
1287 const struct ovs_list
*msgs
))
1289 struct connmgr
*mgr
= ofconn
->connmgr
;
1291 for (size_t i
= 0; i
< N_SCHEDULERS
; i
++) {
1292 struct ovs_list txq
;
1294 pinsched_run(ofconn
->schedulers
[i
], &txq
);
1295 do_send_packet_ins(ofconn
, &txq
);
1298 rconn_run(ofconn
->rconn
);
1300 /* Limit the number of iterations to avoid starving other tasks. */
1301 for (int i
= 0; i
< 50 && ofconn_may_recv(ofconn
); i
++) {
1302 struct ofpbuf
*of_msg
= rconn_recv(ofconn
->rconn
);
1307 if (mgr
->fail_open
) {
1308 fail_open_maybe_recover(mgr
->fail_open
);
1311 struct ovs_list msgs
;
1312 enum ofperr error
= ofpmp_assembler_execute(&ofconn
->assembler
, of_msg
,
1313 &msgs
, time_msec());
1315 ofconn_send_error(ofconn
, of_msg
->data
, error
);
1316 ofpbuf_delete(of_msg
);
1317 } else if (!ovs_list_is_empty(&msgs
)) {
1318 handle_openflow(ofconn
, &msgs
);
1319 ofpbuf_list_delete(&msgs
);
1323 long long int now
= time_msec();
1325 if (now
>= ofconn
->next_bundle_expiry_check
) {
1326 ofconn
->next_bundle_expiry_check
= now
+ BUNDLE_EXPIRY_INTERVAL
;
1327 bundle_remove_expired(ofconn
, now
);
1330 if (now
>= ofconn
->next_op_report
) {
1331 ofconn_log_flow_mods(ofconn
);
1334 struct ofpbuf
*error
= ofpmp_assembler_run(&ofconn
->assembler
,
1337 ofconn_send(ofconn
, error
, NULL
);
1340 ovs_mutex_lock(&ofproto_mutex
);
1341 if (rconn_is_reliable(ofconn
->rconn
)
1342 ? !rconn_is_connected(ofconn
->rconn
)
1343 : !rconn_is_alive(ofconn
->rconn
)) {
1344 ofconn_destroy(ofconn
);
1346 ovs_mutex_unlock(&ofproto_mutex
);
1350 ofconn_wait(struct ofconn
*ofconn
)
1352 for (int i
= 0; i
< N_SCHEDULERS
; i
++) {
1353 pinsched_wait(ofconn
->schedulers
[i
]);
1355 rconn_run_wait(ofconn
->rconn
);
1356 if (ofconn_may_recv(ofconn
)) {
1357 rconn_recv_wait(ofconn
->rconn
);
1359 if (ofconn
->next_op_report
!= LLONG_MAX
) {
1360 poll_timer_wait_until(ofconn
->next_op_report
);
1362 poll_timer_wait_until(ofpmp_assembler_wait(&ofconn
->assembler
));
1366 ofconn_log_flow_mods(struct ofconn
*ofconn
)
1368 int n_flow_mods
= ofconn
->n_add
+ ofconn
->n_delete
+ ofconn
->n_modify
;
1370 long long int ago
= (time_msec() - ofconn
->first_op
) / 1000;
1371 long long int interval
= (ofconn
->last_op
- ofconn
->first_op
) / 1000;
1375 ds_put_format(&s
, "%d flow_mods ", n_flow_mods
);
1376 if (interval
== ago
) {
1377 ds_put_format(&s
, "in the last %lld s", ago
);
1378 } else if (interval
) {
1379 ds_put_format(&s
, "in the %lld s starting %lld s ago",
1382 ds_put_format(&s
, "%lld s ago", ago
);
1385 ds_put_cstr(&s
, " (");
1386 if (ofconn
->n_add
) {
1387 ds_put_format(&s
, "%d adds, ", ofconn
->n_add
);
1389 if (ofconn
->n_delete
) {
1390 ds_put_format(&s
, "%d deletes, ", ofconn
->n_delete
);
1392 if (ofconn
->n_modify
) {
1393 ds_put_format(&s
, "%d modifications, ", ofconn
->n_modify
);
1396 ds_put_char(&s
, ')');
1398 VLOG_INFO("%s: %s", rconn_get_name(ofconn
->rconn
), ds_cstr(&s
));
1401 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1403 ofconn
->next_op_report
= LLONG_MAX
;
1406 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1407 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1408 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1409 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1412 ofconn_receives_async_msg(const struct ofconn
*ofconn
,
1413 enum ofputil_async_msg_type type
,
1414 unsigned int reason
)
1416 ovs_assert(reason
< 32);
1417 ovs_assert((unsigned int) type
< OAM_N_TYPES
);
1419 if (!rconn_is_connected(ofconn
->rconn
) || !ofconn_get_protocol(ofconn
)) {
1423 /* Keep the following code in sync with the documentation in the
1424 * "Asynchronous Messages" section in 'topics/design' */
1426 if (ofconn
->type
== OFCONN_SERVICE
&& !ofconn
->miss_send_len
) {
1427 /* Service connections don't get asynchronous messages unless they have
1428 * explicitly asked for them by setting a nonzero miss send length. */
1432 struct ofputil_async_cfg ac
= ofconn_get_async_config(ofconn
);
1433 uint32_t *masks
= (ofconn
->role
== OFPCR12_ROLE_SLAVE
1436 return (masks
[type
] & (1u << reason
)) != 0;
1439 /* This function returns true to indicate that a packet_in message
1440 * for a "table-miss" should be sent to at least one controller.
1442 * False otherwise. */
1444 connmgr_wants_packet_in_on_miss(struct connmgr
*mgr
)
1448 atomic_read_relaxed(&mgr
->want_packet_in_on_miss
, &count
);
1452 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1453 * 'target', suitable for use in log messages for identifying the connection.
1455 * The name is dynamically allocated. The caller should free it (with free())
1456 * when it is no longer needed. */
1458 ofconn_make_name(const struct connmgr
*mgr
, const char *target
)
1460 return xasprintf("%s<->%s", mgr
->name
, target
);
1464 ofconn_set_rate_limit(struct ofconn
*ofconn
, int rate
, int burst
)
1466 for (int i
= 0; i
< N_SCHEDULERS
; i
++) {
1467 struct pinsched
**s
= &ofconn
->schedulers
[i
];
1471 *s
= pinsched_create(rate
, burst
);
1473 pinsched_set_limits(*s
, rate
, burst
);
1476 pinsched_destroy(*s
);
1483 ofconn_send(const struct ofconn
*ofconn
, struct ofpbuf
*msg
,
1484 struct rconn_packet_counter
*counter
)
1486 ofpmsg_update_length(msg
);
1487 rconn_send(ofconn
->rconn
, msg
, counter
);
1490 /* Sending asynchronous messages. */
1492 /* Sends an OFPT_PORT_STATUS message with 'new_pp' and 'reason' to appropriate
1493 * controllers managed by 'mgr'. For messages caused by a controller
1494 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1495 * request; otherwise, specify 'source' as NULL.
1497 * If 'reason' is OFPPR_MODIFY and 'old_pp' is nonnull, then messages are
1498 * suppressed in the case where the change would not be visible to a particular
1499 * controller. For example, OpenFlow 1.0 does not have the OFPPS_LIVE flag, so
1500 * this would suppress a change solely to that flag from being sent to an
1501 * OpenFlow 1.0 controller. */
1503 connmgr_send_port_status(struct connmgr
*mgr
, struct ofconn
*source
,
1504 const struct ofputil_phy_port
*old_pp
,
1505 const struct ofputil_phy_port
*new_pp
,
1508 /* XXX Should limit the number of queued port status change messages. */
1509 struct ofputil_port_status new_ps
= { reason
, *new_pp
};
1511 struct ofconn
*ofconn
;
1512 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
1513 if (ofconn_receives_async_msg(ofconn
, OAM_PORT_STATUS
, reason
)) {
1514 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1515 * generate OFPT_PORT_STATUS messages. That requirement was a
1516 * relic of how OpenFlow originally supported a single controller,
1517 * so that one could expect the controller to already know the
1518 * changes it had made.
1520 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1521 * OFPT_PORT_STATUS messages to every controller. This is
1522 * obviously more useful in the multi-controller case. We could
1523 * always implement it that way in OVS, but that would risk
1524 * confusing controllers that are intended for single-controller
1525 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1526 * in response to any OFPT_PORT_STATUS!)
1528 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1529 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1530 * originating controller. In a single-controller environment, in
1531 * particular, this means that it will never generate
1532 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1533 if (ofconn
== source
1534 && rconn_get_version(ofconn
->rconn
) < OFP15_VERSION
) {
1538 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1539 struct ofpbuf
*msg
= ofputil_encode_port_status(&new_ps
, protocol
);
1540 if (reason
== OFPPR_MODIFY
&& old_pp
) {
1541 struct ofputil_port_status old_ps
= { reason
, *old_pp
};
1542 struct ofpbuf
*old_msg
= ofputil_encode_port_status(&old_ps
,
1544 bool suppress
= ofpbuf_equal(msg
, old_msg
);
1545 ofpbuf_delete(old_msg
);
1553 ofconn_send(ofconn
, msg
, NULL
);
1558 /* Sends an OFPT_REQUESTFORWARD message with 'request' and 'reason' to
1559 * appropriate controllers managed by 'mgr'. For messages caused by a
1560 * controller OFPT_GROUP_MOD and OFPT_METER_MOD, specify 'source' as the
1561 * controller connection that sent the request; otherwise, specify 'source'
1564 connmgr_send_requestforward(struct connmgr
*mgr
, const struct ofconn
*source
,
1565 const struct ofputil_requestforward
*rf
)
1567 struct ofconn
*ofconn
;
1569 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
1570 /* METER_MOD only supported in OF13 and up. */
1571 if (rf
->reason
== OFPRFR_METER_MOD
1572 && rconn_get_version(ofconn
->rconn
) < OFP13_VERSION
) {
1576 if (ofconn_receives_async_msg(ofconn
, OAM_REQUESTFORWARD
, rf
->reason
)
1577 && ofconn
!= source
) {
1578 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1579 ofconn_send(ofconn
, ofputil_encode_requestforward(rf
, protocol
),
1585 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1586 * appropriate controllers managed by 'mgr'.
1588 * This may be called from the RCU thread. */
1590 connmgr_send_flow_removed(struct connmgr
*mgr
,
1591 const struct ofputil_flow_removed
*fr
)
1592 OVS_REQUIRES(ofproto_mutex
)
1594 struct ofconn
*ofconn
;
1596 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
1597 if (ofconn_receives_async_msg(ofconn
, OAM_FLOW_REMOVED
, fr
->reason
)) {
1598 /* Account flow expirations as replies to OpenFlow requests. That
1599 * works because preventing OpenFlow requests from being processed
1600 * also prevents new flows from being added (and expiring). (It
1601 * also prevents processing OpenFlow requests that would not add
1602 * new flows, so it is imperfect.) */
1603 struct ofpbuf
*msg
= ofputil_encode_flow_removed(
1604 fr
, ofconn_get_protocol(ofconn
));
1605 ofconn_send_reply(ofconn
, msg
);
1610 /* Sends an OFPT_TABLE_STATUS message with 'reason' to appropriate controllers
1611 * managed by 'mgr'. When the table state changes, the controller needs to be
1612 * informed with the OFPT_TABLE_STATUS message. The reason values
1613 * OFPTR_VACANCY_DOWN and OFPTR_VACANCY_UP identify a vacancy message. The
1614 * vacancy events are generated when the remaining space in the flow table
1615 * changes and crosses one of the vacancy thereshold specified by
1616 * OFPT_TABLE_MOD. */
1618 connmgr_send_table_status(struct connmgr
*mgr
,
1619 const struct ofputil_table_desc
*td
,
1622 struct ofputil_table_status ts
= {
1627 struct ofconn
*ofconn
;
1628 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
1629 if (ofconn_receives_async_msg(ofconn
, OAM_TABLE_STATUS
, reason
)) {
1632 msg
= ofputil_encode_table_status(&ts
,
1633 ofconn_get_protocol(ofconn
));
1635 ofconn_send(ofconn
, msg
, NULL
);
1641 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1642 * necessary according to their individual configurations. */
1644 connmgr_send_async_msg(struct connmgr
*mgr
,
1645 const struct ofproto_async_msg
*am
)
1647 struct ofconn
*ofconn
;
1649 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
1650 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1651 if (protocol
== OFPUTIL_P_NONE
|| !rconn_is_connected(ofconn
->rconn
)
1652 || ofconn
->controller_id
!= am
->controller_id
1653 || !ofconn_receives_async_msg(ofconn
, am
->oam
,
1654 am
->pin
.up
.base
.reason
)) {
1658 struct ofpbuf
*msg
= ofputil_encode_packet_in_private(
1659 &am
->pin
.up
, protocol
, ofconn
->packet_in_format
);
1661 struct ovs_list txq
;
1662 bool is_miss
= (am
->pin
.up
.base
.reason
== OFPR_NO_MATCH
||
1663 am
->pin
.up
.base
.reason
== OFPR_EXPLICIT_MISS
||
1664 am
->pin
.up
.base
.reason
== OFPR_IMPLICIT_MISS
);
1665 pinsched_send(ofconn
->schedulers
[is_miss
],
1666 am
->pin
.up
.base
.flow_metadata
.flow
.in_port
.ofp_port
,
1668 do_send_packet_ins(ofconn
, &txq
);
1673 do_send_packet_ins(struct ofconn
*ofconn
, struct ovs_list
*txq
)
1677 LIST_FOR_EACH_POP (pin
, list_node
, txq
) {
1678 if (rconn_send_with_limit(ofconn
->rconn
, pin
,
1679 ofconn
->packet_in_counter
, 100) == EAGAIN
) {
1680 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(5, 5);
1682 VLOG_INFO_RL(&rll
, "%s: dropping packet-in due to queue overflow",
1683 rconn_get_name(ofconn
->rconn
));
1688 /* Fail-open settings. */
1690 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1691 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1692 enum ofproto_fail_mode
1693 connmgr_get_fail_mode(const struct connmgr
*mgr
)
1695 return mgr
->fail_mode
;
1698 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1699 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1701 connmgr_set_fail_mode(struct connmgr
*mgr
, enum ofproto_fail_mode fail_mode
)
1703 if (mgr
->fail_mode
!= fail_mode
) {
1704 mgr
->fail_mode
= fail_mode
;
1705 update_fail_open(mgr
);
1706 if (!connmgr_has_controllers(mgr
)) {
1707 ofproto_flush_flows(mgr
->ofproto
);
1712 /* Fail-open implementation. */
1714 /* Returns the longest probe interval among the primary controllers configured
1715 * on 'mgr'. Returns 0 if there are no primary controllers. */
1717 connmgr_get_max_probe_interval(const struct connmgr
*mgr
)
1719 int max_probe_interval
= 0;
1721 struct ofservice
*ofservice
;
1722 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
1723 if (ofservice
->type
== OFCONN_PRIMARY
) {
1724 int probe_interval
= ofservice
->s
.probe_interval
;
1725 max_probe_interval
= MAX(max_probe_interval
, probe_interval
);
1728 return max_probe_interval
;
1731 /* Returns the number of seconds for which all of 'mgr's active, primary
1732 * controllers have been disconnected. Returns 0 if 'mgr' has no active,
1733 * primary controllers. */
1735 connmgr_failure_duration(const struct connmgr
*mgr
)
1737 int min_failure_duration
= INT_MAX
;
1739 struct ofservice
*ofservice
;
1740 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
1741 if (ofservice
->s
.type
== OFCONN_PRIMARY
&& ofservice
->rconn
) {
1742 int failure_duration
= rconn_failure_duration(ofservice
->rconn
);
1743 min_failure_duration
= MIN(min_failure_duration
, failure_duration
);
1747 return min_failure_duration
!= INT_MAX
? min_failure_duration
: 0;
1750 /* Returns true if at least one primary controller is connected (regardless of
1751 * whether those controllers are believed to have authenticated and accepted
1752 * this switch), false if none of them are connected. */
1754 connmgr_is_any_controller_connected(const struct connmgr
*mgr
)
1756 struct ofservice
*ofservice
;
1757 HMAP_FOR_EACH (ofservice
, hmap_node
, &mgr
->services
) {
1758 if (ofservice
->s
.type
== OFCONN_PRIMARY
1759 && !ovs_list_is_empty(&ofservice
->conns
)) {
1766 /* Returns true if at least one primary controller is believed to have
1767 * authenticated and accepted this switch, false otherwise. */
1769 connmgr_is_any_controller_admitted(const struct connmgr
*mgr
)
1771 const struct ofconn
*ofconn
;
1773 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
1774 if (ofconn
->type
== OFCONN_PRIMARY
1775 && rconn_is_admitted(ofconn
->rconn
)) {
1782 /* In-band configuration. */
1784 static bool any_extras_changed(const struct connmgr
*,
1785 const struct sockaddr_in
*extras
, size_t n
);
1787 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1788 * in-band control should guarantee access, in the same way that in-band
1789 * control guarantees access to OpenFlow controllers. */
1791 connmgr_set_extra_in_band_remotes(struct connmgr
*mgr
,
1792 const struct sockaddr_in
*extras
, size_t n
)
1794 if (!any_extras_changed(mgr
, extras
, n
)) {
1798 free(mgr
->extra_in_band_remotes
);
1799 mgr
->n_extra_remotes
= n
;
1800 mgr
->extra_in_band_remotes
= xmemdup(extras
, n
* sizeof *extras
);
1802 update_in_band_remotes(mgr
);
1805 /* Sets the OpenFlow queue used by flows set up by in-band control on
1806 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1807 * flows will use the default queue. */
1809 connmgr_set_in_band_queue(struct connmgr
*mgr
, int queue_id
)
1811 if (queue_id
!= mgr
->in_band_queue
) {
1812 mgr
->in_band_queue
= queue_id
;
1813 update_in_band_remotes(mgr
);
1818 any_extras_changed(const struct connmgr
*mgr
,
1819 const struct sockaddr_in
*extras
, size_t n
)
1821 if (n
!= mgr
->n_extra_remotes
) {
1825 for (size_t i
= 0; i
< n
; i
++) {
1826 const struct sockaddr_in
*old
= &mgr
->extra_in_band_remotes
[i
];
1827 const struct sockaddr_in
*new = &extras
[i
];
1829 if (old
->sin_addr
.s_addr
!= new->sin_addr
.s_addr
||
1830 old
->sin_port
!= new->sin_port
) {
1838 /* In-band implementation. */
1841 connmgr_has_in_band(struct connmgr
*mgr
)
1843 return mgr
->in_band
!= NULL
;
1846 /* Fail-open and in-band implementation. */
1848 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1849 * and standalone mode to re-create their flows.
1851 * In-band control has more sophisticated code that manages flows itself. */
1853 connmgr_flushed(struct connmgr
*mgr
)
1854 OVS_EXCLUDED(ofproto_mutex
)
1856 if (mgr
->fail_open
) {
1857 fail_open_flushed(mgr
->fail_open
);
1860 /* If there are no controllers and we're in standalone mode, set up a flow
1861 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1862 * us). Otherwise, the switch is in secure mode and we won't pass any
1863 * traffic until a controller has been defined and it tells us to do so. */
1864 if (!connmgr_has_controllers(mgr
)
1865 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
1866 struct ofpbuf ofpacts
;
1869 ofpbuf_init(&ofpacts
, sizeof(struct ofpact_output
));
1870 ofpact_put_OUTPUT(&ofpacts
)->port
= OFPP_NORMAL
;
1872 match_init_catchall(&match
);
1873 ofproto_add_flow(mgr
->ofproto
, &match
, 0, ofpacts
.data
,
1876 ofpbuf_uninit(&ofpacts
);
1880 /* Returns the number of hidden rules created by the in-band and fail-open
1881 * implementations in table 0. (Subtracting this count from the number of
1882 * rules in the table 0 classifier, as maintained in struct oftable, yields
1883 * the number of flows that OVS should report via OpenFlow for table 0.) */
1885 connmgr_count_hidden_rules(const struct connmgr
*mgr
)
1889 n_hidden
+= in_band_count_rules(mgr
->in_band
);
1891 if (mgr
->fail_open
) {
1892 n_hidden
+= fail_open_count_rules(mgr
->fail_open
);
1897 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1898 * otherwise a positive errno value.
1900 * ofservice_reconfigure() must be called to fully configure the new
1903 ofservice_create(struct connmgr
*mgr
, const char *target
,
1904 const struct ofproto_controller
*c
)
1905 OVS_REQUIRES(ofproto_mutex
)
1907 struct pvconn
*pvconn
= NULL
;
1908 struct rconn
*rconn
= NULL
;
1909 if (!vconn_verify_name(target
)) {
1910 char *name
= ofconn_make_name(mgr
, target
);
1911 rconn
= rconn_create(5, 8, c
->dscp
, c
->allowed_versions
);
1912 rconn_connect(rconn
, target
, name
);
1914 } else if (!pvconn_verify_name(target
)) {
1915 int error
= pvconn_open(target
, c
->allowed_versions
, c
->dscp
, &pvconn
);
1920 VLOG_WARN_RL(&rl
, "%s: unsupported controller \"%s\"",
1925 struct ofservice
*ofservice
= xzalloc(sizeof *ofservice
);
1926 hmap_insert(&mgr
->services
, &ofservice
->hmap_node
, hash_string(target
, 0));
1927 ofservice
->connmgr
= mgr
;
1928 ofservice
->target
= xstrdup(target
);
1929 ovs_list_init(&ofservice
->conns
);
1930 ofservice
->type
= c
->type
;
1931 ofservice
->rconn
= rconn
;
1932 ofservice
->pvconn
= pvconn
;
1934 ofservice_reconfigure(ofservice
, c
);
1936 VLOG_INFO("%s: added %s controller \"%s\"",
1937 mgr
->name
, ofconn_type_to_string(ofservice
->type
), target
);
1941 ofservice_close_all(struct ofservice
*ofservice
)
1942 OVS_REQUIRES(ofproto_mutex
)
1944 struct ofconn
*ofconn
, *next
;
1945 LIST_FOR_EACH_SAFE (ofconn
, next
, ofservice_node
, &ofservice
->conns
) {
1946 ofconn_destroy(ofconn
);
1951 ofservice_destroy(struct ofservice
*ofservice
)
1952 OVS_REQUIRES(ofproto_mutex
)
1958 ofservice_close_all(ofservice
);
1960 hmap_remove(&ofservice
->connmgr
->services
, &ofservice
->hmap_node
);
1961 free(ofservice
->target
);
1962 if (ofservice
->pvconn
) {
1963 pvconn_close(ofservice
->pvconn
);
1965 if (ofservice
->rconn
) {
1966 rconn_destroy(ofservice
->rconn
);
1972 ofservice_run(struct ofservice
*ofservice
)
1974 if (ofservice
->pvconn
) {
1975 struct vconn
*vconn
;
1976 int retval
= pvconn_accept(ofservice
->pvconn
, &vconn
);
1978 /* Passing default value for creation of the rconn */
1979 struct rconn
*rconn
= rconn_create(
1980 ofservice
->s
.probe_interval
, ofservice
->s
.max_backoff
,
1981 ofservice
->s
.dscp
, ofservice
->s
.allowed_versions
);
1982 char *name
= ofconn_make_name(ofservice
->connmgr
,
1983 vconn_get_name(vconn
));
1984 rconn_connect_unreliably(rconn
, vconn
, name
);
1987 ofconn_create(ofservice
, rconn
, &ofservice
->s
);
1988 } else if (retval
!= EAGAIN
) {
1989 VLOG_WARN_RL(&rl
, "accept failed (%s)", ovs_strerror(retval
));
1992 rconn_run(ofservice
->rconn
);
1994 bool connected
= rconn_is_connected(ofservice
->rconn
);
1995 bool has_ofconn
= !ovs_list_is_empty(&ofservice
->conns
);
1996 if (connected
&& !has_ofconn
) {
1997 ofconn_create(ofservice
, ofservice
->rconn
, &ofservice
->s
);
2003 ofservice_wait(struct ofservice
*ofservice
)
2005 if (ofservice
->pvconn
) {
2006 pvconn_wait(ofservice
->pvconn
);
2011 ofservice_reconfigure(struct ofservice
*ofservice
,
2012 const struct ofproto_controller
*settings
)
2013 OVS_REQUIRES(ofproto_mutex
)
2015 /* If the allowed OpenFlow versions change, close all of the existing
2016 * connections to allow them to reconnect and possibly negotiate a new
2018 if (ofservice
->s
.allowed_versions
!= settings
->allowed_versions
) {
2019 ofservice_close_all(ofservice
);
2022 ofservice
->s
= *settings
;
2024 struct ofconn
*ofconn
;
2025 LIST_FOR_EACH (ofconn
, ofservice_node
, &ofservice
->conns
) {
2026 ofconn_reconfigure(ofconn
, settings
);
2030 /* Finds and returns the ofservice within 'mgr' that has the given
2031 * 'target', or a null pointer if none exists. */
2032 static struct ofservice
*
2033 ofservice_lookup(struct connmgr
*mgr
, const char *target
)
2035 struct ofservice
*ofservice
;
2037 HMAP_FOR_EACH_WITH_HASH (ofservice
, hmap_node
, hash_string(target
, 0),
2039 if (!strcmp(ofservice
->target
, target
)) {
2046 /* Flow monitors (NXST_FLOW_MONITOR). */
2048 /* A counter incremented when something significant happens to an OpenFlow
2051 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
2052 * the current value (which is then incremented).
2054 * - When a rule is modified, its 'modify_seqno' is set to the current
2055 * value (which is then incremented).
2057 * Thus, by comparing an old value of monitor_seqno against a rule's
2058 * 'add_seqno', one can tell whether the rule was added before or after the old
2059 * value was read, and similarly for 'modify_seqno'.
2061 * 32 bits should normally be sufficient (and would be nice, to save space in
2062 * each rule) but then we'd have to have some special cases for wraparound.
2064 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
2066 static uint64_t monitor_seqno
= 1;
2068 COVERAGE_DEFINE(ofmonitor_pause
);
2069 COVERAGE_DEFINE(ofmonitor_resume
);
2072 ofmonitor_create(const struct ofputil_flow_monitor_request
*request
,
2073 struct ofconn
*ofconn
, struct ofmonitor
**monitorp
)
2074 OVS_REQUIRES(ofproto_mutex
)
2078 struct ofmonitor
*m
= ofmonitor_lookup(ofconn
, request
->id
);
2080 return OFPERR_OFPMOFC_MONITOR_EXISTS
;
2083 m
= xmalloc(sizeof *m
);
2085 hmap_insert(&ofconn
->monitors
, &m
->ofconn_node
, hash_int(request
->id
, 0));
2086 m
->id
= request
->id
;
2087 m
->flags
= request
->flags
;
2088 m
->out_port
= request
->out_port
;
2089 m
->table_id
= request
->table_id
;
2090 minimatch_init(&m
->match
, &request
->match
);
2097 ofmonitor_lookup(struct ofconn
*ofconn
, uint32_t id
)
2098 OVS_REQUIRES(ofproto_mutex
)
2100 struct ofmonitor
*m
;
2102 HMAP_FOR_EACH_IN_BUCKET (m
, ofconn_node
, hash_int(id
, 0),
2103 &ofconn
->monitors
) {
2112 ofmonitor_destroy(struct ofmonitor
*m
)
2113 OVS_REQUIRES(ofproto_mutex
)
2116 minimatch_destroy(&m
->match
);
2117 hmap_remove(&m
->ofconn
->monitors
, &m
->ofconn_node
);
2123 ofmonitor_report(struct connmgr
*mgr
, struct rule
*rule
,
2124 enum nx_flow_update_event event
,
2125 enum ofp_flow_removed_reason reason
,
2126 const struct ofconn
*abbrev_ofconn
, ovs_be32 abbrev_xid
,
2127 const struct rule_actions
*old_actions
)
2128 OVS_REQUIRES(ofproto_mutex
)
2130 if (rule_is_hidden(rule
)) {
2134 enum nx_flow_monitor_flags update
;
2138 rule
->add_seqno
= rule
->modify_seqno
= monitor_seqno
++;
2142 update
= NXFMF_DELETE
;
2145 case NXFME_MODIFIED
:
2146 update
= NXFMF_MODIFY
;
2147 rule
->modify_seqno
= monitor_seqno
++;
2155 struct ofconn
*ofconn
;
2156 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
2157 if (ofconn
->monitor_paused
) {
2158 /* Only send NXFME_DELETED notifications for flows that were added
2159 * before we paused. */
2160 if (event
!= NXFME_DELETED
2161 || rule
->add_seqno
> ofconn
->monitor_paused
) {
2166 enum nx_flow_monitor_flags flags
= 0;
2167 struct ofmonitor
*m
;
2168 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2169 if (m
->flags
& update
2170 && (m
->table_id
== 0xff || m
->table_id
== rule
->table_id
)
2171 && (ofproto_rule_has_out_port(rule
, m
->out_port
)
2173 && ofpacts_output_to_port(old_actions
->ofpacts
,
2174 old_actions
->ofpacts_len
,
2176 && cls_rule_is_loose_match(&rule
->cr
, &m
->match
)) {
2182 if (ovs_list_is_empty(&ofconn
->updates
)) {
2183 ofputil_start_flow_update(&ofconn
->updates
);
2184 ofconn
->sent_abbrev_update
= false;
2187 if (flags
& NXFMF_OWN
|| ofconn
!= abbrev_ofconn
2188 || ofconn
->monitor_paused
) {
2189 struct ofputil_flow_update fu
;
2192 fu
.reason
= event
== NXFME_DELETED
? reason
: 0;
2193 fu
.table_id
= rule
->table_id
;
2194 fu
.cookie
= rule
->flow_cookie
;
2195 minimatch_expand(&rule
->cr
.match
, &fu
.match
);
2196 fu
.priority
= rule
->cr
.priority
;
2198 ovs_mutex_lock(&rule
->mutex
);
2199 fu
.idle_timeout
= rule
->idle_timeout
;
2200 fu
.hard_timeout
= rule
->hard_timeout
;
2201 ovs_mutex_unlock(&rule
->mutex
);
2203 if (flags
& NXFMF_ACTIONS
) {
2204 const struct rule_actions
*actions
2205 = rule_get_actions(rule
);
2206 fu
.ofpacts
= actions
->ofpacts
;
2207 fu
.ofpacts_len
= actions
->ofpacts_len
;
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
;
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
));
2222 ofconn
->sent_abbrev_update
= true;
2229 ofmonitor_flush(struct connmgr
*mgr
)
2230 OVS_REQUIRES(ofproto_mutex
)
2232 struct ofconn
*ofconn
;
2234 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
2235 struct rconn_packet_counter
*counter
= ofconn
->monitor_counter
;
2238 LIST_FOR_EACH_POP (msg
, list_node
, &ofconn
->updates
) {
2239 ofconn_send(ofconn
, msg
, counter
);
2242 if (!ofconn
->monitor_paused
2243 && rconn_packet_counter_n_bytes(counter
) > 128 * 1024) {
2244 COVERAGE_INC(ofmonitor_pause
);
2245 ofconn
->monitor_paused
= monitor_seqno
++;
2246 struct ofpbuf
*pause
= ofpraw_alloc_xid(
2247 OFPRAW_NXT_FLOW_MONITOR_PAUSED
, OFP10_VERSION
, htonl(0), 0);
2248 ofconn_send(ofconn
, pause
, counter
);
2254 ofmonitor_resume(struct ofconn
*ofconn
)
2255 OVS_REQUIRES(ofproto_mutex
)
2257 struct rule_collection rules
;
2258 rule_collection_init(&rules
);
2260 struct ofmonitor
*m
;
2261 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2262 ofmonitor_collect_resume_rules(m
, ofconn
->monitor_paused
, &rules
);
2265 struct ovs_list msgs
= OVS_LIST_INITIALIZER(&msgs
);
2266 ofmonitor_compose_refresh_updates(&rules
, &msgs
);
2268 struct ofpbuf
*resumed
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED
,
2269 OFP10_VERSION
, htonl(0), 0);
2270 ovs_list_push_back(&msgs
, &resumed
->list_node
);
2271 ofconn_send_replies(ofconn
, &msgs
);
2273 ofconn
->monitor_paused
= 0;
2277 ofmonitor_may_resume(const struct ofconn
*ofconn
)
2278 OVS_REQUIRES(ofproto_mutex
)
2280 return (ofconn
->monitor_paused
!= 0
2281 && !rconn_packet_counter_n_packets(ofconn
->monitor_counter
));
2285 ofmonitor_run(struct connmgr
*mgr
)
2287 ovs_mutex_lock(&ofproto_mutex
);
2288 struct ofconn
*ofconn
;
2289 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
2290 if (ofmonitor_may_resume(ofconn
)) {
2291 COVERAGE_INC(ofmonitor_resume
);
2292 ofmonitor_resume(ofconn
);
2295 ovs_mutex_unlock(&ofproto_mutex
);
2299 ofmonitor_wait(struct connmgr
*mgr
)
2301 ovs_mutex_lock(&ofproto_mutex
);
2302 struct ofconn
*ofconn
;
2303 LIST_FOR_EACH (ofconn
, connmgr_node
, &mgr
->conns
) {
2304 if (ofmonitor_may_resume(ofconn
)) {
2305 poll_immediate_wake();
2308 ovs_mutex_unlock(&ofproto_mutex
);
2312 ofproto_async_msg_free(struct ofproto_async_msg
*am
)
2314 free(am
->pin
.up
.base
.packet
);
2315 free(am
->pin
.up
.base
.userdata
);
2316 free(am
->pin
.up
.stack
);
2317 free(am
->pin
.up
.actions
);
2318 free(am
->pin
.up
.action_set
);