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