2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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"
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 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 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 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
; /* Controller "struct ofconn"s. */
199 struct list all_conns
; /* Contains "struct ofconn"s. */
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 packets
+= pinsched_count_txqlen(ofconn
->schedulers
[i
]);
419 packets
+= pktbuf_count_packets(ofconn
->pktbuf
);
421 simap_increase(usage
, "ofconns", ofconns
);
422 simap_increase(usage
, "packets", packets
);
425 /* Returns the ofproto that owns 'ofconn''s connmgr. */
427 ofconn_get_ofproto(const struct ofconn
*ofconn
)
429 return ofconn
->connmgr
->ofproto
;
432 /* OpenFlow configuration. */
434 static void add_controller(struct connmgr
*, const char *target
, uint8_t dscp
,
435 uint32_t allowed_versions
)
436 OVS_REQUIRES(ofproto_mutex
);
437 static struct ofconn
*find_controller_by_target(struct connmgr
*,
439 static void update_fail_open(struct connmgr
*) OVS_EXCLUDED(ofproto_mutex
);
440 static int set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
441 const struct sset
*);
443 /* Returns true if 'mgr' has any configured primary controllers.
445 * Service controllers do not count, but configured primary controllers do
446 * count whether or not they are currently connected. */
448 connmgr_has_controllers(const struct connmgr
*mgr
)
450 return !hmap_is_empty(&mgr
->controllers
);
453 /* Initializes 'info' and populates it with information about each configured
454 * primary controller. The keys in 'info' are the controllers' targets; the
455 * data values are corresponding "struct ofproto_controller_info".
457 * The caller owns 'info' and everything in it and should free it when it is no
460 connmgr_get_controller_info(struct connmgr
*mgr
, struct shash
*info
)
462 const struct ofconn
*ofconn
;
464 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
465 const struct rconn
*rconn
= ofconn
->rconn
;
466 const char *target
= rconn_get_target(rconn
);
468 if (!shash_find(info
, target
)) {
469 struct ofproto_controller_info
*cinfo
= xmalloc(sizeof *cinfo
);
470 time_t now
= time_now();
471 time_t last_connection
= rconn_get_last_connection(rconn
);
472 time_t last_disconnect
= rconn_get_last_disconnect(rconn
);
473 int last_error
= rconn_get_last_error(rconn
);
475 shash_add(info
, target
, cinfo
);
477 cinfo
->is_connected
= rconn_is_connected(rconn
);
478 cinfo
->role
= ofconn
->role
;
483 cinfo
->pairs
.keys
[cinfo
->pairs
.n
] = "last_error";
484 cinfo
->pairs
.values
[cinfo
->pairs
.n
++]
485 = xstrdup(ovs_retval_to_string(last_error
));
488 cinfo
->pairs
.keys
[cinfo
->pairs
.n
] = "state";
489 cinfo
->pairs
.values
[cinfo
->pairs
.n
++]
490 = xstrdup(rconn_get_state(rconn
));
492 if (last_connection
!= TIME_MIN
) {
493 cinfo
->pairs
.keys
[cinfo
->pairs
.n
] = "sec_since_connect";
494 cinfo
->pairs
.values
[cinfo
->pairs
.n
++]
495 = xasprintf("%ld", (long int) (now
- last_connection
));
498 if (last_disconnect
!= TIME_MIN
) {
499 cinfo
->pairs
.keys
[cinfo
->pairs
.n
] = "sec_since_disconnect";
500 cinfo
->pairs
.values
[cinfo
->pairs
.n
++]
501 = xasprintf("%ld", (long int) (now
- last_disconnect
));
508 connmgr_free_controller_info(struct shash
*info
)
510 struct shash_node
*node
;
512 SHASH_FOR_EACH (node
, info
) {
513 struct ofproto_controller_info
*cinfo
= node
->data
;
514 while (cinfo
->pairs
.n
) {
515 free(CONST_CAST(char *, cinfo
->pairs
.values
[--cinfo
->pairs
.n
]));
522 /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in
525 connmgr_set_controllers(struct connmgr
*mgr
,
526 const struct ofproto_controller
*controllers
,
527 size_t n_controllers
, uint32_t allowed_versions
)
528 OVS_EXCLUDED(ofproto_mutex
)
530 bool had_controllers
= connmgr_has_controllers(mgr
);
531 struct shash new_controllers
;
532 struct ofconn
*ofconn
, *next_ofconn
;
533 struct ofservice
*ofservice
, *next_ofservice
;
536 /* Required to add and remove ofconns. This could probably be narrowed to
537 * cover a smaller amount of code, if that yielded some benefit. */
538 ovs_mutex_lock(&ofproto_mutex
);
540 /* Create newly configured controllers and services.
541 * Create a name to ofproto_controller mapping in 'new_controllers'. */
542 shash_init(&new_controllers
);
543 for (i
= 0; i
< n_controllers
; i
++) {
544 const struct ofproto_controller
*c
= &controllers
[i
];
546 if (!vconn_verify_name(c
->target
)) {
548 ofconn
= find_controller_by_target(mgr
, c
->target
);
550 VLOG_INFO("%s: added primary controller \"%s\"",
551 mgr
->name
, c
->target
);
553 } else if (rconn_get_allowed_versions(ofconn
->rconn
) !=
555 VLOG_INFO("%s: re-added primary controller \"%s\"",
556 mgr
->name
, c
->target
);
558 ofconn_destroy(ofconn
);
561 add_controller(mgr
, c
->target
, c
->dscp
, allowed_versions
);
563 } else if (!pvconn_verify_name(c
->target
)) {
565 ofservice
= ofservice_lookup(mgr
, c
->target
);
567 VLOG_INFO("%s: added service controller \"%s\"",
568 mgr
->name
, c
->target
);
570 } else if (ofservice
->allowed_versions
!= allowed_versions
) {
571 VLOG_INFO("%s: re-added service controller \"%s\"",
572 mgr
->name
, c
->target
);
573 ofservice_destroy(mgr
, ofservice
);
577 ofservice_create(mgr
, c
->target
, allowed_versions
, c
->dscp
);
580 VLOG_WARN_RL(&rl
, "%s: unsupported controller \"%s\"",
581 mgr
->name
, c
->target
);
585 shash_add_once(&new_controllers
, c
->target
, &controllers
[i
]);
588 /* Delete controllers that are no longer configured.
589 * Update configuration of all now-existing controllers. */
590 HMAP_FOR_EACH_SAFE (ofconn
, next_ofconn
, hmap_node
, &mgr
->controllers
) {
591 const char *target
= ofconn_get_target(ofconn
);
592 struct ofproto_controller
*c
;
594 c
= shash_find_data(&new_controllers
, target
);
596 VLOG_INFO("%s: removed primary controller \"%s\"",
598 ofconn_destroy(ofconn
);
600 ofconn_reconfigure(ofconn
, c
);
604 /* Delete services that are no longer configured.
605 * Update configuration of all now-existing services. */
606 HMAP_FOR_EACH_SAFE (ofservice
, next_ofservice
, node
, &mgr
->services
) {
607 const char *target
= pvconn_get_name(ofservice
->pvconn
);
608 struct ofproto_controller
*c
;
610 c
= shash_find_data(&new_controllers
, target
);
612 VLOG_INFO("%s: removed service controller \"%s\"",
614 ofservice_destroy(mgr
, ofservice
);
616 ofservice_reconfigure(ofservice
, c
);
620 shash_destroy(&new_controllers
);
622 ovs_mutex_unlock(&ofproto_mutex
);
624 update_in_band_remotes(mgr
);
625 update_fail_open(mgr
);
626 if (had_controllers
!= connmgr_has_controllers(mgr
)) {
627 ofproto_flush_flows(mgr
->ofproto
);
631 /* Drops the connections between 'mgr' and all of its primary and secondary
632 * controllers, forcing them to reconnect. */
634 connmgr_reconnect(const struct connmgr
*mgr
)
636 struct ofconn
*ofconn
;
638 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
639 rconn_reconnect(ofconn
->rconn
);
643 /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'.
645 * A "snoop" is a pvconn to which every OpenFlow message to or from the most
646 * important controller on 'mgr' is mirrored. */
648 connmgr_set_snoops(struct connmgr
*mgr
, const struct sset
*snoops
)
650 return set_pvconns(&mgr
->snoops
, &mgr
->n_snoops
, snoops
);
653 /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */
655 connmgr_get_snoops(const struct connmgr
*mgr
, struct sset
*snoops
)
659 for (i
= 0; i
< mgr
->n_snoops
; i
++) {
660 sset_add(snoops
, pvconn_get_name(mgr
->snoops
[i
]));
664 /* Returns true if 'mgr' has at least one snoop, false if it has none. */
666 connmgr_has_snoops(const struct connmgr
*mgr
)
668 return mgr
->n_snoops
> 0;
671 /* Creates a new controller for 'target' in 'mgr'. update_controller() needs
672 * to be called later to finish the new ofconn's configuration. */
674 add_controller(struct connmgr
*mgr
, const char *target
, uint8_t dscp
,
675 uint32_t allowed_versions
)
676 OVS_REQUIRES(ofproto_mutex
)
678 char *name
= ofconn_make_name(mgr
, target
);
679 struct ofconn
*ofconn
;
681 ofconn
= ofconn_create(mgr
, rconn_create(5, 8, dscp
, allowed_versions
),
682 OFCONN_PRIMARY
, true);
683 ofconn
->pktbuf
= pktbuf_create();
684 rconn_connect(ofconn
->rconn
, target
, name
);
685 hmap_insert(&mgr
->controllers
, &ofconn
->hmap_node
, hash_string(target
, 0));
690 static struct ofconn
*
691 find_controller_by_target(struct connmgr
*mgr
, const char *target
)
693 struct ofconn
*ofconn
;
695 HMAP_FOR_EACH_WITH_HASH (ofconn
, hmap_node
,
696 hash_string(target
, 0), &mgr
->controllers
) {
697 if (!strcmp(ofconn_get_target(ofconn
), target
)) {
705 update_in_band_remotes(struct connmgr
*mgr
)
707 struct sockaddr_in
*addrs
;
708 size_t max_addrs
, n_addrs
;
709 struct ofconn
*ofconn
;
712 /* Allocate enough memory for as many remotes as we could possibly have. */
713 max_addrs
= mgr
->n_extra_remotes
+ hmap_count(&mgr
->controllers
);
714 addrs
= xmalloc(max_addrs
* sizeof *addrs
);
717 /* Add all the remotes. */
718 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
719 const char *target
= rconn_get_target(ofconn
->rconn
);
720 struct sockaddr_storage ss
;
722 if (ofconn
->band
== OFPROTO_IN_BAND
723 && stream_parse_target_with_default_port(target
, OFP_OLD_PORT
, &ss
)
724 && ss
.ss_family
== AF_INET
) {
725 addrs
[n_addrs
++] = *(struct sockaddr_in
*) &ss
;
728 for (i
= 0; i
< mgr
->n_extra_remotes
; i
++) {
729 addrs
[n_addrs
++] = mgr
->extra_in_band_remotes
[i
];
732 /* Create or update or destroy in-band. */
735 in_band_create(mgr
->ofproto
, mgr
->local_port_name
, &mgr
->in_band
);
737 in_band_set_queue(mgr
->in_band
, mgr
->in_band_queue
);
739 /* in_band_run() needs a chance to delete any existing in-band flows.
740 * We will destroy mgr->in_band after it's done with that. */
743 in_band_set_remotes(mgr
->in_band
, addrs
, n_addrs
);
751 update_fail_open(struct connmgr
*mgr
)
752 OVS_EXCLUDED(ofproto_mutex
)
754 if (connmgr_has_controllers(mgr
)
755 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
756 if (!mgr
->fail_open
) {
757 mgr
->fail_open
= fail_open_create(mgr
->ofproto
, mgr
);
760 fail_open_destroy(mgr
->fail_open
);
761 mgr
->fail_open
= NULL
;
766 set_pvconns(struct pvconn
***pvconnsp
, size_t *n_pvconnsp
,
767 const struct sset
*sset
)
769 struct pvconn
**pvconns
= *pvconnsp
;
770 size_t n_pvconns
= *n_pvconnsp
;
775 for (i
= 0; i
< n_pvconns
; i
++) {
776 pvconn_close(pvconns
[i
]);
780 pvconns
= xmalloc(sset_count(sset
) * sizeof *pvconns
);
782 SSET_FOR_EACH (name
, sset
) {
783 struct pvconn
*pvconn
;
785 error
= pvconn_open(name
, 0, 0, &pvconn
);
787 pvconns
[n_pvconns
++] = pvconn
;
789 VLOG_ERR("failed to listen on %s: %s", name
, ovs_strerror(error
));
797 *n_pvconnsp
= n_pvconns
;
802 /* Returns a "preference level" for snooping 'ofconn'. A higher return value
803 * means that 'ofconn' is more interesting for monitoring than a lower return
806 snoop_preference(const struct ofconn
*ofconn
)
808 switch (ofconn
->role
) {
809 case OFPCR12_ROLE_MASTER
:
811 case OFPCR12_ROLE_EQUAL
:
813 case OFPCR12_ROLE_SLAVE
:
815 case OFPCR12_ROLE_NOCHANGE
:
817 /* Shouldn't happen. */
822 /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'.
823 * Connects this vconn to a controller. */
825 add_snooper(struct connmgr
*mgr
, struct vconn
*vconn
)
827 struct ofconn
*ofconn
, *best
;
829 /* Pick a controller for monitoring. */
831 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
832 if (ofconn
->type
== OFCONN_PRIMARY
833 && (!best
|| snoop_preference(ofconn
) > snoop_preference(best
))) {
839 rconn_add_monitor(best
->rconn
, vconn
);
841 VLOG_INFO_RL(&rl
, "no controller connection to snoop");
846 /* Public ofconn functions. */
848 /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */
850 ofconn_get_type(const struct ofconn
*ofconn
)
855 /* If a master election id is defined, stores it into '*idp' and returns
856 * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */
858 ofconn_get_master_election_id(const struct ofconn
*ofconn
, uint64_t *idp
)
860 *idp
= (ofconn
->connmgr
->master_election_id_defined
861 ? ofconn
->connmgr
->master_election_id
863 return ofconn
->connmgr
->master_election_id_defined
;
866 /* Sets the master election id.
868 * Returns true if successful, false if the id is stale
871 ofconn_set_master_election_id(struct ofconn
*ofconn
, uint64_t id
)
873 if (ofconn
->connmgr
->master_election_id_defined
875 /* Unsigned difference interpreted as a two's complement signed
877 (int64_t)(id
- ofconn
->connmgr
->master_election_id
) < 0) {
880 ofconn
->connmgr
->master_election_id
= id
;
881 ofconn
->connmgr
->master_election_id_defined
= true;
886 /* Returns the role configured for 'ofconn'.
888 * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */
889 enum ofp12_controller_role
890 ofconn_get_role(const struct ofconn
*ofconn
)
896 ofconn_send_role_status(struct ofconn
*ofconn
, uint32_t role
, uint8_t reason
)
898 struct ofputil_role_status status
;
901 status
.reason
= reason
;
903 ofconn_get_master_election_id(ofconn
, &status
.generation_id
);
905 buf
= ofputil_encode_role_status(&status
, ofconn_get_protocol(ofconn
));
907 ofconn_send(ofconn
, buf
, NULL
);
910 /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then
911 * any existing master is demoted to a slave. */
913 ofconn_set_role(struct ofconn
*ofconn
, enum ofp12_controller_role role
)
915 if (role
!= ofconn
->role
&& role
== OFPCR12_ROLE_MASTER
) {
916 struct ofconn
*other
;
918 HMAP_FOR_EACH (other
, hmap_node
, &ofconn
->connmgr
->controllers
) {
919 if (other
->role
== OFPCR12_ROLE_MASTER
) {
920 other
->role
= OFPCR12_ROLE_SLAVE
;
921 ofconn_send_role_status(other
, OFPCR12_ROLE_SLAVE
, OFPCRR_MASTER_REQUEST
);
929 ofconn_set_invalid_ttl_to_controller(struct ofconn
*ofconn
, bool enable
)
931 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
933 ofconn
->master_async_config
[OAM_PACKET_IN
] |= bit
;
935 ofconn
->master_async_config
[OAM_PACKET_IN
] &= ~bit
;
940 ofconn_get_invalid_ttl_to_controller(struct ofconn
*ofconn
)
942 uint32_t bit
= 1u << OFPR_INVALID_TTL
;
943 return (ofconn
->master_async_config
[OAM_PACKET_IN
] & bit
) != 0;
946 /* Returns the currently configured protocol for 'ofconn', one of OFPUTIL_P_*.
948 * Returns OFPUTIL_P_NONE, which is not a valid protocol, if 'ofconn' hasn't
949 * completed version negotiation. This can't happen if at least one OpenFlow
950 * message, other than OFPT_HELLO, has been received on the connection (such as
951 * in ofproto.c's message handling code), since version negotiation is a
952 * prerequisite for starting to receive messages. This means that
953 * OFPUTIL_P_NONE is a special case that most callers need not worry about. */
954 enum ofputil_protocol
955 ofconn_get_protocol(const struct ofconn
*ofconn
)
957 if (ofconn
->protocol
== OFPUTIL_P_NONE
&&
958 rconn_is_connected(ofconn
->rconn
)) {
959 int version
= rconn_get_version(ofconn
->rconn
);
961 ofconn_set_protocol(CONST_CAST(struct ofconn
*, ofconn
),
962 ofputil_protocol_from_ofp_version(version
));
966 return ofconn
->protocol
;
969 /* Sets the protocol for 'ofconn' to 'protocol' (one of OFPUTIL_P_*).
971 * (This doesn't actually send anything to accomplish this. Presumably the
972 * caller already did that.) */
974 ofconn_set_protocol(struct ofconn
*ofconn
, enum ofputil_protocol protocol
)
976 ofconn
->protocol
= protocol
;
979 /* Returns the currently configured packet in format for 'ofconn', one of
982 * The default, if no other format has been set, is NXPIF_OPENFLOW10. */
983 enum nx_packet_in_format
984 ofconn_get_packet_in_format(struct ofconn
*ofconn
)
986 return ofconn
->packet_in_format
;
989 /* Sets the packet in format for 'ofconn' to 'packet_in_format' (one of
992 ofconn_set_packet_in_format(struct ofconn
*ofconn
,
993 enum nx_packet_in_format packet_in_format
)
995 ofconn
->packet_in_format
= packet_in_format
;
998 /* Sets the controller connection ID for 'ofconn' to 'controller_id'.
1000 * The connection controller ID is used for OFPP_CONTROLLER and
1001 * NXAST_CONTROLLER actions. See "struct nx_action_controller" for details. */
1003 ofconn_set_controller_id(struct ofconn
*ofconn
, uint16_t controller_id
)
1005 ofconn
->controller_id
= controller_id
;
1008 /* Returns the default miss send length for 'ofconn'. */
1010 ofconn_get_miss_send_len(const struct ofconn
*ofconn
)
1012 return ofconn
->miss_send_len
;
1015 /* Sets the default miss send length for 'ofconn' to 'miss_send_len'. */
1017 ofconn_set_miss_send_len(struct ofconn
*ofconn
, int miss_send_len
)
1019 ofconn
->miss_send_len
= miss_send_len
;
1023 ofconn_set_async_config(struct ofconn
*ofconn
,
1024 const uint32_t master_masks
[OAM_N_TYPES
],
1025 const uint32_t slave_masks
[OAM_N_TYPES
])
1027 size_t size
= sizeof ofconn
->master_async_config
;
1028 memcpy(ofconn
->master_async_config
, master_masks
, size
);
1029 memcpy(ofconn
->slave_async_config
, slave_masks
, size
);
1033 ofconn_get_async_config(struct ofconn
*ofconn
,
1034 uint32_t *master_masks
, uint32_t *slave_masks
)
1036 size_t size
= sizeof ofconn
->master_async_config
;
1037 memcpy(master_masks
, ofconn
->master_async_config
, size
);
1038 memcpy(slave_masks
, ofconn
->slave_async_config
, size
);
1041 /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a
1042 * sufficient number of OpenFlow replies in-flight on a single ofconn, then the
1043 * connmgr will stop accepting new OpenFlow requests on that ofconn until the
1044 * controller has accepted some of the replies.) */
1046 ofconn_send_reply(const struct ofconn
*ofconn
, struct ofpbuf
*msg
)
1048 ofconn_send(ofconn
, msg
, ofconn
->reply_counter
);
1051 /* Sends each of the messages in list 'replies' on 'ofconn' in order,
1052 * accounting them as replies. */
1054 ofconn_send_replies(const struct ofconn
*ofconn
, struct list
*replies
)
1056 struct ofpbuf
*reply
, *next
;
1058 LIST_FOR_EACH_SAFE (reply
, next
, list_node
, replies
) {
1059 list_remove(&reply
->list_node
);
1060 ofconn_send_reply(ofconn
, reply
);
1064 /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the
1065 * first 64 bytes of 'request' are used. */
1067 ofconn_send_error(const struct ofconn
*ofconn
,
1068 const struct ofp_header
*request
, enum ofperr error
)
1070 static struct vlog_rate_limit err_rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1071 struct ofpbuf
*reply
;
1073 reply
= ofperr_encode_reply(error
, request
);
1074 if (!VLOG_DROP_INFO(&err_rl
)) {
1075 const char *type_name
;
1079 request_len
= ntohs(request
->length
);
1080 type_name
= (!ofpraw_decode_partial(&raw
, request
,
1081 MIN(64, request_len
))
1082 ? ofpraw_get_name(raw
)
1085 VLOG_INFO("%s: sending %s error reply to %s message",
1086 rconn_get_name(ofconn
->rconn
), ofperr_to_string(error
),
1089 ofconn_send_reply(ofconn
, reply
);
1092 /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */
1094 ofconn_pktbuf_retrieve(struct ofconn
*ofconn
, uint32_t id
,
1095 struct ofpbuf
**bufferp
, ofp_port_t
*in_port
)
1097 return pktbuf_retrieve(ofconn
->pktbuf
, id
, bufferp
, in_port
);
1100 /* Reports that a flow_mod operation of the type specified by 'command' was
1101 * successfully executed by 'ofconn', so that the connmgr can log it. */
1103 ofconn_report_flow_mod(struct ofconn
*ofconn
,
1104 enum ofp_flow_mod_command command
)
1114 case OFPFC_MODIFY_STRICT
:
1119 case OFPFC_DELETE_STRICT
:
1125 if (ofconn
->next_op_report
== LLONG_MAX
) {
1126 ofconn
->first_op
= now
;
1127 ofconn
->next_op_report
= MAX(now
+ 10 * 1000, ofconn
->op_backoff
);
1128 ofconn
->op_backoff
= ofconn
->next_op_report
+ 60 * 1000;
1130 ofconn
->last_op
= now
;
1134 ofconn_get_bundles(struct ofconn
*ofconn
)
1136 return &ofconn
->bundles
;
1140 /* Private ofconn functions. */
1143 ofconn_get_target(const struct ofconn
*ofconn
)
1145 return rconn_get_target(ofconn
->rconn
);
1148 static struct ofconn
*
1149 ofconn_create(struct connmgr
*mgr
, struct rconn
*rconn
, enum ofconn_type type
,
1150 bool enable_async_msgs
)
1152 struct ofconn
*ofconn
;
1154 ofconn
= xzalloc(sizeof *ofconn
);
1155 ofconn
->connmgr
= mgr
;
1156 list_push_back(&mgr
->all_conns
, &ofconn
->node
);
1157 ofconn
->rconn
= rconn
;
1158 ofconn
->type
= type
;
1159 ofconn
->enable_async_msgs
= enable_async_msgs
;
1161 hmap_init(&ofconn
->monitors
);
1162 list_init(&ofconn
->updates
);
1164 hmap_init(&ofconn
->bundles
);
1166 ofconn_flush(ofconn
);
1171 /* Clears all of the state in 'ofconn' that should not persist from one
1172 * connection to the next. */
1174 ofconn_flush(struct ofconn
*ofconn
)
1175 OVS_REQUIRES(ofproto_mutex
)
1177 struct ofmonitor
*monitor
, *next_monitor
;
1180 ofconn_log_flow_mods(ofconn
);
1182 ofconn
->role
= OFPCR12_ROLE_EQUAL
;
1183 ofconn_set_protocol(ofconn
, OFPUTIL_P_NONE
);
1184 ofconn
->packet_in_format
= NXPIF_OPENFLOW10
;
1186 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1187 ofconn
->packet_in_counter
= rconn_packet_counter_create();
1188 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1189 if (ofconn
->schedulers
[i
]) {
1192 pinsched_get_limits(ofconn
->schedulers
[i
], &rate
, &burst
);
1193 pinsched_destroy(ofconn
->schedulers
[i
]);
1194 ofconn
->schedulers
[i
] = pinsched_create(rate
, burst
);
1197 if (ofconn
->pktbuf
) {
1198 pktbuf_destroy(ofconn
->pktbuf
);
1199 ofconn
->pktbuf
= pktbuf_create();
1201 ofconn
->miss_send_len
= (ofconn
->type
== OFCONN_PRIMARY
1202 ? OFP_DEFAULT_MISS_SEND_LEN
1204 ofconn
->controller_id
= 0;
1206 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1207 ofconn
->reply_counter
= rconn_packet_counter_create();
1209 if (ofconn
->enable_async_msgs
) {
1210 uint32_t *master
= ofconn
->master_async_config
;
1211 uint32_t *slave
= ofconn
->slave_async_config
;
1213 /* "master" and "other" roles get all asynchronous messages by default,
1214 * except that the controller needs to enable nonstandard "packet-in"
1215 * reasons itself. */
1216 master
[OAM_PACKET_IN
] = (1u << OFPR_NO_MATCH
) | (1u << OFPR_ACTION
);
1217 master
[OAM_PORT_STATUS
] = ((1u << OFPPR_ADD
)
1218 | (1u << OFPPR_DELETE
)
1219 | (1u << OFPPR_MODIFY
));
1220 master
[OAM_FLOW_REMOVED
] = ((1u << OFPRR_IDLE_TIMEOUT
)
1221 | (1u << OFPRR_HARD_TIMEOUT
)
1222 | (1u << OFPRR_DELETE
));
1224 /* "slave" role gets port status updates by default. */
1225 slave
[OAM_PACKET_IN
] = 0;
1226 slave
[OAM_PORT_STATUS
] = ((1u << OFPPR_ADD
)
1227 | (1u << OFPPR_DELETE
)
1228 | (1u << OFPPR_MODIFY
));
1229 slave
[OAM_FLOW_REMOVED
] = 0;
1231 memset(ofconn
->master_async_config
, 0,
1232 sizeof ofconn
->master_async_config
);
1233 memset(ofconn
->slave_async_config
, 0,
1234 sizeof ofconn
->slave_async_config
);
1237 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1238 ofconn
->first_op
= ofconn
->last_op
= LLONG_MIN
;
1239 ofconn
->next_op_report
= LLONG_MAX
;
1240 ofconn
->op_backoff
= LLONG_MIN
;
1242 HMAP_FOR_EACH_SAFE (monitor
, next_monitor
, ofconn_node
,
1243 &ofconn
->monitors
) {
1244 ofmonitor_destroy(monitor
);
1246 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1247 ofconn
->monitor_counter
= rconn_packet_counter_create();
1248 ofpbuf_list_delete(&ofconn
->updates
); /* ...but it should be empty. */
1252 ofconn_destroy(struct ofconn
*ofconn
)
1253 OVS_REQUIRES(ofproto_mutex
)
1255 ofconn_flush(ofconn
);
1257 if (ofconn
->type
== OFCONN_PRIMARY
) {
1258 hmap_remove(&ofconn
->connmgr
->controllers
, &ofconn
->hmap_node
);
1261 ofp_bundle_remove_all(ofconn
);
1263 hmap_destroy(&ofconn
->monitors
);
1264 list_remove(&ofconn
->node
);
1265 rconn_destroy(ofconn
->rconn
);
1266 rconn_packet_counter_destroy(ofconn
->packet_in_counter
);
1267 rconn_packet_counter_destroy(ofconn
->reply_counter
);
1268 pktbuf_destroy(ofconn
->pktbuf
);
1269 rconn_packet_counter_destroy(ofconn
->monitor_counter
);
1273 /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same
1276 ofconn_reconfigure(struct ofconn
*ofconn
, const struct ofproto_controller
*c
)
1280 ofconn
->band
= c
->band
;
1281 ofconn
->enable_async_msgs
= c
->enable_async_msgs
;
1283 rconn_set_max_backoff(ofconn
->rconn
, c
->max_backoff
);
1285 probe_interval
= c
->probe_interval
? MAX(c
->probe_interval
, 5) : 0;
1286 rconn_set_probe_interval(ofconn
->rconn
, probe_interval
);
1288 ofconn_set_rate_limit(ofconn
, c
->rate_limit
, c
->burst_limit
);
1290 /* If dscp value changed reconnect. */
1291 if (c
->dscp
!= rconn_get_dscp(ofconn
->rconn
)) {
1292 rconn_set_dscp(ofconn
->rconn
, c
->dscp
);
1293 rconn_reconnect(ofconn
->rconn
);
1297 /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow
1300 ofconn_may_recv(const struct ofconn
*ofconn
)
1302 int count
= rconn_packet_counter_n_packets(ofconn
->reply_counter
);
1303 return count
< OFCONN_REPLY_MAX
;
1307 ofconn_run(struct ofconn
*ofconn
,
1308 void (*handle_openflow
)(struct ofconn
*,
1309 const struct ofpbuf
*ofp_msg
))
1311 struct connmgr
*mgr
= ofconn
->connmgr
;
1314 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1317 pinsched_run(ofconn
->schedulers
[i
], &txq
);
1318 do_send_packet_ins(ofconn
, &txq
);
1321 rconn_run(ofconn
->rconn
);
1323 /* Limit the number of iterations to avoid starving other tasks. */
1324 for (i
= 0; i
< 50 && ofconn_may_recv(ofconn
); i
++) {
1325 struct ofpbuf
*of_msg
= rconn_recv(ofconn
->rconn
);
1330 if (mgr
->fail_open
) {
1331 fail_open_maybe_recover(mgr
->fail_open
);
1334 handle_openflow(ofconn
, of_msg
);
1335 ofpbuf_delete(of_msg
);
1338 if (time_msec() >= ofconn
->next_op_report
) {
1339 ofconn_log_flow_mods(ofconn
);
1342 ovs_mutex_lock(&ofproto_mutex
);
1343 if (!rconn_is_alive(ofconn
->rconn
)) {
1344 ofconn_destroy(ofconn
);
1345 } else if (!rconn_is_connected(ofconn
->rconn
)) {
1346 ofconn_flush(ofconn
);
1348 ovs_mutex_unlock(&ofproto_mutex
);
1352 ofconn_wait(struct ofconn
*ofconn
)
1356 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1357 pinsched_wait(ofconn
->schedulers
[i
]);
1359 rconn_run_wait(ofconn
->rconn
);
1360 if (ofconn_may_recv(ofconn
)) {
1361 rconn_recv_wait(ofconn
->rconn
);
1363 if (ofconn
->next_op_report
!= LLONG_MAX
) {
1364 poll_timer_wait_until(ofconn
->next_op_report
);
1369 ofconn_log_flow_mods(struct ofconn
*ofconn
)
1371 int n_flow_mods
= ofconn
->n_add
+ ofconn
->n_delete
+ ofconn
->n_modify
;
1373 long long int ago
= (time_msec() - ofconn
->first_op
) / 1000;
1374 long long int interval
= (ofconn
->last_op
- ofconn
->first_op
) / 1000;
1378 ds_put_format(&s
, "%d flow_mods ", n_flow_mods
);
1379 if (interval
== ago
) {
1380 ds_put_format(&s
, "in the last %lld s", ago
);
1381 } else if (interval
) {
1382 ds_put_format(&s
, "in the %lld s starting %lld s ago",
1385 ds_put_format(&s
, "%lld s ago", ago
);
1388 ds_put_cstr(&s
, " (");
1389 if (ofconn
->n_add
) {
1390 ds_put_format(&s
, "%d adds, ", ofconn
->n_add
);
1392 if (ofconn
->n_delete
) {
1393 ds_put_format(&s
, "%d deletes, ", ofconn
->n_delete
);
1395 if (ofconn
->n_modify
) {
1396 ds_put_format(&s
, "%d modifications, ", ofconn
->n_modify
);
1399 ds_put_char(&s
, ')');
1401 VLOG_INFO("%s: %s", rconn_get_name(ofconn
->rconn
), ds_cstr(&s
));
1404 ofconn
->n_add
= ofconn
->n_delete
= ofconn
->n_modify
= 0;
1406 ofconn
->next_op_report
= LLONG_MAX
;
1409 /* Returns true if 'ofconn' should receive asynchronous messages of the given
1410 * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN,
1411 * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for
1412 * OAM_FLOW_REMOVED. Returns false if the message should not be sent on
1415 ofconn_receives_async_msg(const struct ofconn
*ofconn
,
1416 enum ofconn_async_msg_type type
,
1417 unsigned int reason
)
1419 const uint32_t *async_config
;
1421 ovs_assert(reason
< 32);
1422 ovs_assert((unsigned int) type
< OAM_N_TYPES
);
1424 if (ofconn_get_protocol(ofconn
) == OFPUTIL_P_NONE
1425 || !rconn_is_connected(ofconn
->rconn
)) {
1429 /* Keep the following code in sync with the documentation in the
1430 * "Asynchronous Messages" section in DESIGN. */
1432 if (ofconn
->type
== OFCONN_SERVICE
&& !ofconn
->miss_send_len
) {
1433 /* Service connections don't get asynchronous messages unless they have
1434 * explicitly asked for them by setting a nonzero miss send length. */
1438 async_config
= (ofconn
->role
== OFPCR12_ROLE_SLAVE
1439 ? ofconn
->slave_async_config
1440 : ofconn
->master_async_config
);
1441 if (!(async_config
[type
] & (1u << reason
))) {
1448 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1449 * packet rather than to send the packet to the controller.
1451 * This function returns false to indicate the packet should be dropped if
1452 * the controller action was the result of the default table-miss behaviour
1453 * and the controller is using OpenFlow1.3+.
1455 * Otherwise true is returned to indicate the packet should be forwarded to
1458 ofconn_wants_packet_in_on_miss(struct ofconn
*ofconn
,
1459 const struct ofproto_packet_in
*pin
)
1461 if (pin
->miss_type
== OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW
) {
1462 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1464 if (protocol
!= OFPUTIL_P_NONE
1465 && ofputil_protocol_to_ofp_version(protocol
) >= OFP13_VERSION
) {
1466 enum ofproto_table_config config
;
1468 config
= ofproto_table_get_config(ofconn
->connmgr
->ofproto
,
1470 if (config
== OFPROTO_TABLE_MISS_DEFAULT
) {
1478 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
1479 * packet rather than to send the packet to the controller.
1481 * This function returns false to indicate that a packet_in message
1482 * for a "table-miss" should be sent to at least one controller.
1483 * That is there is at least one controller with controller_id 0
1484 * which connected using an OpenFlow version earlier than OpenFlow1.3.
1488 * This logic assumes that "table-miss" packet_in messages
1489 * are always sent to controller_id 0. */
1491 connmgr_wants_packet_in_on_miss(struct connmgr
*mgr
)
1493 struct ofconn
*ofconn
;
1495 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1496 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1498 if (ofconn
->controller_id
== 0 &&
1499 (protocol
== OFPUTIL_P_NONE
||
1500 ofputil_protocol_to_ofp_version(protocol
) < OFP13_VERSION
)) {
1507 /* Returns a human-readable name for an OpenFlow connection between 'mgr' and
1508 * 'target', suitable for use in log messages for identifying the connection.
1510 * The name is dynamically allocated. The caller should free it (with free())
1511 * when it is no longer needed. */
1513 ofconn_make_name(const struct connmgr
*mgr
, const char *target
)
1515 return xasprintf("%s<->%s", mgr
->name
, target
);
1519 ofconn_set_rate_limit(struct ofconn
*ofconn
, int rate
, int burst
)
1523 for (i
= 0; i
< N_SCHEDULERS
; i
++) {
1524 struct pinsched
**s
= &ofconn
->schedulers
[i
];
1528 *s
= pinsched_create(rate
, burst
);
1530 pinsched_set_limits(*s
, rate
, burst
);
1533 pinsched_destroy(*s
);
1540 ofconn_send(const struct ofconn
*ofconn
, struct ofpbuf
*msg
,
1541 struct rconn_packet_counter
*counter
)
1543 ofpmsg_update_length(msg
);
1544 rconn_send(ofconn
->rconn
, msg
, counter
);
1547 /* Sending asynchronous messages. */
1549 static void schedule_packet_in(struct ofconn
*, struct ofproto_packet_in
,
1550 enum ofp_packet_in_reason wire_reason
);
1552 /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate
1553 * controllers managed by 'mgr'. For messages caused by a controller
1554 * OFPT_PORT_MOD, specify 'source' as the controller connection that sent the
1555 * request; otherwise, specify 'source' as NULL. */
1557 connmgr_send_port_status(struct connmgr
*mgr
, struct ofconn
*source
,
1558 const struct ofputil_phy_port
*pp
, uint8_t reason
)
1560 /* XXX Should limit the number of queued port status change messages. */
1561 struct ofputil_port_status ps
;
1562 struct ofconn
*ofconn
;
1566 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1567 if (ofconn_receives_async_msg(ofconn
, OAM_PORT_STATUS
, reason
)) {
1570 /* Before 1.5, OpenFlow specified that OFPT_PORT_MOD should not
1571 * generate OFPT_PORT_STATUS messages. That requirement was a
1572 * relic of how OpenFlow originally supported a single controller,
1573 * so that one could expect the controller to already know the
1574 * changes it had made.
1576 * EXT-338 changes OpenFlow 1.5 OFPT_PORT_MOD to send
1577 * OFPT_PORT_STATUS messages to every controller. This is
1578 * obviously more useful in the multi-controller case. We could
1579 * always implement it that way in OVS, but that would risk
1580 * confusing controllers that are intended for single-controller
1581 * use only. (Imagine a controller that generates an OFPT_PORT_MOD
1582 * in response to any OFPT_PORT_STATUS!)
1584 * So this compromises: for OpenFlow 1.4 and earlier, it generates
1585 * OFPT_PORT_STATUS for OFPT_PORT_MOD, but not back to the
1586 * originating controller. In a single-controller environment, in
1587 * particular, this means that it will never generate
1588 * OFPT_PORT_STATUS for OFPT_PORT_MOD at all. */
1589 if (ofconn
== source
1590 && rconn_get_version(ofconn
->rconn
) < OFP15_VERSION
) {
1594 msg
= ofputil_encode_port_status(&ps
, ofconn_get_protocol(ofconn
));
1595 ofconn_send(ofconn
, msg
, NULL
);
1600 /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to
1601 * appropriate controllers managed by 'mgr'. */
1603 connmgr_send_flow_removed(struct connmgr
*mgr
,
1604 const struct ofputil_flow_removed
*fr
)
1606 struct ofconn
*ofconn
;
1608 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1609 if (ofconn_receives_async_msg(ofconn
, OAM_FLOW_REMOVED
, fr
->reason
)) {
1612 /* Account flow expirations as replies to OpenFlow requests. That
1613 * works because preventing OpenFlow requests from being processed
1614 * also prevents new flows from being added (and expiring). (It
1615 * also prevents processing OpenFlow requests that would not add
1616 * new flows, so it is imperfect.) */
1617 msg
= ofputil_encode_flow_removed(fr
, ofconn_get_protocol(ofconn
));
1618 ofconn_send_reply(ofconn
, msg
);
1623 /* Normally a send-to-controller action uses reason OFPR_ACTION. However, in
1624 * OpenFlow 1.3 and later, packet_ins generated by a send-to-controller action
1625 * in a "table-miss" flow (one with priority 0 and completely wildcarded) are
1626 * sent as OFPR_NO_MATCH. This function returns the reason that should
1627 * actually be sent on 'ofconn' for 'pin'. */
1628 static enum ofp_packet_in_reason
1629 wire_reason(struct ofconn
*ofconn
, const struct ofproto_packet_in
*pin
)
1631 if (pin
->miss_type
== OFPROTO_PACKET_IN_MISS_FLOW
1632 && pin
->up
.reason
== OFPR_ACTION
) {
1633 enum ofputil_protocol protocol
= ofconn_get_protocol(ofconn
);
1635 if (protocol
!= OFPUTIL_P_NONE
1636 && ofputil_protocol_to_ofp_version(protocol
) >= OFP13_VERSION
) {
1637 return OFPR_NO_MATCH
;
1640 return pin
->up
.reason
;
1643 /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as
1644 * necessary according to their individual configurations.
1646 * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */
1648 connmgr_send_packet_in(struct connmgr
*mgr
,
1649 const struct ofproto_packet_in
*pin
)
1651 struct ofconn
*ofconn
;
1653 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
1654 enum ofp_packet_in_reason reason
= wire_reason(ofconn
, pin
);
1656 if (ofconn_wants_packet_in_on_miss(ofconn
, pin
)
1657 && ofconn_receives_async_msg(ofconn
, OAM_PACKET_IN
, pin
->up
.reason
)
1658 && ofconn
->controller_id
== pin
->controller_id
) {
1659 schedule_packet_in(ofconn
, *pin
, reason
);
1665 do_send_packet_ins(struct ofconn
*ofconn
, struct list
*txq
)
1667 struct ofpbuf
*pin
, *next_pin
;
1669 LIST_FOR_EACH_SAFE (pin
, next_pin
, list_node
, txq
) {
1670 list_remove(&pin
->list_node
);
1672 if (rconn_send_with_limit(ofconn
->rconn
, pin
,
1673 ofconn
->packet_in_counter
, 100) == EAGAIN
) {
1674 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
1676 VLOG_INFO_RL(&rl
, "%s: dropping packet-in due to queue overflow",
1677 rconn_get_name(ofconn
->rconn
));
1682 /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it
1683 * to 'ofconn''s packet scheduler for sending. */
1685 schedule_packet_in(struct ofconn
*ofconn
, struct ofproto_packet_in pin
,
1686 enum ofp_packet_in_reason wire_reason
)
1688 struct connmgr
*mgr
= ofconn
->connmgr
;
1689 uint16_t controller_max_len
;
1692 pin
.up
.total_len
= pin
.up
.packet_len
;
1694 pin
.up
.reason
= wire_reason
;
1695 if (pin
.up
.reason
== OFPR_ACTION
) {
1696 controller_max_len
= pin
.send_len
; /* max_len */
1698 controller_max_len
= ofconn
->miss_send_len
;
1701 /* Get OpenFlow buffer_id.
1702 * For OpenFlow 1.2+, OFPCML_NO_BUFFER (== UINT16_MAX) specifies
1703 * unbuffered. This behaviour doesn't violate prior versions, too. */
1704 if (controller_max_len
== UINT16_MAX
) {
1705 pin
.up
.buffer_id
= UINT32_MAX
;
1706 } else if (mgr
->fail_open
&& fail_open_is_active(mgr
->fail_open
)) {
1707 pin
.up
.buffer_id
= pktbuf_get_null();
1708 } else if (!ofconn
->pktbuf
) {
1709 pin
.up
.buffer_id
= UINT32_MAX
;
1711 pin
.up
.buffer_id
= pktbuf_save(ofconn
->pktbuf
,
1712 pin
.up
.packet
, pin
.up
.packet_len
,
1713 pin
.up
.fmd
.in_port
);
1716 /* Figure out how much of the packet to send.
1717 * If not buffered, send the entire packet. Otherwise, depending on
1718 * the reason of packet-in, send what requested by the controller. */
1719 if (pin
.up
.buffer_id
!= UINT32_MAX
1720 && controller_max_len
< pin
.up
.packet_len
) {
1721 pin
.up
.packet_len
= controller_max_len
;
1724 /* Make OFPT_PACKET_IN and hand over to packet scheduler. */
1725 pinsched_send(ofconn
->schedulers
[pin
.up
.reason
== OFPR_NO_MATCH
? 0 : 1],
1727 ofputil_encode_packet_in(&pin
.up
,
1728 ofconn_get_protocol(ofconn
),
1729 ofconn
->packet_in_format
),
1731 do_send_packet_ins(ofconn
, &txq
);
1734 /* Fail-open settings. */
1736 /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or
1737 * OFPROTO_FAIL_STANDALONE) for 'mgr'. */
1738 enum ofproto_fail_mode
1739 connmgr_get_fail_mode(const struct connmgr
*mgr
)
1741 return mgr
->fail_mode
;
1744 /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either
1745 * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */
1747 connmgr_set_fail_mode(struct connmgr
*mgr
, enum ofproto_fail_mode fail_mode
)
1749 if (mgr
->fail_mode
!= fail_mode
) {
1750 mgr
->fail_mode
= fail_mode
;
1751 update_fail_open(mgr
);
1752 if (!connmgr_has_controllers(mgr
)) {
1753 ofproto_flush_flows(mgr
->ofproto
);
1758 /* Fail-open implementation. */
1760 /* Returns the longest probe interval among the primary controllers configured
1761 * on 'mgr'. Returns 0 if there are no primary controllers. */
1763 connmgr_get_max_probe_interval(const struct connmgr
*mgr
)
1765 const struct ofconn
*ofconn
;
1766 int max_probe_interval
;
1768 max_probe_interval
= 0;
1769 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1770 int probe_interval
= rconn_get_probe_interval(ofconn
->rconn
);
1771 max_probe_interval
= MAX(max_probe_interval
, probe_interval
);
1773 return max_probe_interval
;
1776 /* Returns the number of seconds for which all of 'mgr's primary controllers
1777 * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */
1779 connmgr_failure_duration(const struct connmgr
*mgr
)
1781 const struct ofconn
*ofconn
;
1782 int min_failure_duration
;
1784 if (!connmgr_has_controllers(mgr
)) {
1788 min_failure_duration
= INT_MAX
;
1789 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1790 int failure_duration
= rconn_failure_duration(ofconn
->rconn
);
1791 min_failure_duration
= MIN(min_failure_duration
, failure_duration
);
1793 return min_failure_duration
;
1796 /* Returns true if at least one primary controller is connected (regardless of
1797 * whether those controllers are believed to have authenticated and accepted
1798 * this switch), false if none of them are connected. */
1800 connmgr_is_any_controller_connected(const struct connmgr
*mgr
)
1802 const struct ofconn
*ofconn
;
1804 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1805 if (rconn_is_connected(ofconn
->rconn
)) {
1812 /* Returns true if at least one primary controller is believed to have
1813 * authenticated and accepted this switch, false otherwise. */
1815 connmgr_is_any_controller_admitted(const struct connmgr
*mgr
)
1817 const struct ofconn
*ofconn
;
1819 HMAP_FOR_EACH (ofconn
, hmap_node
, &mgr
->controllers
) {
1820 if (rconn_is_admitted(ofconn
->rconn
)) {
1827 /* In-band configuration. */
1829 static bool any_extras_changed(const struct connmgr
*,
1830 const struct sockaddr_in
*extras
, size_t n
);
1832 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s
1833 * in-band control should guarantee access, in the same way that in-band
1834 * control guarantees access to OpenFlow controllers. */
1836 connmgr_set_extra_in_band_remotes(struct connmgr
*mgr
,
1837 const struct sockaddr_in
*extras
, size_t n
)
1839 if (!any_extras_changed(mgr
, extras
, n
)) {
1843 free(mgr
->extra_in_band_remotes
);
1844 mgr
->n_extra_remotes
= n
;
1845 mgr
->extra_in_band_remotes
= xmemdup(extras
, n
* sizeof *extras
);
1847 update_in_band_remotes(mgr
);
1850 /* Sets the OpenFlow queue used by flows set up by in-band control on
1851 * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control
1852 * flows will use the default queue. */
1854 connmgr_set_in_band_queue(struct connmgr
*mgr
, int queue_id
)
1856 if (queue_id
!= mgr
->in_band_queue
) {
1857 mgr
->in_band_queue
= queue_id
;
1858 update_in_band_remotes(mgr
);
1863 any_extras_changed(const struct connmgr
*mgr
,
1864 const struct sockaddr_in
*extras
, size_t n
)
1868 if (n
!= mgr
->n_extra_remotes
) {
1872 for (i
= 0; i
< n
; i
++) {
1873 const struct sockaddr_in
*old
= &mgr
->extra_in_band_remotes
[i
];
1874 const struct sockaddr_in
*new = &extras
[i
];
1876 if (old
->sin_addr
.s_addr
!= new->sin_addr
.s_addr
||
1877 old
->sin_port
!= new->sin_port
) {
1885 /* In-band implementation. */
1888 connmgr_has_in_band(struct connmgr
*mgr
)
1890 return mgr
->in_band
!= NULL
;
1893 /* Fail-open and in-band implementation. */
1895 /* Called by 'ofproto' after all flows have been flushed, to allow fail-open
1896 * and standalone mode to re-create their flows.
1898 * In-band control has more sophisticated code that manages flows itself. */
1900 connmgr_flushed(struct connmgr
*mgr
)
1901 OVS_EXCLUDED(ofproto_mutex
)
1903 if (mgr
->fail_open
) {
1904 fail_open_flushed(mgr
->fail_open
);
1907 /* If there are no controllers and we're in standalone mode, set up a flow
1908 * that matches every packet and directs them to OFPP_NORMAL (which goes to
1909 * us). Otherwise, the switch is in secure mode and we won't pass any
1910 * traffic until a controller has been defined and it tells us to do so. */
1911 if (!connmgr_has_controllers(mgr
)
1912 && mgr
->fail_mode
== OFPROTO_FAIL_STANDALONE
) {
1913 struct ofpbuf ofpacts
;
1916 ofpbuf_init(&ofpacts
, OFPACT_OUTPUT_SIZE
);
1917 ofpact_put_OUTPUT(&ofpacts
)->port
= OFPP_NORMAL
;
1918 ofpact_pad(&ofpacts
);
1920 match_init_catchall(&match
);
1921 ofproto_add_flow(mgr
->ofproto
, &match
, 0, ofpbuf_data(&ofpacts
),
1922 ofpbuf_size(&ofpacts
));
1924 ofpbuf_uninit(&ofpacts
);
1928 /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful,
1929 * otherwise a positive errno value.
1931 * ofservice_reconfigure() must be called to fully configure the new
1934 ofservice_create(struct connmgr
*mgr
, const char *target
,
1935 uint32_t allowed_versions
, uint8_t dscp
)
1937 struct ofservice
*ofservice
;
1938 struct pvconn
*pvconn
;
1941 error
= pvconn_open(target
, allowed_versions
, dscp
, &pvconn
);
1946 ofservice
= xzalloc(sizeof *ofservice
);
1947 hmap_insert(&mgr
->services
, &ofservice
->node
, hash_string(target
, 0));
1948 ofservice
->pvconn
= pvconn
;
1949 ofservice
->allowed_versions
= allowed_versions
;
1955 ofservice_destroy(struct connmgr
*mgr
, struct ofservice
*ofservice
)
1957 hmap_remove(&mgr
->services
, &ofservice
->node
);
1958 pvconn_close(ofservice
->pvconn
);
1963 ofservice_reconfigure(struct ofservice
*ofservice
,
1964 const struct ofproto_controller
*c
)
1966 ofservice
->probe_interval
= c
->probe_interval
;
1967 ofservice
->rate_limit
= c
->rate_limit
;
1968 ofservice
->burst_limit
= c
->burst_limit
;
1969 ofservice
->enable_async_msgs
= c
->enable_async_msgs
;
1970 ofservice
->dscp
= c
->dscp
;
1973 /* Finds and returns the ofservice within 'mgr' that has the given
1974 * 'target', or a null pointer if none exists. */
1975 static struct ofservice
*
1976 ofservice_lookup(struct connmgr
*mgr
, const char *target
)
1978 struct ofservice
*ofservice
;
1980 HMAP_FOR_EACH_WITH_HASH (ofservice
, node
, hash_string(target
, 0),
1982 if (!strcmp(pvconn_get_name(ofservice
->pvconn
), target
)) {
1989 /* Flow monitors (NXST_FLOW_MONITOR). */
1991 /* A counter incremented when something significant happens to an OpenFlow
1994 * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to
1995 * the current value (which is then incremented).
1997 * - When a rule is modified, its 'modify_seqno' is set to the current
1998 * value (which is then incremented).
2000 * Thus, by comparing an old value of monitor_seqno against a rule's
2001 * 'add_seqno', one can tell whether the rule was added before or after the old
2002 * value was read, and similarly for 'modify_seqno'.
2004 * 32 bits should normally be sufficient (and would be nice, to save space in
2005 * each rule) but then we'd have to have some special cases for wraparound.
2007 * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid
2009 static uint64_t monitor_seqno
= 1;
2011 COVERAGE_DEFINE(ofmonitor_pause
);
2012 COVERAGE_DEFINE(ofmonitor_resume
);
2015 ofmonitor_create(const struct ofputil_flow_monitor_request
*request
,
2016 struct ofconn
*ofconn
, struct ofmonitor
**monitorp
)
2017 OVS_REQUIRES(ofproto_mutex
)
2019 struct ofmonitor
*m
;
2023 m
= ofmonitor_lookup(ofconn
, request
->id
);
2025 return OFPERR_OFPMOFC_MONITOR_EXISTS
;
2028 m
= xmalloc(sizeof *m
);
2030 hmap_insert(&ofconn
->monitors
, &m
->ofconn_node
, hash_int(request
->id
, 0));
2031 m
->id
= request
->id
;
2032 m
->flags
= request
->flags
;
2033 m
->out_port
= request
->out_port
;
2034 m
->table_id
= request
->table_id
;
2035 minimatch_init(&m
->match
, &request
->match
);
2042 ofmonitor_lookup(struct ofconn
*ofconn
, uint32_t id
)
2043 OVS_REQUIRES(ofproto_mutex
)
2045 struct ofmonitor
*m
;
2047 HMAP_FOR_EACH_IN_BUCKET (m
, ofconn_node
, hash_int(id
, 0),
2048 &ofconn
->monitors
) {
2057 ofmonitor_destroy(struct ofmonitor
*m
)
2058 OVS_REQUIRES(ofproto_mutex
)
2061 minimatch_destroy(&m
->match
);
2062 hmap_remove(&m
->ofconn
->monitors
, &m
->ofconn_node
);
2068 ofmonitor_report(struct connmgr
*mgr
, struct rule
*rule
,
2069 enum nx_flow_update_event event
,
2070 enum ofp_flow_removed_reason reason
,
2071 const struct ofconn
*abbrev_ofconn
, ovs_be32 abbrev_xid
)
2072 OVS_REQUIRES(ofproto_mutex
)
2074 enum nx_flow_monitor_flags update
;
2075 struct ofconn
*ofconn
;
2077 if (rule_is_hidden(rule
)) {
2084 rule
->add_seqno
= rule
->modify_seqno
= monitor_seqno
++;
2088 update
= NXFMF_DELETE
;
2091 case NXFME_MODIFIED
:
2092 update
= NXFMF_MODIFY
;
2093 rule
->modify_seqno
= monitor_seqno
++;
2101 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2102 enum nx_flow_monitor_flags flags
= 0;
2103 struct ofmonitor
*m
;
2105 if (ofconn
->monitor_paused
) {
2106 /* Only send NXFME_DELETED notifications for flows that were added
2107 * before we paused. */
2108 if (event
!= NXFME_DELETED
2109 || rule
->add_seqno
> ofconn
->monitor_paused
) {
2114 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2115 if (m
->flags
& update
2116 && (m
->table_id
== 0xff || m
->table_id
== rule
->table_id
)
2117 && cls_rule_is_loose_match(&rule
->cr
, &m
->match
)) {
2123 if (list_is_empty(&ofconn
->updates
)) {
2124 ofputil_start_flow_update(&ofconn
->updates
);
2125 ofconn
->sent_abbrev_update
= false;
2128 if (flags
& NXFMF_OWN
|| ofconn
!= abbrev_ofconn
2129 || ofconn
->monitor_paused
) {
2130 struct ofputil_flow_update fu
;
2134 fu
.reason
= event
== NXFME_DELETED
? reason
: 0;
2135 fu
.table_id
= rule
->table_id
;
2136 fu
.cookie
= rule
->flow_cookie
;
2137 minimatch_expand(&rule
->cr
.match
, &match
);
2139 fu
.priority
= rule
->cr
.priority
;
2141 ovs_mutex_lock(&rule
->mutex
);
2142 fu
.idle_timeout
= rule
->idle_timeout
;
2143 fu
.hard_timeout
= rule
->hard_timeout
;
2144 ovs_mutex_unlock(&rule
->mutex
);
2146 if (flags
& NXFMF_ACTIONS
) {
2147 const struct rule_actions
*actions
= rule_get_actions(rule
);
2148 fu
.ofpacts
= actions
->ofpacts
;
2149 fu
.ofpacts_len
= actions
->ofpacts_len
;
2154 ofputil_append_flow_update(&fu
, &ofconn
->updates
);
2155 } else if (!ofconn
->sent_abbrev_update
) {
2156 struct ofputil_flow_update fu
;
2158 fu
.event
= NXFME_ABBREV
;
2159 fu
.xid
= abbrev_xid
;
2160 ofputil_append_flow_update(&fu
, &ofconn
->updates
);
2162 ofconn
->sent_abbrev_update
= true;
2169 ofmonitor_flush(struct connmgr
*mgr
)
2170 OVS_REQUIRES(ofproto_mutex
)
2172 struct ofconn
*ofconn
;
2174 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2175 struct ofpbuf
*msg
, *next
;
2177 LIST_FOR_EACH_SAFE (msg
, next
, list_node
, &ofconn
->updates
) {
2178 unsigned int n_bytes
;
2180 list_remove(&msg
->list_node
);
2181 ofconn_send(ofconn
, msg
, ofconn
->monitor_counter
);
2182 n_bytes
= rconn_packet_counter_n_bytes(ofconn
->monitor_counter
);
2183 if (!ofconn
->monitor_paused
&& n_bytes
> 128 * 1024) {
2184 struct ofpbuf
*pause
;
2186 COVERAGE_INC(ofmonitor_pause
);
2187 ofconn
->monitor_paused
= monitor_seqno
++;
2188 pause
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED
,
2189 OFP10_VERSION
, htonl(0), 0);
2190 ofconn_send(ofconn
, pause
, ofconn
->monitor_counter
);
2197 ofmonitor_resume(struct ofconn
*ofconn
)
2198 OVS_REQUIRES(ofproto_mutex
)
2200 struct rule_collection rules
;
2201 struct ofpbuf
*resumed
;
2202 struct ofmonitor
*m
;
2205 rule_collection_init(&rules
);
2206 HMAP_FOR_EACH (m
, ofconn_node
, &ofconn
->monitors
) {
2207 ofmonitor_collect_resume_rules(m
, ofconn
->monitor_paused
, &rules
);
2211 ofmonitor_compose_refresh_updates(&rules
, &msgs
);
2213 resumed
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED
, OFP10_VERSION
,
2215 list_push_back(&msgs
, &resumed
->list_node
);
2216 ofconn_send_replies(ofconn
, &msgs
);
2218 ofconn
->monitor_paused
= 0;
2222 ofmonitor_may_resume(const struct ofconn
*ofconn
)
2223 OVS_REQUIRES(ofproto_mutex
)
2225 return (ofconn
->monitor_paused
!= 0
2226 && !rconn_packet_counter_n_packets(ofconn
->monitor_counter
));
2230 ofmonitor_run(struct connmgr
*mgr
)
2232 struct ofconn
*ofconn
;
2234 ovs_mutex_lock(&ofproto_mutex
);
2235 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2236 if (ofmonitor_may_resume(ofconn
)) {
2237 COVERAGE_INC(ofmonitor_resume
);
2238 ofmonitor_resume(ofconn
);
2241 ovs_mutex_unlock(&ofproto_mutex
);
2245 ofmonitor_wait(struct connmgr
*mgr
)
2247 struct ofconn
*ofconn
;
2249 ovs_mutex_lock(&ofproto_mutex
);
2250 LIST_FOR_EACH (ofconn
, node
, &mgr
->all_conns
) {
2251 if (ofmonitor_may_resume(ofconn
)) {
2252 poll_immediate_wake();
2255 ovs_mutex_unlock(&ofproto_mutex
);