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