2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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.
25 #include "dynamic-string.h"
26 #include "fail-open.h"
29 #include "ofp-actions.h"
33 #include "ofproto-provider.h"
35 #include "poll-loop.h"
42 #include "openvswitch/vconn.h"
43 #include "openvswitch/vlog.h"
47 VLOG_DEFINE_THIS_MODULE(connmgr
);
48 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
50 /* An OpenFlow connection.
56 * 'ofproto_mutex' must be held whenever an ofconn is created or destroyed or,
57 * more or less equivalently, whenever an ofconn is added to or removed from a
58 * connmgr. 'ofproto_mutex' doesn't protect the data inside the ofconn, except
59 * as specifically noted below. */
61 /* Configuration that persists from one connection to the next. */
63 struct ovs_list node
; /* In struct connmgr's "all_conns" list. */
64 struct hmap_node hmap_node
; /* In struct connmgr's "controllers" map. */
66 struct connmgr
*connmgr
; /* Connection's manager. */
67 struct rconn
*rconn
; /* OpenFlow connection. */
68 enum ofconn_type type
; /* Type. */
69 enum ofproto_band band
; /* In-band or out-of-band? */
70 bool enable_async_msgs
; /* Initially enable async messages? */
72 /* State that should be cleared from one connection to the next. */
75 enum ofp12_controller_role role
; /* Role. */
76 enum ofputil_protocol protocol
; /* Current protocol variant. */
77 enum nx_packet_in_format packet_in_format
; /* OFPT_PACKET_IN format. */
79 /* OFPT_PACKET_IN related data. */
80 struct rconn_packet_counter
*packet_in_counter
; /* # queued on 'rconn'. */
81 #define N_SCHEDULERS 2
82 struct pinsched
*schedulers
[N_SCHEDULERS
];
83 struct pktbuf
*pktbuf
; /* OpenFlow packet buffers. */
84 int miss_send_len
; /* Bytes to send of buffered packets. */
85 uint16_t controller_id
; /* Connection controller ID. */
87 /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow
88 * requests, and the maximum number before we stop reading OpenFlow
90 #define OFCONN_REPLY_MAX 100
91 struct rconn_packet_counter
*reply_counter
;
93 /* Asynchronous message configuration in each possible roles.
95 * A 1-bit enables sending an asynchronous message for one possible reason
96 * that the message might be generated, a 0-bit disables it. */
97 uint32_t master_async_config
[OAM_N_TYPES
]; /* master, other */
98 uint32_t slave_async_config
[OAM_N_TYPES
]; /* slave */
100 /* Flow table operation logging. */
101 int n_add
, n_delete
, n_modify
; /* Number of unreported ops of each kind. */
102 long long int first_op
, last_op
; /* Range of times for unreported ops. */
103 long long int next_op_report
; /* Time to report ops, or LLONG_MAX. */
104 long long int op_backoff
; /* Earliest time to report ops again. */
106 /* Flow monitors (e.g. NXST_FLOW_MONITOR). */
108 /* Configuration. Contains "struct ofmonitor"s. */
109 struct hmap monitors
OVS_GUARDED_BY(ofproto_mutex
);
113 * When too many flow monitor notifications back up in the transmit buffer,
114 * we pause the transmission of further notifications. These members track
115 * the flow control state.
117 * When notifications are flowing, 'monitor_paused' is 0. When
118 * notifications are paused, 'monitor_paused' is the value of
119 * 'monitor_seqno' at the point we paused.
121 * 'monitor_counter' counts the OpenFlow messages and bytes currently in
122 * flight. This value growing too large triggers pausing. */
123 uint64_t monitor_paused
OVS_GUARDED_BY(ofproto_mutex
);
124 struct rconn_packet_counter
*monitor_counter
OVS_GUARDED_BY(ofproto_mutex
);
126 /* State of monitors for a single ongoing flow_mod.
128 * 'updates' is a list of "struct ofpbuf"s that contain
129 * NXST_FLOW_MONITOR_REPLY messages representing the changes made by the
132 * When 'updates' is nonempty, 'sent_abbrev_update' is true if 'updates'
133 * contains an update event of type NXFME_ABBREV and false otherwise.. */
134 struct ovs_list updates
OVS_GUARDED_BY(ofproto_mutex
);
135 bool sent_abbrev_update
OVS_GUARDED_BY(ofproto_mutex
);
137 /* Active bundles. Contains "struct ofp_bundle"s. */
141 static struct ofconn
*ofconn_create(struct connmgr
*, struct rconn
*,
142 enum ofconn_type
, bool enable_async_msgs
)
143 OVS_REQUIRES(ofproto_mutex
);
144 static void ofconn_destroy(struct ofconn
*) OVS_REQUIRES(ofproto_mutex
);
145 static void ofconn_flush(struct ofconn
*) OVS_REQUIRES(ofproto_mutex
);
147 static void ofconn_reconfigure(struct ofconn
*,
148 const struct ofproto_controller
*);
150 static void ofconn_run(struct ofconn
*,
151 void (*handle_openflow
)(struct ofconn
*,
152 const struct ofpbuf
*ofp_msg
));
153 static void ofconn_wait(struct ofconn
*);
155 static void ofconn_log_flow_mods(struct ofconn
*);
157 static const char *ofconn_get_target(const struct ofconn
*);
158 static char *ofconn_make_name(const struct connmgr
*, const char *target
);
160 static void ofconn_set_rate_limit(struct ofconn
*, int rate
, int burst
);
162 static void ofconn_send(const struct ofconn
*, struct ofpbuf
*,
163 struct rconn_packet_counter
*);
165 static void do_send_packet_ins(struct ofconn
*, struct ovs_list
*txq
);
167 /* A listener for incoming OpenFlow "service" connections. */
169 struct hmap_node node
; /* In struct connmgr's "services" hmap. */
170 struct pvconn
*pvconn
; /* OpenFlow connection listener. */
172 /* These are not used by ofservice directly. They are settings for
173 * accepted "struct ofconn"s from the pvconn. */
174 int probe_interval
; /* Max idle time before probing, in seconds. */
175 int rate_limit
; /* Max packet-in rate in packets per second. */
176 int burst_limit
; /* Limit on accumulating packet credits. */
177 bool enable_async_msgs
; /* Initially enable async messages? */
178 uint8_t dscp
; /* DSCP Value for controller connection */
179 uint32_t allowed_versions
; /* OpenFlow protocol versions that may
180 * be negotiated for a session. */
183 static void ofservice_reconfigure(struct ofservice
*,
184 const struct ofproto_controller
*);
185 static int ofservice_create(struct connmgr
*mgr
, const char *target
,
186 uint32_t allowed_versions
, uint8_t dscp
);
187 static void ofservice_destroy(struct connmgr
*, struct ofservice
*);
188 static struct ofservice
*ofservice_lookup(struct connmgr
*,
191 /* Connection manager for an OpenFlow switch. */
193 struct ofproto
*ofproto
;
195 char *local_port_name
;
197 /* OpenFlow connections. */
198 struct hmap controllers
; /* All OFCONN_PRIMARY controllers. */
199 struct ovs_list all_conns
; /* All controllers. */
200 uint64_t master_election_id
; /* monotonically increasing sequence number
201 * for master election */
202 bool master_election_id_defined
;
204 /* OpenFlow listeners. */
205 struct hmap services
; /* Contains "struct ofservice"s. */
206 struct pvconn
**snoops
;
210 struct fail_open
*fail_open
;
211 enum ofproto_fail_mode fail_mode
;
213 /* In-band control. */
214 struct in_band
*in_band
;
215 struct sockaddr_in
*extra_in_band_remotes
;
216 size_t n_extra_remotes
;
220 static void update_in_band_remotes(struct connmgr
*);
221 static void add_snooper(struct connmgr
*, struct vconn
*);
222 static void ofmonitor_run(struct connmgr
*);
223 static void ofmonitor_wait(struct connmgr
*);
225 /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is
226 * a name for the ofproto suitable for using in log messages.
227 * 'local_port_name' is the name of the local port (OFPP_LOCAL) within
230 connmgr_create(struct ofproto
*ofproto
,
231 const char *name
, const char *local_port_name
)
235 mgr
= xmalloc(sizeof *mgr
);
236 mgr
->ofproto
= ofproto
;
237 mgr
->name
= xstrdup(name
);
238 mgr
->local_port_name
= xstrdup(local_port_name
);
240 hmap_init(&mgr
->controllers
);
241 list_init(&mgr
->all_conns
);
242 mgr
->master_election_id
= 0;
243 mgr
->master_election_id_defined
= false;
245 hmap_init(&mgr
->services
);
249 mgr
->fail_open
= NULL
;
250 mgr
->fail_mode
= OFPROTO_FAIL_SECURE
;
253 mgr
->extra_in_band_remotes
= NULL
;
254 mgr
->n_extra_remotes
= 0;
255 mgr
->in_band_queue
= -1;
260 /* Frees 'mgr' and all of its resources. */
262 connmgr_destroy(struct connmgr
*mgr
)
264 struct ofservice
*ofservice
, *next_ofservice
;
265 struct ofconn
*ofconn
, *next_ofconn
;
272 ovs_mutex_lock(&ofproto_mutex
);
273 LIST_FOR_EACH_SAFE (ofconn
, next_ofconn
, node
, &mgr
->all_conns
) {
274 ofconn_destroy(ofconn
);
276 ovs_mutex_unlock(&ofproto_mutex
);
278 hmap_destroy(&mgr
->controllers
);
280 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, node
, &mgr
->services
) {
281 ofservice_destroy(mgr
, ofservice
);
283 hmap_destroy(&mgr
->services
);
285 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
286 pvconn_close(mgr
->snoops
[i
]);
290 fail_open_destroy(mgr
->fail_open
);
291 mgr
->fail_open
= NULL
;
293 in_band_destroy(mgr
->in_band
);
295 free(mgr
->extra_in_band_remotes
);
297 free(mgr
->local_port_name
);
302 /* Does all of the periodic maintenance required by 'mgr'. Calls
303 * 'handle_openflow' for each message received on an OpenFlow connection,
304 * passing along the OpenFlow connection itself and the message that was sent.
305 * 'handle_openflow' must not modify or free the message. */
307 connmgr_run(struct connmgr
*mgr
,
308 void (*handle_openflow
)(struct ofconn
*,
309 const struct ofpbuf
*ofp_msg
))
310 OVS_EXCLUDED(ofproto_mutex
)
312 struct ofconn
*ofconn
, *next_ofconn
;
313 struct ofservice
*ofservice
;
317 if (!in_band_run(mgr
->in_band
)) {
318 in_band_destroy(mgr
->in_band
);
323 LIST_FOR_EACH_SAFE (ofconn
, next_ofconn
, node
, &mgr
->all_conns
) {
324 ofconn_run(ofconn
, handle_openflow
);
328 /* Fail-open maintenance. Do this after processing the ofconns since
329 * fail-open checks the status of the controller rconn. */
330 if (mgr
->fail_open
) {
331 fail_open_run(mgr
->fail_open
);
334 HMAP_FOR_EACH (ofservice
, node
, &mgr
->services
) {
338 retval
= pvconn_accept(ofservice
->pvconn
, &vconn
);
343 /* Passing default value for creation of the rconn */
344 rconn
= rconn_create(ofservice
->probe_interval
, 0, ofservice
->dscp
,
345 vconn_get_allowed_versions(vconn
));
346 name
= ofconn_make_name(mgr
, vconn_get_name(vconn
));
347 rconn_connect_unreliably(rconn
, vconn
, name
);
350 ovs_mutex_lock(&ofproto_mutex
);
351 ofconn
= ofconn_create(mgr
, rconn
, OFCONN_SERVICE
,
352 ofservice
->enable_async_msgs
);
353 ovs_mutex_unlock(&ofproto_mutex
);
355 ofconn_set_rate_limit(ofconn
, ofservice
->rate_limit
,
356 ofservice
->burst_limit
);
357 } else if (retval
!= EAGAIN
) {
358 VLOG_WARN_RL(&rl
, "accept failed (%s)", ovs_strerror(retval
));
362 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
366 retval
= pvconn_accept(mgr
->snoops
[i
], &vconn
);
368 add_snooper(mgr
, vconn
);
369 } else if (retval
!= EAGAIN
) {
370 VLOG_WARN_RL(&rl
, "accept failed (%s)", ovs_strerror(retval
));
375 /* Causes the poll loop to wake up when connmgr_run() needs to run. */
377 connmgr_wait(struct connmgr
*mgr
)
379 struct ofservice
*ofservice
;
380 struct ofconn
*ofconn
;
383 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
388 in_band_wait(mgr
->in_band
);
390 if (mgr
->fail_open
) {
391 fail_open_wait(mgr
->fail_open
);
393 HMAP_FOR_EACH (ofservice
, node
, &mgr
->services
) {
394 pvconn_wait(ofservice
->pvconn
);
396 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
397 pvconn_wait(mgr
->snoops
[i
]);
401 /* Adds some memory usage statistics for 'mgr' into 'usage', for use with
402 * memory_report(). */
404 connmgr_get_memory_usage(const struct connmgr
*mgr
, struct simap
*usage
)
406 const struct ofconn
*ofconn
;
407 unsigned int packets
= 0;
408 unsigned int ofconns
= 0;
410 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
415 packets
+= rconn_count_txqlen(ofconn
->rconn
);
416 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
417 struct pinsched_stats stats
;
419 pinsched_get_stats(ofconn
->schedulers
[i
], &stats
);
420 packets
+= stats
.n_queued
;;
422 packets
+= pktbuf_count_packets(ofconn
->pktbuf
);
424 simap_increase(usage
, "ofconns", ofconns
);
425 simap_increase(usage
, "packets", packets
);
428 /* Returns the ofproto that owns 'ofconn''s connmgr. */
430 ofconn_get_ofproto(const struct ofconn
*ofconn
)
432 return ofconn
->connmgr
->ofproto
;
435 /* OpenFlow configuration. */
437 static void add_controller(struct connmgr
*, const char *target
, uint8_t dscp
,
438 uint32_t allowed_versions
)
439 OVS_REQUIRES(ofproto_mutex
);
440 static struct ofconn
*find_controller_by_target(struct connmgr
*,
442 static void update_fail_open(struct connmgr
*) OVS_EXCLUDED(ofproto_mutex
);
443 static int set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
444 const struct sset
*);
446 /* Returns true if 'mgr' has any configured primary controllers.
448 * Service controllers do not count, but configured primary controllers do
449 * count whether or not they are currently connected. */
451 connmgr_has_controllers(const struct connmgr
*mgr
)
453 return !hmap_is_empty(&mgr
->controllers
);
456 /* Initializes 'info' and populates it with information about each configured
457 * primary controller. The keys in 'info' are the controllers' targets; the
458 * data values are corresponding "struct ofproto_controller_info".
460 * The caller owns 'info' and everything in it and should free it when it is no
463 connmgr_get_controller_info(struct connmgr
*mgr
, struct shash
*info
)
465 const struct ofconn
*ofconn
;
467 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
468 const struct rconn
*rconn
= ofconn
->rconn
;
469 const char *target
= rconn_get_target(rconn
);
471 if (!shash_find(info
, target
)) {
472 struct ofproto_controller_info
*cinfo
= xmalloc(sizeof *cinfo
);
473 time_t now
= time_now();
474 time_t last_connection
= rconn_get_last_connection(rconn
);
475 time_t last_disconnect
= rconn_get_last_disconnect(rconn
);
476 int last_error
= rconn_get_last_error(rconn
);
479 shash_add(info
, target
, cinfo
);
481 cinfo
->is_connected
= rconn_is_connected(rconn
);
482 cinfo
->role
= ofconn
->role
;
484 smap_init(&cinfo
->pairs
);
486 smap_add(&cinfo
->pairs
, "last_error",
487 ovs_retval_to_string(last_error
));
490 smap_add(&cinfo
->pairs
, "state", rconn_get_state(rconn
));
492 if (last_connection
!= TIME_MIN
) {
493 smap_add_format(&cinfo
->pairs
, "sec_since_connect",
494 "%ld", (long int) (now
- last_connection
));
497 if (last_disconnect
!= TIME_MIN
) {
498 smap_add_format(&cinfo
->pairs
, "sec_since_disconnect",
499 "%ld", (long int) (now
- last_disconnect
));
502 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
503 if (ofconn
->schedulers
[i
]) {
504 const char *name
= i
? "miss" : "action";
505 struct pinsched_stats stats
;
507 pinsched_get_stats(ofconn
->schedulers
[i
], &stats
);
508 smap_add_nocopy(&cinfo
->pairs
,
509 xasprintf("packet-in-%s-backlog", name
),
510 xasprintf("%u", stats
.n_queued
));
511 smap_add_nocopy(&cinfo
->pairs
,
512 xasprintf("packet-in-%s-bypassed", name
),
513 xasprintf("%llu", stats
.n_normal
));
514 smap_add_nocopy(&cinfo
->pairs
,
515 xasprintf("packet-in-%s-queued", name
),
516 xasprintf("%llu", stats
.n_limited
));
517 smap_add_nocopy(&cinfo
->pairs
,
518 xasprintf("packet-in-%s-dropped", name
),
519 xasprintf("%llu", stats
.n_queue_dropped
));
527 connmgr_free_controller_info(struct shash
*info
)
529 struct shash_node
*node
;
531 SHASH_FOR_EACH (node
, info
) {
532 struct ofproto_controller_info
*cinfo
= node
->data
;
533 smap_destroy(&cinfo
->pairs
);
539 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
542 connmgr_set_controllers(struct connmgr
*mgr
,
543 const struct ofproto_controller
*controllers
,
544 size_t n_controllers
, uint32_t allowed_versions
)
545 OVS_EXCLUDED(ofproto_mutex
)
547 bool had_controllers
= connmgr_has_controllers(mgr
);
548 struct shash new_controllers
;
549 struct ofconn
*ofconn
, *next_ofconn
;
550 struct ofservice
*ofservice
, *next_ofservice
;
553 /* Required to add and remove ofconns. This could probably be narrowed to
554 * cover a smaller amount of code, if that yielded some benefit. */
555 ovs_mutex_lock(&ofproto_mutex
);
557 /* Create newly configured controllers and services.
558 * Create a name to ofproto_controller mapping in 'new_controllers'. */
559 shash_init(&new_controllers
);
560 for (i
= 0; i
< n_controllers
; i
++) {
561 const struct ofproto_controller
*c
= &controllers
[i
];
563 if (!vconn_verify_name(c
->target
)) {
565 ofconn
= find_controller_by_target(mgr
, c
->target
);
567 VLOG_INFO("%s: added primary controller \"%s\"",
568 mgr
->name
, c
->target
);
570 } else if (rconn_get_allowed_versions(ofconn
->rconn
) !=
572 VLOG_INFO("%s: re-added primary controller \"%s\"",
573 mgr
->name
, c
->target
);
575 ofconn_destroy(ofconn
);
578 add_controller(mgr
, c
->target
, c
->dscp
, allowed_versions
);
580 } else if (!pvconn_verify_name(c
->target
)) {
582 ofservice
= ofservice_lookup(mgr
, c
->target
);
584 VLOG_INFO("%s: added service controller \"%s\"",
585 mgr
->name
, c
->target
);
587 } else if (ofservice
->allowed_versions
!= allowed_versions
) {
588 VLOG_INFO("%s: re-added service controller \"%s\"",
589 mgr
->name
, c
->target
);
590 ofservice_destroy(mgr
, ofservice
);
594 ofservice_create(mgr
, c
->target
, allowed_versions
, c
->dscp
);
597 VLOG_WARN_RL(&rl
, "%s: unsupported controller \"%s\"",
598 mgr
->name
, c
->target
);
602 shash_add_once(&new_controllers
, c
->target
, &controllers
[i
]);
605 /* Delete controllers that are no longer configured.
606 * Update configuration of all now-existing controllers. */
607 HMAP_FOR_EACH_SAFE (ofconn
, next_ofconn
, hmap_node
, &mgr
->controllers
) {
608 const char *target
= ofconn_get_target(ofconn
);
609 struct ofproto_controller
*c
;
611 c
= shash_find_data(&new_controllers
, target
);
613 VLOG_INFO("%s: removed primary controller \"%s\"",
615 ofconn_destroy(ofconn
);
617 ofconn_reconfigure(ofconn
, c
);
621 /* Delete services that are no longer configured.
622 * Update configuration of all now-existing services. */
623 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, node
, &mgr
->services
) {
624 const char *target
= pvconn_get_name(ofservice
->pvconn
);
625 struct ofproto_controller
*c
;
627 c
= shash_find_data(&new_controllers
, target
);
629 VLOG_INFO("%s: removed service controller \"%s\"",
631 ofservice_destroy(mgr
, ofservice
);
633 ofservice_reconfigure(ofservice
, c
);
637 shash_destroy(&new_controllers
);
639 ovs_mutex_unlock(&ofproto_mutex
);
641 update_in_band_remotes(mgr
);
642 update_fail_open(mgr
);
643 if (had_controllers
!= connmgr_has_controllers(mgr
)) {
644 ofproto_flush_flows(mgr
->ofproto
);
648 /* Drops the connections between 'mgr' and all of its primary and secondary
649 * controllers, forcing them to reconnect. */
651 connmgr_reconnect(const struct connmgr
*mgr
)
653 struct ofconn
*ofconn
;
655 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
656 rconn_reconnect(ofconn
->rconn
);
660 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
662 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
663 * important controller on 'mgr' is mirrored. */
665 connmgr_set_snoops(struct connmgr
*mgr
, const struct sset
*snoops
)
667 return set_pvconns(&mgr
->snoops
, &mgr
->n_snoops
, snoops
);
670 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
672 connmgr_get_snoops(const struct connmgr
*mgr
, struct sset
*snoops
)
676 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
677 sset_add(snoops
, pvconn_get_name(mgr
->snoops
[i
]));
681 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
683 connmgr_has_snoops(const struct connmgr
*mgr
)
685 return mgr
->n_snoops
> 0;
688 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
689 * to be called later to finish the new ofconn's configuration. */
691 add_controller(struct connmgr
*mgr
, const char *target
, uint8_t dscp
,
692 uint32_t allowed_versions
)
693 OVS_REQUIRES(ofproto_mutex
)
695 char *name
= ofconn_make_name(mgr
, target
);
696 struct ofconn
*ofconn
;
698 ofconn
= ofconn_create(mgr
, rconn_create(5, 8, dscp
, allowed_versions
),
699 OFCONN_PRIMARY
, true);
700 ofconn
->pktbuf
= pktbuf_create();
701 rconn_connect(ofconn
->rconn
, target
, name
);
702 hmap_insert(&mgr
->controllers
, &ofconn
->hmap_node
, hash_string(target
, 0));
707 static struct ofconn
*
708 find_controller_by_target(struct connmgr
*mgr
, const char *target
)
710 struct ofconn
*ofconn
;
712 HMAP_FOR_EACH_WITH_HASH (ofconn
, hmap_node
,
713 hash_string(target
, 0), &mgr
->controllers
) {
714 if (!strcmp(ofconn_get_target(ofconn
), target
)) {
722 update_in_band_remotes(struct connmgr
*mgr
)
724 struct sockaddr_in
*addrs
;
725 size_t max_addrs
, n_addrs
;
726 struct ofconn
*ofconn
;
729 /* Allocate enough memory for as many remotes as we could possibly have. */
730 max_addrs
= mgr
->n_extra_remotes
+ hmap_count(&mgr
->controllers
);
731 addrs
= xmalloc(max_addrs
* sizeof *addrs
);
734 /* Add all the remotes. */
735 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
736 const char *target
= rconn_get_target(ofconn
->rconn
);
738 struct sockaddr_storage ss
;
739 struct sockaddr_in in
;
742 if (ofconn
->band
== OFPROTO_IN_BAND
743 && stream_parse_target_with_default_port(target
, OFP_PORT
, &sa
.ss
)
744 && sa
.ss
.ss_family
== AF_INET
) {
745 addrs
[n_addrs
++] = sa
.in
;
748 for (i
= 0; i
< mgr
->n_extra_remotes
; i
++) {
749 addrs
[n_addrs
++] = mgr
->extra_in_band_remotes
[i
];
752 /* Create or update or destroy in-band. */
755 in_band_create(mgr
->ofproto
, mgr
->local_port_name
, &mgr
->in_band
);
757 in_band_set_queue(mgr
->in_band
, mgr
->in_band_queue
);
759 /* in_band_run() needs a chance to delete any existing in-band flows.
760 * We will destroy mgr->in_band after it's done with that. */
763 in_band_set_remotes(mgr
->in_band
, addrs
, n_addrs
);
771 update_fail_open(struct connmgr
*mgr
)
772 OVS_EXCLUDED(ofproto_mutex
)
774 if (connmgr_has_controllers(mgr
)
775 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
776 if (!mgr
->fail_open
) {
777 mgr
->fail_open
= fail_open_create(mgr
->ofproto
, mgr
);
780 fail_open_destroy(mgr
->fail_open
);
781 mgr
->fail_open
= NULL
;
786 set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
787 const struct sset
*sset
)
789 struct pvconn
**pvconns
= *pvconnsp
;
790 size_t n_pvconns
= *n_pvconnsp
;
795 for (i
= 0; i
< n_pvconns
; i
++) {
796 pvconn_close(pvconns
[i
]);
800 pvconns
= xmalloc(sset_count(sset
) * sizeof *pvconns
);
802 SSET_FOR_EACH (name
, sset
) {
803 struct pvconn
*pvconn
;
805 error
= pvconn_open(name
, 0, 0, &pvconn
);
807 pvconns
[n_pvconns
++] = pvconn
;
809 VLOG_ERR("failed to listen on %s: %s", name
, ovs_strerror(error
));
817 *n_pvconnsp
= n_pvconns
;
822 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
823 * means that 'ofconn' is more interesting for monitoring than a lower return
826 snoop_preference(const struct ofconn
*ofconn
)
828 switch (ofconn
->role
) {
829 case OFPCR12_ROLE_MASTER
:
831 case OFPCR12_ROLE_EQUAL
:
833 case OFPCR12_ROLE_SLAVE
:
835 case OFPCR12_ROLE_NOCHANGE
:
837 /* Shouldn't happen. */
842 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
843 * Connects this vconn to a controller. */
845 add_snooper(struct connmgr
*mgr
, struct vconn
*vconn
)
847 struct ofconn
*ofconn
, *best
;
849 /* Pick a controller for monitoring. */
851 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
852 if (ofconn
->type
== OFCONN_PRIMARY
853 && (!best
|| snoop_preference(ofconn
) > snoop_preference(best
))) {
859 rconn_add_monitor(best
->rconn
, vconn
);
861 VLOG_INFO_RL(&rl
, "no controller connection to snoop");
866 /* Public ofconn functions. */
868 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
870 ofconn_get_type(const struct ofconn
*ofconn
)
875 /* If a master election id is defined, stores it into '*idp' and returns
876 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
878 ofconn_get_master_election_id(const struct ofconn
*ofconn
, uint64_t *idp
)
880 *idp
= (ofconn
->connmgr
->master_election_id_defined
881 ? ofconn
->connmgr
->master_election_id
883 return ofconn
->connmgr
->master_election_id_defined
;
886 /* Sets the master election id.
888 * Returns true if successful, false if the id is stale
891 ofconn_set_master_election_id(struct ofconn
*ofconn
, uint64_t id
)
893 if (ofconn
->connmgr
->master_election_id_defined
895 /* Unsigned difference interpreted as a two's complement signed
897 (int64_t)(id
- ofconn
->connmgr
->master_election_id
) < 0) {
900 ofconn
->connmgr
->master_election_id
= id
;
901 ofconn
->connmgr
->master_election_id_defined
= true;
906 /* Returns the role configured for 'ofconn'.
908 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
909 enum ofp12_controller_role
910 ofconn_get_role(const struct ofconn
*ofconn
)
916 ofconn_send_role_status(struct ofconn
*ofconn
, uint32_t role
, uint8_t reason
)
918 struct ofputil_role_status status
;
921 status
.reason
= reason
;
923 ofconn_get_master_election_id(ofconn
, &status
.generation_id
);
925 buf
= ofputil_encode_role_status(&status
, ofconn_get_protocol(ofconn
));
927 ofconn_send(ofconn
, buf
, NULL
);
931 /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
932 * any existing master is demoted to a slave. */
934 ofconn_set_role(struct ofconn
*ofconn
, enum ofp12_controller_role role
)
936 if (role
!= ofconn
->role
&& role
== OFPCR12_ROLE_MASTER
) {
937 struct ofconn
*other
;
939 LIST_FOR_EACH (other
, node
, &ofconn
->connmgr
->all_conns
) {
940 if (other
->role
== OFPCR12_ROLE_MASTER
) {
941 other
->role
= OFPCR12_ROLE_SLAVE
;
942 ofconn_send_role_status(other
, OFPCR12_ROLE_SLAVE
, OFPCRR_MASTER_REQUEST
);
950 ofconn_set_invalid_ttl_to_controller(struct ofconn
*ofconn
, bool enable
)
952 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
954 ofconn
->master_async_config
[OAM_PACKET_IN
] |= bit
;
956 ofconn
->master_async_config
[OAM_PACKET_IN
] &= ~bit
;
961 ofconn_get_invalid_ttl_to_controller(struct ofconn
*ofconn
)
963 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
964 return (ofconn
->master_async_config
[OAM_PACKET_IN
] & bit
) != 0;
967 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
969 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
970 * completed version negotiation. This can't happen if at least one OpenFlow
971 * message, other than OFPT_HELLO, has been received on the connection (such as
972 * in ofproto.c's message handling code), since version negotiation is a
973 * prerequisite for starting to receive messages. This means that
974 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
975 enum ofputil_protocol
976 ofconn_get_protocol(const struct ofconn
*ofconn
)
978 if (ofconn
->protocol
== OFPUTIL_P_NONE
&&
979 rconn_is_connected(ofconn
->rconn
)) {
980 int version
= rconn_get_version(ofconn
->rconn
);
982 ofconn_set_protocol(CONST_CAST(struct ofconn
*, ofconn
),
983 ofputil_protocol_from_ofp_version(version
));
987 return ofconn
->protocol
;
990 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
992 * (This doesn't actually send anything to accomplish this. Presumably the
993 * caller already did that.) */
995 ofconn_set_protocol(struct ofconn
*ofconn
, enum ofputil_protocol protocol
)
997 ofconn
->protocol
= protocol
;
998 if (!(protocol
& OFPUTIL_P_OF14_UP
)) {
999 uint32_t *master
= ofconn
->master_async_config
;
1000 uint32_t *slave
= ofconn
->slave_async_config
;
1002 /* OFPR_ACTION_SET is not supported before OF1.4 */
1003 master
[OAM_PACKET_IN
] &= ~(1u << OFPR_ACTION_SET
);
1004 slave
[OAM_PACKET_IN
] &= ~(1u << OFPR_ACTION_SET
);
1006 /* OFPR_GROUP is not supported before OF1.4 */
1007 master
[OAM_PACKET_IN
] &= ~(1u << OFPR_GROUP
);
1008 slave
[OAM_PACKET_IN
] &= ~(1u << OFPR_GROUP
);
1012 /* Returns the currently configured packet in format for 'ofconn', one of
1015 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
1016 enum nx_packet_in_format
1017 ofconn_get_packet_in_format(struct ofconn
*ofconn
)
1019 return ofconn
->packet_in_format
;
1022 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
1025 ofconn_set_packet_in_format(struct ofconn
*ofconn
,
1026 enum nx_packet_in_format packet_in_format
)
1028 ofconn
->packet_in_format
= packet_in_format
;
1031 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
1033 * The connection controller ID is used for OFPP_CONTROLLER and
1034 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
1036 ofconn_set_controller_id(struct ofconn
*ofconn
, uint16_t controller_id
)
1038 ofconn
->controller_id
= controller_id
;
1041 /* Returns the default miss send length for 'ofconn'. */
1043 ofconn_get_miss_send_len(const struct ofconn
*ofconn
)
1045 return ofconn
->miss_send_len
;
1048 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
1050 ofconn_set_miss_send_len(struct ofconn
*ofconn
, int miss_send_len
)
1052 ofconn
->miss_send_len
= miss_send_len
;
1056 ofconn_set_async_config(struct ofconn
*ofconn
,
1057 const uint32_t master_masks
[OAM_N_TYPES
],
1058 const uint32_t slave_masks
[OAM_N_TYPES
])
1060 size_t size
= sizeof ofconn
->master_async_config
;
1061 memcpy(ofconn
->master_async_config
, master_masks
, size
);
1062 memcpy(ofconn
->slave_async_config
, slave_masks
, size
);
1066 ofconn_get_async_config(struct ofconn
*ofconn
,
1067 uint32_t *master_masks
, uint32_t *slave_masks
)
1069 size_t size
= sizeof ofconn
->master_async_config
;
1071 /* Make sure we know the protocol version and the async_config
1072 * masks are properly updated by calling ofconn_get_protocol() */
1073 if (OFPUTIL_P_NONE
== ofconn_get_protocol(ofconn
)){
1077 memcpy(master_masks
, ofconn
->master_async_config
, size
);
1078 memcpy(slave_masks
, ofconn
->slave_async_config
, size
);
1081 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1082 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1083 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1084 * controller has accepted some of the replies.) */
1086 ofconn_send_reply(const struct ofconn
*ofconn
, struct ofpbuf
*msg
)
1088 ofconn_send(ofconn
, msg
, ofconn
->reply_counter
);
1091 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
1092 * accounting them as replies. */
1094 ofconn_send_replies(const struct ofconn
*ofconn
, struct ovs_list
*replies
)
1096 struct ofpbuf
*reply
;
1098 LIST_FOR_EACH_POP (reply
, list_node
, replies
) {
1099 ofconn_send_reply(ofconn
, reply
);
1103 /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
1104 * first 64 bytes of 'request' are used. */
1106 ofconn_send_error(const struct ofconn
*ofconn
,
1107 const struct ofp_header
*request
, enum ofperr error
)
1109 static struct vlog_rate_limit err_rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1110 struct ofpbuf
*reply
;
1112 reply
= ofperr_encode_reply(error
, request
);
1113 if (!VLOG_DROP_INFO(&err_rl
)) {
1114 const char *type_name
;
1118 request_len
= ntohs(request
->length
);
1119 type_name
= (!ofpraw_decode_partial(&raw
, request
,
1120 MIN(64, request_len
))
1121 ? ofpraw_get_name(raw
)
1124 VLOG_INFO("%s: sending %s error reply to %s message",
1125 rconn_get_name(ofconn
->rconn
), ofperr_to_string(error
),
1128 ofconn_send_reply(ofconn
, reply
);
1131 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
1133 ofconn_pktbuf_retrieve(struct ofconn
*ofconn
, uint32_t id
,
1134 struct dp_packet
**bufferp
, ofp_port_t
*in_port
)
1136 return pktbuf_retrieve(ofconn
->pktbuf
, id
, bufferp
, in_port
);
1139 /* Reports that a flow_mod operation of the type specified by 'command' was
1140 * successfully executed by 'ofconn', so that the connmgr can log it. */
1142 ofconn_report_flow_mod(struct ofconn
*ofconn
,
1143 enum ofp_flow_mod_command command
)
1153 case OFPFC_MODIFY_STRICT
:
1158 case OFPFC_DELETE_STRICT
:
1164 if (ofconn
->next_op_report
== LLONG_MAX
) {
1165 ofconn
->first_op
= now
;
1166 ofconn
->next_op_report
= MAX(now
+ 10 * 1000, ofconn
->op_backoff
);
1167 ofconn
->op_backoff
= ofconn
->next_op_report
+ 60 * 1000;
1169 ofconn
->last_op
= now
;
1172 /* OpenFlow 1.4 bundles. */
1174 static inline uint32_t
1175 bundle_hash(uint32_t id
)
1177 return hash_int(id
, 0);
1181 ofconn_get_bundle(struct ofconn
*ofconn
, uint32_t id
)
1183 struct ofp_bundle
*bundle
;
1185 HMAP_FOR_EACH_IN_BUCKET(bundle
, node
, bundle_hash(id
), &ofconn
->bundles
) {
1186 if (bundle
->id
== id
) {
1195 ofconn_insert_bundle(struct ofconn
*ofconn
, struct ofp_bundle
*bundle
)
1197 /* XXX: Check the limit of open bundles */
1199 hmap_insert(&ofconn
->bundles
, &bundle
->node
, bundle_hash(bundle
->id
));
1205 ofconn_remove_bundle(struct ofconn
*ofconn
, struct ofp_bundle
*bundle
)
1207 hmap_remove(&ofconn
->bundles
, &bundle
->node
);
1213 bundle_remove_all(struct ofconn
*ofconn
)
1215 struct ofp_bundle
*b
, *next
;
1217 HMAP_FOR_EACH_SAFE (b
, next
, node
, &ofconn
->bundles
) {
1218 ofp_bundle_remove__(ofconn
, b
, false);
1222 /* Private ofconn functions. */
1225 ofconn_get_target(const struct ofconn
*ofconn
)
1227 return rconn_get_target(ofconn
->rconn
);
1230 static struct ofconn
*
1231 ofconn_create(struct connmgr
*mgr
, struct rconn
*rconn
, enum ofconn_type type
,
1232 bool enable_async_msgs
)
1234 struct ofconn
*ofconn
;
1236 ofconn
= xzalloc(sizeof *ofconn
);
1237 ofconn
->connmgr
= mgr
;
1238 list_push_back(&mgr
->all_conns
, &ofconn
->node
);
1239 ofconn
->rconn
= rconn
;
1240 ofconn
->type
= type
;
1241 ofconn
->enable_async_msgs
= enable_async_msgs
;
1243 hmap_init(&ofconn
->monitors
);
1244 list_init(&ofconn
->updates
);
1246 hmap_init(&ofconn
->bundles
);
1248 ofconn_flush(ofconn
);
1253 /* Clears all of the state in 'ofconn' that should not persist from one
1254 * connection to the next. */
1256 ofconn_flush(struct ofconn
*ofconn
)
1257 OVS_REQUIRES(ofproto_mutex
)
1259 struct ofmonitor
*monitor
, *next_monitor
;
1262 ofconn_log_flow_mods(ofconn
);
1264 ofconn
->role
= OFPCR12_ROLE_EQUAL
;
1265 ofconn_set_protocol(ofconn
, OFPUTIL_P_NONE
);
1266 ofconn
->packet_in_format
= NXPIF_OPENFLOW10
;
1268 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1269 ofconn
->packet_in_counter
= rconn_packet_counter_create();
1270 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1271 if (ofconn
->schedulers
[i
]) {
1274 pinsched_get_limits(ofconn
->schedulers
[i
], &rate
, &burst
);
1275 pinsched_destroy(ofconn
->schedulers
[i
]);
1276 ofconn
->schedulers
[i
] = pinsched_create(rate
, burst
);
1279 if (ofconn
->pktbuf
) {
1280 pktbuf_destroy(ofconn
->pktbuf
);
1281 ofconn
->pktbuf
= pktbuf_create();
1283 ofconn
->miss_send_len
= (ofconn
->type
== OFCONN_PRIMARY
1284 ? OFP_DEFAULT_MISS_SEND_LEN
1286 ofconn
->controller_id
= 0;
1288 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1289 ofconn
->reply_counter
= rconn_packet_counter_create();
1291 if (ofconn
->enable_async_msgs
) {
1292 uint32_t *master
= ofconn
->master_async_config
;
1293 uint32_t *slave
= ofconn
->slave_async_config
;
1295 /* "master" and "other" roles get all asynchronous messages by default,
1296 * except that the controller needs to enable nonstandard "packet-in"
1297 * reasons itself. */
1298 master
[OAM_PACKET_IN
] = ((1u << OFPR_NO_MATCH
)
1299 | (1u << OFPR_ACTION
)
1300 | (1u << OFPR_ACTION_SET
)
1301 | (1u << OFPR_GROUP
));
1302 master
[OAM_PORT_STATUS
] = ((1u << OFPPR_ADD
)
1303 | (1u << OFPPR_DELETE
)
1304 | (1u << OFPPR_MODIFY
));
1305 master
[OAM_FLOW_REMOVED
] = ((1u << OFPRR_IDLE_TIMEOUT
)
1306 | (1u << OFPRR_HARD_TIMEOUT
)
1307 | (1u << OFPRR_DELETE
));
1309 /* "slave" role gets port status updates by default. */
1310 slave
[OAM_PACKET_IN
] = 0;
1311 slave
[OAM_PORT_STATUS
] = ((1u << OFPPR_ADD
)
1312 | (1u << OFPPR_DELETE
)
1313 | (1u << OFPPR_MODIFY
));
1314 slave
[OAM_FLOW_REMOVED
] = 0;
1316 memset(ofconn
->master_async_config
, 0,
1317 sizeof ofconn
->master_async_config
);
1318 memset(ofconn
->slave_async_config
, 0,
1319 sizeof ofconn
->slave_async_config
);
1322 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1323 ofconn
->first_op
= ofconn
->last_op
= LLONG_MIN
;
1324 ofconn
->next_op_report
= LLONG_MAX
;
1325 ofconn
->op_backoff
= LLONG_MIN
;
1327 HMAP_FOR_EACH_SAFE (monitor
, next_monitor
, ofconn_node
,
1328 &ofconn
->monitors
) {
1329 ofmonitor_destroy(monitor
);
1331 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1332 ofconn
->monitor_counter
= rconn_packet_counter_create();
1333 ofpbuf_list_delete(&ofconn
->updates
); /* ...but it should be empty. */
1337 ofconn_destroy(struct ofconn
*ofconn
)
1338 OVS_REQUIRES(ofproto_mutex
)
1340 ofconn_flush(ofconn
);
1342 if (ofconn
->type
== OFCONN_PRIMARY
) {
1343 hmap_remove(&ofconn
->connmgr
->controllers
, &ofconn
->hmap_node
);
1346 bundle_remove_all(ofconn
);
1347 hmap_destroy(&ofconn
->bundles
);
1349 hmap_destroy(&ofconn
->monitors
);
1350 list_remove(&ofconn
->node
);
1351 rconn_destroy(ofconn
->rconn
);
1352 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1353 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1354 pktbuf_destroy(ofconn
->pktbuf
);
1355 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1359 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1362 ofconn_reconfigure(struct ofconn
*ofconn
, const struct ofproto_controller
*c
)
1366 ofconn
->band
= c
->band
;
1367 ofconn
->enable_async_msgs
= c
->enable_async_msgs
;
1369 rconn_set_max_backoff(ofconn
->rconn
, c
->max_backoff
);
1371 probe_interval
= c
->probe_interval
? MAX(c
->probe_interval
, 5) : 0;
1372 rconn_set_probe_interval(ofconn
->rconn
, probe_interval
);
1374 ofconn_set_rate_limit(ofconn
, c
->rate_limit
, c
->burst_limit
);
1376 /* If dscp value changed reconnect. */
1377 if (c
->dscp
!= rconn_get_dscp(ofconn
->rconn
)) {
1378 rconn_set_dscp(ofconn
->rconn
, c
->dscp
);
1379 rconn_reconnect(ofconn
->rconn
);
1383 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1386 ofconn_may_recv(const struct ofconn
*ofconn
)
1388 int count
= rconn_packet_counter_n_packets(ofconn
->reply_counter
);
1389 return count
< OFCONN_REPLY_MAX
;
1393 ofconn_run(struct ofconn
*ofconn
,
1394 void (*handle_openflow
)(struct ofconn
*,
1395 const struct ofpbuf
*ofp_msg
))
1397 struct connmgr
*mgr
= ofconn
->connmgr
;
1400 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1401 struct ovs_list txq
;
1403 pinsched_run(ofconn
->schedulers
[i
], &txq
);
1404 do_send_packet_ins(ofconn
, &txq
);
1407 rconn_run(ofconn
->rconn
);
1409 /* Limit the number of iterations to avoid starving other tasks. */
1410 for (i
= 0; i
< 50 && ofconn_may_recv(ofconn
); i
++) {
1411 struct ofpbuf
*of_msg
= rconn_recv(ofconn
->rconn
);
1416 if (mgr
->fail_open
) {
1417 fail_open_maybe_recover(mgr
->fail_open
);
1420 handle_openflow(ofconn
, of_msg
);
1421 ofpbuf_delete(of_msg
);
1424 if (time_msec() >= ofconn
->next_op_report
) {
1425 ofconn_log_flow_mods(ofconn
);
1428 ovs_mutex_lock(&ofproto_mutex
);
1429 if (!rconn_is_alive(ofconn
->rconn
)) {
1430 ofconn_destroy(ofconn
);
1431 } else if (!rconn_is_connected(ofconn
->rconn
)) {
1432 ofconn_flush(ofconn
);
1434 ovs_mutex_unlock(&ofproto_mutex
);
1438 ofconn_wait(struct ofconn
*ofconn
)
1442 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1443 pinsched_wait(ofconn
->schedulers
[i
]);
1445 rconn_run_wait(ofconn
->rconn
);
1446 if (ofconn_may_recv(ofconn
)) {
1447 rconn_recv_wait(ofconn
->rconn
);
1449 if (ofconn
->next_op_report
!= LLONG_MAX
) {
1450 poll_timer_wait_until(ofconn
->next_op_report
);
1455 ofconn_log_flow_mods(struct ofconn
*ofconn
)
1457 int n_flow_mods
= ofconn
->n_add
+ ofconn
->n_delete
+ ofconn
->n_modify
;
1459 long long int ago
= (time_msec() - ofconn
->first_op
) / 1000;
1460 long long int interval
= (ofconn
->last_op
- ofconn
->first_op
) / 1000;
1464 ds_put_format(&s
, "%d flow_mods ", n_flow_mods
);
1465 if (interval
== ago
) {
1466 ds_put_format(&s
, "in the last %lld s", ago
);
1467 } else if (interval
) {
1468 ds_put_format(&s
, "in the %lld s starting %lld s ago",
1471 ds_put_format(&s
, "%lld s ago", ago
);
1474 ds_put_cstr(&s
, " (");
1475 if (ofconn
->n_add
) {
1476 ds_put_format(&s
, "%d adds, ", ofconn
->n_add
);
1478 if (ofconn
->n_delete
) {
1479 ds_put_format(&s
, "%d deletes, ", ofconn
->n_delete
);
1481 if (ofconn
->n_modify
) {
1482 ds_put_format(&s
, "%d modifications, ", ofconn
->n_modify
);
1485 ds_put_char(&s
, ')');
1487 VLOG_INFO("%s: %s", rconn_get_name(ofconn
->rconn
), ds_cstr(&s
));
1490 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1492 ofconn
->next_op_report
= LLONG_MAX
;
1495 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1496 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1497 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1498 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1501 ofconn_receives_async_msg(const struct ofconn
*ofconn
,
1502 enum ofconn_async_msg_type type
,
1503 unsigned int reason
)
1505 const uint32_t *async_config
;
1507 ovs_assert(reason
< 32);
1508 ovs_assert((unsigned int) type
< OAM_N_TYPES
);
1510 if (ofconn_get_protocol(ofconn
) == OFPUTIL_P_NONE
1511 || !rconn_is_connected(ofconn
->rconn
)) {
1515 /* Keep the following code in sync with the documentation in the
1516 * "Asynchronous Messages" section in DESIGN. */
1518 if (ofconn
->type
== OFCONN_SERVICE
&& !ofconn
->miss_send_len
) {
1519 /* Service connections don't get asynchronous messages unless they have
1520 * explicitly asked for them by setting a nonzero miss send length. */
1524 async_config
= (ofconn
->role
== OFPCR12_ROLE_SLAVE
1525 ? ofconn
->slave_async_config
1526 : ofconn
->master_async_config
);
1527 if (!(async_config
[type
] & (1u << reason
))) {
1534 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1535 * packet rather than to send the packet to the controller.
1537 * This function returns false to indicate the packet should be dropped if
1538 * the controller action was the result of the default table-miss behaviour
1539 * and the controller is using OpenFlow1.3+.
1541 * Otherwise true is returned to indicate the packet should be forwarded to
1544 ofconn_wants_packet_in_on_miss(struct ofconn
*ofconn
,
1545 const struct ofproto_packet_in
*pin
)
1547 if (pin
->miss_type
== OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW
) {
1548 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1550 if (protocol
!= OFPUTIL_P_NONE
1551 && ofputil_protocol_to_ofp_version(protocol
) >= OFP13_VERSION
1552 && (ofproto_table_get_miss_config(ofconn
->connmgr
->ofproto
,
1554 == OFPUTIL_TABLE_MISS_DEFAULT
)) {
1561 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1562 * packet rather than to send the packet to the controller.
1564 * This function returns true to indicate that a packet_in message
1565 * for a "table-miss" should be sent to at least one controller.
1566 * That is there is at least one controller with controller_id 0
1567 * which connected using an OpenFlow version earlier than OpenFlow1.3.
1571 * This logic assumes that "table-miss" packet_in messages
1572 * are always sent to controller_id 0. */
1574 connmgr_wants_packet_in_on_miss(struct connmgr
*mgr
) OVS_EXCLUDED(ofproto_mutex
)
1576 struct ofconn
*ofconn
;
1578 ovs_mutex_lock(&ofproto_mutex
);
1579 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1580 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1582 if (ofconn
->controller_id
== 0 &&
1583 (protocol
== OFPUTIL_P_NONE
||
1584 ofputil_protocol_to_ofp_version(protocol
) < OFP13_VERSION
)) {
1585 ovs_mutex_unlock(&ofproto_mutex
);
1589 ovs_mutex_unlock(&ofproto_mutex
);
1594 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1595 * 'target', suitable for use in log messages for identifying the connection.
1597 * The name is dynamically allocated. The caller should free it (with free())
1598 * when it is no longer needed. */
1600 ofconn_make_name(const struct connmgr
*mgr
, const char *target
)
1602 return xasprintf("%s<->%s", mgr
->name
, target
);
1606 ofconn_set_rate_limit(struct ofconn
*ofconn
, int rate
, int burst
)
1610 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1611 struct pinsched
**s
= &ofconn
->schedulers
[i
];
1615 *s
= pinsched_create(rate
, burst
);
1617 pinsched_set_limits(*s
, rate
, burst
);
1620 pinsched_destroy(*s
);
1627 ofconn_send(const struct ofconn
*ofconn
, struct ofpbuf
*msg
,
1628 struct rconn_packet_counter
*counter
)
1630 ofpmsg_update_length(msg
);
1631 rconn_send(ofconn
->rconn
, msg
, counter
);
1634 /* Sending asynchronous messages. */
1636 static void schedule_packet_in(struct ofconn
*, struct ofproto_packet_in
,
1637 enum ofp_packet_in_reason wire_reason
);
1639 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1640 * controllers managed by 'mgr'. For messages caused by a controller
1641 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1642 * request; otherwise, specify 'source' as NULL. */
1644 connmgr_send_port_status(struct connmgr
*mgr
, struct ofconn
*source
,
1645 const struct ofputil_phy_port
*pp
, uint8_t reason
)
1647 /* XXX Should limit the number of queued port status change messages. */
1648 struct ofputil_port_status ps
;
1649 struct ofconn
*ofconn
;
1653 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1654 if (ofconn_receives_async_msg(ofconn
, OAM_PORT_STATUS
, reason
)) {
1657 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1658 * generate OFPT_PORT_STATUS messages. That requirement was a
1659 * relic of how OpenFlow originally supported a single controller,
1660 * so that one could expect the controller to already know the
1661 * changes it had made.
1663 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1664 * OFPT_PORT_STATUS messages to every controller. This is
1665 * obviously more useful in the multi-controller case. We could
1666 * always implement it that way in OVS, but that would risk
1667 * confusing controllers that are intended for single-controller
1668 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1669 * in response to any OFPT_PORT_STATUS!)
1671 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1672 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1673 * originating controller. In a single-controller environment, in
1674 * particular, this means that it will never generate
1675 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1676 if (ofconn
== source
1677 && rconn_get_version(ofconn
->rconn
) < OFP15_VERSION
) {
1681 msg
= ofputil_encode_port_status(&ps
, ofconn_get_protocol(ofconn
));
1682 ofconn_send(ofconn
, msg
, NULL
);
1687 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1688 * appropriate controllers managed by 'mgr'. */
1690 connmgr_send_flow_removed(struct connmgr
*mgr
,
1691 const struct ofputil_flow_removed
*fr
)
1693 struct ofconn
*ofconn
;
1695 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1696 if (ofconn_receives_async_msg(ofconn
, OAM_FLOW_REMOVED
, fr
->reason
)) {
1699 /* Account flow expirations as replies to OpenFlow requests. That
1700 * works because preventing OpenFlow requests from being processed
1701 * also prevents new flows from being added (and expiring). (It
1702 * also prevents processing OpenFlow requests that would not add
1703 * new flows, so it is imperfect.) */
1704 msg
= ofputil_encode_flow_removed(fr
, ofconn_get_protocol(ofconn
));
1705 ofconn_send_reply(ofconn
, msg
);
1710 /* Normally a send-to-controller action uses reason OFPR_ACTION. However, in
1711 * OpenFlow 1.3 and later, packet_ins generated by a send-to-controller action
1712 * in a "table-miss" flow (one with priority 0 and completely wildcarded) are
1713 * sent as OFPR_NO_MATCH. This function returns the reason that should
1714 * actually be sent on 'ofconn' for 'pin'. */
1715 static enum ofp_packet_in_reason
1716 wire_reason(struct ofconn
*ofconn
, const struct ofproto_packet_in
*pin
)
1718 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1720 if (pin
->miss_type
== OFPROTO_PACKET_IN_MISS_FLOW
1721 && pin
->up
.reason
== OFPR_ACTION
1722 && protocol
!= OFPUTIL_P_NONE
1723 && ofputil_protocol_to_ofp_version(protocol
) >= OFP13_VERSION
) {
1724 return OFPR_NO_MATCH
;
1727 switch (pin
->up
.reason
) {
1728 case OFPR_ACTION_SET
:
1730 case OFPR_PACKET_OUT
:
1731 if (!(protocol
& OFPUTIL_P_OF14_UP
)) {
1732 /* Only supported in OF1.4+ */
1738 case OFPR_INVALID_TTL
:
1739 case OFPR_N_REASONS
:
1741 return pin
->up
.reason
;
1745 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1746 * necessary according to their individual configurations.
1748 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
1750 connmgr_send_packet_in(struct connmgr
*mgr
,
1751 const struct ofproto_packet_in
*pin
)
1753 struct ofconn
*ofconn
;
1755 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1756 enum ofp_packet_in_reason reason
= wire_reason(ofconn
, pin
);
1758 if (ofconn_wants_packet_in_on_miss(ofconn
, pin
)
1759 && ofconn_receives_async_msg(ofconn
, OAM_PACKET_IN
, reason
)
1760 && ofconn
->controller_id
== pin
->controller_id
) {
1761 schedule_packet_in(ofconn
, *pin
, reason
);
1767 do_send_packet_ins(struct ofconn
*ofconn
, struct ovs_list
*txq
)
1771 LIST_FOR_EACH_POP (pin
, list_node
, txq
) {
1772 if (rconn_send_with_limit(ofconn
->rconn
, pin
,
1773 ofconn
->packet_in_counter
, 100) == EAGAIN
) {
1774 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
1776 VLOG_INFO_RL(&rl
, "%s: dropping packet-in due to queue overflow",
1777 rconn_get_name(ofconn
->rconn
));
1782 /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1783 * to 'ofconn''s packet scheduler for sending. */
1785 schedule_packet_in(struct ofconn
*ofconn
, struct ofproto_packet_in pin
,
1786 enum ofp_packet_in_reason wire_reason
)
1788 struct connmgr
*mgr
= ofconn
->connmgr
;
1789 uint16_t controller_max_len
;
1790 struct ovs_list txq
;
1792 pin
.up
.total_len
= pin
.up
.packet_len
;
1794 pin
.up
.reason
= wire_reason
;
1795 if (pin
.up
.reason
== OFPR_ACTION
) {
1796 controller_max_len
= pin
.send_len
; /* max_len */
1798 controller_max_len
= ofconn
->miss_send_len
;
1801 /* Get OpenFlow buffer_id.
1802 * For OpenFlow 1.2+, OFPCML_NO_BUFFER (== UINT16_MAX) specifies
1803 * unbuffered. This behaviour doesn't violate prior versions, too. */
1804 if (controller_max_len
== UINT16_MAX
) {
1805 pin
.up
.buffer_id
= UINT32_MAX
;
1806 } else if (mgr
->fail_open
&& fail_open_is_active(mgr
->fail_open
)) {
1807 pin
.up
.buffer_id
= pktbuf_get_null();
1808 } else if (!ofconn
->pktbuf
) {
1809 pin
.up
.buffer_id
= UINT32_MAX
;
1811 pin
.up
.buffer_id
= pktbuf_save(ofconn
->pktbuf
,
1812 pin
.up
.packet
, pin
.up
.packet_len
,
1813 pin
.up
.flow_metadata
.flow
.in_port
.ofp_port
);
1816 /* Figure out how much of the packet to send.
1817 * If not buffered, send the entire packet. Otherwise, depending on
1818 * the reason of packet-in, send what requested by the controller. */
1819 if (pin
.up
.buffer_id
!= UINT32_MAX
1820 && controller_max_len
< pin
.up
.packet_len
) {
1821 pin
.up
.packet_len
= controller_max_len
;
1824 /* Make OFPT_PACKET_IN and hand over to packet scheduler. */
1825 pinsched_send(ofconn
->schedulers
[pin
.up
.reason
== OFPR_NO_MATCH
? 0 : 1],
1826 pin
.up
.flow_metadata
.flow
.in_port
.ofp_port
,
1827 ofputil_encode_packet_in(&pin
.up
,
1828 ofconn_get_protocol(ofconn
),
1829 ofconn
->packet_in_format
),
1831 do_send_packet_ins(ofconn
, &txq
);
1834 /* Fail-open settings. */
1836 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1837 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1838 enum ofproto_fail_mode
1839 connmgr_get_fail_mode(const struct connmgr
*mgr
)
1841 return mgr
->fail_mode
;
1844 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1845 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1847 connmgr_set_fail_mode(struct connmgr
*mgr
, enum ofproto_fail_mode fail_mode
)
1849 if (mgr
->fail_mode
!= fail_mode
) {
1850 mgr
->fail_mode
= fail_mode
;
1851 update_fail_open(mgr
);
1852 if (!connmgr_has_controllers(mgr
)) {
1853 ofproto_flush_flows(mgr
->ofproto
);
1858 /* Fail-open implementation. */
1860 /* Returns the longest probe interval among the primary controllers configured
1861 * on 'mgr'. Returns 0 if there are no primary controllers. */
1863 connmgr_get_max_probe_interval(const struct connmgr
*mgr
)
1865 const struct ofconn
*ofconn
;
1866 int max_probe_interval
;
1868 max_probe_interval
= 0;
1869 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1870 int probe_interval
= rconn_get_probe_interval(ofconn
->rconn
);
1871 max_probe_interval
= MAX(max_probe_interval
, probe_interval
);
1873 return max_probe_interval
;
1876 /* Returns the number of seconds for which all of 'mgr's primary controllers
1877 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1879 connmgr_failure_duration(const struct connmgr
*mgr
)
1881 const struct ofconn
*ofconn
;
1882 int min_failure_duration
;
1884 if (!connmgr_has_controllers(mgr
)) {
1888 min_failure_duration
= INT_MAX
;
1889 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1890 int failure_duration
= rconn_failure_duration(ofconn
->rconn
);
1891 min_failure_duration
= MIN(min_failure_duration
, failure_duration
);
1893 return min_failure_duration
;
1896 /* Returns true if at least one primary controller is connected (regardless of
1897 * whether those controllers are believed to have authenticated and accepted
1898 * this switch), false if none of them are connected. */
1900 connmgr_is_any_controller_connected(const struct connmgr
*mgr
)
1902 const struct ofconn
*ofconn
;
1904 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1905 if (rconn_is_connected(ofconn
->rconn
)) {
1912 /* Returns true if at least one primary controller is believed to have
1913 * authenticated and accepted this switch, false otherwise. */
1915 connmgr_is_any_controller_admitted(const struct connmgr
*mgr
)
1917 const struct ofconn
*ofconn
;
1919 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1920 if (rconn_is_admitted(ofconn
->rconn
)) {
1927 /* In-band configuration. */
1929 static bool any_extras_changed(const struct connmgr
*,
1930 const struct sockaddr_in
*extras
, size_t n
);
1932 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1933 * in-band control should guarantee access, in the same way that in-band
1934 * control guarantees access to OpenFlow controllers. */
1936 connmgr_set_extra_in_band_remotes(struct connmgr
*mgr
,
1937 const struct sockaddr_in
*extras
, size_t n
)
1939 if (!any_extras_changed(mgr
, extras
, n
)) {
1943 free(mgr
->extra_in_band_remotes
);
1944 mgr
->n_extra_remotes
= n
;
1945 mgr
->extra_in_band_remotes
= xmemdup(extras
, n
* sizeof *extras
);
1947 update_in_band_remotes(mgr
);
1950 /* Sets the OpenFlow queue used by flows set up by in-band control on
1951 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1952 * flows will use the default queue. */
1954 connmgr_set_in_band_queue(struct connmgr
*mgr
, int queue_id
)
1956 if (queue_id
!= mgr
->in_band_queue
) {
1957 mgr
->in_band_queue
= queue_id
;
1958 update_in_band_remotes(mgr
);
1963 any_extras_changed(const struct connmgr
*mgr
,
1964 const struct sockaddr_in
*extras
, size_t n
)
1968 if (n
!= mgr
->n_extra_remotes
) {
1972 for (i
= 0; i
< n
; i
++) {
1973 const struct sockaddr_in
*old
= &mgr
->extra_in_band_remotes
[i
];
1974 const struct sockaddr_in
*new = &extras
[i
];
1976 if (old
->sin_addr
.s_addr
!= new->sin_addr
.s_addr
||
1977 old
->sin_port
!= new->sin_port
) {
1985 /* In-band implementation. */
1988 connmgr_has_in_band(struct connmgr
*mgr
)
1990 return mgr
->in_band
!= NULL
;
1993 /* Fail-open and in-band implementation. */
1995 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1996 * and standalone mode to re-create their flows.
1998 * In-band control has more sophisticated code that manages flows itself. */
2000 connmgr_flushed(struct connmgr
*mgr
)
2001 OVS_EXCLUDED(ofproto_mutex
)
2003 if (mgr
->fail_open
) {
2004 fail_open_flushed(mgr
->fail_open
);
2007 /* If there are no controllers and we're in standalone mode, set up a flow
2008 * that matches every packet and directs them to OFPP_NORMAL (which goes to
2009 * us). Otherwise, the switch is in secure mode and we won't pass any
2010 * traffic until a controller has been defined and it tells us to do so. */
2011 if (!connmgr_has_controllers(mgr
)
2012 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
2013 struct ofpbuf ofpacts
;
2016 ofpbuf_init(&ofpacts
, OFPACT_OUTPUT_SIZE
);
2017 ofpact_put_OUTPUT(&ofpacts
)->port
= OFPP_NORMAL
;
2018 ofpact_pad(&ofpacts
);
2020 match_init_catchall(&match
);
2021 ofproto_add_flow(mgr
->ofproto
, &match
, 0, ofpacts
.data
,
2024 ofpbuf_uninit(&ofpacts
);
2028 /* Returns the number of hidden rules created by the in-band and fail-open
2029 * implementations in table 0. (Subtracting this count from the number of
2030 * rules in the table 0 classifier, as maintained in struct oftable, yields
2031 * the number of flows that OVS should report via OpenFlow for table 0.) */
2033 connmgr_count_hidden_rules(const struct connmgr
*mgr
)
2037 n_hidden
+= in_band_count_rules(mgr
->in_band
);
2039 if (mgr
->fail_open
) {
2040 n_hidden
+= fail_open_count_rules(mgr
->fail_open
);
2045 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
2046 * otherwise a positive errno value.
2048 * ofservice_reconfigure() must be called to fully configure the new
2051 ofservice_create(struct connmgr
*mgr
, const char *target
,
2052 uint32_t allowed_versions
, uint8_t dscp
)
2054 struct ofservice
*ofservice
;
2055 struct pvconn
*pvconn
;
2058 error
= pvconn_open(target
, allowed_versions
, dscp
, &pvconn
);
2063 ofservice
= xzalloc(sizeof *ofservice
);
2064 hmap_insert(&mgr
->services
, &ofservice
->node
, hash_string(target
, 0));
2065 ofservice
->pvconn
= pvconn
;
2066 ofservice
->allowed_versions
= allowed_versions
;
2072 ofservice_destroy(struct connmgr
*mgr
, struct ofservice
*ofservice
)
2074 hmap_remove(&mgr
->services
, &ofservice
->node
);
2075 pvconn_close(ofservice
->pvconn
);
2080 ofservice_reconfigure(struct ofservice
*ofservice
,
2081 const struct ofproto_controller
*c
)
2083 ofservice
->probe_interval
= c
->probe_interval
;
2084 ofservice
->rate_limit
= c
->rate_limit
;
2085 ofservice
->burst_limit
= c
->burst_limit
;
2086 ofservice
->enable_async_msgs
= c
->enable_async_msgs
;
2087 ofservice
->dscp
= c
->dscp
;
2090 /* Finds and returns the ofservice within 'mgr' that has the given
2091 * 'target', or a null pointer if none exists. */
2092 static struct ofservice
*
2093 ofservice_lookup(struct connmgr
*mgr
, const char *target
)
2095 struct ofservice
*ofservice
;
2097 HMAP_FOR_EACH_WITH_HASH (ofservice
, node
, hash_string(target
, 0),
2099 if (!strcmp(pvconn_get_name(ofservice
->pvconn
), target
)) {
2106 /* Flow monitors (NXST_FLOW_MONITOR). */
2108 /* A counter incremented when something significant happens to an OpenFlow
2111 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
2112 * the current value (which is then incremented).
2114 * - When a rule is modified, its 'modify_seqno' is set to the current
2115 * value (which is then incremented).
2117 * Thus, by comparing an old value of monitor_seqno against a rule's
2118 * 'add_seqno', one can tell whether the rule was added before or after the old
2119 * value was read, and similarly for 'modify_seqno'.
2121 * 32 bits should normally be sufficient (and would be nice, to save space in
2122 * each rule) but then we'd have to have some special cases for wraparound.
2124 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
2126 static uint64_t monitor_seqno
= 1;
2128 COVERAGE_DEFINE(ofmonitor_pause
);
2129 COVERAGE_DEFINE(ofmonitor_resume
);
2132 ofmonitor_create(const struct ofputil_flow_monitor_request
*request
,
2133 struct ofconn
*ofconn
, struct ofmonitor
**monitorp
)
2134 OVS_REQUIRES(ofproto_mutex
)
2136 struct ofmonitor
*m
;
2140 m
= ofmonitor_lookup(ofconn
, request
->id
);
2142 return OFPERR_OFPMOFC_MONITOR_EXISTS
;
2145 m
= xmalloc(sizeof *m
);
2147 hmap_insert(&ofconn
->monitors
, &m
->ofconn_node
, hash_int(request
->id
, 0));
2148 m
->id
= request
->id
;
2149 m
->flags
= request
->flags
;
2150 m
->out_port
= request
->out_port
;
2151 m
->table_id
= request
->table_id
;
2152 minimatch_init(&m
->match
, &request
->match
);
2159 ofmonitor_lookup(struct ofconn
*ofconn
, uint32_t id
)
2160 OVS_REQUIRES(ofproto_mutex
)
2162 struct ofmonitor
*m
;
2164 HMAP_FOR_EACH_IN_BUCKET (m
, ofconn_node
, hash_int(id
, 0),
2165 &ofconn
->monitors
) {
2174 ofmonitor_destroy(struct ofmonitor
*m
)
2175 OVS_REQUIRES(ofproto_mutex
)
2178 minimatch_destroy(&m
->match
);
2179 hmap_remove(&m
->ofconn
->monitors
, &m
->ofconn_node
);
2185 ofmonitor_report(struct connmgr
*mgr
, struct rule
*rule
,
2186 enum nx_flow_update_event event
,
2187 enum ofp_flow_removed_reason reason
,
2188 const struct ofconn
*abbrev_ofconn
, ovs_be32 abbrev_xid
,
2189 const struct rule_actions
*old_actions
)
2190 OVS_REQUIRES(ofproto_mutex
)
2192 enum nx_flow_monitor_flags update
;
2193 struct ofconn
*ofconn
;
2195 if (rule_is_hidden(rule
)) {
2202 rule
->add_seqno
= rule
->modify_seqno
= monitor_seqno
++;
2206 update
= NXFMF_DELETE
;
2209 case NXFME_MODIFIED
:
2210 update
= NXFMF_MODIFY
;
2211 rule
->modify_seqno
= monitor_seqno
++;
2219 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2220 enum nx_flow_monitor_flags flags
= 0;
2221 struct ofmonitor
*m
;
2223 if (ofconn
->monitor_paused
) {
2224 /* Only send NXFME_DELETED notifications for flows that were added
2225 * before we paused. */
2226 if (event
!= NXFME_DELETED
2227 || rule
->add_seqno
> ofconn
->monitor_paused
) {
2232 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2233 if (m
->flags
& update
2234 && (m
->table_id
== 0xff || m
->table_id
== rule
->table_id
)
2235 && (ofproto_rule_has_out_port(rule
, m
->out_port
)
2237 && ofpacts_output_to_port(old_actions
->ofpacts
,
2238 old_actions
->ofpacts_len
,
2240 && cls_rule_is_loose_match(&rule
->cr
, &m
->match
)) {
2246 if (list_is_empty(&ofconn
->updates
)) {
2247 ofputil_start_flow_update(&ofconn
->updates
);
2248 ofconn
->sent_abbrev_update
= false;
2251 if (flags
& NXFMF_OWN
|| ofconn
!= abbrev_ofconn
2252 || ofconn
->monitor_paused
) {
2253 struct ofputil_flow_update fu
;
2257 fu
.reason
= event
== NXFME_DELETED
? reason
: 0;
2258 fu
.table_id
= rule
->table_id
;
2259 fu
.cookie
= rule
->flow_cookie
;
2260 minimatch_expand(&rule
->cr
.match
, &match
);
2262 fu
.priority
= rule
->cr
.priority
;
2264 ovs_mutex_lock(&rule
->mutex
);
2265 fu
.idle_timeout
= rule
->idle_timeout
;
2266 fu
.hard_timeout
= rule
->hard_timeout
;
2267 ovs_mutex_unlock(&rule
->mutex
);
2269 if (flags
& NXFMF_ACTIONS
) {
2270 const struct rule_actions
*actions
= rule_get_actions(rule
);
2271 fu
.ofpacts
= actions
->ofpacts
;
2272 fu
.ofpacts_len
= actions
->ofpacts_len
;
2277 ofputil_append_flow_update(&fu
, &ofconn
->updates
);
2278 } else if (!ofconn
->sent_abbrev_update
) {
2279 struct ofputil_flow_update fu
;
2281 fu
.event
= NXFME_ABBREV
;
2282 fu
.xid
= abbrev_xid
;
2283 ofputil_append_flow_update(&fu
, &ofconn
->updates
);
2285 ofconn
->sent_abbrev_update
= true;
2292 ofmonitor_flush(struct connmgr
*mgr
)
2293 OVS_REQUIRES(ofproto_mutex
)
2295 struct ofconn
*ofconn
;
2297 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2300 LIST_FOR_EACH_POP (msg
, list_node
, &ofconn
->updates
) {
2301 unsigned int n_bytes
;
2303 ofconn_send(ofconn
, msg
, ofconn
->monitor_counter
);
2304 n_bytes
= rconn_packet_counter_n_bytes(ofconn
->monitor_counter
);
2305 if (!ofconn
->monitor_paused
&& n_bytes
> 128 * 1024) {
2306 struct ofpbuf
*pause
;
2308 COVERAGE_INC(ofmonitor_pause
);
2309 ofconn
->monitor_paused
= monitor_seqno
++;
2310 pause
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED
,
2311 OFP10_VERSION
, htonl(0), 0);
2312 ofconn_send(ofconn
, pause
, ofconn
->monitor_counter
);
2319 ofmonitor_resume(struct ofconn
*ofconn
)
2320 OVS_REQUIRES(ofproto_mutex
)
2322 struct rule_collection rules
;
2323 struct ofpbuf
*resumed
;
2324 struct ofmonitor
*m
;
2325 struct ovs_list msgs
;
2327 rule_collection_init(&rules
);
2328 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2329 ofmonitor_collect_resume_rules(m
, ofconn
->monitor_paused
, &rules
);
2333 ofmonitor_compose_refresh_updates(&rules
, &msgs
);
2335 resumed
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED
, OFP10_VERSION
,
2337 list_push_back(&msgs
, &resumed
->list_node
);
2338 ofconn_send_replies(ofconn
, &msgs
);
2340 ofconn
->monitor_paused
= 0;
2344 ofmonitor_may_resume(const struct ofconn
*ofconn
)
2345 OVS_REQUIRES(ofproto_mutex
)
2347 return (ofconn
->monitor_paused
!= 0
2348 && !rconn_packet_counter_n_packets(ofconn
->monitor_counter
));
2352 ofmonitor_run(struct connmgr
*mgr
)
2354 struct ofconn
*ofconn
;
2356 ovs_mutex_lock(&ofproto_mutex
);
2357 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2358 if (ofmonitor_may_resume(ofconn
)) {
2359 COVERAGE_INC(ofmonitor_resume
);
2360 ofmonitor_resume(ofconn
);
2363 ovs_mutex_unlock(&ofproto_mutex
);
2367 ofmonitor_wait(struct connmgr
*mgr
)
2369 struct ofconn
*ofconn
;
2371 ovs_mutex_lock(&ofproto_mutex
);
2372 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2373 if (ofmonitor_may_resume(ofconn
)) {
2374 poll_immediate_wake();
2377 ovs_mutex_unlock(&ofproto_mutex
);