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