]>
Commit | Line | Data |
---|---|---|
19a87e36 | 1 | /* |
82c8c53c | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013 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> | |
18 | ||
19 | #include "connmgr.h" | |
20 | ||
21 | #include <errno.h> | |
22 | #include <stdlib.h> | |
23 | ||
24 | #include "coverage.h" | |
19a87e36 BP |
25 | #include "fail-open.h" |
26 | #include "in-band.h" | |
27 | #include "odp-util.h" | |
f25d0cf3 | 28 | #include "ofp-actions.h" |
982697a4 | 29 | #include "ofp-msgs.h" |
19a87e36 BP |
30 | #include "ofp-util.h" |
31 | #include "ofpbuf.h" | |
5bee6e26 | 32 | #include "ofproto-provider.h" |
19a87e36 BP |
33 | #include "pinsched.h" |
34 | #include "poll-loop.h" | |
35 | #include "pktbuf.h" | |
36 | #include "rconn.h" | |
37 | #include "shash.h" | |
0d085684 | 38 | #include "simap.h" |
5bd31620 | 39 | #include "stream.h" |
19a87e36 BP |
40 | #include "timeval.h" |
41 | #include "vconn.h" | |
42 | #include "vlog.h" | |
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 | ||
19a87e36 | 60 | struct 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 | 75 | |
7ee20df1 BP |
76 | /* Asynchronous flow table operation support. */ |
77 | struct list opgroups; /* Contains pending "ofopgroups", if any. */ | |
78 | struct ofpbuf *blocked; /* Postponed OpenFlow message, if any. */ | |
79 | bool retry; /* True if 'blocked' is ready to try again. */ | |
80 | ||
19a87e36 BP |
81 | /* OFPT_PACKET_IN related data. */ |
82 | struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */ | |
83 | #define N_SCHEDULERS 2 | |
84 | struct pinsched *schedulers[N_SCHEDULERS]; | |
85 | struct pktbuf *pktbuf; /* OpenFlow packet buffers. */ | |
86 | int miss_send_len; /* Bytes to send of buffered packets. */ | |
a7349929 | 87 | uint16_t controller_id; /* Connection controller ID. */ |
19a87e36 BP |
88 | |
89 | /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow | |
90 | * requests, and the maximum number before we stop reading OpenFlow | |
91 | * requests. */ | |
92 | #define OFCONN_REPLY_MAX 100 | |
93 | struct rconn_packet_counter *reply_counter; | |
80d5aefd BP |
94 | |
95 | /* Asynchronous message configuration in each possible roles. | |
96 | * | |
97 | * A 1-bit enables sending an asynchronous message for one possible reason | |
98 | * that the message might be generated, a 0-bit disables it. */ | |
99 | uint32_t master_async_config[OAM_N_TYPES]; /* master, other */ | |
100 | uint32_t slave_async_config[OAM_N_TYPES]; /* slave */ | |
2b07c8b1 | 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.. */ | |
130 | struct list updates OVS_GUARDED_BY(ofproto_mutex); | |
131 | bool sent_abbrev_update OVS_GUARDED_BY(ofproto_mutex); | |
19a87e36 BP |
132 | }; |
133 | ||
134 | static struct ofconn *ofconn_create(struct connmgr *, struct rconn *, | |
3b0c6b56 BP |
135 | enum ofconn_type, bool enable_async_msgs) |
136 | OVS_REQUIRES(ofproto_mutex); | |
137 | static void ofconn_destroy(struct ofconn *) OVS_REQUIRES(ofproto_mutex); | |
138 | static void ofconn_flush(struct ofconn *) OVS_REQUIRES(ofproto_mutex); | |
19a87e36 BP |
139 | |
140 | static void ofconn_reconfigure(struct ofconn *, | |
141 | const struct ofproto_controller *); | |
142 | ||
143 | static void ofconn_run(struct ofconn *, | |
7ee20df1 | 144 | bool (*handle_openflow)(struct ofconn *, |
e03248b7 | 145 | const struct ofpbuf *ofp_msg)); |
7ee20df1 | 146 | static void ofconn_wait(struct ofconn *, bool handling_openflow); |
19a87e36 BP |
147 | |
148 | static const char *ofconn_get_target(const struct ofconn *); | |
149 | static char *ofconn_make_name(const struct connmgr *, const char *target); | |
150 | ||
151 | static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst); | |
152 | ||
19a87e36 BP |
153 | static void ofconn_send(const struct ofconn *, struct ofpbuf *, |
154 | struct rconn_packet_counter *); | |
155 | ||
a6f75961 | 156 | static void do_send_packet_ins(struct ofconn *, struct list *txq); |
19a87e36 BP |
157 | |
158 | /* A listener for incoming OpenFlow "service" connections. */ | |
159 | struct ofservice { | |
160 | struct hmap_node node; /* In struct connmgr's "services" hmap. */ | |
161 | struct pvconn *pvconn; /* OpenFlow connection listener. */ | |
162 | ||
163 | /* These are not used by ofservice directly. They are settings for | |
164 | * accepted "struct ofconn"s from the pvconn. */ | |
165 | int probe_interval; /* Max idle time before probing, in seconds. */ | |
166 | int rate_limit; /* Max packet-in rate in packets per second. */ | |
167 | int burst_limit; /* Limit on accumulating packet credits. */ | |
9886b662 | 168 | bool enable_async_msgs; /* Initially enable async messages? */ |
f125905c | 169 | uint8_t dscp; /* DSCP Value for controller connection */ |
90ef0206 SH |
170 | uint32_t allowed_versions; /* OpenFlow protocol versions that may |
171 | * be negotiated for a session. */ | |
19a87e36 BP |
172 | }; |
173 | ||
174 | static void ofservice_reconfigure(struct ofservice *, | |
175 | const struct ofproto_controller *); | |
00401ef0 SH |
176 | static int ofservice_create(struct connmgr *mgr, const char *target, |
177 | uint32_t allowed_versions, uint8_t dscp); | |
19a87e36 BP |
178 | static void ofservice_destroy(struct connmgr *, struct ofservice *); |
179 | static struct ofservice *ofservice_lookup(struct connmgr *, | |
180 | const char *target); | |
181 | ||
182 | /* Connection manager for an OpenFlow switch. */ | |
183 | struct connmgr { | |
184 | struct ofproto *ofproto; | |
185 | char *name; | |
186 | char *local_port_name; | |
187 | ||
188 | /* OpenFlow connections. */ | |
189 | struct hmap controllers; /* Controller "struct ofconn"s. */ | |
190 | struct list all_conns; /* Contains "struct ofconn"s. */ | |
6ea4776b JR |
191 | uint64_t master_election_id; /* monotonically increasing sequence number |
192 | * for master election */ | |
193 | bool master_election_id_defined; | |
19a87e36 BP |
194 | |
195 | /* OpenFlow listeners. */ | |
196 | struct hmap services; /* Contains "struct ofservice"s. */ | |
197 | struct pvconn **snoops; | |
198 | size_t n_snoops; | |
199 | ||
200 | /* Fail open. */ | |
201 | struct fail_open *fail_open; | |
202 | enum ofproto_fail_mode fail_mode; | |
203 | ||
204 | /* In-band control. */ | |
205 | struct in_band *in_band; | |
19a87e36 BP |
206 | struct sockaddr_in *extra_in_band_remotes; |
207 | size_t n_extra_remotes; | |
208 | int in_band_queue; | |
209 | }; | |
210 | ||
211 | static void update_in_band_remotes(struct connmgr *); | |
212 | static void add_snooper(struct connmgr *, struct vconn *); | |
2b07c8b1 BP |
213 | static void ofmonitor_run(struct connmgr *); |
214 | static void ofmonitor_wait(struct connmgr *); | |
19a87e36 BP |
215 | |
216 | /* Creates and returns a new connection manager owned by 'ofproto'. 'name' is | |
217 | * a name for the ofproto suitable for using in log messages. | |
218 | * 'local_port_name' is the name of the local port (OFPP_LOCAL) within | |
219 | * 'ofproto'. */ | |
220 | struct connmgr * | |
221 | connmgr_create(struct ofproto *ofproto, | |
222 | const char *name, const char *local_port_name) | |
223 | { | |
224 | struct connmgr *mgr; | |
225 | ||
226 | mgr = xmalloc(sizeof *mgr); | |
227 | mgr->ofproto = ofproto; | |
228 | mgr->name = xstrdup(name); | |
229 | mgr->local_port_name = xstrdup(local_port_name); | |
230 | ||
231 | hmap_init(&mgr->controllers); | |
232 | list_init(&mgr->all_conns); | |
6ea4776b JR |
233 | mgr->master_election_id = 0; |
234 | mgr->master_election_id_defined = false; | |
19a87e36 BP |
235 | |
236 | hmap_init(&mgr->services); | |
237 | mgr->snoops = NULL; | |
238 | mgr->n_snoops = 0; | |
239 | ||
240 | mgr->fail_open = NULL; | |
241 | mgr->fail_mode = OFPROTO_FAIL_SECURE; | |
242 | ||
243 | mgr->in_band = NULL; | |
19a87e36 BP |
244 | mgr->extra_in_band_remotes = NULL; |
245 | mgr->n_extra_remotes = 0; | |
246 | mgr->in_band_queue = -1; | |
247 | ||
248 | return mgr; | |
249 | } | |
250 | ||
251 | /* Frees 'mgr' and all of its resources. */ | |
252 | void | |
253 | connmgr_destroy(struct connmgr *mgr) | |
254 | { | |
255 | struct ofservice *ofservice, *next_ofservice; | |
256 | struct ofconn *ofconn, *next_ofconn; | |
257 | size_t i; | |
258 | ||
259 | if (!mgr) { | |
260 | return; | |
261 | } | |
262 | ||
3b0c6b56 | 263 | ovs_mutex_lock(&ofproto_mutex); |
19a87e36 BP |
264 | LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) { |
265 | ofconn_destroy(ofconn); | |
266 | } | |
3b0c6b56 BP |
267 | ovs_mutex_unlock(&ofproto_mutex); |
268 | ||
19a87e36 BP |
269 | hmap_destroy(&mgr->controllers); |
270 | ||
271 | HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) { | |
272 | ofservice_destroy(mgr, ofservice); | |
273 | } | |
274 | hmap_destroy(&mgr->services); | |
275 | ||
276 | for (i = 0; i < mgr->n_snoops; i++) { | |
277 | pvconn_close(mgr->snoops[i]); | |
278 | } | |
279 | free(mgr->snoops); | |
280 | ||
281 | fail_open_destroy(mgr->fail_open); | |
282 | mgr->fail_open = NULL; | |
283 | ||
284 | in_band_destroy(mgr->in_band); | |
285 | mgr->in_band = NULL; | |
286 | free(mgr->extra_in_band_remotes); | |
287 | free(mgr->name); | |
288 | free(mgr->local_port_name); | |
289 | ||
290 | free(mgr); | |
291 | } | |
292 | ||
7ee20df1 BP |
293 | /* Does all of the periodic maintenance required by 'mgr'. |
294 | * | |
295 | * If 'handle_openflow' is nonnull, calls 'handle_openflow' for each message | |
296 | * received on an OpenFlow connection, passing along the OpenFlow connection | |
297 | * itself and the message that was sent. If 'handle_openflow' returns true, | |
298 | * the message is considered to be fully processed. If 'handle_openflow' | |
299 | * returns false, the message is considered not to have been processed at all; | |
300 | * it will be stored and re-presented to 'handle_openflow' following the next | |
301 | * call to connmgr_retry(). 'handle_openflow' must not modify or free the | |
302 | * message. | |
303 | * | |
304 | * If 'handle_openflow' is NULL, no OpenFlow messages will be processed and | |
305 | * other activities that could affect the flow table (in-band processing, | |
306 | * fail-open processing) are suppressed too. */ | |
19a87e36 BP |
307 | void |
308 | connmgr_run(struct connmgr *mgr, | |
e03248b7 JR |
309 | bool (*handle_openflow)(struct ofconn *, |
310 | const struct ofpbuf *ofp_msg)) | |
15aaf599 | 311 | OVS_EXCLUDED(ofproto_mutex) |
19a87e36 BP |
312 | { |
313 | struct ofconn *ofconn, *next_ofconn; | |
314 | struct ofservice *ofservice; | |
315 | size_t i; | |
316 | ||
7ee20df1 | 317 | if (handle_openflow && mgr->in_band) { |
6da1e809 BP |
318 | if (!in_band_run(mgr->in_band)) { |
319 | in_band_destroy(mgr->in_band); | |
320 | mgr->in_band = NULL; | |
321 | } | |
19a87e36 BP |
322 | } |
323 | ||
324 | LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &mgr->all_conns) { | |
325 | ofconn_run(ofconn, handle_openflow); | |
326 | } | |
2b07c8b1 | 327 | ofmonitor_run(mgr); |
19a87e36 BP |
328 | |
329 | /* Fail-open maintenance. Do this after processing the ofconns since | |
330 | * fail-open checks the status of the controller rconn. */ | |
7ee20df1 | 331 | if (handle_openflow && mgr->fail_open) { |
19a87e36 BP |
332 | fail_open_run(mgr->fail_open); |
333 | } | |
334 | ||
335 | HMAP_FOR_EACH (ofservice, node, &mgr->services) { | |
336 | struct vconn *vconn; | |
337 | int retval; | |
338 | ||
7a25bd99 | 339 | retval = pvconn_accept(ofservice->pvconn, &vconn); |
19a87e36 BP |
340 | if (!retval) { |
341 | struct rconn *rconn; | |
342 | char *name; | |
343 | ||
f125905c | 344 | /* Passing default value for creation of the rconn */ |
1d9ffc17 SH |
345 | rconn = rconn_create(ofservice->probe_interval, 0, ofservice->dscp, |
346 | vconn_get_allowed_versions(vconn)); | |
19a87e36 BP |
347 | name = ofconn_make_name(mgr, vconn_get_name(vconn)); |
348 | rconn_connect_unreliably(rconn, vconn, name); | |
349 | free(name); | |
350 | ||
3b0c6b56 | 351 | ovs_mutex_lock(&ofproto_mutex); |
9886b662 BP |
352 | ofconn = ofconn_create(mgr, rconn, OFCONN_SERVICE, |
353 | ofservice->enable_async_msgs); | |
3b0c6b56 BP |
354 | ovs_mutex_unlock(&ofproto_mutex); |
355 | ||
19a87e36 BP |
356 | ofconn_set_rate_limit(ofconn, ofservice->rate_limit, |
357 | ofservice->burst_limit); | |
358 | } else if (retval != EAGAIN) { | |
10a89ef0 | 359 | VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval)); |
19a87e36 BP |
360 | } |
361 | } | |
362 | ||
363 | for (i = 0; i < mgr->n_snoops; i++) { | |
364 | struct vconn *vconn; | |
365 | int retval; | |
366 | ||
7a25bd99 | 367 | retval = pvconn_accept(mgr->snoops[i], &vconn); |
19a87e36 BP |
368 | if (!retval) { |
369 | add_snooper(mgr, vconn); | |
370 | } else if (retval != EAGAIN) { | |
10a89ef0 | 371 | VLOG_WARN_RL(&rl, "accept failed (%s)", ovs_strerror(retval)); |
19a87e36 BP |
372 | } |
373 | } | |
374 | } | |
375 | ||
7ee20df1 BP |
376 | /* Causes the poll loop to wake up when connmgr_run() needs to run. |
377 | * | |
378 | * If 'handling_openflow' is true, arriving OpenFlow messages and other | |
379 | * activities that affect the flow table will wake up the poll loop. If | |
380 | * 'handling_openflow' is false, they will not. */ | |
19a87e36 | 381 | void |
7ee20df1 | 382 | connmgr_wait(struct connmgr *mgr, bool handling_openflow) |
19a87e36 BP |
383 | { |
384 | struct ofservice *ofservice; | |
385 | struct ofconn *ofconn; | |
386 | size_t i; | |
387 | ||
388 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
7ee20df1 | 389 | ofconn_wait(ofconn, handling_openflow); |
19a87e36 | 390 | } |
2b07c8b1 | 391 | ofmonitor_wait(mgr); |
7ee20df1 | 392 | if (handling_openflow && mgr->in_band) { |
19a87e36 BP |
393 | in_band_wait(mgr->in_band); |
394 | } | |
7ee20df1 | 395 | if (handling_openflow && mgr->fail_open) { |
19a87e36 BP |
396 | fail_open_wait(mgr->fail_open); |
397 | } | |
398 | HMAP_FOR_EACH (ofservice, node, &mgr->services) { | |
399 | pvconn_wait(ofservice->pvconn); | |
400 | } | |
401 | for (i = 0; i < mgr->n_snoops; i++) { | |
402 | pvconn_wait(mgr->snoops[i]); | |
403 | } | |
404 | } | |
405 | ||
0d085684 BP |
406 | /* Adds some memory usage statistics for 'mgr' into 'usage', for use with |
407 | * memory_report(). */ | |
408 | void | |
409 | connmgr_get_memory_usage(const struct connmgr *mgr, struct simap *usage) | |
410 | { | |
411 | const struct ofconn *ofconn; | |
412 | unsigned int packets = 0; | |
413 | unsigned int ofconns = 0; | |
414 | ||
415 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
416 | int i; | |
417 | ||
418 | ofconns++; | |
419 | ||
420 | packets += rconn_count_txqlen(ofconn->rconn); | |
421 | for (i = 0; i < N_SCHEDULERS; i++) { | |
422 | packets += pinsched_count_txqlen(ofconn->schedulers[i]); | |
423 | } | |
424 | packets += pktbuf_count_packets(ofconn->pktbuf); | |
425 | } | |
426 | simap_increase(usage, "ofconns", ofconns); | |
427 | simap_increase(usage, "packets", packets); | |
428 | } | |
429 | ||
19a87e36 BP |
430 | /* Returns the ofproto that owns 'ofconn''s connmgr. */ |
431 | struct ofproto * | |
432 | ofconn_get_ofproto(const struct ofconn *ofconn) | |
433 | { | |
434 | return ofconn->connmgr->ofproto; | |
435 | } | |
7ee20df1 BP |
436 | |
437 | /* If processing of OpenFlow messages was blocked on any 'mgr' ofconns by | |
438 | * returning false to the 'handle_openflow' callback to connmgr_run(), this | |
439 | * re-enables them. */ | |
440 | void | |
441 | connmgr_retry(struct connmgr *mgr) | |
442 | { | |
443 | struct ofconn *ofconn; | |
444 | ||
445 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
446 | ofconn->retry = true; | |
447 | } | |
448 | } | |
19a87e36 BP |
449 | \f |
450 | /* OpenFlow configuration. */ | |
451 | ||
1d9ffc17 | 452 | static void add_controller(struct connmgr *, const char *target, uint8_t dscp, |
3b0c6b56 BP |
453 | uint32_t allowed_versions) |
454 | OVS_REQUIRES(ofproto_mutex); | |
19a87e36 BP |
455 | static struct ofconn *find_controller_by_target(struct connmgr *, |
456 | const char *target); | |
457 | static void update_fail_open(struct connmgr *); | |
458 | static int set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp, | |
81e2083f | 459 | const struct sset *); |
19a87e36 BP |
460 | |
461 | /* Returns true if 'mgr' has any configured primary controllers. | |
462 | * | |
463 | * Service controllers do not count, but configured primary controllers do | |
464 | * count whether or not they are currently connected. */ | |
465 | bool | |
466 | connmgr_has_controllers(const struct connmgr *mgr) | |
467 | { | |
468 | return !hmap_is_empty(&mgr->controllers); | |
469 | } | |
470 | ||
471 | /* Initializes 'info' and populates it with information about each configured | |
472 | * primary controller. The keys in 'info' are the controllers' targets; the | |
473 | * data values are corresponding "struct ofproto_controller_info". | |
474 | * | |
475 | * The caller owns 'info' and everything in it and should free it when it is no | |
476 | * longer needed. */ | |
477 | void | |
478 | connmgr_get_controller_info(struct connmgr *mgr, struct shash *info) | |
479 | { | |
480 | const struct ofconn *ofconn; | |
481 | ||
19a87e36 BP |
482 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { |
483 | const struct rconn *rconn = ofconn->rconn; | |
5d279086 | 484 | const char *target = rconn_get_target(rconn); |
19a87e36 | 485 | |
5d279086 AE |
486 | if (!shash_find(info, target)) { |
487 | struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo); | |
488 | time_t now = time_now(); | |
489 | time_t last_connection = rconn_get_last_connection(rconn); | |
490 | time_t last_disconnect = rconn_get_last_disconnect(rconn); | |
491 | int last_error = rconn_get_last_error(rconn); | |
19a87e36 | 492 | |
5d279086 | 493 | shash_add(info, target, cinfo); |
19a87e36 | 494 | |
5d279086 AE |
495 | cinfo->is_connected = rconn_is_connected(rconn); |
496 | cinfo->role = ofconn->role; | |
19a87e36 | 497 | |
5d279086 | 498 | cinfo->pairs.n = 0; |
19a87e36 | 499 | |
5d279086 AE |
500 | if (last_error) { |
501 | cinfo->pairs.keys[cinfo->pairs.n] = "last_error"; | |
502 | cinfo->pairs.values[cinfo->pairs.n++] | |
503 | = xstrdup(ovs_retval_to_string(last_error)); | |
504 | } | |
19a87e36 | 505 | |
5d279086 | 506 | cinfo->pairs.keys[cinfo->pairs.n] = "state"; |
19a87e36 | 507 | cinfo->pairs.values[cinfo->pairs.n++] |
5d279086 AE |
508 | = xstrdup(rconn_get_state(rconn)); |
509 | ||
510 | if (last_connection != TIME_MIN) { | |
511 | cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_connect"; | |
512 | cinfo->pairs.values[cinfo->pairs.n++] | |
513 | = xasprintf("%ld", (long int) (now - last_connection)); | |
514 | } | |
515 | ||
516 | if (last_disconnect != TIME_MIN) { | |
517 | cinfo->pairs.keys[cinfo->pairs.n] = "sec_since_disconnect"; | |
518 | cinfo->pairs.values[cinfo->pairs.n++] | |
519 | = xasprintf("%ld", (long int) (now - last_disconnect)); | |
520 | } | |
19a87e36 BP |
521 | } |
522 | } | |
523 | } | |
524 | ||
72ba2ed3 AE |
525 | void |
526 | connmgr_free_controller_info(struct shash *info) | |
527 | { | |
528 | struct shash_node *node; | |
529 | ||
530 | SHASH_FOR_EACH (node, info) { | |
531 | struct ofproto_controller_info *cinfo = node->data; | |
532 | while (cinfo->pairs.n) { | |
ebc56baa | 533 | free(CONST_CAST(char *, cinfo->pairs.values[--cinfo->pairs.n])); |
72ba2ed3 AE |
534 | } |
535 | free(cinfo); | |
536 | } | |
537 | shash_destroy(info); | |
538 | } | |
539 | ||
19a87e36 BP |
540 | /* Changes 'mgr''s set of controllers to the 'n_controllers' controllers in |
541 | * 'controllers'. */ | |
542 | void | |
543 | connmgr_set_controllers(struct connmgr *mgr, | |
544 | const struct ofproto_controller *controllers, | |
1d9ffc17 | 545 | size_t n_controllers, uint32_t allowed_versions) |
3b0c6b56 | 546 | OVS_EXCLUDED(ofproto_mutex) |
19a87e36 | 547 | { |
08fd19f0 | 548 | bool had_controllers = connmgr_has_controllers(mgr); |
19a87e36 BP |
549 | struct shash new_controllers; |
550 | struct ofconn *ofconn, *next_ofconn; | |
551 | struct ofservice *ofservice, *next_ofservice; | |
19a87e36 BP |
552 | size_t i; |
553 | ||
3b0c6b56 BP |
554 | /* Required to add and remove ofconns. This could probably be narrowed to |
555 | * cover a smaller amount of code, if that yielded some benefit. */ | |
556 | ovs_mutex_lock(&ofproto_mutex); | |
557 | ||
19a87e36 BP |
558 | /* Create newly configured controllers and services. |
559 | * Create a name to ofproto_controller mapping in 'new_controllers'. */ | |
560 | shash_init(&new_controllers); | |
561 | for (i = 0; i < n_controllers; i++) { | |
562 | const struct ofproto_controller *c = &controllers[i]; | |
563 | ||
564 | if (!vconn_verify_name(c->target)) { | |
90ef0206 SH |
565 | bool add = false; |
566 | ofconn = find_controller_by_target(mgr, c->target); | |
567 | if (!ofconn) { | |
1ea9e609 BP |
568 | VLOG_INFO("%s: added primary controller \"%s\"", |
569 | mgr->name, c->target); | |
90ef0206 SH |
570 | add = true; |
571 | } else if (rconn_get_allowed_versions(ofconn->rconn) != | |
572 | allowed_versions) { | |
573 | VLOG_INFO("%s: re-added primary controller \"%s\"", | |
574 | mgr->name, c->target); | |
575 | add = true; | |
576 | ofconn_destroy(ofconn); | |
577 | } | |
578 | if (add) { | |
1d9ffc17 | 579 | add_controller(mgr, c->target, c->dscp, allowed_versions); |
19a87e36 BP |
580 | } |
581 | } else if (!pvconn_verify_name(c->target)) { | |
90ef0206 SH |
582 | bool add = false; |
583 | ofservice = ofservice_lookup(mgr, c->target); | |
584 | if (!ofservice) { | |
1ea9e609 BP |
585 | VLOG_INFO("%s: added service controller \"%s\"", |
586 | mgr->name, c->target); | |
90ef0206 SH |
587 | add = true; |
588 | } else if (ofservice->allowed_versions != allowed_versions) { | |
589 | VLOG_INFO("%s: re-added service controller \"%s\"", | |
590 | mgr->name, c->target); | |
591 | ofservice_destroy(mgr, ofservice); | |
592 | add = true; | |
593 | } | |
594 | if (add) { | |
1d9ffc17 | 595 | ofservice_create(mgr, c->target, allowed_versions, c->dscp); |
19a87e36 BP |
596 | } |
597 | } else { | |
598 | VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"", | |
599 | mgr->name, c->target); | |
600 | continue; | |
601 | } | |
602 | ||
603 | shash_add_once(&new_controllers, c->target, &controllers[i]); | |
604 | } | |
605 | ||
606 | /* Delete controllers that are no longer configured. | |
607 | * Update configuration of all now-existing controllers. */ | |
19a87e36 | 608 | HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &mgr->controllers) { |
1ea9e609 | 609 | const char *target = ofconn_get_target(ofconn); |
19a87e36 BP |
610 | struct ofproto_controller *c; |
611 | ||
1ea9e609 | 612 | c = shash_find_data(&new_controllers, target); |
19a87e36 | 613 | if (!c) { |
1ea9e609 BP |
614 | VLOG_INFO("%s: removed primary controller \"%s\"", |
615 | mgr->name, target); | |
19a87e36 BP |
616 | ofconn_destroy(ofconn); |
617 | } else { | |
618 | ofconn_reconfigure(ofconn, c); | |
619 | } | |
620 | } | |
621 | ||
622 | /* Delete services that are no longer configured. | |
623 | * Update configuration of all now-existing services. */ | |
624 | HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &mgr->services) { | |
1ea9e609 | 625 | const char *target = pvconn_get_name(ofservice->pvconn); |
19a87e36 BP |
626 | struct ofproto_controller *c; |
627 | ||
1ea9e609 | 628 | c = shash_find_data(&new_controllers, target); |
19a87e36 | 629 | if (!c) { |
1ea9e609 BP |
630 | VLOG_INFO("%s: removed service controller \"%s\"", |
631 | mgr->name, target); | |
19a87e36 BP |
632 | ofservice_destroy(mgr, ofservice); |
633 | } else { | |
634 | ofservice_reconfigure(ofservice, c); | |
635 | } | |
636 | } | |
637 | ||
638 | shash_destroy(&new_controllers); | |
639 | ||
640 | update_in_band_remotes(mgr); | |
641 | update_fail_open(mgr); | |
08fd19f0 BP |
642 | if (had_controllers != connmgr_has_controllers(mgr)) { |
643 | ofproto_flush_flows(mgr->ofproto); | |
644 | } | |
3b0c6b56 | 645 | ovs_mutex_unlock(&ofproto_mutex); |
19a87e36 BP |
646 | } |
647 | ||
648 | /* Drops the connections between 'mgr' and all of its primary and secondary | |
649 | * controllers, forcing them to reconnect. */ | |
650 | void | |
651 | connmgr_reconnect(const struct connmgr *mgr) | |
652 | { | |
653 | struct ofconn *ofconn; | |
654 | ||
655 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
656 | rconn_reconnect(ofconn->rconn); | |
657 | } | |
658 | } | |
659 | ||
660 | /* Sets the "snoops" for 'mgr' to the pvconn targets listed in 'snoops'. | |
661 | * | |
662 | * A "snoop" is a pvconn to which every OpenFlow message to or from the most | |
663 | * important controller on 'mgr' is mirrored. */ | |
664 | int | |
81e2083f | 665 | connmgr_set_snoops(struct connmgr *mgr, const struct sset *snoops) |
19a87e36 BP |
666 | { |
667 | return set_pvconns(&mgr->snoops, &mgr->n_snoops, snoops); | |
668 | } | |
669 | ||
670 | /* Adds each of the snoops currently configured on 'mgr' to 'snoops'. */ | |
671 | void | |
81e2083f | 672 | connmgr_get_snoops(const struct connmgr *mgr, struct sset *snoops) |
19a87e36 BP |
673 | { |
674 | size_t i; | |
675 | ||
676 | for (i = 0; i < mgr->n_snoops; i++) { | |
81e2083f | 677 | sset_add(snoops, pvconn_get_name(mgr->snoops[i])); |
19a87e36 BP |
678 | } |
679 | } | |
680 | ||
81e2083f BP |
681 | /* Returns true if 'mgr' has at least one snoop, false if it has none. */ |
682 | bool | |
683 | connmgr_has_snoops(const struct connmgr *mgr) | |
684 | { | |
685 | return mgr->n_snoops > 0; | |
686 | } | |
687 | ||
19a87e36 BP |
688 | /* Creates a new controller for 'target' in 'mgr'. update_controller() needs |
689 | * to be called later to finish the new ofconn's configuration. */ | |
690 | static void | |
1d9ffc17 SH |
691 | add_controller(struct connmgr *mgr, const char *target, uint8_t dscp, |
692 | uint32_t allowed_versions) | |
3b0c6b56 | 693 | OVS_REQUIRES(ofproto_mutex) |
19a87e36 BP |
694 | { |
695 | char *name = ofconn_make_name(mgr, target); | |
696 | struct ofconn *ofconn; | |
697 | ||
1d9ffc17 | 698 | ofconn = ofconn_create(mgr, rconn_create(5, 8, dscp, allowed_versions), |
6042457b | 699 | OFCONN_PRIMARY, true); |
19a87e36 | 700 | ofconn->pktbuf = pktbuf_create(); |
19a87e36 BP |
701 | rconn_connect(ofconn->rconn, target, name); |
702 | hmap_insert(&mgr->controllers, &ofconn->hmap_node, hash_string(target, 0)); | |
703 | ||
704 | free(name); | |
705 | } | |
706 | ||
707 | static struct ofconn * | |
708 | find_controller_by_target(struct connmgr *mgr, const char *target) | |
709 | { | |
710 | struct ofconn *ofconn; | |
711 | ||
712 | HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node, | |
713 | hash_string(target, 0), &mgr->controllers) { | |
714 | if (!strcmp(ofconn_get_target(ofconn), target)) { | |
715 | return ofconn; | |
716 | } | |
717 | } | |
718 | return NULL; | |
719 | } | |
720 | ||
721 | static void | |
722 | update_in_band_remotes(struct connmgr *mgr) | |
723 | { | |
724 | struct sockaddr_in *addrs; | |
725 | size_t max_addrs, n_addrs; | |
726 | struct ofconn *ofconn; | |
727 | size_t i; | |
728 | ||
729 | /* Allocate enough memory for as many remotes as we could possibly have. */ | |
730 | max_addrs = mgr->n_extra_remotes + hmap_count(&mgr->controllers); | |
731 | addrs = xmalloc(max_addrs * sizeof *addrs); | |
732 | n_addrs = 0; | |
733 | ||
734 | /* Add all the remotes. */ | |
735 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
736 | struct sockaddr_in *sin = &addrs[n_addrs]; | |
ac4c900d | 737 | const char *target = rconn_get_target(ofconn->rconn); |
19a87e36 BP |
738 | |
739 | if (ofconn->band == OFPROTO_OUT_OF_BAND) { | |
740 | continue; | |
741 | } | |
742 | ||
ca843648 | 743 | if (stream_parse_target_with_default_port(target, |
125b0291 | 744 | OFP_OLD_PORT, |
ca843648 | 745 | sin)) { |
19a87e36 BP |
746 | n_addrs++; |
747 | } | |
748 | } | |
749 | for (i = 0; i < mgr->n_extra_remotes; i++) { | |
750 | addrs[n_addrs++] = mgr->extra_in_band_remotes[i]; | |
751 | } | |
752 | ||
753 | /* Create or update or destroy in-band. */ | |
754 | if (n_addrs) { | |
755 | if (!mgr->in_band) { | |
756 | in_band_create(mgr->ofproto, mgr->local_port_name, &mgr->in_band); | |
757 | } | |
19a87e36 | 758 | in_band_set_queue(mgr->in_band, mgr->in_band_queue); |
19a87e36 | 759 | } else { |
6da1e809 BP |
760 | /* in_band_run() needs a chance to delete any existing in-band flows. |
761 | * We will destroy mgr->in_band after it's done with that. */ | |
762 | } | |
763 | if (mgr->in_band) { | |
764 | in_band_set_remotes(mgr->in_band, addrs, n_addrs); | |
19a87e36 BP |
765 | } |
766 | ||
767 | /* Clean up. */ | |
768 | free(addrs); | |
769 | } | |
770 | ||
771 | static void | |
772 | update_fail_open(struct connmgr *mgr) | |
773 | { | |
774 | if (connmgr_has_controllers(mgr) | |
775 | && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) { | |
776 | if (!mgr->fail_open) { | |
777 | mgr->fail_open = fail_open_create(mgr->ofproto, mgr); | |
778 | } | |
779 | } else { | |
780 | fail_open_destroy(mgr->fail_open); | |
781 | mgr->fail_open = NULL; | |
782 | } | |
783 | } | |
784 | ||
785 | static int | |
786 | set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp, | |
81e2083f | 787 | const struct sset *sset) |
19a87e36 BP |
788 | { |
789 | struct pvconn **pvconns = *pvconnsp; | |
790 | size_t n_pvconns = *n_pvconnsp; | |
81e2083f | 791 | const char *name; |
19a87e36 BP |
792 | int retval = 0; |
793 | size_t i; | |
794 | ||
795 | for (i = 0; i < n_pvconns; i++) { | |
796 | pvconn_close(pvconns[i]); | |
797 | } | |
798 | free(pvconns); | |
799 | ||
81e2083f | 800 | pvconns = xmalloc(sset_count(sset) * sizeof *pvconns); |
19a87e36 | 801 | n_pvconns = 0; |
81e2083f | 802 | SSET_FOR_EACH (name, sset) { |
19a87e36 BP |
803 | struct pvconn *pvconn; |
804 | int error; | |
82c8c53c | 805 | error = pvconn_open(name, 0, 0, &pvconn); |
19a87e36 BP |
806 | if (!error) { |
807 | pvconns[n_pvconns++] = pvconn; | |
808 | } else { | |
10a89ef0 | 809 | VLOG_ERR("failed to listen on %s: %s", name, ovs_strerror(error)); |
19a87e36 BP |
810 | if (!retval) { |
811 | retval = error; | |
812 | } | |
813 | } | |
814 | } | |
815 | ||
816 | *pvconnsp = pvconns; | |
817 | *n_pvconnsp = n_pvconns; | |
818 | ||
819 | return retval; | |
820 | } | |
821 | ||
822 | /* Returns a "preference level" for snooping 'ofconn'. A higher return value | |
823 | * means that 'ofconn' is more interesting for monitoring than a lower return | |
824 | * value. */ | |
825 | static int | |
826 | snoop_preference(const struct ofconn *ofconn) | |
827 | { | |
828 | switch (ofconn->role) { | |
f4f1ea7e | 829 | case OFPCR12_ROLE_MASTER: |
19a87e36 | 830 | return 3; |
f4f1ea7e | 831 | case OFPCR12_ROLE_EQUAL: |
19a87e36 | 832 | return 2; |
f4f1ea7e | 833 | case OFPCR12_ROLE_SLAVE: |
19a87e36 | 834 | return 1; |
f4f1ea7e | 835 | case OFPCR12_ROLE_NOCHANGE: |
19a87e36 BP |
836 | default: |
837 | /* Shouldn't happen. */ | |
838 | return 0; | |
839 | } | |
840 | } | |
841 | ||
842 | /* One of 'mgr''s "snoop" pvconns has accepted a new connection on 'vconn'. | |
843 | * Connects this vconn to a controller. */ | |
844 | static void | |
845 | add_snooper(struct connmgr *mgr, struct vconn *vconn) | |
846 | { | |
847 | struct ofconn *ofconn, *best; | |
848 | ||
849 | /* Pick a controller for monitoring. */ | |
850 | best = NULL; | |
851 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
852 | if (ofconn->type == OFCONN_PRIMARY | |
853 | && (!best || snoop_preference(ofconn) > snoop_preference(best))) { | |
854 | best = ofconn; | |
855 | } | |
856 | } | |
857 | ||
858 | if (best) { | |
859 | rconn_add_monitor(best->rconn, vconn); | |
860 | } else { | |
861 | VLOG_INFO_RL(&rl, "no controller connection to snoop"); | |
862 | vconn_close(vconn); | |
863 | } | |
864 | } | |
865 | \f | |
866 | /* Public ofconn functions. */ | |
867 | ||
868 | /* Returns the connection type, either OFCONN_PRIMARY or OFCONN_SERVICE. */ | |
869 | enum ofconn_type | |
870 | ofconn_get_type(const struct ofconn *ofconn) | |
871 | { | |
872 | return ofconn->type; | |
873 | } | |
874 | ||
147cc9d3 BP |
875 | /* If a master election id is defined, stores it into '*idp' and returns |
876 | * true. Otherwise, stores UINT64_MAX into '*idp' and returns false. */ | |
877 | bool | |
878 | ofconn_get_master_election_id(const struct ofconn *ofconn, uint64_t *idp) | |
879 | { | |
880 | *idp = (ofconn->connmgr->master_election_id_defined | |
881 | ? ofconn->connmgr->master_election_id | |
882 | : UINT64_MAX); | |
883 | return ofconn->connmgr->master_election_id_defined; | |
884 | } | |
885 | ||
6ea4776b JR |
886 | /* Sets the master election id. |
887 | * | |
888 | * Returns true if successful, false if the id is stale | |
889 | */ | |
890 | bool | |
891 | ofconn_set_master_election_id(struct ofconn *ofconn, uint64_t id) | |
892 | { | |
893 | if (ofconn->connmgr->master_election_id_defined | |
894 | && | |
895 | /* Unsigned difference interpreted as a two's complement signed | |
896 | * value */ | |
897 | (int64_t)(id - ofconn->connmgr->master_election_id) < 0) { | |
898 | return false; | |
899 | } | |
900 | ofconn->connmgr->master_election_id = id; | |
901 | ofconn->connmgr->master_election_id_defined = true; | |
902 | ||
903 | return true; | |
904 | } | |
905 | ||
19a87e36 BP |
906 | /* Returns the role configured for 'ofconn'. |
907 | * | |
f4f1ea7e BP |
908 | * The default role, if no other role has been set, is OFPCR12_ROLE_EQUAL. */ |
909 | enum ofp12_controller_role | |
19a87e36 BP |
910 | ofconn_get_role(const struct ofconn *ofconn) |
911 | { | |
912 | return ofconn->role; | |
913 | } | |
914 | ||
00467f73 AC |
915 | void |
916 | ofconn_send_role_status(struct ofconn *ofconn, uint32_t role, uint8_t reason) | |
917 | { | |
918 | struct ofputil_role_status status; | |
919 | struct ofpbuf *buf; | |
920 | ||
921 | status.reason = reason; | |
922 | status.role = role; | |
923 | ofconn_get_master_election_id(ofconn, &status.generation_id); | |
924 | ||
925 | buf = ofputil_encode_role_status(&status, ofconn_get_protocol(ofconn)); | |
926 | ||
927 | ofconn_send(ofconn, buf, NULL); | |
928 | } | |
929 | ||
f4f1ea7e BP |
930 | /* Changes 'ofconn''s role to 'role'. If 'role' is OFPCR12_ROLE_MASTER then |
931 | * any existing master is demoted to a slave. */ | |
19a87e36 | 932 | void |
f4f1ea7e | 933 | ofconn_set_role(struct ofconn *ofconn, enum ofp12_controller_role role) |
19a87e36 | 934 | { |
00467f73 | 935 | if (role != ofconn->role && role == OFPCR12_ROLE_MASTER) { |
19a87e36 BP |
936 | struct ofconn *other; |
937 | ||
938 | HMAP_FOR_EACH (other, hmap_node, &ofconn->connmgr->controllers) { | |
f4f1ea7e BP |
939 | if (other->role == OFPCR12_ROLE_MASTER) { |
940 | other->role = OFPCR12_ROLE_SLAVE; | |
00467f73 | 941 | ofconn_send_role_status(other, OFPCR12_ROLE_SLAVE, OFPCRR_MASTER_REQUEST); |
19a87e36 BP |
942 | } |
943 | } | |
944 | } | |
945 | ofconn->role = role; | |
946 | } | |
947 | ||
f0fd1a17 | 948 | void |
80d5aefd | 949 | ofconn_set_invalid_ttl_to_controller(struct ofconn *ofconn, bool enable) |
f0fd1a17 | 950 | { |
80d5aefd BP |
951 | uint32_t bit = 1u << OFPR_INVALID_TTL; |
952 | if (enable) { | |
953 | ofconn->master_async_config[OAM_PACKET_IN] |= bit; | |
954 | } else { | |
955 | ofconn->master_async_config[OAM_PACKET_IN] &= ~bit; | |
956 | } | |
f0fd1a17 PS |
957 | } |
958 | ||
959 | bool | |
960 | ofconn_get_invalid_ttl_to_controller(struct ofconn *ofconn) | |
961 | { | |
80d5aefd BP |
962 | uint32_t bit = 1u << OFPR_INVALID_TTL; |
963 | return (ofconn->master_async_config[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 | * | |
1002 | * The default, if no other format has been set, is NXPIF_OPENFLOW10. */ | |
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, | |
1044 | const uint32_t master_masks[OAM_N_TYPES], | |
1045 | const uint32_t slave_masks[OAM_N_TYPES]) | |
1046 | { | |
1047 | size_t size = sizeof ofconn->master_async_config; | |
1048 | memcpy(ofconn->master_async_config, master_masks, size); | |
1049 | memcpy(ofconn->slave_async_config, slave_masks, size); | |
1050 | } | |
1051 | ||
c423b3b3 AC |
1052 | void |
1053 | ofconn_get_async_config(struct ofconn *ofconn, | |
1054 | uint32_t *master_masks, uint32_t *slave_masks) | |
1055 | { | |
1056 | size_t size = sizeof ofconn->master_async_config; | |
1057 | memcpy(master_masks, ofconn->master_async_config, size); | |
1058 | memcpy(slave_masks, ofconn->slave_async_config, size); | |
1059 | } | |
1060 | ||
19a87e36 BP |
1061 | /* Sends 'msg' on 'ofconn', accounting it as a reply. (If there is a |
1062 | * sufficient number of OpenFlow replies in-flight on a single ofconn, then the | |
1063 | * connmgr will stop accepting new OpenFlow requests on that ofconn until the | |
1064 | * controller has accepted some of the replies.) */ | |
1065 | void | |
1066 | ofconn_send_reply(const struct ofconn *ofconn, struct ofpbuf *msg) | |
1067 | { | |
1068 | ofconn_send(ofconn, msg, ofconn->reply_counter); | |
1069 | } | |
1070 | ||
63f2140a BP |
1071 | /* Sends each of the messages in list 'replies' on 'ofconn' in order, |
1072 | * accounting them as replies. */ | |
1073 | void | |
1074 | ofconn_send_replies(const struct ofconn *ofconn, struct list *replies) | |
1075 | { | |
1076 | struct ofpbuf *reply, *next; | |
1077 | ||
1078 | LIST_FOR_EACH_SAFE (reply, next, list_node, replies) { | |
1079 | list_remove(&reply->list_node); | |
1080 | ofconn_send_reply(ofconn, reply); | |
1081 | } | |
1082 | } | |
1083 | ||
90bf1e07 | 1084 | /* Sends 'error' on 'ofconn', as a reply to 'request'. Only at most the |
1be5ff75 BP |
1085 | * first 64 bytes of 'request' are used. */ |
1086 | void | |
1087 | ofconn_send_error(const struct ofconn *ofconn, | |
90bf1e07 | 1088 | const struct ofp_header *request, enum ofperr error) |
1be5ff75 | 1089 | { |
07c8ec21 | 1090 | static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(10, 10); |
90bf1e07 | 1091 | struct ofpbuf *reply; |
76589937 | 1092 | |
90bf1e07 | 1093 | reply = ofperr_encode_reply(error, request); |
07c8ec21 BP |
1094 | if (!VLOG_DROP_INFO(&err_rl)) { |
1095 | const char *type_name; | |
1096 | size_t request_len; | |
1097 | enum ofpraw raw; | |
1098 | ||
1099 | request_len = ntohs(request->length); | |
1100 | type_name = (!ofpraw_decode_partial(&raw, request, | |
1101 | MIN(64, request_len)) | |
1102 | ? ofpraw_get_name(raw) | |
1103 | : "invalid"); | |
1104 | ||
1105 | VLOG_INFO("%s: sending %s error reply to %s message", | |
1106 | rconn_get_name(ofconn->rconn), ofperr_to_string(error), | |
1107 | type_name); | |
1108 | } | |
1109 | ofconn_send_reply(ofconn, reply); | |
1be5ff75 BP |
1110 | } |
1111 | ||
19a87e36 | 1112 | /* Same as pktbuf_retrieve(), using the pktbuf owned by 'ofconn'. */ |
90bf1e07 | 1113 | enum ofperr |
19a87e36 | 1114 | ofconn_pktbuf_retrieve(struct ofconn *ofconn, uint32_t id, |
4e022ec0 | 1115 | struct ofpbuf **bufferp, ofp_port_t *in_port) |
19a87e36 BP |
1116 | { |
1117 | return pktbuf_retrieve(ofconn->pktbuf, id, bufferp, in_port); | |
1118 | } | |
7ee20df1 BP |
1119 | |
1120 | /* Returns true if 'ofconn' has any pending opgroups. */ | |
1121 | bool | |
1122 | ofconn_has_pending_opgroups(const struct ofconn *ofconn) | |
1123 | { | |
1124 | return !list_is_empty(&ofconn->opgroups); | |
1125 | } | |
1126 | ||
7ee20df1 BP |
1127 | /* Adds 'ofconn_node' to 'ofconn''s list of pending opgroups. |
1128 | * | |
1129 | * If 'ofconn' is destroyed or its connection drops, then 'ofconn' will remove | |
1130 | * 'ofconn_node' from the list and re-initialize it with list_init(). The | |
1131 | * client may, therefore, use list_is_empty(ofconn_node) to determine whether | |
1132 | * 'ofconn_node' is still associated with an active ofconn. | |
1133 | * | |
1134 | * The client may also remove ofconn_node from the list itself, with | |
1135 | * list_remove(). */ | |
1136 | void | |
1137 | ofconn_add_opgroup(struct ofconn *ofconn, struct list *ofconn_node) | |
1138 | { | |
1139 | list_push_back(&ofconn->opgroups, ofconn_node); | |
1140 | } | |
19a87e36 BP |
1141 | \f |
1142 | /* Private ofconn functions. */ | |
1143 | ||
1144 | static const char * | |
1145 | ofconn_get_target(const struct ofconn *ofconn) | |
1146 | { | |
1147 | return rconn_get_target(ofconn->rconn); | |
1148 | } | |
1149 | ||
1150 | static struct ofconn * | |
9886b662 BP |
1151 | ofconn_create(struct connmgr *mgr, struct rconn *rconn, enum ofconn_type type, |
1152 | bool enable_async_msgs) | |
19a87e36 | 1153 | { |
c4b31872 BP |
1154 | struct ofconn *ofconn; |
1155 | ||
1156 | ofconn = xzalloc(sizeof *ofconn); | |
19a87e36 BP |
1157 | ofconn->connmgr = mgr; |
1158 | list_push_back(&mgr->all_conns, &ofconn->node); | |
1159 | ofconn->rconn = rconn; | |
1160 | ofconn->type = type; | |
9886b662 | 1161 | ofconn->enable_async_msgs = enable_async_msgs; |
c4b31872 | 1162 | |
7ee20df1 | 1163 | list_init(&ofconn->opgroups); |
c4b31872 | 1164 | |
2b07c8b1 BP |
1165 | hmap_init(&ofconn->monitors); |
1166 | list_init(&ofconn->updates); | |
1167 | ||
c4b31872 BP |
1168 | ofconn_flush(ofconn); |
1169 | ||
19a87e36 BP |
1170 | return ofconn; |
1171 | } | |
1172 | ||
c4b31872 BP |
1173 | /* Clears all of the state in 'ofconn' that should not persist from one |
1174 | * connection to the next. */ | |
7ee20df1 BP |
1175 | static void |
1176 | ofconn_flush(struct ofconn *ofconn) | |
3b0c6b56 | 1177 | OVS_REQUIRES(ofproto_mutex) |
7ee20df1 | 1178 | { |
2b07c8b1 | 1179 | struct ofmonitor *monitor, *next_monitor; |
c4b31872 BP |
1180 | int i; |
1181 | ||
f4f1ea7e | 1182 | ofconn->role = OFPCR12_ROLE_EQUAL; |
eb12aa89 | 1183 | ofconn_set_protocol(ofconn, OFPUTIL_P_NONE); |
c4b31872 | 1184 | ofconn->packet_in_format = NXPIF_OPENFLOW10; |
c4b31872 BP |
1185 | |
1186 | /* Disassociate 'ofconn' from all of the ofopgroups that it initiated that | |
1187 | * have not yet completed. (Those ofopgroups will still run to completion | |
1188 | * in the usual way, but any errors that they run into will not be reported | |
1189 | * on any OpenFlow channel.) | |
1190 | * | |
1191 | * Also discard any blocked operation on 'ofconn'. */ | |
7ee20df1 BP |
1192 | while (!list_is_empty(&ofconn->opgroups)) { |
1193 | list_init(list_pop_front(&ofconn->opgroups)); | |
1194 | } | |
1195 | ofpbuf_delete(ofconn->blocked); | |
1196 | ofconn->blocked = NULL; | |
c4b31872 BP |
1197 | |
1198 | rconn_packet_counter_destroy(ofconn->packet_in_counter); | |
1199 | ofconn->packet_in_counter = rconn_packet_counter_create(); | |
1200 | for (i = 0; i < N_SCHEDULERS; i++) { | |
1201 | if (ofconn->schedulers[i]) { | |
1202 | int rate, burst; | |
1203 | ||
1204 | pinsched_get_limits(ofconn->schedulers[i], &rate, &burst); | |
1205 | pinsched_destroy(ofconn->schedulers[i]); | |
1206 | ofconn->schedulers[i] = pinsched_create(rate, burst); | |
1207 | } | |
1208 | } | |
1209 | if (ofconn->pktbuf) { | |
1210 | pktbuf_destroy(ofconn->pktbuf); | |
1211 | ofconn->pktbuf = pktbuf_create(); | |
1212 | } | |
1213 | ofconn->miss_send_len = (ofconn->type == OFCONN_PRIMARY | |
1214 | ? OFP_DEFAULT_MISS_SEND_LEN | |
1215 | : 0); | |
a7349929 | 1216 | ofconn->controller_id = 0; |
c4b31872 BP |
1217 | |
1218 | rconn_packet_counter_destroy(ofconn->reply_counter); | |
1219 | ofconn->reply_counter = rconn_packet_counter_create(); | |
80d5aefd | 1220 | |
9886b662 BP |
1221 | if (ofconn->enable_async_msgs) { |
1222 | uint32_t *master = ofconn->master_async_config; | |
1223 | uint32_t *slave = ofconn->slave_async_config; | |
1224 | ||
1225 | /* "master" and "other" roles get all asynchronous messages by default, | |
1226 | * except that the controller needs to enable nonstandard "packet-in" | |
1227 | * reasons itself. */ | |
1228 | master[OAM_PACKET_IN] = (1u << OFPR_NO_MATCH) | (1u << OFPR_ACTION); | |
1229 | master[OAM_PORT_STATUS] = ((1u << OFPPR_ADD) | |
1230 | | (1u << OFPPR_DELETE) | |
1231 | | (1u << OFPPR_MODIFY)); | |
1232 | master[OAM_FLOW_REMOVED] = ((1u << OFPRR_IDLE_TIMEOUT) | |
1233 | | (1u << OFPRR_HARD_TIMEOUT) | |
1234 | | (1u << OFPRR_DELETE)); | |
1235 | ||
1236 | /* "slave" role gets port status updates by default. */ | |
1237 | slave[OAM_PACKET_IN] = 0; | |
1238 | slave[OAM_PORT_STATUS] = ((1u << OFPPR_ADD) | |
1239 | | (1u << OFPPR_DELETE) | |
1240 | | (1u << OFPPR_MODIFY)); | |
1241 | slave[OAM_FLOW_REMOVED] = 0; | |
1242 | } else { | |
1243 | memset(ofconn->master_async_config, 0, | |
1244 | sizeof ofconn->master_async_config); | |
1245 | memset(ofconn->slave_async_config, 0, | |
1246 | sizeof ofconn->slave_async_config); | |
1247 | } | |
2b07c8b1 BP |
1248 | |
1249 | HMAP_FOR_EACH_SAFE (monitor, next_monitor, ofconn_node, | |
1250 | &ofconn->monitors) { | |
1251 | ofmonitor_destroy(monitor); | |
1252 | } | |
1253 | rconn_packet_counter_destroy(ofconn->monitor_counter); | |
1254 | ofconn->monitor_counter = rconn_packet_counter_create(); | |
1255 | ofpbuf_list_delete(&ofconn->updates); /* ...but it should be empty. */ | |
7ee20df1 BP |
1256 | } |
1257 | ||
19a87e36 BP |
1258 | static void |
1259 | ofconn_destroy(struct ofconn *ofconn) | |
3b0c6b56 | 1260 | OVS_REQUIRES(ofproto_mutex) |
19a87e36 | 1261 | { |
7ee20df1 BP |
1262 | ofconn_flush(ofconn); |
1263 | ||
19a87e36 BP |
1264 | if (ofconn->type == OFCONN_PRIMARY) { |
1265 | hmap_remove(&ofconn->connmgr->controllers, &ofconn->hmap_node); | |
1266 | } | |
1267 | ||
92bbc96a | 1268 | hmap_destroy(&ofconn->monitors); |
19a87e36 BP |
1269 | list_remove(&ofconn->node); |
1270 | rconn_destroy(ofconn->rconn); | |
1271 | rconn_packet_counter_destroy(ofconn->packet_in_counter); | |
1272 | rconn_packet_counter_destroy(ofconn->reply_counter); | |
1273 | pktbuf_destroy(ofconn->pktbuf); | |
2b07c8b1 | 1274 | rconn_packet_counter_destroy(ofconn->monitor_counter); |
19a87e36 BP |
1275 | free(ofconn); |
1276 | } | |
1277 | ||
1278 | /* Reconfigures 'ofconn' to match 'c'. 'ofconn' and 'c' must have the same | |
1279 | * target. */ | |
1280 | static void | |
1281 | ofconn_reconfigure(struct ofconn *ofconn, const struct ofproto_controller *c) | |
1282 | { | |
1283 | int probe_interval; | |
1284 | ||
1285 | ofconn->band = c->band; | |
065825e9 | 1286 | ofconn->enable_async_msgs = c->enable_async_msgs; |
19a87e36 BP |
1287 | |
1288 | rconn_set_max_backoff(ofconn->rconn, c->max_backoff); | |
1289 | ||
1290 | probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0; | |
1291 | rconn_set_probe_interval(ofconn->rconn, probe_interval); | |
1292 | ||
1293 | ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit); | |
0442efd9 MM |
1294 | |
1295 | /* If dscp value changed reconnect. */ | |
1296 | if (c->dscp != rconn_get_dscp(ofconn->rconn)) { | |
1297 | rconn_set_dscp(ofconn->rconn, c->dscp); | |
1298 | rconn_reconnect(ofconn->rconn); | |
1299 | } | |
19a87e36 BP |
1300 | } |
1301 | ||
7ee20df1 BP |
1302 | /* Returns true if it makes sense for 'ofconn' to receive and process OpenFlow |
1303 | * messages. */ | |
1304 | static bool | |
1305 | ofconn_may_recv(const struct ofconn *ofconn) | |
1306 | { | |
a3d1ff00 | 1307 | int count = rconn_packet_counter_n_packets(ofconn->reply_counter); |
7ee20df1 BP |
1308 | return (!ofconn->blocked || ofconn->retry) && count < OFCONN_REPLY_MAX; |
1309 | } | |
1310 | ||
19a87e36 BP |
1311 | static void |
1312 | ofconn_run(struct ofconn *ofconn, | |
e03248b7 JR |
1313 | bool (*handle_openflow)(struct ofconn *, |
1314 | const struct ofpbuf *ofp_msg)) | |
19a87e36 BP |
1315 | { |
1316 | struct connmgr *mgr = ofconn->connmgr; | |
19a87e36 BP |
1317 | size_t i; |
1318 | ||
1319 | for (i = 0; i < N_SCHEDULERS; i++) { | |
a6f75961 BP |
1320 | struct list txq; |
1321 | ||
1322 | pinsched_run(ofconn->schedulers[i], &txq); | |
1323 | do_send_packet_ins(ofconn, &txq); | |
19a87e36 BP |
1324 | } |
1325 | ||
1326 | rconn_run(ofconn->rconn); | |
1327 | ||
7ee20df1 BP |
1328 | if (handle_openflow) { |
1329 | /* Limit the number of iterations to avoid starving other tasks. */ | |
1330 | for (i = 0; i < 50 && ofconn_may_recv(ofconn); i++) { | |
1331 | struct ofpbuf *of_msg; | |
1332 | ||
1333 | of_msg = (ofconn->blocked | |
1334 | ? ofconn->blocked | |
1335 | : rconn_recv(ofconn->rconn)); | |
19a87e36 BP |
1336 | if (!of_msg) { |
1337 | break; | |
1338 | } | |
1339 | if (mgr->fail_open) { | |
1340 | fail_open_maybe_recover(mgr->fail_open); | |
1341 | } | |
7ee20df1 BP |
1342 | |
1343 | if (handle_openflow(ofconn, of_msg)) { | |
1344 | ofpbuf_delete(of_msg); | |
1345 | ofconn->blocked = NULL; | |
1346 | } else { | |
1347 | ofconn->blocked = of_msg; | |
1348 | ofconn->retry = false; | |
1349 | } | |
19a87e36 BP |
1350 | } |
1351 | } | |
1352 | ||
3b0c6b56 | 1353 | ovs_mutex_lock(&ofproto_mutex); |
19a87e36 BP |
1354 | if (!rconn_is_alive(ofconn->rconn)) { |
1355 | ofconn_destroy(ofconn); | |
7ee20df1 BP |
1356 | } else if (!rconn_is_connected(ofconn->rconn)) { |
1357 | ofconn_flush(ofconn); | |
19a87e36 | 1358 | } |
3b0c6b56 | 1359 | ovs_mutex_unlock(&ofproto_mutex); |
19a87e36 BP |
1360 | } |
1361 | ||
1362 | static void | |
7ee20df1 | 1363 | ofconn_wait(struct ofconn *ofconn, bool handling_openflow) |
19a87e36 BP |
1364 | { |
1365 | int i; | |
1366 | ||
1367 | for (i = 0; i < N_SCHEDULERS; i++) { | |
1368 | pinsched_wait(ofconn->schedulers[i]); | |
1369 | } | |
1370 | rconn_run_wait(ofconn->rconn); | |
7ee20df1 | 1371 | if (handling_openflow && ofconn_may_recv(ofconn)) { |
19a87e36 | 1372 | rconn_recv_wait(ofconn->rconn); |
19a87e36 BP |
1373 | } |
1374 | } | |
1375 | ||
80d5aefd BP |
1376 | /* Returns true if 'ofconn' should receive asynchronous messages of the given |
1377 | * OAM_* 'type' and 'reason', which should be a OFPR_* value for OAM_PACKET_IN, | |
1378 | * a OFPPR_* value for OAM_PORT_STATUS, or an OFPRR_* value for | |
1379 | * OAM_FLOW_REMOVED. Returns false if the message should not be sent on | |
1380 | * 'ofconn'. */ | |
19a87e36 | 1381 | static bool |
80d5aefd BP |
1382 | ofconn_receives_async_msg(const struct ofconn *ofconn, |
1383 | enum ofconn_async_msg_type type, | |
1384 | unsigned int reason) | |
19a87e36 | 1385 | { |
80d5aefd BP |
1386 | const uint32_t *async_config; |
1387 | ||
cb22974d BP |
1388 | ovs_assert(reason < 32); |
1389 | ovs_assert((unsigned int) type < OAM_N_TYPES); | |
19a87e36 | 1390 | |
eb12aa89 BP |
1391 | if (ofconn_get_protocol(ofconn) == OFPUTIL_P_NONE |
1392 | || !rconn_is_connected(ofconn->rconn)) { | |
f0fd1a17 | 1393 | return false; |
f0fd1a17 | 1394 | } |
f0fd1a17 | 1395 | |
80d5aefd BP |
1396 | /* Keep the following code in sync with the documentation in the |
1397 | * "Asynchronous Messages" section in DESIGN. */ | |
1398 | ||
1399 | if (ofconn->type == OFCONN_SERVICE && !ofconn->miss_send_len) { | |
1400 | /* Service connections don't get asynchronous messages unless they have | |
1401 | * explicitly asked for them by setting a nonzero miss send length. */ | |
f0fd1a17 | 1402 | return false; |
f0fd1a17 | 1403 | } |
80d5aefd | 1404 | |
f4f1ea7e | 1405 | async_config = (ofconn->role == OFPCR12_ROLE_SLAVE |
80d5aefd BP |
1406 | ? ofconn->slave_async_config |
1407 | : ofconn->master_async_config); | |
1408 | if (!(async_config[type] & (1u << reason))) { | |
1409 | return false; | |
1410 | } | |
1411 | ||
1412 | return true; | |
f0fd1a17 PS |
1413 | } |
1414 | ||
19a87e36 BP |
1415 | /* Returns a human-readable name for an OpenFlow connection between 'mgr' and |
1416 | * 'target', suitable for use in log messages for identifying the connection. | |
1417 | * | |
1418 | * The name is dynamically allocated. The caller should free it (with free()) | |
1419 | * when it is no longer needed. */ | |
1420 | static char * | |
1421 | ofconn_make_name(const struct connmgr *mgr, const char *target) | |
1422 | { | |
1423 | return xasprintf("%s<->%s", mgr->name, target); | |
1424 | } | |
1425 | ||
1426 | static void | |
1427 | ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst) | |
1428 | { | |
1429 | int i; | |
1430 | ||
1431 | for (i = 0; i < N_SCHEDULERS; i++) { | |
1432 | struct pinsched **s = &ofconn->schedulers[i]; | |
1433 | ||
1434 | if (rate > 0) { | |
1435 | if (!*s) { | |
1436 | *s = pinsched_create(rate, burst); | |
1437 | } else { | |
1438 | pinsched_set_limits(*s, rate, burst); | |
1439 | } | |
1440 | } else { | |
1441 | pinsched_destroy(*s); | |
1442 | *s = NULL; | |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | static void | |
1448 | ofconn_send(const struct ofconn *ofconn, struct ofpbuf *msg, | |
1449 | struct rconn_packet_counter *counter) | |
1450 | { | |
982697a4 | 1451 | ofpmsg_update_length(msg); |
acb9da40 | 1452 | rconn_send(ofconn->rconn, msg, counter); |
19a87e36 BP |
1453 | } |
1454 | \f | |
1455 | /* Sending asynchronous messages. */ | |
1456 | ||
cfa955b0 YT |
1457 | static void schedule_packet_in(struct ofconn *, struct ofproto_packet_in, |
1458 | enum ofp_packet_in_reason wire_reason); | |
19a87e36 BP |
1459 | |
1460 | /* Sends an OFPT_PORT_STATUS message with 'opp' and 'reason' to appropriate | |
9cfcdadf | 1461 | * controllers managed by 'mgr'. */ |
19a87e36 | 1462 | void |
9e1fd49b BP |
1463 | connmgr_send_port_status(struct connmgr *mgr, |
1464 | const struct ofputil_phy_port *pp, uint8_t reason) | |
19a87e36 BP |
1465 | { |
1466 | /* XXX Should limit the number of queued port status change messages. */ | |
9e1fd49b | 1467 | struct ofputil_port_status ps; |
19a87e36 BP |
1468 | struct ofconn *ofconn; |
1469 | ||
9e1fd49b BP |
1470 | ps.reason = reason; |
1471 | ps.desc = *pp; | |
19a87e36 | 1472 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { |
80d5aefd | 1473 | if (ofconn_receives_async_msg(ofconn, OAM_PORT_STATUS, reason)) { |
9e1fd49b | 1474 | struct ofpbuf *msg; |
80d5aefd | 1475 | |
eb12aa89 | 1476 | msg = ofputil_encode_port_status(&ps, ofconn_get_protocol(ofconn)); |
9e1fd49b | 1477 | ofconn_send(ofconn, msg, NULL); |
19a87e36 | 1478 | } |
19a87e36 BP |
1479 | } |
1480 | } | |
1481 | ||
1482 | /* Sends an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message based on 'fr' to | |
1483 | * appropriate controllers managed by 'mgr'. */ | |
1484 | void | |
1485 | connmgr_send_flow_removed(struct connmgr *mgr, | |
1486 | const struct ofputil_flow_removed *fr) | |
1487 | { | |
1488 | struct ofconn *ofconn; | |
1489 | ||
1490 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
80d5aefd BP |
1491 | if (ofconn_receives_async_msg(ofconn, OAM_FLOW_REMOVED, fr->reason)) { |
1492 | struct ofpbuf *msg; | |
1493 | ||
1494 | /* Account flow expirations as replies to OpenFlow requests. That | |
1495 | * works because preventing OpenFlow requests from being processed | |
1496 | * also prevents new flows from being added (and expiring). (It | |
1497 | * also prevents processing OpenFlow requests that would not add | |
1498 | * new flows, so it is imperfect.) */ | |
eb12aa89 | 1499 | msg = ofputil_encode_flow_removed(fr, ofconn_get_protocol(ofconn)); |
80d5aefd | 1500 | ofconn_send_reply(ofconn, msg); |
19a87e36 | 1501 | } |
19a87e36 BP |
1502 | } |
1503 | } | |
1504 | ||
cfa955b0 YT |
1505 | /* Normally a send-to-controller action uses reason OFPR_ACTION. However, in |
1506 | * OpenFlow 1.3 and later, packet_ins generated by a send-to-controller action | |
1507 | * in a "table-miss" flow (one with priority 0 and completely wildcarded) are | |
1508 | * sent as OFPR_NO_MATCH. This function returns the reason that should | |
1509 | * actually be sent on 'ofconn' for 'pin'. */ | |
1510 | static enum ofp_packet_in_reason | |
1511 | wire_reason(struct ofconn *ofconn, const struct ofproto_packet_in *pin) | |
1512 | { | |
1513 | if (pin->generated_by_table_miss && pin->up.reason == OFPR_ACTION) { | |
1514 | enum ofputil_protocol protocol = ofconn_get_protocol(ofconn); | |
cfa955b0 | 1515 | |
b9b00872 YT |
1516 | if (protocol != OFPUTIL_P_NONE |
1517 | && ofputil_protocol_to_ofp_version(protocol) >= OFP13_VERSION) { | |
cfa955b0 YT |
1518 | return OFPR_NO_MATCH; |
1519 | } | |
1520 | } | |
1521 | return pin->up.reason; | |
1522 | } | |
1523 | ||
78bd1cd0 | 1524 | /* Given 'pin', sends an OFPT_PACKET_IN message to each OpenFlow controller as |
2ed1202f BP |
1525 | * necessary according to their individual configurations. |
1526 | * | |
1527 | * The caller doesn't need to fill in pin->buffer_id or pin->total_len. */ | |
19a87e36 | 1528 | void |
78bd1cd0 | 1529 | connmgr_send_packet_in(struct connmgr *mgr, |
0fb7792a | 1530 | const struct ofproto_packet_in *pin) |
19a87e36 | 1531 | { |
29ebe880 | 1532 | struct ofconn *ofconn; |
19a87e36 | 1533 | |
19a87e36 | 1534 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { |
cfa955b0 YT |
1535 | enum ofp_packet_in_reason reason = wire_reason(ofconn, pin); |
1536 | ||
1537 | if (ofconn_receives_async_msg(ofconn, OAM_PACKET_IN, reason) | |
f11c7538 | 1538 | && ofconn->controller_id == pin->controller_id) { |
cfa955b0 | 1539 | schedule_packet_in(ofconn, *pin, reason); |
19a87e36 BP |
1540 | } |
1541 | } | |
19a87e36 BP |
1542 | } |
1543 | ||
19a87e36 | 1544 | static void |
a6f75961 | 1545 | do_send_packet_ins(struct ofconn *ofconn, struct list *txq) |
19a87e36 | 1546 | { |
a6f75961 BP |
1547 | struct ofpbuf *pin, *next_pin; |
1548 | ||
1549 | LIST_FOR_EACH_SAFE (pin, next_pin, list_node, txq) { | |
1550 | list_remove(&pin->list_node); | |
19a87e36 | 1551 | |
a6f75961 BP |
1552 | rconn_send_with_limit(ofconn->rconn, pin, |
1553 | ofconn->packet_in_counter, 100); | |
1554 | } | |
19a87e36 BP |
1555 | } |
1556 | ||
d8653c38 BP |
1557 | /* Takes 'pin', composes an OpenFlow packet-in message from it, and passes it |
1558 | * to 'ofconn''s packet scheduler for sending. */ | |
19a87e36 | 1559 | static void |
cfa955b0 YT |
1560 | schedule_packet_in(struct ofconn *ofconn, struct ofproto_packet_in pin, |
1561 | enum ofp_packet_in_reason wire_reason) | |
19a87e36 BP |
1562 | { |
1563 | struct connmgr *mgr = ofconn->connmgr; | |
a1192496 | 1564 | uint16_t controller_max_len; |
a6f75961 | 1565 | struct list txq; |
19a87e36 | 1566 | |
0fb7792a | 1567 | pin.up.total_len = pin.up.packet_len; |
2ed1202f | 1568 | |
cfa955b0 | 1569 | pin.up.reason = wire_reason; |
0fb7792a | 1570 | if (pin.up.reason == OFPR_ACTION) { |
d38a3c7b | 1571 | controller_max_len = pin.send_len; /* max_len */ |
a1192496 YT |
1572 | } else { |
1573 | controller_max_len = ofconn->miss_send_len; | |
1574 | } | |
1575 | ||
1576 | /* Get OpenFlow buffer_id. | |
1577 | * For OpenFlow 1.2+, OFPCML_NO_BUFFER (== UINT16_MAX) specifies | |
1578 | * unbuffered. This behaviour doesn't violate prior versions, too. */ | |
1579 | if (controller_max_len == UINT16_MAX) { | |
0fb7792a | 1580 | pin.up.buffer_id = UINT32_MAX; |
19a87e36 | 1581 | } else if (mgr->fail_open && fail_open_is_active(mgr->fail_open)) { |
0fb7792a | 1582 | pin.up.buffer_id = pktbuf_get_null(); |
19a87e36 | 1583 | } else if (!ofconn->pktbuf) { |
0fb7792a | 1584 | pin.up.buffer_id = UINT32_MAX; |
19a87e36 | 1585 | } else { |
0fb7792a BP |
1586 | pin.up.buffer_id = pktbuf_save(ofconn->pktbuf, |
1587 | pin.up.packet, pin.up.packet_len, | |
1588 | pin.up.fmd.in_port); | |
19a87e36 BP |
1589 | } |
1590 | ||
a1192496 YT |
1591 | /* Figure out how much of the packet to send. |
1592 | * If not buffered, send the entire packet. Otherwise, depending on | |
1593 | * the reason of packet-in, send what requested by the controller. */ | |
d38a3c7b BP |
1594 | if (pin.up.buffer_id != UINT32_MAX |
1595 | && controller_max_len < pin.up.packet_len) { | |
1596 | pin.up.packet_len = controller_max_len; | |
19a87e36 | 1597 | } |
19a87e36 | 1598 | |
a6f75961 | 1599 | /* Make OFPT_PACKET_IN and hand over to packet scheduler. */ |
0fb7792a BP |
1600 | pinsched_send(ofconn->schedulers[pin.up.reason == OFPR_NO_MATCH ? 0 : 1], |
1601 | pin.up.fmd.in_port, | |
1602 | ofputil_encode_packet_in(&pin.up, | |
1603 | ofconn_get_protocol(ofconn), | |
d94240ec | 1604 | ofconn->packet_in_format), |
a6f75961 BP |
1605 | &txq); |
1606 | do_send_packet_ins(ofconn, &txq); | |
19a87e36 BP |
1607 | } |
1608 | \f | |
1609 | /* Fail-open settings. */ | |
1610 | ||
1611 | /* Returns the failure handling mode (OFPROTO_FAIL_SECURE or | |
1612 | * OFPROTO_FAIL_STANDALONE) for 'mgr'. */ | |
1613 | enum ofproto_fail_mode | |
1614 | connmgr_get_fail_mode(const struct connmgr *mgr) | |
1615 | { | |
1616 | return mgr->fail_mode; | |
1617 | } | |
1618 | ||
1619 | /* Sets the failure handling mode for 'mgr' to 'fail_mode' (either | |
1620 | * OFPROTO_FAIL_SECURE or OFPROTO_FAIL_STANDALONE). */ | |
1621 | void | |
1622 | connmgr_set_fail_mode(struct connmgr *mgr, enum ofproto_fail_mode fail_mode) | |
1623 | { | |
08fd19f0 BP |
1624 | if (mgr->fail_mode != fail_mode) { |
1625 | mgr->fail_mode = fail_mode; | |
1626 | update_fail_open(mgr); | |
1627 | if (!connmgr_has_controllers(mgr)) { | |
1628 | ofproto_flush_flows(mgr->ofproto); | |
1629 | } | |
1630 | } | |
19a87e36 BP |
1631 | } |
1632 | \f | |
1633 | /* Fail-open implementation. */ | |
1634 | ||
1635 | /* Returns the longest probe interval among the primary controllers configured | |
1636 | * on 'mgr'. Returns 0 if there are no primary controllers. */ | |
1637 | int | |
1638 | connmgr_get_max_probe_interval(const struct connmgr *mgr) | |
1639 | { | |
1640 | const struct ofconn *ofconn; | |
1641 | int max_probe_interval; | |
1642 | ||
1643 | max_probe_interval = 0; | |
1644 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
1645 | int probe_interval = rconn_get_probe_interval(ofconn->rconn); | |
1646 | max_probe_interval = MAX(max_probe_interval, probe_interval); | |
1647 | } | |
1648 | return max_probe_interval; | |
1649 | } | |
1650 | ||
1651 | /* Returns the number of seconds for which all of 'mgr's primary controllers | |
1652 | * have been disconnected. Returns 0 if 'mgr' has no primary controllers. */ | |
1653 | int | |
1654 | connmgr_failure_duration(const struct connmgr *mgr) | |
1655 | { | |
1656 | const struct ofconn *ofconn; | |
1657 | int min_failure_duration; | |
1658 | ||
1659 | if (!connmgr_has_controllers(mgr)) { | |
1660 | return 0; | |
1661 | } | |
1662 | ||
1663 | min_failure_duration = INT_MAX; | |
1664 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
1665 | int failure_duration = rconn_failure_duration(ofconn->rconn); | |
1666 | min_failure_duration = MIN(min_failure_duration, failure_duration); | |
1667 | } | |
1668 | return min_failure_duration; | |
1669 | } | |
1670 | ||
1671 | /* Returns true if at least one primary controller is connected (regardless of | |
1672 | * whether those controllers are believed to have authenticated and accepted | |
1673 | * this switch), false if none of them are connected. */ | |
1674 | bool | |
1675 | connmgr_is_any_controller_connected(const struct connmgr *mgr) | |
1676 | { | |
1677 | const struct ofconn *ofconn; | |
1678 | ||
1679 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
1680 | if (rconn_is_connected(ofconn->rconn)) { | |
1681 | return true; | |
1682 | } | |
1683 | } | |
1684 | return false; | |
1685 | } | |
1686 | ||
1687 | /* Returns true if at least one primary controller is believed to have | |
1688 | * authenticated and accepted this switch, false otherwise. */ | |
1689 | bool | |
1690 | connmgr_is_any_controller_admitted(const struct connmgr *mgr) | |
1691 | { | |
1692 | const struct ofconn *ofconn; | |
1693 | ||
1694 | HMAP_FOR_EACH (ofconn, hmap_node, &mgr->controllers) { | |
1695 | if (rconn_is_admitted(ofconn->rconn)) { | |
1696 | return true; | |
1697 | } | |
1698 | } | |
1699 | return false; | |
1700 | } | |
19a87e36 BP |
1701 | \f |
1702 | /* In-band configuration. */ | |
1703 | ||
1704 | static bool any_extras_changed(const struct connmgr *, | |
1705 | const struct sockaddr_in *extras, size_t n); | |
1706 | ||
1707 | /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'mgr''s | |
1708 | * in-band control should guarantee access, in the same way that in-band | |
1709 | * control guarantees access to OpenFlow controllers. */ | |
1710 | void | |
1711 | connmgr_set_extra_in_band_remotes(struct connmgr *mgr, | |
1712 | const struct sockaddr_in *extras, size_t n) | |
1713 | { | |
1714 | if (!any_extras_changed(mgr, extras, n)) { | |
1715 | return; | |
1716 | } | |
1717 | ||
1718 | free(mgr->extra_in_band_remotes); | |
1719 | mgr->n_extra_remotes = n; | |
1720 | mgr->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras); | |
1721 | ||
1722 | update_in_band_remotes(mgr); | |
1723 | } | |
1724 | ||
1725 | /* Sets the OpenFlow queue used by flows set up by in-band control on | |
1726 | * 'mgr' to 'queue_id'. If 'queue_id' is negative, then in-band control | |
1727 | * flows will use the default queue. */ | |
1728 | void | |
1729 | connmgr_set_in_band_queue(struct connmgr *mgr, int queue_id) | |
1730 | { | |
1731 | if (queue_id != mgr->in_band_queue) { | |
1732 | mgr->in_band_queue = queue_id; | |
1733 | update_in_band_remotes(mgr); | |
1734 | } | |
1735 | } | |
1736 | ||
1737 | static bool | |
1738 | any_extras_changed(const struct connmgr *mgr, | |
1739 | const struct sockaddr_in *extras, size_t n) | |
1740 | { | |
1741 | size_t i; | |
1742 | ||
1743 | if (n != mgr->n_extra_remotes) { | |
1744 | return true; | |
1745 | } | |
1746 | ||
1747 | for (i = 0; i < n; i++) { | |
1748 | const struct sockaddr_in *old = &mgr->extra_in_band_remotes[i]; | |
1749 | const struct sockaddr_in *new = &extras[i]; | |
1750 | ||
1751 | if (old->sin_addr.s_addr != new->sin_addr.s_addr || | |
1752 | old->sin_port != new->sin_port) { | |
1753 | return true; | |
1754 | } | |
1755 | } | |
1756 | ||
1757 | return false; | |
1758 | } | |
1759 | \f | |
1760 | /* In-band implementation. */ | |
1761 | ||
1762 | bool | |
f7f1ea29 | 1763 | connmgr_has_in_band(struct connmgr *mgr) |
19a87e36 | 1764 | { |
f7f1ea29 | 1765 | return mgr->in_band != NULL; |
19a87e36 BP |
1766 | } |
1767 | \f | |
1768 | /* Fail-open and in-band implementation. */ | |
1769 | ||
1770 | /* Called by 'ofproto' after all flows have been flushed, to allow fail-open | |
7ee20df1 BP |
1771 | * and standalone mode to re-create their flows. |
1772 | * | |
1773 | * In-band control has more sophisticated code that manages flows itself. */ | |
19a87e36 BP |
1774 | void |
1775 | connmgr_flushed(struct connmgr *mgr) | |
15aaf599 | 1776 | OVS_EXCLUDED(ofproto_mutex) |
19a87e36 | 1777 | { |
19a87e36 BP |
1778 | if (mgr->fail_open) { |
1779 | fail_open_flushed(mgr->fail_open); | |
1780 | } | |
08fd19f0 BP |
1781 | |
1782 | /* If there are no controllers and we're in standalone mode, set up a flow | |
1783 | * that matches every packet and directs them to OFPP_NORMAL (which goes to | |
1784 | * us). Otherwise, the switch is in secure mode and we won't pass any | |
1785 | * traffic until a controller has been defined and it tells us to do so. */ | |
1786 | if (!connmgr_has_controllers(mgr) | |
1787 | && mgr->fail_mode == OFPROTO_FAIL_STANDALONE) { | |
f25d0cf3 | 1788 | struct ofpbuf ofpacts; |
81a76618 | 1789 | struct match match; |
08fd19f0 | 1790 | |
f25d0cf3 BP |
1791 | ofpbuf_init(&ofpacts, OFPACT_OUTPUT_SIZE); |
1792 | ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL; | |
1793 | ofpact_pad(&ofpacts); | |
1794 | ||
81a76618 BP |
1795 | match_init_catchall(&match); |
1796 | ofproto_add_flow(mgr->ofproto, &match, 0, ofpacts.data, ofpacts.size); | |
f25d0cf3 BP |
1797 | |
1798 | ofpbuf_uninit(&ofpacts); | |
08fd19f0 | 1799 | } |
19a87e36 BP |
1800 | } |
1801 | \f | |
1802 | /* Creates a new ofservice for 'target' in 'mgr'. Returns 0 if successful, | |
1803 | * otherwise a positive errno value. | |
1804 | * | |
1805 | * ofservice_reconfigure() must be called to fully configure the new | |
1806 | * ofservice. */ | |
1807 | static int | |
00401ef0 SH |
1808 | ofservice_create(struct connmgr *mgr, const char *target, |
1809 | uint32_t allowed_versions, uint8_t dscp) | |
19a87e36 BP |
1810 | { |
1811 | struct ofservice *ofservice; | |
1812 | struct pvconn *pvconn; | |
1813 | int error; | |
1814 | ||
82c8c53c | 1815 | error = pvconn_open(target, allowed_versions, dscp, &pvconn); |
19a87e36 BP |
1816 | if (error) { |
1817 | return error; | |
1818 | } | |
1819 | ||
1820 | ofservice = xzalloc(sizeof *ofservice); | |
1821 | hmap_insert(&mgr->services, &ofservice->node, hash_string(target, 0)); | |
1822 | ofservice->pvconn = pvconn; | |
90ef0206 | 1823 | ofservice->allowed_versions = allowed_versions; |
19a87e36 BP |
1824 | |
1825 | return 0; | |
1826 | } | |
1827 | ||
1828 | static void | |
1829 | ofservice_destroy(struct connmgr *mgr, struct ofservice *ofservice) | |
1830 | { | |
1831 | hmap_remove(&mgr->services, &ofservice->node); | |
1832 | pvconn_close(ofservice->pvconn); | |
1833 | free(ofservice); | |
1834 | } | |
1835 | ||
1836 | static void | |
1837 | ofservice_reconfigure(struct ofservice *ofservice, | |
1838 | const struct ofproto_controller *c) | |
1839 | { | |
1840 | ofservice->probe_interval = c->probe_interval; | |
1841 | ofservice->rate_limit = c->rate_limit; | |
1842 | ofservice->burst_limit = c->burst_limit; | |
9886b662 | 1843 | ofservice->enable_async_msgs = c->enable_async_msgs; |
f125905c | 1844 | ofservice->dscp = c->dscp; |
19a87e36 BP |
1845 | } |
1846 | ||
1847 | /* Finds and returns the ofservice within 'mgr' that has the given | |
1848 | * 'target', or a null pointer if none exists. */ | |
1849 | static struct ofservice * | |
1850 | ofservice_lookup(struct connmgr *mgr, const char *target) | |
1851 | { | |
1852 | struct ofservice *ofservice; | |
1853 | ||
1854 | HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0), | |
1855 | &mgr->services) { | |
1856 | if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) { | |
1857 | return ofservice; | |
1858 | } | |
1859 | } | |
1860 | return NULL; | |
1861 | } | |
2b07c8b1 BP |
1862 | \f |
1863 | /* Flow monitors (NXST_FLOW_MONITOR). */ | |
1864 | ||
1865 | /* A counter incremented when something significant happens to an OpenFlow | |
1866 | * rule. | |
1867 | * | |
1868 | * - When a rule is added, its 'add_seqno' and 'modify_seqno' are set to | |
1869 | * the current value (which is then incremented). | |
1870 | * | |
1871 | * - When a rule is modified, its 'modify_seqno' is set to the current | |
1872 | * value (which is then incremented). | |
1873 | * | |
1874 | * Thus, by comparing an old value of monitor_seqno against a rule's | |
1875 | * 'add_seqno', one can tell whether the rule was added before or after the old | |
1876 | * value was read, and similarly for 'modify_seqno'. | |
1877 | * | |
1878 | * 32 bits should normally be sufficient (and would be nice, to save space in | |
1879 | * each rule) but then we'd have to have some special cases for wraparound. | |
1880 | * | |
1881 | * We initialize monitor_seqno to 1 to allow 0 to be used as an invalid | |
1882 | * value. */ | |
1883 | static uint64_t monitor_seqno = 1; | |
1884 | ||
1885 | COVERAGE_DEFINE(ofmonitor_pause); | |
1886 | COVERAGE_DEFINE(ofmonitor_resume); | |
1887 | ||
1888 | enum ofperr | |
1889 | ofmonitor_create(const struct ofputil_flow_monitor_request *request, | |
1890 | struct ofconn *ofconn, struct ofmonitor **monitorp) | |
4cd1bc9d | 1891 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
1892 | { |
1893 | struct ofmonitor *m; | |
1894 | ||
1895 | *monitorp = NULL; | |
1896 | ||
1897 | m = ofmonitor_lookup(ofconn, request->id); | |
1898 | if (m) { | |
1899 | return OFPERR_NXBRC_FM_DUPLICATE_ID; | |
1900 | } | |
1901 | ||
1902 | m = xmalloc(sizeof *m); | |
1903 | m->ofconn = ofconn; | |
1904 | hmap_insert(&ofconn->monitors, &m->ofconn_node, hash_int(request->id, 0)); | |
1905 | m->id = request->id; | |
1906 | m->flags = request->flags; | |
1907 | m->out_port = request->out_port; | |
1908 | m->table_id = request->table_id; | |
5cb7a798 | 1909 | minimatch_init(&m->match, &request->match); |
2b07c8b1 BP |
1910 | |
1911 | *monitorp = m; | |
1912 | return 0; | |
1913 | } | |
1914 | ||
1915 | struct ofmonitor * | |
1916 | ofmonitor_lookup(struct ofconn *ofconn, uint32_t id) | |
4cd1bc9d | 1917 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
1918 | { |
1919 | struct ofmonitor *m; | |
1920 | ||
1921 | HMAP_FOR_EACH_IN_BUCKET (m, ofconn_node, hash_int(id, 0), | |
1922 | &ofconn->monitors) { | |
1923 | if (m->id == id) { | |
1924 | return m; | |
1925 | } | |
1926 | } | |
1927 | return NULL; | |
1928 | } | |
1929 | ||
1930 | void | |
1931 | ofmonitor_destroy(struct ofmonitor *m) | |
4cd1bc9d | 1932 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
1933 | { |
1934 | if (m) { | |
5684eb74 | 1935 | minimatch_destroy(&m->match); |
2b07c8b1 BP |
1936 | hmap_remove(&m->ofconn->monitors, &m->ofconn_node); |
1937 | free(m); | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | void | |
1942 | ofmonitor_report(struct connmgr *mgr, struct rule *rule, | |
1943 | enum nx_flow_update_event event, | |
1944 | enum ofp_flow_removed_reason reason, | |
1945 | const struct ofconn *abbrev_ofconn, ovs_be32 abbrev_xid) | |
15aaf599 | 1946 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
1947 | { |
1948 | enum nx_flow_monitor_flags update; | |
1949 | struct ofconn *ofconn; | |
1950 | ||
1951 | switch (event) { | |
1952 | case NXFME_ADDED: | |
1953 | update = NXFMF_ADD; | |
1954 | rule->add_seqno = rule->modify_seqno = monitor_seqno++; | |
1955 | break; | |
1956 | ||
1957 | case NXFME_DELETED: | |
1958 | update = NXFMF_DELETE; | |
1959 | break; | |
1960 | ||
1961 | case NXFME_MODIFIED: | |
1962 | update = NXFMF_MODIFY; | |
1963 | rule->modify_seqno = monitor_seqno++; | |
1964 | break; | |
1965 | ||
1966 | default: | |
1967 | case NXFME_ABBREV: | |
1968 | NOT_REACHED(); | |
1969 | } | |
1970 | ||
1971 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
1972 | enum nx_flow_monitor_flags flags = 0; | |
1973 | struct ofmonitor *m; | |
1974 | ||
1975 | if (ofconn->monitor_paused) { | |
1976 | /* Only send NXFME_DELETED notifications for flows that were added | |
1977 | * before we paused. */ | |
1978 | if (event != NXFME_DELETED | |
1979 | || rule->add_seqno > ofconn->monitor_paused) { | |
1980 | continue; | |
1981 | } | |
1982 | } | |
1983 | ||
1984 | HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) { | |
1985 | if (m->flags & update | |
1986 | && (m->table_id == 0xff || m->table_id == rule->table_id) | |
1987 | && ofoperation_has_out_port(rule->pending, m->out_port) | |
1988 | && cls_rule_is_loose_match(&rule->cr, &m->match)) { | |
1989 | flags |= m->flags; | |
1990 | } | |
1991 | } | |
1992 | ||
1993 | if (flags) { | |
1994 | if (list_is_empty(&ofconn->updates)) { | |
1995 | ofputil_start_flow_update(&ofconn->updates); | |
1996 | ofconn->sent_abbrev_update = false; | |
1997 | } | |
1998 | ||
1999 | if (ofconn != abbrev_ofconn || ofconn->monitor_paused) { | |
2000 | struct ofputil_flow_update fu; | |
5cb7a798 | 2001 | struct match match; |
2b07c8b1 BP |
2002 | |
2003 | fu.event = event; | |
2004 | fu.reason = event == NXFME_DELETED ? reason : 0; | |
2b07c8b1 BP |
2005 | fu.table_id = rule->table_id; |
2006 | fu.cookie = rule->flow_cookie; | |
5cb7a798 BP |
2007 | minimatch_expand(&rule->cr.match, &match); |
2008 | fu.match = &match; | |
6b140a4e | 2009 | fu.priority = rule->cr.priority; |
a3779dbc | 2010 | |
d10976b7 | 2011 | ovs_mutex_lock(&rule->mutex); |
a3779dbc EJ |
2012 | fu.idle_timeout = rule->idle_timeout; |
2013 | fu.hard_timeout = rule->hard_timeout; | |
d10976b7 | 2014 | ovs_mutex_unlock(&rule->mutex); |
a3779dbc | 2015 | |
2b07c8b1 | 2016 | if (flags & NXFMF_ACTIONS) { |
6f00e29b BP |
2017 | fu.ofpacts = rule->actions->ofpacts; |
2018 | fu.ofpacts_len = rule->actions->ofpacts_len; | |
2b07c8b1 BP |
2019 | } else { |
2020 | fu.ofpacts = NULL; | |
2021 | fu.ofpacts_len = 0; | |
2022 | } | |
2023 | ofputil_append_flow_update(&fu, &ofconn->updates); | |
2024 | } else if (!ofconn->sent_abbrev_update) { | |
2025 | struct ofputil_flow_update fu; | |
2026 | ||
2027 | fu.event = NXFME_ABBREV; | |
2028 | fu.xid = abbrev_xid; | |
2029 | ofputil_append_flow_update(&fu, &ofconn->updates); | |
2030 | ||
2031 | ofconn->sent_abbrev_update = true; | |
2032 | } | |
2033 | } | |
2034 | } | |
2035 | } | |
2036 | ||
2037 | void | |
2038 | ofmonitor_flush(struct connmgr *mgr) | |
4cd1bc9d | 2039 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
2040 | { |
2041 | struct ofconn *ofconn; | |
2042 | ||
2043 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { | |
2044 | struct ofpbuf *msg, *next; | |
2045 | ||
2046 | LIST_FOR_EACH_SAFE (msg, next, list_node, &ofconn->updates) { | |
a3d1ff00 BP |
2047 | unsigned int n_bytes; |
2048 | ||
2b07c8b1 BP |
2049 | list_remove(&msg->list_node); |
2050 | ofconn_send(ofconn, msg, ofconn->monitor_counter); | |
a3d1ff00 BP |
2051 | n_bytes = rconn_packet_counter_n_bytes(ofconn->monitor_counter); |
2052 | if (!ofconn->monitor_paused && n_bytes > 128 * 1024) { | |
2b07c8b1 BP |
2053 | struct ofpbuf *pause; |
2054 | ||
2055 | COVERAGE_INC(ofmonitor_pause); | |
2056 | ofconn->monitor_paused = monitor_seqno++; | |
982697a4 BP |
2057 | pause = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_PAUSED, |
2058 | OFP10_VERSION, htonl(0), 0); | |
2b07c8b1 BP |
2059 | ofconn_send(ofconn, pause, ofconn->monitor_counter); |
2060 | } | |
2061 | } | |
2062 | } | |
2063 | } | |
2064 | ||
2065 | static void | |
2066 | ofmonitor_resume(struct ofconn *ofconn) | |
4cd1bc9d | 2067 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 | 2068 | { |
a8e547c1 | 2069 | struct rule_collection rules; |
982697a4 | 2070 | struct ofpbuf *resumed; |
2b07c8b1 | 2071 | struct ofmonitor *m; |
2b07c8b1 BP |
2072 | struct list msgs; |
2073 | ||
a8e547c1 | 2074 | rule_collection_init(&rules); |
2b07c8b1 BP |
2075 | HMAP_FOR_EACH (m, ofconn_node, &ofconn->monitors) { |
2076 | ofmonitor_collect_resume_rules(m, ofconn->monitor_paused, &rules); | |
2077 | } | |
2078 | ||
2079 | list_init(&msgs); | |
2080 | ofmonitor_compose_refresh_updates(&rules, &msgs); | |
2081 | ||
982697a4 BP |
2082 | resumed = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_MONITOR_RESUMED, OFP10_VERSION, |
2083 | htonl(0), 0); | |
2084 | list_push_back(&msgs, &resumed->list_node); | |
2b07c8b1 BP |
2085 | ofconn_send_replies(ofconn, &msgs); |
2086 | ||
2087 | ofconn->monitor_paused = 0; | |
2088 | } | |
2089 | ||
4cd1bc9d BP |
2090 | static bool |
2091 | ofmonitor_may_resume(const struct ofconn *ofconn) | |
2092 | OVS_REQUIRES(ofproto_mutex) | |
2093 | { | |
2094 | return (ofconn->monitor_paused != 0 | |
2095 | && !rconn_packet_counter_n_packets(ofconn->monitor_counter)); | |
2096 | } | |
2097 | ||
2b07c8b1 BP |
2098 | static void |
2099 | ofmonitor_run(struct connmgr *mgr) | |
2100 | { | |
2101 | struct ofconn *ofconn; | |
2102 | ||
4cd1bc9d | 2103 | ovs_mutex_lock(&ofproto_mutex); |
2b07c8b1 | 2104 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { |
4cd1bc9d | 2105 | if (ofmonitor_may_resume(ofconn)) { |
2b07c8b1 BP |
2106 | COVERAGE_INC(ofmonitor_resume); |
2107 | ofmonitor_resume(ofconn); | |
2108 | } | |
2109 | } | |
4cd1bc9d | 2110 | ovs_mutex_unlock(&ofproto_mutex); |
2b07c8b1 BP |
2111 | } |
2112 | ||
2113 | static void | |
2114 | ofmonitor_wait(struct connmgr *mgr) | |
2115 | { | |
2116 | struct ofconn *ofconn; | |
2117 | ||
4cd1bc9d | 2118 | ovs_mutex_lock(&ofproto_mutex); |
2b07c8b1 | 2119 | LIST_FOR_EACH (ofconn, node, &mgr->all_conns) { |
4cd1bc9d | 2120 | if (ofmonitor_may_resume(ofconn)) { |
2b07c8b1 BP |
2121 | poll_immediate_wake(); |
2122 | } | |
2123 | } | |
4cd1bc9d | 2124 | ovs_mutex_unlock(&ofproto_mutex); |
2b07c8b1 | 2125 | } |