]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
db5ce514 | 2 | * Copyright (c) 2009, 2010, 2011 Nicira Networks. |
43253595 | 3 | * Copyright (c) 2010 Jean Tourrilhes - HP-Labs. |
064af421 | 4 | * |
a14bc59f BP |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at: | |
064af421 | 8 | * |
a14bc59f BP |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
064af421 BP |
16 | */ |
17 | ||
18 | #include <config.h> | |
19 | #include "ofproto.h" | |
20 | #include <errno.h> | |
21 | #include <inttypes.h> | |
064af421 BP |
22 | #include <stdbool.h> |
23 | #include <stdlib.h> | |
10a24935 | 24 | #include "byte-order.h" |
064af421 | 25 | #include "classifier.h" |
19a87e36 | 26 | #include "connmgr.h" |
064af421 | 27 | #include "coverage.h" |
4f2cad2c | 28 | #include "dynamic-string.h" |
ca0f572c BP |
29 | #include "hash.h" |
30 | #include "hmap.h" | |
064af421 | 31 | #include "netdev.h" |
09246b99 | 32 | #include "nx-match.h" |
064af421 | 33 | #include "ofp-print.h" |
fa37b408 | 34 | #include "ofp-util.h" |
064af421 | 35 | #include "ofpbuf.h" |
5bee6e26 | 36 | #include "ofproto-provider.h" |
064af421 BP |
37 | #include "openflow/nicira-ext.h" |
38 | #include "openflow/openflow.h" | |
064af421 BP |
39 | #include "packets.h" |
40 | #include "pinsched.h" | |
41 | #include "pktbuf.h" | |
42 | #include "poll-loop.h" | |
064af421 | 43 | #include "shash.h" |
b3c01ed3 | 44 | #include "sset.h" |
064af421 | 45 | #include "timeval.h" |
c4617b3c | 46 | #include "unaligned.h" |
4f2cad2c | 47 | #include "unixctl.h" |
5136ce49 | 48 | #include "vlog.h" |
064af421 | 49 | |
d98e6007 | 50 | VLOG_DEFINE_THIS_MODULE(ofproto); |
064af421 | 51 | |
d76f09ea | 52 | COVERAGE_DEFINE(ofproto_error); |
d76f09ea | 53 | COVERAGE_DEFINE(ofproto_flush); |
d76f09ea | 54 | COVERAGE_DEFINE(ofproto_no_packet_in); |
d76f09ea BP |
55 | COVERAGE_DEFINE(ofproto_packet_out); |
56 | COVERAGE_DEFINE(ofproto_queue_req); | |
57 | COVERAGE_DEFINE(ofproto_recv_openflow); | |
58 | COVERAGE_DEFINE(ofproto_reinit_ports); | |
d76f09ea BP |
59 | COVERAGE_DEFINE(ofproto_uninstallable); |
60 | COVERAGE_DEFINE(ofproto_update_port); | |
61 | ||
7ee20df1 BP |
62 | enum ofproto_state { |
63 | S_OPENFLOW, /* Processing OpenFlow commands. */ | |
64 | S_FLUSH, /* Deleting all flow table rules. */ | |
65 | }; | |
66 | ||
67 | enum ofoperation_type { | |
68 | OFOPERATION_ADD, | |
69 | OFOPERATION_DELETE, | |
70 | OFOPERATION_MODIFY | |
71 | }; | |
72 | ||
73 | /* A single OpenFlow request can execute any number of operations. The | |
74 | * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the | |
75 | * ofconn to which an error reply should be sent if necessary. | |
76 | * | |
77 | * ofproto initiates some operations internally. These operations are still | |
78 | * assigned to groups but will not have an associated ofconn. */ | |
79 | struct ofopgroup { | |
80 | struct ofproto *ofproto; /* Owning ofproto. */ | |
81 | struct list ofproto_node; /* In ofproto's "pending" list. */ | |
82 | struct list ops; /* List of "struct ofoperation"s. */ | |
83 | ||
84 | /* Data needed to send OpenFlow reply on failure or to send a buffered | |
85 | * packet on success. | |
86 | * | |
87 | * If list_is_empty(ofconn_node) then this ofopgroup never had an | |
88 | * associated ofconn or its ofconn's connection dropped after it initiated | |
89 | * the operation. In the latter case 'ofconn' is a wild pointer that | |
90 | * refers to freed memory, so the 'ofconn' member must be used only if | |
91 | * !list_is_empty(ofconn_node). | |
92 | */ | |
93 | struct list ofconn_node; /* In ofconn's list of pending opgroups. */ | |
94 | struct ofconn *ofconn; /* ofconn for reply (but see note above). */ | |
95 | struct ofp_header *request; /* Original request (truncated at 64 bytes). */ | |
96 | uint32_t buffer_id; /* Buffer id from original request. */ | |
97 | int error; /* 0 if no error yet, otherwise error code. */ | |
98 | }; | |
99 | ||
7024dffe BP |
100 | static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *); |
101 | static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *, | |
102 | const struct ofp_header *, | |
103 | uint32_t buffer_id); | |
7ee20df1 BP |
104 | static void ofopgroup_submit(struct ofopgroup *); |
105 | static void ofopgroup_destroy(struct ofopgroup *); | |
106 | ||
107 | /* A single flow table operation. */ | |
108 | struct ofoperation { | |
109 | struct ofopgroup *group; /* Owning group. */ | |
110 | struct list group_node; /* In ofopgroup's "ops" list. */ | |
111 | struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */ | |
112 | struct rule *rule; /* Rule being operated upon. */ | |
113 | enum ofoperation_type type; /* Type of operation. */ | |
114 | int status; /* -1 if pending, otherwise 0 or error code. */ | |
115 | struct rule *victim; /* OFOPERATION_ADDING: Replaced rule. */ | |
116 | union ofp_action *actions; /* OFOPERATION_MODIFYING: Replaced actions. */ | |
117 | int n_actions; /* OFOPERATION_MODIFYING: # of old actions. */ | |
118 | ovs_be64 flow_cookie; /* Rule's old flow cookie. */ | |
119 | }; | |
120 | ||
121 | static void ofoperation_create(struct ofopgroup *, struct rule *, | |
122 | enum ofoperation_type); | |
123 | static void ofoperation_destroy(struct ofoperation *); | |
124 | ||
abe529af BP |
125 | static void ofport_destroy__(struct ofport *); |
126 | static void ofport_destroy(struct ofport *); | |
878ae780 | 127 | |
abe529af BP |
128 | static uint64_t pick_datapath_id(const struct ofproto *); |
129 | static uint64_t pick_fallback_dpid(void); | |
064af421 | 130 | |
abe529af | 131 | static void ofproto_destroy__(struct ofproto *); |
064af421 | 132 | |
abe529af BP |
133 | static void ofproto_rule_destroy__(struct rule *); |
134 | static void ofproto_rule_send_removed(struct rule *, uint8_t reason); | |
f29152ca | 135 | |
7ee20df1 BP |
136 | static void ofopgroup_destroy(struct ofopgroup *); |
137 | ||
a9a2da38 | 138 | static int add_flow(struct ofproto *, struct ofconn *, |
1b9d6420 BP |
139 | const struct ofputil_flow_mod *, |
140 | const struct ofp_header *); | |
7ee20df1 | 141 | |
7ee20df1 | 142 | static bool handle_openflow(struct ofconn *, struct ofpbuf *); |
75a75043 BP |
143 | static int handle_flow_mod__(struct ofproto *, struct ofconn *, |
144 | const struct ofputil_flow_mod *, | |
145 | const struct ofp_header *); | |
f29152ca | 146 | |
abe529af BP |
147 | static void update_port(struct ofproto *, const char *devname); |
148 | static int init_ports(struct ofproto *); | |
149 | static void reinit_ports(struct ofproto *); | |
f29152ca | 150 | |
abe529af | 151 | static void ofproto_unixctl_init(void); |
f29152ca | 152 | |
abe529af BP |
153 | /* All registered ofproto classes, in probe order. */ |
154 | static const struct ofproto_class **ofproto_classes; | |
155 | static size_t n_ofproto_classes; | |
156 | static size_t allocated_ofproto_classes; | |
7aa697dd | 157 | |
f797957a | 158 | /* Map from datapath name to struct ofproto, for use by unixctl commands. */ |
abe529af | 159 | static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos); |
ebe482fd | 160 | |
abe529af | 161 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
f29152ca | 162 | |
abe529af BP |
163 | static void |
164 | ofproto_initialize(void) | |
165 | { | |
166 | static bool inited; | |
f29152ca | 167 | |
abe529af BP |
168 | if (!inited) { |
169 | inited = true; | |
170 | ofproto_class_register(&ofproto_dpif_class); | |
171 | } | |
172 | } | |
f29152ca | 173 | |
abe529af BP |
174 | /* 'type' should be a normalized datapath type, as returned by |
175 | * ofproto_normalize_type(). Returns the corresponding ofproto_class | |
176 | * structure, or a null pointer if there is none registered for 'type'. */ | |
177 | static const struct ofproto_class * | |
178 | ofproto_class_find__(const char *type) | |
179 | { | |
180 | size_t i; | |
f29152ca | 181 | |
abe529af BP |
182 | ofproto_initialize(); |
183 | for (i = 0; i < n_ofproto_classes; i++) { | |
184 | const struct ofproto_class *class = ofproto_classes[i]; | |
185 | struct sset types; | |
186 | bool found; | |
064af421 | 187 | |
abe529af BP |
188 | sset_init(&types); |
189 | class->enumerate_types(&types); | |
190 | found = sset_contains(&types, type); | |
191 | sset_destroy(&types); | |
bcf84111 | 192 | |
abe529af BP |
193 | if (found) { |
194 | return class; | |
195 | } | |
196 | } | |
197 | VLOG_WARN("unknown datapath type %s", type); | |
198 | return NULL; | |
199 | } | |
064af421 | 200 | |
abe529af BP |
201 | /* Registers a new ofproto class. After successful registration, new ofprotos |
202 | * of that type can be created using ofproto_create(). */ | |
203 | int | |
204 | ofproto_class_register(const struct ofproto_class *new_class) | |
205 | { | |
206 | size_t i; | |
7aa697dd | 207 | |
abe529af BP |
208 | for (i = 0; i < n_ofproto_classes; i++) { |
209 | if (ofproto_classes[i] == new_class) { | |
210 | return EEXIST; | |
211 | } | |
212 | } | |
064af421 | 213 | |
abe529af BP |
214 | if (n_ofproto_classes >= allocated_ofproto_classes) { |
215 | ofproto_classes = x2nrealloc(ofproto_classes, | |
216 | &allocated_ofproto_classes, | |
217 | sizeof *ofproto_classes); | |
218 | } | |
219 | ofproto_classes[n_ofproto_classes++] = new_class; | |
220 | return 0; | |
221 | } | |
064af421 | 222 | |
abe529af BP |
223 | /* Unregisters a datapath provider. 'type' must have been previously |
224 | * registered and not currently be in use by any ofprotos. After | |
225 | * unregistration new datapaths of that type cannot be opened using | |
226 | * ofproto_create(). */ | |
227 | int | |
228 | ofproto_class_unregister(const struct ofproto_class *class) | |
229 | { | |
230 | size_t i; | |
76ce9432 | 231 | |
abe529af BP |
232 | for (i = 0; i < n_ofproto_classes; i++) { |
233 | if (ofproto_classes[i] == class) { | |
234 | for (i++; i < n_ofproto_classes; i++) { | |
235 | ofproto_classes[i - 1] = ofproto_classes[i]; | |
236 | } | |
237 | n_ofproto_classes--; | |
238 | return 0; | |
239 | } | |
240 | } | |
241 | VLOG_WARN("attempted to unregister an ofproto class that is not " | |
242 | "registered"); | |
243 | return EAFNOSUPPORT; | |
244 | } | |
4a4cdb3b | 245 | |
f79e673f BP |
246 | /* Clears 'types' and enumerates all registered ofproto types into it. The |
247 | * caller must first initialize the sset. */ | |
248 | void | |
249 | ofproto_enumerate_types(struct sset *types) | |
250 | { | |
abe529af | 251 | size_t i; |
064af421 | 252 | |
abe529af BP |
253 | ofproto_initialize(); |
254 | for (i = 0; i < n_ofproto_classes; i++) { | |
255 | ofproto_classes[i]->enumerate_types(types); | |
256 | } | |
f79e673f | 257 | } |
064af421 | 258 | |
f79e673f BP |
259 | /* Returns the fully spelled out name for the given ofproto 'type'. |
260 | * | |
261 | * Normalized type string can be compared with strcmp(). Unnormalized type | |
262 | * string might be the same even if they have different spellings. */ | |
263 | const char * | |
264 | ofproto_normalize_type(const char *type) | |
265 | { | |
abe529af | 266 | return type && type[0] ? type : "system"; |
f79e673f | 267 | } |
064af421 | 268 | |
f79e673f BP |
269 | /* Clears 'names' and enumerates the names of all known created ofprotos with |
270 | * the given 'type'. The caller must first initialize the sset. Returns 0 if | |
271 | * successful, otherwise a positive errno value. | |
272 | * | |
273 | * Some kinds of datapaths might not be practically enumerable. This is not | |
274 | * considered an error. */ | |
275 | int | |
276 | ofproto_enumerate_names(const char *type, struct sset *names) | |
277 | { | |
abe529af BP |
278 | const struct ofproto_class *class = ofproto_class_find__(type); |
279 | return class ? class->enumerate_names(type, names) : EAFNOSUPPORT; | |
280 | } | |
7aa697dd | 281 | |
064af421 | 282 | int |
abe529af | 283 | ofproto_create(const char *datapath_name, const char *datapath_type, |
064af421 BP |
284 | struct ofproto **ofprotop) |
285 | { | |
abe529af | 286 | const struct ofproto_class *class; |
b772ded8 | 287 | struct classifier *table; |
abe529af | 288 | struct ofproto *ofproto; |
073e2a6f | 289 | int n_tables; |
064af421 BP |
290 | int error; |
291 | ||
292 | *ofprotop = NULL; | |
293 | ||
abe529af | 294 | ofproto_initialize(); |
7aa697dd BP |
295 | ofproto_unixctl_init(); |
296 | ||
abe529af BP |
297 | datapath_type = ofproto_normalize_type(datapath_type); |
298 | class = ofproto_class_find__(datapath_type); | |
299 | if (!class) { | |
300 | VLOG_WARN("could not create datapath %s of unknown type %s", | |
301 | datapath_name, datapath_type); | |
302 | return EAFNOSUPPORT; | |
064af421 BP |
303 | } |
304 | ||
abe529af BP |
305 | ofproto = class->alloc(); |
306 | if (!ofproto) { | |
307 | VLOG_ERR("failed to allocate datapath %s of type %s", | |
308 | datapath_name, datapath_type); | |
309 | return ENOMEM; | |
310 | } | |
311 | ||
312 | /* Initialize. */ | |
313 | memset(ofproto, 0, sizeof *ofproto); | |
314 | ofproto->ofproto_class = class; | |
315 | ofproto->name = xstrdup(datapath_name); | |
316 | ofproto->type = xstrdup(datapath_type); | |
317 | hmap_insert(&all_ofprotos, &ofproto->hmap_node, | |
318 | hash_string(ofproto->name, 0)); | |
319 | ofproto->datapath_id = 0; | |
084f5290 SH |
320 | ofproto_set_flow_eviction_threshold(ofproto, |
321 | OFPROTO_FLOW_EVICTON_THRESHOLD_DEFAULT); | |
8402c74b | 322 | ofproto->forward_bpdu = false; |
abe529af BP |
323 | ofproto->fallback_dpid = pick_fallback_dpid(); |
324 | ofproto->mfr_desc = xstrdup(DEFAULT_MFR_DESC); | |
325 | ofproto->hw_desc = xstrdup(DEFAULT_HW_DESC); | |
326 | ofproto->sw_desc = xstrdup(DEFAULT_SW_DESC); | |
327 | ofproto->serial_desc = xstrdup(DEFAULT_SERIAL_DESC); | |
328 | ofproto->dp_desc = xstrdup(DEFAULT_DP_DESC); | |
abe529af BP |
329 | hmap_init(&ofproto->ports); |
330 | shash_init(&ofproto->port_by_name); | |
6c1491fb BP |
331 | ofproto->tables = NULL; |
332 | ofproto->n_tables = 0; | |
abe529af | 333 | ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name); |
7ee20df1 BP |
334 | ofproto->state = S_OPENFLOW; |
335 | list_init(&ofproto->pending); | |
dd5616b3 | 336 | ofproto->n_pending = 0; |
7ee20df1 | 337 | hmap_init(&ofproto->deletions); |
abe529af | 338 | |
073e2a6f | 339 | error = ofproto->ofproto_class->construct(ofproto, &n_tables); |
19a87e36 | 340 | if (error) { |
abe529af BP |
341 | VLOG_ERR("failed to open datapath %s: %s", |
342 | datapath_name, strerror(error)); | |
343 | ofproto_destroy__(ofproto); | |
19a87e36 BP |
344 | return error; |
345 | } | |
073e2a6f BP |
346 | |
347 | assert(n_tables >= 1 && n_tables <= 255); | |
348 | ofproto->n_tables = n_tables; | |
349 | ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables); | |
b772ded8 BP |
350 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
351 | classifier_init(table); | |
073e2a6f | 352 | } |
19a87e36 | 353 | |
abe529af BP |
354 | ofproto->datapath_id = pick_datapath_id(ofproto); |
355 | VLOG_INFO("using datapath ID %016"PRIx64, ofproto->datapath_id); | |
356 | init_ports(ofproto); | |
7aa697dd | 357 | |
abe529af | 358 | *ofprotop = ofproto; |
064af421 BP |
359 | return 0; |
360 | } | |
361 | ||
362 | void | |
363 | ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id) | |
364 | { | |
365 | uint64_t old_dpid = p->datapath_id; | |
fa60c019 | 366 | p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p); |
064af421 | 367 | if (p->datapath_id != old_dpid) { |
b123cc3c | 368 | VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id); |
76ce9432 BP |
369 | |
370 | /* Force all active connections to reconnect, since there is no way to | |
371 | * notify a controller that the datapath ID has changed. */ | |
fa05809b | 372 | ofproto_reconnect_controllers(p); |
064af421 BP |
373 | } |
374 | } | |
375 | ||
76ce9432 BP |
376 | void |
377 | ofproto_set_controllers(struct ofproto *p, | |
378 | const struct ofproto_controller *controllers, | |
379 | size_t n_controllers) | |
380 | { | |
19a87e36 | 381 | connmgr_set_controllers(p->connmgr, controllers, n_controllers); |
064af421 BP |
382 | } |
383 | ||
31681a5d JP |
384 | void |
385 | ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode) | |
386 | { | |
19a87e36 | 387 | connmgr_set_fail_mode(p->connmgr, fail_mode); |
31681a5d JP |
388 | } |
389 | ||
fa05809b BP |
390 | /* Drops the connections between 'ofproto' and all of its controllers, forcing |
391 | * them to reconnect. */ | |
392 | void | |
393 | ofproto_reconnect_controllers(struct ofproto *ofproto) | |
394 | { | |
19a87e36 | 395 | connmgr_reconnect(ofproto->connmgr); |
917e50e1 BP |
396 | } |
397 | ||
398 | /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s | |
399 | * in-band control should guarantee access, in the same way that in-band | |
400 | * control guarantees access to OpenFlow controllers. */ | |
401 | void | |
402 | ofproto_set_extra_in_band_remotes(struct ofproto *ofproto, | |
403 | const struct sockaddr_in *extras, size_t n) | |
404 | { | |
19a87e36 | 405 | connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n); |
917e50e1 BP |
406 | } |
407 | ||
b1da6250 BP |
408 | /* Sets the OpenFlow queue used by flows set up by in-band control on |
409 | * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control | |
410 | * flows will use the default queue. */ | |
411 | void | |
412 | ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id) | |
413 | { | |
19a87e36 | 414 | connmgr_set_in_band_queue(ofproto->connmgr, queue_id); |
b1da6250 BP |
415 | } |
416 | ||
084f5290 SH |
417 | /* Sets the number of flows at which eviction from the kernel flow table |
418 | * will occur. */ | |
419 | void | |
420 | ofproto_set_flow_eviction_threshold(struct ofproto *ofproto, unsigned threshold) | |
421 | { | |
422 | if (threshold < OFPROTO_FLOW_EVICTION_THRESHOLD_MIN) { | |
423 | ofproto->flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_MIN; | |
424 | } else { | |
425 | ofproto->flow_eviction_threshold = threshold; | |
426 | } | |
427 | } | |
428 | ||
b53055f4 | 429 | /* If forward_bpdu is true, the NORMAL action will forward frames with |
8402c74b SS |
430 | * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false, |
431 | * the NORMAL action will drop these frames. */ | |
432 | void | |
433 | ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu) | |
434 | { | |
435 | bool old_val = ofproto->forward_bpdu; | |
436 | ofproto->forward_bpdu = forward_bpdu; | |
437 | if (old_val != ofproto->forward_bpdu) { | |
438 | if (ofproto->ofproto_class->forward_bpdu_changed) { | |
439 | ofproto->ofproto_class->forward_bpdu_changed(ofproto); | |
440 | } | |
b53055f4 | 441 | } |
8402c74b SS |
442 | } |
443 | ||
064af421 BP |
444 | void |
445 | ofproto_set_desc(struct ofproto *p, | |
5a719c38 JP |
446 | const char *mfr_desc, const char *hw_desc, |
447 | const char *sw_desc, const char *serial_desc, | |
8abc4ed7 | 448 | const char *dp_desc) |
064af421 | 449 | { |
5a719c38 JP |
450 | struct ofp_desc_stats *ods; |
451 | ||
452 | if (mfr_desc) { | |
453 | if (strlen(mfr_desc) >= sizeof ods->mfr_desc) { | |
454 | VLOG_WARN("truncating mfr_desc, must be less than %zu characters", | |
455 | sizeof ods->mfr_desc); | |
456 | } | |
457 | free(p->mfr_desc); | |
458 | p->mfr_desc = xstrdup(mfr_desc); | |
064af421 | 459 | } |
5a719c38 JP |
460 | if (hw_desc) { |
461 | if (strlen(hw_desc) >= sizeof ods->hw_desc) { | |
462 | VLOG_WARN("truncating hw_desc, must be less than %zu characters", | |
463 | sizeof ods->hw_desc); | |
464 | } | |
465 | free(p->hw_desc); | |
466 | p->hw_desc = xstrdup(hw_desc); | |
064af421 | 467 | } |
5a719c38 JP |
468 | if (sw_desc) { |
469 | if (strlen(sw_desc) >= sizeof ods->sw_desc) { | |
470 | VLOG_WARN("truncating sw_desc, must be less than %zu characters", | |
471 | sizeof ods->sw_desc); | |
472 | } | |
473 | free(p->sw_desc); | |
474 | p->sw_desc = xstrdup(sw_desc); | |
475 | } | |
476 | if (serial_desc) { | |
477 | if (strlen(serial_desc) >= sizeof ods->serial_num) { | |
478 | VLOG_WARN("truncating serial_desc, must be less than %zu " | |
479 | "characters", | |
480 | sizeof ods->serial_num); | |
481 | } | |
482 | free(p->serial_desc); | |
483 | p->serial_desc = xstrdup(serial_desc); | |
064af421 | 484 | } |
8abc4ed7 | 485 | if (dp_desc) { |
5a719c38 JP |
486 | if (strlen(dp_desc) >= sizeof ods->dp_desc) { |
487 | VLOG_WARN("truncating dp_desc, must be less than %zu characters", | |
488 | sizeof ods->dp_desc); | |
489 | } | |
8abc4ed7 JP |
490 | free(p->dp_desc); |
491 | p->dp_desc = xstrdup(dp_desc); | |
492 | } | |
064af421 BP |
493 | } |
494 | ||
064af421 | 495 | int |
81e2083f | 496 | ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops) |
064af421 | 497 | { |
19a87e36 | 498 | return connmgr_set_snoops(ofproto->connmgr, snoops); |
064af421 BP |
499 | } |
500 | ||
501 | int | |
0193b2af JG |
502 | ofproto_set_netflow(struct ofproto *ofproto, |
503 | const struct netflow_options *nf_options) | |
064af421 | 504 | { |
abe529af BP |
505 | if (nf_options && sset_is_empty(&nf_options->collectors)) { |
506 | nf_options = NULL; | |
507 | } | |
508 | ||
509 | if (ofproto->ofproto_class->set_netflow) { | |
510 | return ofproto->ofproto_class->set_netflow(ofproto, nf_options); | |
064af421 | 511 | } else { |
abe529af | 512 | return nf_options ? EOPNOTSUPP : 0; |
064af421 BP |
513 | } |
514 | } | |
515 | ||
abe529af | 516 | int |
72b06300 BP |
517 | ofproto_set_sflow(struct ofproto *ofproto, |
518 | const struct ofproto_sflow_options *oso) | |
519 | { | |
abe529af BP |
520 | if (oso && sset_is_empty(&oso->targets)) { |
521 | oso = NULL; | |
522 | } | |
72b06300 | 523 | |
abe529af BP |
524 | if (ofproto->ofproto_class->set_sflow) { |
525 | return ofproto->ofproto_class->set_sflow(ofproto, oso); | |
72b06300 | 526 | } else { |
abe529af | 527 | return oso ? EOPNOTSUPP : 0; |
72b06300 BP |
528 | } |
529 | } | |
e7934396 BP |
530 | \f |
531 | /* Connectivity Fault Management configuration. */ | |
532 | ||
892815f5 | 533 | /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */ |
e7934396 | 534 | void |
892815f5 | 535 | ofproto_port_clear_cfm(struct ofproto *ofproto, uint16_t ofp_port) |
e7934396 | 536 | { |
abe529af BP |
537 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
538 | if (ofport && ofproto->ofproto_class->set_cfm) { | |
a5610457 | 539 | ofproto->ofproto_class->set_cfm(ofport, NULL); |
e7934396 BP |
540 | } |
541 | } | |
72b06300 | 542 | |
892815f5 | 543 | /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes |
93b8df38 EJ |
544 | * basic configuration from the configuration members in 'cfm', and the remote |
545 | * maintenance point ID from remote_mpid. Ignores the statistics members of | |
546 | * 'cfm'. | |
e7934396 | 547 | * |
892815f5 | 548 | * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */ |
e7934396 | 549 | void |
892815f5 | 550 | ofproto_port_set_cfm(struct ofproto *ofproto, uint16_t ofp_port, |
a5610457 | 551 | const struct cfm_settings *s) |
e7934396 BP |
552 | { |
553 | struct ofport *ofport; | |
abe529af | 554 | int error; |
e7934396 | 555 | |
abe529af | 556 | ofport = ofproto_get_port(ofproto, ofp_port); |
e7934396 | 557 | if (!ofport) { |
892815f5 BP |
558 | VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16, |
559 | ofproto->name, ofp_port); | |
e7934396 BP |
560 | return; |
561 | } | |
562 | ||
93b8df38 EJ |
563 | /* XXX: For configuration simplicity, we only support one remote_mpid |
564 | * outside of the CFM module. It's not clear if this is the correct long | |
565 | * term solution or not. */ | |
abe529af | 566 | error = (ofproto->ofproto_class->set_cfm |
a5610457 | 567 | ? ofproto->ofproto_class->set_cfm(ofport, s) |
abe529af BP |
568 | : EOPNOTSUPP); |
569 | if (error) { | |
570 | VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)", | |
571 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), | |
572 | strerror(error)); | |
e7934396 | 573 | } |
e7934396 | 574 | } |
e7934396 | 575 | |
fa066f01 BP |
576 | /* Checks the status of LACP negotiation for 'ofp_port' within ofproto. |
577 | * Returns 1 if LACP partner information for 'ofp_port' is up-to-date, | |
578 | * 0 if LACP partner information is not current (generally indicating a | |
579 | * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */ | |
580 | int | |
581 | ofproto_port_is_lacp_current(struct ofproto *ofproto, uint16_t ofp_port) | |
582 | { | |
abe529af BP |
583 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
584 | return (ofport && ofproto->ofproto_class->port_is_lacp_current | |
585 | ? ofproto->ofproto_class->port_is_lacp_current(ofport) | |
fa066f01 | 586 | : -1); |
e7934396 | 587 | } |
e7934396 | 588 | \f |
fa066f01 | 589 | /* Bundles. */ |
e7934396 | 590 | |
abe529af BP |
591 | /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'. |
592 | * A bundle is the same concept as a Port in OVSDB, that is, it consists of one | |
593 | * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN | |
594 | * configuration plus, if there is more than one slave, a bonding | |
595 | * configuration. | |
596 | * | |
597 | * If 'aux' is already registered then this function updates its configuration | |
598 | * to 's'. Otherwise, this function registers a new bundle. | |
599 | * | |
600 | * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL | |
601 | * port. */ | |
602 | int | |
603 | ofproto_bundle_register(struct ofproto *ofproto, void *aux, | |
604 | const struct ofproto_bundle_settings *s) | |
fa066f01 | 605 | { |
abe529af BP |
606 | return (ofproto->ofproto_class->bundle_set |
607 | ? ofproto->ofproto_class->bundle_set(ofproto, aux, s) | |
608 | : EOPNOTSUPP); | |
fa066f01 BP |
609 | } |
610 | ||
abe529af BP |
611 | /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'. |
612 | * If no such bundle has been registered, this has no effect. */ | |
613 | int | |
614 | ofproto_bundle_unregister(struct ofproto *ofproto, void *aux) | |
e7934396 | 615 | { |
abe529af | 616 | return ofproto_bundle_register(ofproto, aux, NULL); |
e7934396 | 617 | } |
fa066f01 | 618 | |
e7934396 | 619 | \f |
abe529af BP |
620 | /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'. |
621 | * If 'aux' is already registered then this function updates its configuration | |
622 | * to 's'. Otherwise, this function registers a new mirror. | |
623 | * | |
624 | * Mirrors affect only the treatment of packets output to the OFPP_NORMAL | |
625 | * port. */ | |
626 | int | |
627 | ofproto_mirror_register(struct ofproto *ofproto, void *aux, | |
628 | const struct ofproto_mirror_settings *s) | |
064af421 | 629 | { |
abe529af BP |
630 | return (ofproto->ofproto_class->mirror_set |
631 | ? ofproto->ofproto_class->mirror_set(ofproto, aux, s) | |
632 | : EOPNOTSUPP); | |
064af421 BP |
633 | } |
634 | ||
abe529af BP |
635 | /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'. |
636 | * If no mirror has been registered, this has no effect. */ | |
637 | int | |
638 | ofproto_mirror_unregister(struct ofproto *ofproto, void *aux) | |
064af421 | 639 | { |
abe529af | 640 | return ofproto_mirror_register(ofproto, aux, NULL); |
064af421 BP |
641 | } |
642 | ||
abe529af BP |
643 | /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on |
644 | * which all packets are flooded, instead of using MAC learning. If | |
645 | * 'flood_vlans' is NULL, then MAC learning applies to all VLANs. | |
646 | * | |
647 | * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL | |
648 | * port. */ | |
649 | int | |
650 | ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans) | |
abdfe474 | 651 | { |
abe529af BP |
652 | return (ofproto->ofproto_class->set_flood_vlans |
653 | ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans) | |
654 | : EOPNOTSUPP); | |
abdfe474 JP |
655 | } |
656 | ||
abe529af BP |
657 | /* Returns true if 'aux' is a registered bundle that is currently in use as the |
658 | * output for a mirror. */ | |
659 | bool | |
660 | ofproto_is_mirror_output_bundle(struct ofproto *ofproto, void *aux) | |
661 | { | |
662 | return (ofproto->ofproto_class->is_mirror_output_bundle | |
663 | ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux) | |
664 | : false); | |
665 | } | |
666 | \f | |
81e2083f BP |
667 | bool |
668 | ofproto_has_snoops(const struct ofproto *ofproto) | |
669 | { | |
670 | return connmgr_has_snoops(ofproto->connmgr); | |
671 | } | |
672 | ||
064af421 | 673 | void |
81e2083f | 674 | ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops) |
064af421 | 675 | { |
19a87e36 | 676 | connmgr_get_snoops(ofproto->connmgr, snoops); |
064af421 BP |
677 | } |
678 | ||
7ee20df1 BP |
679 | static void |
680 | ofproto_flush__(struct ofproto *ofproto) | |
681 | { | |
682 | struct classifier *table; | |
683 | struct ofopgroup *group; | |
684 | ||
685 | if (ofproto->ofproto_class->flush) { | |
686 | ofproto->ofproto_class->flush(ofproto); | |
687 | } | |
688 | ||
7024dffe | 689 | group = ofopgroup_create_unattached(ofproto); |
b772ded8 | 690 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
7ee20df1 BP |
691 | struct rule *rule, *next_rule; |
692 | struct cls_cursor cursor; | |
693 | ||
694 | cls_cursor_init(&cursor, table, NULL); | |
695 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) { | |
696 | if (!rule->pending) { | |
697 | ofoperation_create(group, rule, OFOPERATION_DELETE); | |
698 | classifier_remove(table, &rule->cr); | |
699 | ofproto->ofproto_class->rule_destruct(rule); | |
700 | } | |
701 | } | |
702 | } | |
703 | ofopgroup_submit(group); | |
704 | } | |
705 | ||
abe529af BP |
706 | static void |
707 | ofproto_destroy__(struct ofproto *ofproto) | |
708 | { | |
b772ded8 | 709 | struct classifier *table; |
6c1491fb | 710 | |
7ee20df1 | 711 | assert(list_is_empty(&ofproto->pending)); |
dd5616b3 | 712 | assert(!ofproto->n_pending); |
7ee20df1 | 713 | |
abe529af | 714 | connmgr_destroy(ofproto->connmgr); |
fa066f01 | 715 | |
abe529af BP |
716 | hmap_remove(&all_ofprotos, &ofproto->hmap_node); |
717 | free(ofproto->name); | |
955a7127 | 718 | free(ofproto->type); |
abe529af BP |
719 | free(ofproto->mfr_desc); |
720 | free(ofproto->hw_desc); | |
721 | free(ofproto->sw_desc); | |
722 | free(ofproto->serial_desc); | |
723 | free(ofproto->dp_desc); | |
abe529af BP |
724 | hmap_destroy(&ofproto->ports); |
725 | shash_destroy(&ofproto->port_by_name); | |
6c1491fb | 726 | |
b772ded8 BP |
727 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
728 | assert(classifier_is_empty(table)); | |
729 | classifier_destroy(table); | |
6c1491fb BP |
730 | } |
731 | free(ofproto->tables); | |
fa066f01 | 732 | |
7ee20df1 BP |
733 | hmap_destroy(&ofproto->deletions); |
734 | ||
abe529af BP |
735 | ofproto->ofproto_class->dealloc(ofproto); |
736 | } | |
fa066f01 | 737 | |
064af421 BP |
738 | void |
739 | ofproto_destroy(struct ofproto *p) | |
740 | { | |
ca0f572c | 741 | struct ofport *ofport, *next_ofport; |
064af421 BP |
742 | |
743 | if (!p) { | |
744 | return; | |
745 | } | |
746 | ||
7ee20df1 | 747 | ofproto_flush__(p); |
4e8e4213 | 748 | HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) { |
abe529af | 749 | ofport_destroy(ofport); |
064af421 | 750 | } |
064af421 | 751 | |
abe529af BP |
752 | p->ofproto_class->destruct(p); |
753 | ofproto_destroy__(p); | |
064af421 BP |
754 | } |
755 | ||
abe529af BP |
756 | /* Destroys the datapath with the respective 'name' and 'type'. With the Linux |
757 | * kernel datapath, for example, this destroys the datapath in the kernel, and | |
758 | * with the netdev-based datapath, it tears down the data structures that | |
759 | * represent the datapath. | |
760 | * | |
761 | * The datapath should not be currently open as an ofproto. */ | |
064af421 | 762 | int |
abe529af | 763 | ofproto_delete(const char *name, const char *type) |
064af421 | 764 | { |
abe529af BP |
765 | const struct ofproto_class *class = ofproto_class_find__(type); |
766 | return (!class ? EAFNOSUPPORT | |
767 | : !class->del ? EACCES | |
768 | : class->del(type, name)); | |
064af421 BP |
769 | } |
770 | ||
e9e28be3 BP |
771 | static void |
772 | process_port_change(struct ofproto *ofproto, int error, char *devname) | |
773 | { | |
774 | if (error == ENOBUFS) { | |
775 | reinit_ports(ofproto); | |
776 | } else if (!error) { | |
777 | update_port(ofproto, devname); | |
778 | free(devname); | |
779 | } | |
780 | } | |
781 | ||
064af421 | 782 | int |
abe529af | 783 | ofproto_run(struct ofproto *p) |
064af421 | 784 | { |
031d8bff | 785 | struct ofport *ofport; |
064af421 BP |
786 | char *devname; |
787 | int error; | |
064af421 | 788 | |
abe529af BP |
789 | error = p->ofproto_class->run(p); |
790 | if (error == ENODEV) { | |
791 | /* Someone destroyed the datapath behind our back. The caller | |
792 | * better destroy us and give up, because we're just going to | |
793 | * spin from here on out. */ | |
794 | static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5); | |
795 | VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally", | |
796 | p->name); | |
797 | return ENODEV; | |
149f577a JG |
798 | } |
799 | ||
5bf0e941 BP |
800 | if (p->ofproto_class->port_poll) { |
801 | while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) { | |
802 | process_port_change(p, error, devname); | |
064af421 | 803 | } |
e9e28be3 | 804 | } |
031d8bff EJ |
805 | |
806 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
807 | unsigned int change_seq = netdev_change_seq(ofport->netdev); | |
808 | if (ofport->change_seq != change_seq) { | |
809 | ofport->change_seq = change_seq; | |
810 | update_port(p, netdev_get_name(ofport->netdev)); | |
811 | } | |
064af421 BP |
812 | } |
813 | ||
7ee20df1 BP |
814 | |
815 | switch (p->state) { | |
816 | case S_OPENFLOW: | |
817 | connmgr_run(p->connmgr, handle_openflow); | |
818 | break; | |
819 | ||
820 | case S_FLUSH: | |
821 | connmgr_run(p->connmgr, NULL); | |
822 | ofproto_flush__(p); | |
823 | if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) { | |
824 | connmgr_flushed(p->connmgr); | |
825 | p->state = S_OPENFLOW; | |
826 | } | |
827 | break; | |
828 | ||
829 | default: | |
830 | NOT_REACHED(); | |
831 | } | |
064af421 | 832 | |
064af421 BP |
833 | return 0; |
834 | } | |
835 | ||
836 | void | |
837 | ofproto_wait(struct ofproto *p) | |
838 | { | |
031d8bff EJ |
839 | struct ofport *ofport; |
840 | ||
abe529af | 841 | p->ofproto_class->wait(p); |
5bf0e941 BP |
842 | if (p->ofproto_class->port_poll_wait) { |
843 | p->ofproto_class->port_poll_wait(p); | |
e7934396 | 844 | } |
031d8bff EJ |
845 | |
846 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
847 | if (ofport->change_seq != netdev_change_seq(ofport->netdev)) { | |
848 | poll_immediate_wake(); | |
849 | } | |
850 | } | |
7ee20df1 BP |
851 | |
852 | switch (p->state) { | |
853 | case S_OPENFLOW: | |
854 | connmgr_wait(p->connmgr, true); | |
855 | break; | |
856 | ||
857 | case S_FLUSH: | |
858 | connmgr_wait(p->connmgr, false); | |
859 | if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) { | |
860 | poll_immediate_wake(); | |
861 | } | |
862 | break; | |
863 | } | |
064af421 BP |
864 | } |
865 | ||
064af421 BP |
866 | bool |
867 | ofproto_is_alive(const struct ofproto *p) | |
868 | { | |
19a87e36 | 869 | return connmgr_has_controllers(p->connmgr); |
064af421 BP |
870 | } |
871 | ||
bffc0589 | 872 | void |
2cdcb898 | 873 | ofproto_get_ofproto_controller_info(const struct ofproto *ofproto, |
bffc0589 AE |
874 | struct shash *info) |
875 | { | |
19a87e36 | 876 | connmgr_get_controller_info(ofproto->connmgr, info); |
bffc0589 AE |
877 | } |
878 | ||
879 | void | |
880 | ofproto_free_ofproto_controller_info(struct shash *info) | |
881 | { | |
72ba2ed3 | 882 | connmgr_free_controller_info(info); |
bffc0589 AE |
883 | } |
884 | ||
b5827b24 BP |
885 | /* Makes a deep copy of 'old' into 'port'. */ |
886 | void | |
887 | ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old) | |
888 | { | |
889 | port->name = xstrdup(old->name); | |
890 | port->type = xstrdup(old->type); | |
891 | port->ofp_port = old->ofp_port; | |
892 | } | |
893 | ||
894 | /* Frees memory allocated to members of 'ofproto_port'. | |
3a6ccc8c | 895 | * |
b5827b24 BP |
896 | * Do not call this function on an ofproto_port obtained from |
897 | * ofproto_port_dump_next(): that function retains ownership of the data in the | |
898 | * ofproto_port. */ | |
899 | void | |
900 | ofproto_port_destroy(struct ofproto_port *ofproto_port) | |
901 | { | |
902 | free(ofproto_port->name); | |
903 | free(ofproto_port->type); | |
904 | } | |
905 | ||
b5827b24 | 906 | /* Initializes 'dump' to begin dumping the ports in an ofproto. |
3a6ccc8c | 907 | * |
b5827b24 BP |
908 | * This function provides no status indication. An error status for the entire |
909 | * dump operation is provided when it is completed by calling | |
910 | * ofproto_port_dump_done(). | |
911 | */ | |
912 | void | |
913 | ofproto_port_dump_start(struct ofproto_port_dump *dump, | |
914 | const struct ofproto *ofproto) | |
915 | { | |
abe529af BP |
916 | dump->ofproto = ofproto; |
917 | dump->error = ofproto->ofproto_class->port_dump_start(ofproto, | |
918 | &dump->state); | |
b5827b24 BP |
919 | } |
920 | ||
921 | /* Attempts to retrieve another port from 'dump', which must have been created | |
922 | * with ofproto_port_dump_start(). On success, stores a new ofproto_port into | |
923 | * 'port' and returns true. On failure, returns false. | |
924 | * | |
925 | * Failure might indicate an actual error or merely that the last port has been | |
926 | * dumped. An error status for the entire dump operation is provided when it | |
927 | * is completed by calling ofproto_port_dump_done(). | |
928 | * | |
929 | * The ofproto owns the data stored in 'port'. It will remain valid until at | |
930 | * least the next time 'dump' is passed to ofproto_port_dump_next() or | |
931 | * ofproto_port_dump_done(). */ | |
932 | bool | |
933 | ofproto_port_dump_next(struct ofproto_port_dump *dump, | |
934 | struct ofproto_port *port) | |
935 | { | |
abe529af BP |
936 | const struct ofproto *ofproto = dump->ofproto; |
937 | ||
938 | if (dump->error) { | |
939 | return false; | |
940 | } | |
b5827b24 | 941 | |
abe529af BP |
942 | dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state, |
943 | port); | |
944 | if (dump->error) { | |
945 | ofproto->ofproto_class->port_dump_done(ofproto, dump->state); | |
946 | return false; | |
b5827b24 | 947 | } |
abe529af | 948 | return true; |
b5827b24 BP |
949 | } |
950 | ||
951 | /* Completes port table dump operation 'dump', which must have been created | |
952 | * with ofproto_port_dump_start(). Returns 0 if the dump operation was | |
953 | * error-free, otherwise a positive errno value describing the problem. */ | |
3a6ccc8c | 954 | int |
b5827b24 | 955 | ofproto_port_dump_done(struct ofproto_port_dump *dump) |
3a6ccc8c | 956 | { |
abe529af BP |
957 | const struct ofproto *ofproto = dump->ofproto; |
958 | if (!dump->error) { | |
959 | dump->error = ofproto->ofproto_class->port_dump_done(ofproto, | |
960 | dump->state); | |
961 | } | |
962 | return dump->error == EOF ? 0 : dump->error; | |
b5827b24 BP |
963 | } |
964 | ||
965 | /* Attempts to add 'netdev' as a port on 'ofproto'. If successful, returns 0 | |
892815f5 | 966 | * and sets '*ofp_portp' to the new port's OpenFlow port number (if 'ofp_portp' |
b5827b24 BP |
967 | * is non-null). On failure, returns a positive errno value and sets |
968 | * '*ofp_portp' to OFPP_NONE (if 'ofp_portp' is non-null). */ | |
969 | int | |
970 | ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev, | |
971 | uint16_t *ofp_portp) | |
972 | { | |
abe529af | 973 | uint16_t ofp_port; |
3a6ccc8c BP |
974 | int error; |
975 | ||
abe529af | 976 | error = ofproto->ofproto_class->port_add(ofproto, netdev, &ofp_port); |
1fa24dea BP |
977 | if (!error) { |
978 | update_port(ofproto, netdev_get_name(netdev)); | |
979 | } | |
b5827b24 | 980 | if (ofp_portp) { |
abe529af | 981 | *ofp_portp = error ? OFPP_NONE : ofp_port; |
3a6ccc8c BP |
982 | } |
983 | return error; | |
984 | } | |
985 | ||
b5827b24 BP |
986 | /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and |
987 | * initializes '*port' appropriately; on failure, returns a positive errno | |
988 | * value. | |
989 | * | |
abe529af | 990 | * The caller owns the data in 'ofproto_port' and must free it with |
b5827b24 BP |
991 | * ofproto_port_destroy() when it is no longer needed. */ |
992 | int | |
993 | ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname, | |
994 | struct ofproto_port *port) | |
a4e2e1f2 | 995 | { |
b5827b24 BP |
996 | int error; |
997 | ||
abe529af BP |
998 | error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port); |
999 | if (error) { | |
1000 | memset(port, 0, sizeof *port); | |
b5827b24 BP |
1001 | } |
1002 | return error; | |
a4e2e1f2 EJ |
1003 | } |
1004 | ||
b5827b24 | 1005 | /* Deletes port number 'ofp_port' from the datapath for 'ofproto'. |
3a6ccc8c | 1006 | * Returns 0 if successful, otherwise a positive errno. */ |
064af421 | 1007 | int |
b5827b24 | 1008 | ofproto_port_del(struct ofproto *ofproto, uint16_t ofp_port) |
064af421 | 1009 | { |
abe529af | 1010 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1264ec95 | 1011 | const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>"; |
3cf10406 | 1012 | int error; |
cdee00fd | 1013 | |
abe529af | 1014 | error = ofproto->ofproto_class->port_del(ofproto, ofp_port); |
892815f5 | 1015 | if (!error && ofport) { |
1264ec95 BP |
1016 | /* 'name' is the netdev's name and update_port() is going to close the |
1017 | * netdev. Just in case update_port() refers to 'name' after it | |
3a6ccc8c BP |
1018 | * destroys 'ofport', make a copy of it around the update_port() |
1019 | * call. */ | |
1020 | char *devname = xstrdup(name); | |
1021 | update_port(ofproto, devname); | |
1022 | free(devname); | |
3cf10406 BP |
1023 | } |
1024 | return error; | |
064af421 BP |
1025 | } |
1026 | ||
6c1491fb | 1027 | /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and |
fa8b054f BP |
1028 | * performs the 'n_actions' actions in 'actions'. The new flow will not |
1029 | * timeout. | |
1030 | * | |
1031 | * If cls_rule->priority is in the range of priorities supported by OpenFlow | |
1032 | * (0...65535, inclusive) then the flow will be visible to OpenFlow | |
1033 | * controllers; otherwise, it will be hidden. | |
1034 | * | |
6c1491fb BP |
1035 | * The caller retains ownership of 'cls_rule' and 'actions'. |
1036 | * | |
1037 | * This is a helper function for in-band control and fail-open. */ | |
064af421 | 1038 | void |
7ee20df1 | 1039 | ofproto_add_flow(struct ofproto *ofproto, const struct cls_rule *cls_rule, |
fa8b054f | 1040 | const union ofp_action *actions, size_t n_actions) |
064af421 | 1041 | { |
7ee20df1 BP |
1042 | const struct rule *rule; |
1043 | ||
1044 | rule = rule_from_cls_rule(classifier_find_rule_exactly( | |
1045 | &ofproto->tables[0], cls_rule)); | |
1046 | if (!rule || !ofputil_actions_equal(rule->actions, rule->n_actions, | |
1047 | actions, n_actions)) { | |
a9a2da38 | 1048 | struct ofputil_flow_mod fm; |
7ee20df1 BP |
1049 | |
1050 | memset(&fm, 0, sizeof fm); | |
1051 | fm.cr = *cls_rule; | |
1052 | fm.buffer_id = UINT32_MAX; | |
1053 | fm.actions = (union ofp_action *) actions; | |
1054 | fm.n_actions = n_actions; | |
1055 | add_flow(ofproto, NULL, &fm, NULL); | |
1056 | } | |
064af421 BP |
1057 | } |
1058 | ||
75a75043 BP |
1059 | /* Executes the flow modification specified in 'fm'. Returns 0 on success, an |
1060 | * OpenFlow error code as encoded by ofp_mkerr() on failure, or | |
1061 | * OFPROTO_POSTPONE if the operation cannot be initiated now but may be retried | |
1062 | * later. | |
1063 | * | |
1064 | * This is a helper function for in-band control and fail-open. */ | |
1065 | int | |
1066 | ofproto_flow_mod(struct ofproto *ofproto, const struct ofputil_flow_mod *fm) | |
1067 | { | |
1068 | return handle_flow_mod__(ofproto, NULL, fm, NULL); | |
1069 | } | |
1070 | ||
6c1491fb BP |
1071 | /* Searches for a rule with matching criteria exactly equal to 'target' in |
1072 | * ofproto's table 0 and, if it finds one, deletes it. | |
1073 | * | |
1074 | * This is a helper function for in-band control and fail-open. */ | |
7ee20df1 | 1075 | bool |
cf3fad8a | 1076 | ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target) |
064af421 BP |
1077 | { |
1078 | struct rule *rule; | |
1079 | ||
6c1491fb BP |
1080 | rule = rule_from_cls_rule(classifier_find_rule_exactly( |
1081 | &ofproto->tables[0], target)); | |
7ee20df1 BP |
1082 | if (!rule) { |
1083 | /* No such rule -> success. */ | |
1084 | return true; | |
1085 | } else if (rule->pending) { | |
1086 | /* An operation on the rule is already pending -> failure. | |
1087 | * Caller must retry later if it's important. */ | |
1088 | return false; | |
1089 | } else { | |
1090 | /* Initiate deletion -> success. */ | |
7024dffe | 1091 | struct ofopgroup *group = ofopgroup_create_unattached(ofproto); |
7ee20df1 BP |
1092 | ofoperation_create(group, rule, OFOPERATION_DELETE); |
1093 | classifier_remove(&ofproto->tables[rule->table_id], &rule->cr); | |
1094 | rule->ofproto->ofproto_class->rule_destruct(rule); | |
1095 | ofopgroup_submit(group); | |
1096 | return true; | |
bcf84111 | 1097 | } |
5ecc9d81 | 1098 | |
142e1f5c BP |
1099 | } |
1100 | ||
7ee20df1 BP |
1101 | /* Starts the process of deleting all of the flows from all of ofproto's flow |
1102 | * tables and then reintroducing the flows required by in-band control and | |
1103 | * fail-open. The process will complete in a later call to ofproto_run(). */ | |
142e1f5c BP |
1104 | void |
1105 | ofproto_flush_flows(struct ofproto *ofproto) | |
1106 | { | |
7ee20df1 BP |
1107 | COVERAGE_INC(ofproto_flush); |
1108 | ofproto->state = S_FLUSH; | |
064af421 BP |
1109 | } |
1110 | \f | |
1111 | static void | |
1112 | reinit_ports(struct ofproto *p) | |
1113 | { | |
abe529af | 1114 | struct ofproto_port_dump dump; |
b3c01ed3 | 1115 | struct sset devnames; |
064af421 | 1116 | struct ofport *ofport; |
abe529af | 1117 | struct ofproto_port ofproto_port; |
b3c01ed3 | 1118 | const char *devname; |
064af421 | 1119 | |
898bf89d JP |
1120 | COVERAGE_INC(ofproto_reinit_ports); |
1121 | ||
b3c01ed3 | 1122 | sset_init(&devnames); |
4e8e4213 | 1123 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { |
1264ec95 | 1124 | sset_add(&devnames, netdev_get_name(ofport->netdev)); |
064af421 | 1125 | } |
abe529af BP |
1126 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { |
1127 | sset_add(&devnames, ofproto_port.name); | |
064af421 | 1128 | } |
064af421 | 1129 | |
b3c01ed3 BP |
1130 | SSET_FOR_EACH (devname, &devnames) { |
1131 | update_port(p, devname); | |
064af421 | 1132 | } |
b3c01ed3 | 1133 | sset_destroy(&devnames); |
064af421 BP |
1134 | } |
1135 | ||
abe529af BP |
1136 | /* Opens and returns a netdev for 'ofproto_port', or a null pointer if the |
1137 | * netdev cannot be opened. On success, also fills in 'opp'. */ | |
b33951b8 | 1138 | static struct netdev * |
abe529af | 1139 | ofport_open(const struct ofproto_port *ofproto_port, struct ofp_phy_port *opp) |
064af421 | 1140 | { |
118c4676 | 1141 | uint32_t curr, advertised, supported, peer; |
064af421 | 1142 | enum netdev_flags flags; |
064af421 | 1143 | struct netdev *netdev; |
064af421 BP |
1144 | int error; |
1145 | ||
18812dff | 1146 | error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev); |
064af421 BP |
1147 | if (error) { |
1148 | VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s " | |
1149 | "cannot be opened (%s)", | |
abe529af BP |
1150 | ofproto_port->name, ofproto_port->ofp_port, |
1151 | ofproto_port->name, strerror(error)); | |
064af421 BP |
1152 | return NULL; |
1153 | } | |
1154 | ||
064af421 | 1155 | netdev_get_flags(netdev, &flags); |
118c4676 | 1156 | netdev_get_features(netdev, &curr, &advertised, &supported, &peer); |
064af421 | 1157 | |
abe529af | 1158 | opp->port_no = htons(ofproto_port->ofp_port); |
b33951b8 | 1159 | netdev_get_etheraddr(netdev, opp->hw_addr); |
abe529af | 1160 | ovs_strzcpy(opp->name, ofproto_port->name, sizeof opp->name); |
118c4676 BP |
1161 | opp->config = flags & NETDEV_UP ? 0 : htonl(OFPPC_PORT_DOWN); |
1162 | opp->state = netdev_get_carrier(netdev) ? 0 : htonl(OFPPS_LINK_DOWN); | |
1163 | opp->curr = htonl(curr); | |
1164 | opp->advertised = htonl(advertised); | |
1165 | opp->supported = htonl(supported); | |
1166 | opp->peer = htonl(peer); | |
1167 | ||
b33951b8 | 1168 | return netdev; |
064af421 BP |
1169 | } |
1170 | ||
b33951b8 BP |
1171 | /* Returns true if most fields of 'a' and 'b' are equal. Differences in name, |
1172 | * port number, and 'config' bits other than OFPPC_PORT_DOWN are | |
1173 | * disregarded. */ | |
1174 | static bool | |
1175 | ofport_equal(const struct ofp_phy_port *a, const struct ofp_phy_port *b) | |
064af421 | 1176 | { |
064af421 | 1177 | BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */ |
b33951b8 | 1178 | return (!memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr) |
064af421 | 1179 | && a->state == b->state |
118c4676 | 1180 | && !((a->config ^ b->config) & htonl(OFPPC_PORT_DOWN)) |
064af421 BP |
1181 | && a->curr == b->curr |
1182 | && a->advertised == b->advertised | |
1183 | && a->supported == b->supported | |
1184 | && a->peer == b->peer); | |
1185 | } | |
1186 | ||
b33951b8 BP |
1187 | /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'. |
1188 | * The caller must ensure that 'p' does not have a conflicting ofport (that is, | |
1189 | * one with the same name or port number). */ | |
064af421 | 1190 | static void |
b33951b8 BP |
1191 | ofport_install(struct ofproto *p, |
1192 | struct netdev *netdev, const struct ofp_phy_port *opp) | |
064af421 | 1193 | { |
b33951b8 BP |
1194 | const char *netdev_name = netdev_get_name(netdev); |
1195 | struct ofport *ofport; | |
abe529af | 1196 | int error; |
72b06300 | 1197 | |
b33951b8 | 1198 | /* Create ofport. */ |
abe529af BP |
1199 | ofport = p->ofproto_class->port_alloc(); |
1200 | if (!ofport) { | |
1201 | error = ENOMEM; | |
1202 | goto error; | |
1203 | } | |
0f7d71a5 | 1204 | ofport->ofproto = p; |
b33951b8 | 1205 | ofport->netdev = netdev; |
031d8bff | 1206 | ofport->change_seq = netdev_change_seq(netdev); |
b33951b8 | 1207 | ofport->opp = *opp; |
abe529af | 1208 | ofport->ofp_port = ntohs(opp->port_no); |
b33951b8 BP |
1209 | |
1210 | /* Add port to 'p'. */ | |
abe529af | 1211 | hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->ofp_port, 0)); |
72b06300 | 1212 | shash_add(&p->port_by_name, netdev_name, ofport); |
abe529af BP |
1213 | |
1214 | /* Let the ofproto_class initialize its private data. */ | |
1215 | error = p->ofproto_class->port_construct(ofport); | |
1216 | if (error) { | |
1217 | goto error; | |
1218 | } | |
1219 | connmgr_send_port_status(p->connmgr, opp, OFPPR_ADD); | |
1220 | return; | |
1221 | ||
1222 | error: | |
1223 | VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)", | |
1224 | p->name, netdev_name, strerror(error)); | |
1225 | if (ofport) { | |
1226 | ofport_destroy__(ofport); | |
1227 | } else { | |
1228 | netdev_close(netdev); | |
72b06300 | 1229 | } |
064af421 BP |
1230 | } |
1231 | ||
b33951b8 | 1232 | /* Removes 'ofport' from 'p' and destroys it. */ |
064af421 | 1233 | static void |
0f7d71a5 | 1234 | ofport_remove(struct ofport *ofport) |
064af421 | 1235 | { |
fa066f01 BP |
1236 | connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->opp, |
1237 | OFPPR_DELETE); | |
abe529af | 1238 | ofport_destroy(ofport); |
b33951b8 BP |
1239 | } |
1240 | ||
1241 | /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and | |
1242 | * destroys it. */ | |
1243 | static void | |
1244 | ofport_remove_with_name(struct ofproto *ofproto, const char *name) | |
1245 | { | |
1246 | struct ofport *port = shash_find_data(&ofproto->port_by_name, name); | |
1247 | if (port) { | |
0f7d71a5 | 1248 | ofport_remove(port); |
b33951b8 BP |
1249 | } |
1250 | } | |
1251 | ||
1252 | /* Updates 'port' within 'ofproto' with the new 'netdev' and 'opp'. | |
1253 | * | |
1254 | * Does not handle a name or port number change. The caller must implement | |
1255 | * such a change as a delete followed by an add. */ | |
1256 | static void | |
abe529af | 1257 | ofport_modified(struct ofport *port, struct ofp_phy_port *opp) |
b33951b8 BP |
1258 | { |
1259 | memcpy(port->opp.hw_addr, opp->hw_addr, ETH_ADDR_LEN); | |
118c4676 BP |
1260 | port->opp.config = ((port->opp.config & ~htonl(OFPPC_PORT_DOWN)) |
1261 | | (opp->config & htonl(OFPPC_PORT_DOWN))); | |
b33951b8 BP |
1262 | port->opp.state = opp->state; |
1263 | port->opp.curr = opp->curr; | |
1264 | port->opp.advertised = opp->advertised; | |
1265 | port->opp.supported = opp->supported; | |
1266 | port->opp.peer = opp->peer; | |
1267 | ||
abe529af | 1268 | connmgr_send_port_status(port->ofproto->connmgr, &port->opp, OFPPR_MODIFY); |
064af421 BP |
1269 | } |
1270 | ||
abe529af BP |
1271 | void |
1272 | ofproto_port_unregister(struct ofproto *ofproto, uint16_t ofp_port) | |
e7934396 | 1273 | { |
abe529af BP |
1274 | struct ofport *port = ofproto_get_port(ofproto, ofp_port); |
1275 | if (port) { | |
1276 | if (port->ofproto->ofproto_class->set_cfm) { | |
a5610457 | 1277 | port->ofproto->ofproto_class->set_cfm(port, NULL); |
abe529af BP |
1278 | } |
1279 | if (port->ofproto->ofproto_class->bundle_remove) { | |
1280 | port->ofproto->ofproto_class->bundle_remove(port); | |
e7934396 BP |
1281 | } |
1282 | } | |
1283 | } | |
1284 | ||
1285 | static void | |
abe529af | 1286 | ofport_destroy__(struct ofport *port) |
e7934396 | 1287 | { |
abe529af BP |
1288 | struct ofproto *ofproto = port->ofproto; |
1289 | const char *name = netdev_get_name(port->netdev); | |
fa066f01 | 1290 | |
abe529af BP |
1291 | hmap_remove(&ofproto->ports, &port->hmap_node); |
1292 | shash_delete(&ofproto->port_by_name, | |
1293 | shash_find(&ofproto->port_by_name, name)); | |
fa066f01 | 1294 | |
abe529af BP |
1295 | netdev_close(port->netdev); |
1296 | ofproto->ofproto_class->port_dealloc(port); | |
e7934396 BP |
1297 | } |
1298 | ||
064af421 | 1299 | static void |
abe529af | 1300 | ofport_destroy(struct ofport *port) |
064af421 | 1301 | { |
fa066f01 | 1302 | if (port) { |
abe529af BP |
1303 | port->ofproto->ofproto_class->port_destruct(port); |
1304 | ofport_destroy__(port); | |
1305 | } | |
064af421 BP |
1306 | } |
1307 | ||
abe529af BP |
1308 | struct ofport * |
1309 | ofproto_get_port(const struct ofproto *ofproto, uint16_t ofp_port) | |
ca0f572c BP |
1310 | { |
1311 | struct ofport *port; | |
1312 | ||
4e8e4213 | 1313 | HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, |
abe529af BP |
1314 | hash_int(ofp_port, 0), &ofproto->ports) { |
1315 | if (port->ofp_port == ofp_port) { | |
ca0f572c BP |
1316 | return port; |
1317 | } | |
1318 | } | |
1319 | return NULL; | |
1320 | } | |
1321 | ||
064af421 | 1322 | static void |
b33951b8 | 1323 | update_port(struct ofproto *ofproto, const char *name) |
064af421 | 1324 | { |
abe529af | 1325 | struct ofproto_port ofproto_port; |
b33951b8 BP |
1326 | struct ofp_phy_port opp; |
1327 | struct netdev *netdev; | |
1328 | struct ofport *port; | |
064af421 BP |
1329 | |
1330 | COVERAGE_INC(ofproto_update_port); | |
c874dc6d | 1331 | |
b33951b8 | 1332 | /* Fetch 'name''s location and properties from the datapath. */ |
abe529af BP |
1333 | netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port) |
1334 | ? ofport_open(&ofproto_port, &opp) | |
b33951b8 BP |
1335 | : NULL); |
1336 | if (netdev) { | |
abe529af | 1337 | port = ofproto_get_port(ofproto, ofproto_port.ofp_port); |
b33951b8 | 1338 | if (port && !strcmp(netdev_get_name(port->netdev), name)) { |
e65942a0 BP |
1339 | struct netdev *old_netdev = port->netdev; |
1340 | ||
b33951b8 BP |
1341 | /* 'name' hasn't changed location. Any properties changed? */ |
1342 | if (!ofport_equal(&port->opp, &opp)) { | |
abe529af BP |
1343 | ofport_modified(port, &opp); |
1344 | } | |
1345 | ||
e65942a0 BP |
1346 | /* Install the newly opened netdev in case it has changed. |
1347 | * Don't close the old netdev yet in case port_modified has to | |
1348 | * remove a retained reference to it.*/ | |
abe529af | 1349 | port->netdev = netdev; |
031d8bff | 1350 | port->change_seq = netdev_change_seq(netdev); |
abe529af BP |
1351 | |
1352 | if (port->ofproto->ofproto_class->port_modified) { | |
1353 | port->ofproto->ofproto_class->port_modified(port); | |
b33951b8 | 1354 | } |
e65942a0 BP |
1355 | |
1356 | netdev_close(old_netdev); | |
b33951b8 BP |
1357 | } else { |
1358 | /* If 'port' is nonnull then its name differs from 'name' and thus | |
1359 | * we should delete it. If we think there's a port named 'name' | |
1360 | * then its port number must be wrong now so delete it too. */ | |
1361 | if (port) { | |
0f7d71a5 | 1362 | ofport_remove(port); |
b33951b8 BP |
1363 | } |
1364 | ofport_remove_with_name(ofproto, name); | |
1365 | ofport_install(ofproto, netdev, &opp); | |
c874dc6d | 1366 | } |
b33951b8 BP |
1367 | } else { |
1368 | /* Any port named 'name' is gone now. */ | |
1369 | ofport_remove_with_name(ofproto, name); | |
c874dc6d | 1370 | } |
abe529af | 1371 | ofproto_port_destroy(&ofproto_port); |
064af421 BP |
1372 | } |
1373 | ||
1374 | static int | |
1375 | init_ports(struct ofproto *p) | |
1376 | { | |
abe529af BP |
1377 | struct ofproto_port_dump dump; |
1378 | struct ofproto_port ofproto_port; | |
1379 | ||
1380 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { | |
1381 | uint16_t ofp_port = ofproto_port.ofp_port; | |
1382 | if (ofproto_get_port(p, ofp_port)) { | |
1383 | VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath", | |
1384 | ofp_port); | |
1385 | } else if (shash_find(&p->port_by_name, ofproto_port.name)) { | |
1386 | VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath", | |
1387 | ofproto_port.name); | |
1388 | } else { | |
b33951b8 BP |
1389 | struct ofp_phy_port opp; |
1390 | struct netdev *netdev; | |
1391 | ||
abe529af | 1392 | netdev = ofport_open(&ofproto_port, &opp); |
b33951b8 BP |
1393 | if (netdev) { |
1394 | ofport_install(p, netdev, &opp); | |
064af421 BP |
1395 | } |
1396 | } | |
1397 | } | |
b0ec0f27 | 1398 | |
064af421 BP |
1399 | return 0; |
1400 | } | |
1401 | \f | |
064af421 | 1402 | static void |
abe529af | 1403 | ofproto_rule_destroy__(struct rule *rule) |
064af421 BP |
1404 | { |
1405 | free(rule->actions); | |
abe529af | 1406 | rule->ofproto->ofproto_class->rule_dealloc(rule); |
064af421 BP |
1407 | } |
1408 | ||
7ee20df1 BP |
1409 | /* This function allows an ofproto implementation to destroy any rules that |
1410 | * remain when its ->destruct() function is called. The caller must have | |
1411 | * already uninitialized any derived members of 'rule' (step 5 described in the | |
5bee6e26 JP |
1412 | * large comment in ofproto/ofproto-provider.h titled "Life Cycle"). |
1413 | * This function implements steps 6 and 7. | |
7ee20df1 BP |
1414 | * |
1415 | * This function should only be called from an ofproto implementation's | |
1416 | * ->destruct() function. It is not suitable elsewhere. */ | |
abe529af BP |
1417 | void |
1418 | ofproto_rule_destroy(struct rule *rule) | |
064af421 | 1419 | { |
7ee20df1 BP |
1420 | assert(!rule->pending); |
1421 | classifier_remove(&rule->ofproto->tables[rule->table_id], &rule->cr); | |
1422 | ofproto_rule_destroy__(rule); | |
064af421 BP |
1423 | } |
1424 | ||
bcf84111 BP |
1425 | /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action |
1426 | * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't | |
1427 | * count). */ | |
064af421 | 1428 | static bool |
9ed18e46 | 1429 | rule_has_out_port(const struct rule *rule, uint16_t out_port) |
064af421 BP |
1430 | { |
1431 | const union ofp_action *oa; | |
b4b8c781 | 1432 | size_t left; |
064af421 | 1433 | |
9ed18e46 | 1434 | if (out_port == OFPP_NONE) { |
064af421 BP |
1435 | return true; |
1436 | } | |
b4b8c781 | 1437 | OFPUTIL_ACTION_FOR_EACH_UNSAFE (oa, left, rule->actions, rule->n_actions) { |
9ed18e46 | 1438 | if (action_outputs_to_port(oa, htons(out_port))) { |
064af421 BP |
1439 | return true; |
1440 | } | |
1441 | } | |
1442 | return false; | |
1443 | } | |
1444 | ||
bcf84111 | 1445 | /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s |
bcf84111 BP |
1446 | * statistics appropriately. 'packet' must have at least sizeof(struct |
1447 | * ofp_packet_in) bytes of headroom. | |
064af421 | 1448 | * |
bcf84111 BP |
1449 | * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited |
1450 | * with statistics for 'packet' either way. | |
1451 | * | |
1452 | * Takes ownership of 'packet'. */ | |
5bf0e941 | 1453 | static int |
abe529af | 1454 | rule_execute(struct rule *rule, uint16_t in_port, struct ofpbuf *packet) |
eedc0097 | 1455 | { |
bcf84111 | 1456 | struct flow flow; |
eedc0097 | 1457 | |
abe529af | 1458 | assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in)); |
eedc0097 | 1459 | |
abe529af | 1460 | flow_extract(packet, 0, in_port, &flow); |
5bf0e941 | 1461 | return rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet); |
350a665f BP |
1462 | } |
1463 | ||
abe529af BP |
1464 | /* Returns true if 'rule' should be hidden from the controller. |
1465 | * | |
1466 | * Rules with priority higher than UINT16_MAX are set up by ofproto itself | |
1467 | * (e.g. by in-band control) and are intentionally hidden from the | |
1468 | * controller. */ | |
1469 | static bool | |
1470 | rule_is_hidden(const struct rule *rule) | |
b6c9e612 | 1471 | { |
abe529af | 1472 | return rule->cr.priority > UINT16_MAX; |
7b064a79 | 1473 | } |
fa066f01 | 1474 | \f |
064af421 | 1475 | static int |
d1e2cf21 | 1476 | handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 1477 | { |
b0421aa2 | 1478 | ofconn_send_reply(ofconn, make_echo_reply(oh)); |
064af421 | 1479 | return 0; |
064af421 BP |
1480 | } |
1481 | ||
064af421 | 1482 | static int |
d1e2cf21 | 1483 | handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 1484 | { |
64ff1d96 | 1485 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
064af421 BP |
1486 | struct ofp_switch_features *osf; |
1487 | struct ofpbuf *buf; | |
064af421 | 1488 | struct ofport *port; |
6c1491fb BP |
1489 | bool arp_match_ip; |
1490 | uint32_t actions; | |
064af421 | 1491 | |
6c1491fb BP |
1492 | ofproto->ofproto_class->get_features(ofproto, &arp_match_ip, &actions); |
1493 | assert(actions & (1 << OFPAT_OUTPUT)); /* sanity check */ | |
064af421 | 1494 | |
064af421 | 1495 | osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf); |
64ff1d96 | 1496 | osf->datapath_id = htonll(ofproto->datapath_id); |
064af421 | 1497 | osf->n_buffers = htonl(pktbuf_capacity()); |
6c1491fb | 1498 | osf->n_tables = ofproto->n_tables; |
064af421 | 1499 | osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS | |
6c1491fb BP |
1500 | OFPC_PORT_STATS); |
1501 | if (arp_match_ip) { | |
1502 | osf->capabilities |= htonl(OFPC_ARP_MATCH_IP); | |
1503 | } | |
1504 | osf->actions = htonl(actions); | |
064af421 | 1505 | |
64ff1d96 | 1506 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
9cfcdadf | 1507 | ofpbuf_put(buf, &port->opp, sizeof port->opp); |
064af421 | 1508 | } |
064af421 | 1509 | |
b0421aa2 | 1510 | ofconn_send_reply(ofconn, buf); |
064af421 BP |
1511 | return 0; |
1512 | } | |
064af421 | 1513 | |
064af421 | 1514 | static int |
d1e2cf21 | 1515 | handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 1516 | { |
64ff1d96 | 1517 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
064af421 BP |
1518 | struct ofpbuf *buf; |
1519 | struct ofp_switch_config *osc; | |
1520 | uint16_t flags; | |
1521 | bool drop_frags; | |
064af421 | 1522 | |
064af421 | 1523 | /* Figure out flags. */ |
abe529af | 1524 | drop_frags = ofproto->ofproto_class->get_drop_frags(ofproto); |
064af421 | 1525 | flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL; |
959a2ecd | 1526 | |
064af421 BP |
1527 | /* Send reply. */ |
1528 | osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf); | |
1529 | osc->flags = htons(flags); | |
810605a2 | 1530 | osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn)); |
b0421aa2 | 1531 | ofconn_send_reply(ofconn, buf); |
2d70a31a | 1532 | |
064af421 BP |
1533 | return 0; |
1534 | } | |
064af421 | 1535 | |
064af421 | 1536 | static int |
d1e2cf21 | 1537 | handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc) |
064af421 | 1538 | { |
64ff1d96 | 1539 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
d1e2cf21 | 1540 | uint16_t flags = ntohs(osc->flags); |
2d70a31a | 1541 | |
1ce0a5fa BP |
1542 | if (ofconn_get_type(ofconn) == OFCONN_PRIMARY |
1543 | && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) { | |
064af421 BP |
1544 | switch (flags & OFPC_FRAG_MASK) { |
1545 | case OFPC_FRAG_NORMAL: | |
abe529af | 1546 | ofproto->ofproto_class->set_drop_frags(ofproto, false); |
064af421 | 1547 | break; |
064af421 | 1548 | case OFPC_FRAG_DROP: |
abe529af | 1549 | ofproto->ofproto_class->set_drop_frags(ofproto, true); |
c1c9c9c4 | 1550 | break; |
064af421 | 1551 | default: |
064af421 BP |
1552 | VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")", |
1553 | osc->flags); | |
064af421 BP |
1554 | break; |
1555 | } | |
1556 | } | |
ebe482fd | 1557 | |
810605a2 | 1558 | ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len)); |
0ad9b732 | 1559 | |
064af421 | 1560 | return 0; |
064af421 BP |
1561 | } |
1562 | ||
9deba63b BP |
1563 | /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow |
1564 | * error message code (composed with ofp_mkerr()) for the caller to propagate | |
1565 | * upward. Otherwise, returns 0. | |
1566 | * | |
2228b50d | 1567 | * The log message mentions 'msg_type'. */ |
9deba63b | 1568 | static int |
1102893d | 1569 | reject_slave_controller(struct ofconn *ofconn, const char *msg_type) |
9deba63b | 1570 | { |
1ce0a5fa BP |
1571 | if (ofconn_get_type(ofconn) == OFCONN_PRIMARY |
1572 | && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) { | |
9deba63b | 1573 | static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5); |
9deba63b | 1574 | VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller", |
2228b50d | 1575 | msg_type); |
9deba63b BP |
1576 | |
1577 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM); | |
1578 | } else { | |
1579 | return 0; | |
1580 | } | |
1581 | } | |
1582 | ||
064af421 | 1583 | static int |
d1e2cf21 | 1584 | handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 1585 | { |
64ff1d96 | 1586 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 BP |
1587 | struct ofp_packet_out *opo; |
1588 | struct ofpbuf payload, *buffer; | |
ac51afaf | 1589 | union ofp_action *ofp_actions; |
ac51afaf | 1590 | struct ofpbuf request; |
ae412e7d | 1591 | struct flow flow; |
ac51afaf | 1592 | size_t n_ofp_actions; |
064af421 | 1593 | uint16_t in_port; |
064af421 BP |
1594 | int error; |
1595 | ||
ac51afaf BP |
1596 | COVERAGE_INC(ofproto_packet_out); |
1597 | ||
2228b50d | 1598 | error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT"); |
9deba63b BP |
1599 | if (error) { |
1600 | return error; | |
1601 | } | |
1602 | ||
ac51afaf | 1603 | /* Get ofp_packet_out. */ |
0bc9407d | 1604 | ofpbuf_use_const(&request, oh, ntohs(oh->length)); |
bbc32a88 | 1605 | opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions)); |
ac51afaf BP |
1606 | |
1607 | /* Get actions. */ | |
1608 | error = ofputil_pull_actions(&request, ntohs(opo->actions_len), | |
1609 | &ofp_actions, &n_ofp_actions); | |
064af421 BP |
1610 | if (error) { |
1611 | return error; | |
1612 | } | |
064af421 | 1613 | |
ac51afaf | 1614 | /* Get payload. */ |
064af421 | 1615 | if (opo->buffer_id != htonl(UINT32_MAX)) { |
10d0a1d2 BP |
1616 | error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id), |
1617 | &buffer, &in_port); | |
7778bd15 | 1618 | if (error || !buffer) { |
064af421 BP |
1619 | return error; |
1620 | } | |
1621 | payload = *buffer; | |
1622 | } else { | |
ac51afaf | 1623 | payload = request; |
064af421 BP |
1624 | buffer = NULL; |
1625 | } | |
1626 | ||
abe529af BP |
1627 | /* Send out packet. */ |
1628 | flow_extract(&payload, 0, ntohs(opo->in_port), &flow); | |
1629 | error = p->ofproto_class->packet_out(p, &payload, &flow, | |
1630 | ofp_actions, n_ofp_actions); | |
ac51afaf | 1631 | ofpbuf_delete(buffer); |
abe529af BP |
1632 | |
1633 | return error; | |
064af421 BP |
1634 | } |
1635 | ||
1636 | static void | |
abe529af | 1637 | update_port_config(struct ofport *port, ovs_be32 config, ovs_be32 mask) |
064af421 | 1638 | { |
abe529af BP |
1639 | ovs_be32 old_config = port->opp.config; |
1640 | ||
064af421 | 1641 | mask &= config ^ port->opp.config; |
118c4676 BP |
1642 | if (mask & htonl(OFPPC_PORT_DOWN)) { |
1643 | if (config & htonl(OFPPC_PORT_DOWN)) { | |
064af421 BP |
1644 | netdev_turn_flags_off(port->netdev, NETDEV_UP, true); |
1645 | } else { | |
1646 | netdev_turn_flags_on(port->netdev, NETDEV_UP, true); | |
1647 | } | |
1648 | } | |
abe529af BP |
1649 | |
1650 | port->opp.config ^= mask & (htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP | | |
1651 | OFPPC_NO_FLOOD | OFPPC_NO_FWD | | |
1652 | OFPPC_NO_PACKET_IN)); | |
1653 | if (port->opp.config != old_config) { | |
1654 | port->ofproto->ofproto_class->port_reconfigured(port, old_config); | |
064af421 BP |
1655 | } |
1656 | } | |
1657 | ||
1658 | static int | |
d1e2cf21 | 1659 | handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 1660 | { |
64ff1d96 | 1661 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
d1e2cf21 | 1662 | const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh; |
064af421 BP |
1663 | struct ofport *port; |
1664 | int error; | |
1665 | ||
2228b50d | 1666 | error = reject_slave_controller(ofconn, "OFPT_PORT_MOD"); |
9deba63b BP |
1667 | if (error) { |
1668 | return error; | |
1669 | } | |
064af421 | 1670 | |
abe529af | 1671 | port = ofproto_get_port(p, ntohs(opm->port_no)); |
064af421 BP |
1672 | if (!port) { |
1673 | return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT); | |
1674 | } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) { | |
1675 | return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR); | |
1676 | } else { | |
abe529af | 1677 | update_port_config(port, opm->config, opm->mask); |
064af421 BP |
1678 | if (opm->advertise) { |
1679 | netdev_set_advertisements(port->netdev, ntohl(opm->advertise)); | |
1680 | } | |
1681 | } | |
1682 | return 0; | |
1683 | } | |
1684 | ||
064af421 | 1685 | static int |
3269c562 | 1686 | handle_desc_stats_request(struct ofconn *ofconn, |
63f2140a | 1687 | const struct ofp_stats_msg *request) |
064af421 | 1688 | { |
64ff1d96 | 1689 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 BP |
1690 | struct ofp_desc_stats *ods; |
1691 | struct ofpbuf *msg; | |
1692 | ||
63f2140a | 1693 | ods = ofputil_make_stats_reply(sizeof *ods, request, &msg); |
5a719c38 JP |
1694 | ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc); |
1695 | ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc); | |
1696 | ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc); | |
1697 | ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num); | |
1698 | ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc); | |
b0421aa2 | 1699 | ofconn_send_reply(ofconn, msg); |
064af421 BP |
1700 | |
1701 | return 0; | |
1702 | } | |
1703 | ||
064af421 | 1704 | static int |
3269c562 | 1705 | handle_table_stats_request(struct ofconn *ofconn, |
63f2140a | 1706 | const struct ofp_stats_msg *request) |
064af421 | 1707 | { |
64ff1d96 | 1708 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 BP |
1709 | struct ofp_table_stats *ots; |
1710 | struct ofpbuf *msg; | |
6c1491fb | 1711 | size_t i; |
064af421 | 1712 | |
63f2140a | 1713 | ofputil_make_stats_reply(sizeof(struct ofp_stats_msg), request, &msg); |
064af421 | 1714 | |
6c1491fb BP |
1715 | ots = ofpbuf_put_zeros(msg, sizeof *ots * p->n_tables); |
1716 | for (i = 0; i < p->n_tables; i++) { | |
1717 | ots[i].table_id = i; | |
35aa7a21 | 1718 | sprintf(ots[i].name, "table%zu", i); |
00794817 | 1719 | ots[i].wildcards = htonl(OFPFW_ALL); |
6c1491fb BP |
1720 | ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */ |
1721 | ots[i].active_count = htonl(classifier_count(&p->tables[i])); | |
1722 | } | |
064af421 | 1723 | |
6c1491fb | 1724 | p->ofproto_class->get_tables(p, ots); |
064af421 | 1725 | |
b0421aa2 | 1726 | ofconn_send_reply(ofconn, msg); |
064af421 BP |
1727 | return 0; |
1728 | } | |
1729 | ||
abaad8cf | 1730 | static void |
63f2140a | 1731 | append_port_stat(struct ofport *port, struct list *replies) |
abaad8cf JP |
1732 | { |
1733 | struct netdev_stats stats; | |
1734 | struct ofp_port_stats *ops; | |
1735 | ||
d295e8e9 JP |
1736 | /* Intentionally ignore return value, since errors will set |
1737 | * 'stats' to all-1s, which is correct for OpenFlow, and | |
abaad8cf JP |
1738 | * netdev_get_stats() will log errors. */ |
1739 | netdev_get_stats(port->netdev, &stats); | |
1740 | ||
63f2140a | 1741 | ops = ofputil_append_stats_reply(sizeof *ops, replies); |
118c4676 | 1742 | ops->port_no = port->opp.port_no; |
abaad8cf | 1743 | memset(ops->pad, 0, sizeof ops->pad); |
c4617b3c BP |
1744 | put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets)); |
1745 | put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets)); | |
1746 | put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes)); | |
1747 | put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes)); | |
1748 | put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped)); | |
1749 | put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped)); | |
1750 | put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors)); | |
1751 | put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors)); | |
1752 | put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors)); | |
1753 | put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors)); | |
1754 | put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors)); | |
1755 | put_32aligned_be64(&ops->collisions, htonll(stats.collisions)); | |
abaad8cf JP |
1756 | } |
1757 | ||
064af421 | 1758 | static int |
63f2140a BP |
1759 | handle_port_stats_request(struct ofconn *ofconn, |
1760 | const struct ofp_port_stats_request *psr) | |
064af421 | 1761 | { |
64ff1d96 | 1762 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 | 1763 | struct ofport *port; |
63f2140a | 1764 | struct list replies; |
064af421 | 1765 | |
63f2140a | 1766 | ofputil_start_stats_reply(&psr->osm, &replies); |
abaad8cf | 1767 | if (psr->port_no != htons(OFPP_NONE)) { |
abe529af | 1768 | port = ofproto_get_port(p, ntohs(psr->port_no)); |
abaad8cf | 1769 | if (port) { |
63f2140a | 1770 | append_port_stat(port, &replies); |
abaad8cf JP |
1771 | } |
1772 | } else { | |
4e8e4213 | 1773 | HMAP_FOR_EACH (port, hmap_node, &p->ports) { |
63f2140a | 1774 | append_port_stat(port, &replies); |
abaad8cf | 1775 | } |
064af421 BP |
1776 | } |
1777 | ||
63f2140a | 1778 | ofconn_send_replies(ofconn, &replies); |
064af421 BP |
1779 | return 0; |
1780 | } | |
1781 | ||
c6ebb8fb | 1782 | static void |
588cd7b5 | 1783 | calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec) |
c6ebb8fb BP |
1784 | { |
1785 | long long int msecs = time_msec() - start; | |
588cd7b5 BP |
1786 | *sec = msecs / 1000; |
1787 | *nsec = (msecs % 1000) * (1000 * 1000); | |
1788 | } | |
1789 | ||
6c1491fb BP |
1790 | static struct classifier * |
1791 | first_matching_table(struct ofproto *ofproto, uint8_t table_id) | |
064af421 | 1792 | { |
6c1491fb BP |
1793 | if (table_id == 0xff) { |
1794 | return &ofproto->tables[0]; | |
1795 | } else if (table_id < ofproto->n_tables) { | |
1796 | return &ofproto->tables[table_id]; | |
a02e5331 BP |
1797 | } else { |
1798 | /* It would probably be better to reply with an error but there doesn't | |
1799 | * seem to be any appropriate value, so that might just be | |
1800 | * confusing. */ | |
1801 | VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8, | |
1802 | table_id); | |
6c1491fb | 1803 | return NULL; |
a02e5331 | 1804 | } |
064af421 BP |
1805 | } |
1806 | ||
6c1491fb BP |
1807 | static struct classifier * |
1808 | next_matching_table(struct ofproto *ofproto, | |
1809 | struct classifier *cls, uint8_t table_id) | |
1810 | { | |
1811 | return (table_id == 0xff && cls != &ofproto->tables[ofproto->n_tables - 1] | |
1812 | ? cls + 1 | |
1813 | : NULL); | |
1814 | } | |
1815 | ||
1816 | /* Assigns CLS to each classifier table, in turn, that matches TABLE_ID in | |
1817 | * OFPROTO: | |
1818 | * | |
1819 | * - If TABLE_ID is 0xff, this iterates over every classifier table in | |
1820 | * OFPROTO. | |
1821 | * | |
1822 | * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates | |
1823 | * only once, for that table. | |
1824 | * | |
1825 | * - Otherwise, TABLE_ID isn't valid for OFPROTO, so ofproto logs a warning | |
1826 | * and does not enter the loop at all. | |
1827 | * | |
1828 | * All parameters are evaluated multiple times. | |
1829 | */ | |
1830 | #define FOR_EACH_MATCHING_TABLE(CLS, TABLE_ID, OFPROTO) \ | |
1831 | for ((CLS) = first_matching_table(OFPROTO, TABLE_ID); \ | |
1832 | (CLS) != NULL; \ | |
1833 | (CLS) = next_matching_table(OFPROTO, CLS, TABLE_ID)) | |
1834 | ||
9ed18e46 BP |
1835 | /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if |
1836 | * 'table_id' is 0xff) that match 'match' in the "loose" way required for | |
1837 | * OpenFlow OFPFC_MODIFY and OFPFC_DELETE requests and puts them on list | |
1838 | * 'rules'. | |
1839 | * | |
1840 | * If 'out_port' is anything other than OFPP_NONE, then only rules that output | |
1841 | * to 'out_port' are included. | |
1842 | * | |
1843 | * Hidden rules are always omitted. | |
1844 | * | |
1845 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
1846 | static int | |
1847 | collect_rules_loose(struct ofproto *ofproto, uint8_t table_id, | |
1848 | const struct cls_rule *match, uint16_t out_port, | |
1849 | struct list *rules) | |
1850 | { | |
1851 | struct classifier *cls; | |
1852 | ||
1853 | list_init(rules); | |
1854 | FOR_EACH_MATCHING_TABLE (cls, table_id, ofproto) { | |
1855 | struct cls_cursor cursor; | |
1856 | struct rule *rule; | |
1857 | ||
1858 | cls_cursor_init(&cursor, cls, match); | |
1859 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
7ee20df1 BP |
1860 | if (rule->pending) { |
1861 | return OFPROTO_POSTPONE; | |
1862 | } | |
9ed18e46 BP |
1863 | if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) { |
1864 | list_push_back(rules, &rule->ofproto_node); | |
1865 | } | |
1866 | } | |
1867 | } | |
1868 | return 0; | |
1869 | } | |
1870 | ||
1871 | /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if | |
1872 | * 'table_id' is 0xff) that match 'match' in the "strict" way required for | |
1873 | * OpenFlow OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests and puts them | |
1874 | * on list 'rules'. | |
1875 | * | |
1876 | * If 'out_port' is anything other than OFPP_NONE, then only rules that output | |
1877 | * to 'out_port' are included. | |
1878 | * | |
1879 | * Hidden rules are always omitted. | |
1880 | * | |
1881 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
1882 | static int | |
1883 | collect_rules_strict(struct ofproto *ofproto, uint8_t table_id, | |
1884 | const struct cls_rule *match, uint16_t out_port, | |
1885 | struct list *rules) | |
1886 | { | |
1887 | struct classifier *cls; | |
1888 | ||
1889 | list_init(rules); | |
1890 | FOR_EACH_MATCHING_TABLE (cls, table_id, ofproto) { | |
1891 | struct rule *rule; | |
1892 | ||
1893 | rule = rule_from_cls_rule(classifier_find_rule_exactly(cls, match)); | |
1894 | if (rule) { | |
7ee20df1 BP |
1895 | if (rule->pending) { |
1896 | return OFPROTO_POSTPONE; | |
1897 | } | |
9ed18e46 BP |
1898 | if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) { |
1899 | list_push_back(rules, &rule->ofproto_node); | |
1900 | } | |
1901 | } | |
1902 | } | |
1903 | return 0; | |
1904 | } | |
1905 | ||
064af421 | 1906 | static int |
63f2140a | 1907 | handle_flow_stats_request(struct ofconn *ofconn, |
349adfb2 | 1908 | const struct ofp_stats_msg *osm) |
064af421 | 1909 | { |
64ff1d96 | 1910 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 1911 | struct ofputil_flow_stats_request fsr; |
63f2140a | 1912 | struct list replies; |
9ed18e46 BP |
1913 | struct list rules; |
1914 | struct rule *rule; | |
09246b99 BP |
1915 | int error; |
1916 | ||
349adfb2 | 1917 | error = ofputil_decode_flow_stats_request(&fsr, &osm->header); |
09246b99 BP |
1918 | if (error) { |
1919 | return error; | |
1920 | } | |
1921 | ||
9ed18e46 BP |
1922 | error = collect_rules_loose(ofproto, fsr.table_id, &fsr.match, |
1923 | fsr.out_port, &rules); | |
1924 | if (error) { | |
1925 | return error; | |
1926 | } | |
5ecc9d81 | 1927 | |
9ed18e46 BP |
1928 | ofputil_start_stats_reply(osm, &replies); |
1929 | LIST_FOR_EACH (rule, ofproto_node, &rules) { | |
1930 | struct ofputil_flow_stats fs; | |
1931 | ||
1932 | fs.rule = rule->cr; | |
1933 | fs.cookie = rule->flow_cookie; | |
1934 | fs.table_id = rule->table_id; | |
1935 | calc_flow_duration__(rule->created, &fs.duration_sec, | |
1936 | &fs.duration_nsec); | |
1937 | fs.idle_timeout = rule->idle_timeout; | |
1938 | fs.hard_timeout = rule->hard_timeout; | |
1939 | ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count, | |
1940 | &fs.byte_count); | |
1941 | fs.actions = rule->actions; | |
1942 | fs.n_actions = rule->n_actions; | |
1943 | ofputil_append_flow_stats_reply(&fs, &replies); | |
3c4486a5 | 1944 | } |
63f2140a | 1945 | ofconn_send_replies(ofconn, &replies); |
5ecc9d81 | 1946 | |
09246b99 BP |
1947 | return 0; |
1948 | } | |
1949 | ||
4f2cad2c | 1950 | static void |
3394b5b6 | 1951 | flow_stats_ds(struct rule *rule, struct ds *results) |
4f2cad2c | 1952 | { |
4f2cad2c | 1953 | uint64_t packet_count, byte_count; |
4f2cad2c | 1954 | |
abe529af BP |
1955 | rule->ofproto->ofproto_class->rule_get_stats(rule, |
1956 | &packet_count, &byte_count); | |
4f2cad2c | 1957 | |
6c1491fb BP |
1958 | if (rule->table_id != 0) { |
1959 | ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id); | |
1960 | } | |
4f2cad2c JP |
1961 | ds_put_format(results, "duration=%llds, ", |
1962 | (time_msec() - rule->created) / 1000); | |
52ae00b3 | 1963 | ds_put_format(results, "priority=%u, ", rule->cr.priority); |
4f2cad2c JP |
1964 | ds_put_format(results, "n_packets=%"PRIu64", ", packet_count); |
1965 | ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count); | |
cb833cf6 | 1966 | cls_rule_format(&rule->cr, results); |
a5df0e72 | 1967 | ds_put_char(results, ','); |
b4b8c781 BP |
1968 | if (rule->n_actions > 0) { |
1969 | ofp_print_actions(results, rule->actions, rule->n_actions); | |
3c8552c1 JP |
1970 | } else { |
1971 | ds_put_cstr(results, "drop"); | |
3dffcf07 | 1972 | } |
4f2cad2c JP |
1973 | ds_put_cstr(results, "\n"); |
1974 | } | |
1975 | ||
d295e8e9 | 1976 | /* Adds a pretty-printed description of all flows to 'results', including |
ee8b231c | 1977 | * hidden flows (e.g., set up by in-band control). */ |
4f2cad2c JP |
1978 | void |
1979 | ofproto_get_all_flows(struct ofproto *p, struct ds *results) | |
1980 | { | |
6c1491fb BP |
1981 | struct classifier *cls; |
1982 | ||
b772ded8 | 1983 | OFPROTO_FOR_EACH_TABLE (cls, p) { |
6c1491fb BP |
1984 | struct cls_cursor cursor; |
1985 | struct rule *rule; | |
064af421 | 1986 | |
6c1491fb BP |
1987 | cls_cursor_init(&cursor, cls, NULL); |
1988 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
1989 | flow_stats_ds(rule, results); | |
1990 | } | |
064af421 | 1991 | } |
064af421 BP |
1992 | } |
1993 | ||
b5827b24 BP |
1994 | /* Obtains the NetFlow engine type and engine ID for 'ofproto' into |
1995 | * '*engine_type' and '*engine_id', respectively. */ | |
1996 | void | |
1997 | ofproto_get_netflow_ids(const struct ofproto *ofproto, | |
1998 | uint8_t *engine_type, uint8_t *engine_id) | |
1999 | { | |
abe529af | 2000 | ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id); |
b5827b24 BP |
2001 | } |
2002 | ||
a5610457 EJ |
2003 | /* Checks the fault status of CFM for 'ofp_port' within 'ofproto'. Returns 1 |
2004 | * if CFM is faulted (generally indiciating a connectivity problem), 0 if CFM | |
2005 | * is not faulted, and -1 if CFM is not enabled on 'ofp_port'. */ | |
2006 | int | |
2007 | ofproto_port_get_cfm_fault(const struct ofproto *ofproto, uint16_t ofp_port) | |
2008 | { | |
2009 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
2010 | return (ofport && ofproto->ofproto_class->get_cfm_fault | |
2011 | ? ofproto->ofproto_class->get_cfm_fault(ofport) | |
2012 | : -1); | |
2013 | } | |
2014 | ||
1de11730 EJ |
2015 | /* Gets the MPIDs of the remote maintenance points broadcasting to 'ofp_port' |
2016 | * within 'ofproto'. Populates 'rmps' with an array of MPIDs owned by | |
2017 | * 'ofproto', and 'n_rmps' with the number of MPIDs in 'rmps'. Returns a | |
2018 | * number less than 0 if CFM is not enabled on 'ofp_port'. */ | |
2019 | int | |
2020 | ofproto_port_get_cfm_remote_mpids(const struct ofproto *ofproto, | |
2021 | uint16_t ofp_port, const uint64_t **rmps, | |
2022 | size_t *n_rmps) | |
2023 | { | |
2024 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
2025 | ||
2026 | *rmps = NULL; | |
2027 | *n_rmps = 0; | |
2028 | return (ofport && ofproto->ofproto_class->get_cfm_remote_mpids | |
2029 | ? ofproto->ofproto_class->get_cfm_remote_mpids(ofport, rmps, | |
2030 | n_rmps) | |
2031 | : -1); | |
2032 | } | |
2033 | ||
76c93b22 BP |
2034 | static int |
2035 | handle_aggregate_stats_request(struct ofconn *ofconn, | |
2036 | const struct ofp_stats_msg *osm) | |
27d34fce | 2037 | { |
76c93b22 | 2038 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 2039 | struct ofputil_flow_stats_request request; |
76c93b22 | 2040 | struct ofputil_aggregate_stats stats; |
5e9d0469 | 2041 | bool unknown_packets, unknown_bytes; |
76c93b22 | 2042 | struct ofpbuf *reply; |
9ed18e46 BP |
2043 | struct list rules; |
2044 | struct rule *rule; | |
76c93b22 | 2045 | int error; |
27d34fce | 2046 | |
76c93b22 BP |
2047 | error = ofputil_decode_flow_stats_request(&request, &osm->header); |
2048 | if (error) { | |
2049 | return error; | |
2050 | } | |
5ecc9d81 | 2051 | |
9ed18e46 BP |
2052 | error = collect_rules_loose(ofproto, request.table_id, &request.match, |
2053 | request.out_port, &rules); | |
2054 | if (error) { | |
2055 | return error; | |
2056 | } | |
3c4486a5 | 2057 | |
9ed18e46 | 2058 | memset(&stats, 0, sizeof stats); |
5e9d0469 | 2059 | unknown_packets = unknown_bytes = false; |
9ed18e46 BP |
2060 | LIST_FOR_EACH (rule, ofproto_node, &rules) { |
2061 | uint64_t packet_count; | |
2062 | uint64_t byte_count; | |
5ecc9d81 | 2063 | |
9ed18e46 BP |
2064 | ofproto->ofproto_class->rule_get_stats(rule, &packet_count, |
2065 | &byte_count); | |
5ecc9d81 | 2066 | |
5e9d0469 BP |
2067 | if (packet_count == UINT64_MAX) { |
2068 | unknown_packets = true; | |
2069 | } else { | |
2070 | stats.packet_count += packet_count; | |
2071 | } | |
2072 | ||
2073 | if (byte_count == UINT64_MAX) { | |
2074 | unknown_bytes = true; | |
2075 | } else { | |
2076 | stats.byte_count += byte_count; | |
2077 | } | |
2078 | ||
9ed18e46 | 2079 | stats.flow_count++; |
3c4486a5 | 2080 | } |
5e9d0469 BP |
2081 | if (unknown_packets) { |
2082 | stats.packet_count = UINT64_MAX; | |
2083 | } | |
2084 | if (unknown_bytes) { | |
2085 | stats.byte_count = UINT64_MAX; | |
2086 | } | |
27d34fce | 2087 | |
76c93b22 BP |
2088 | reply = ofputil_encode_aggregate_stats_reply(&stats, osm); |
2089 | ofconn_send_reply(ofconn, reply); | |
09246b99 BP |
2090 | |
2091 | return 0; | |
2092 | } | |
2093 | ||
c1c9c9c4 | 2094 | struct queue_stats_cbdata { |
ca0f572c | 2095 | struct ofport *ofport; |
63f2140a | 2096 | struct list replies; |
c1c9c9c4 BP |
2097 | }; |
2098 | ||
2099 | static void | |
db9220c3 | 2100 | put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id, |
c1c9c9c4 BP |
2101 | const struct netdev_queue_stats *stats) |
2102 | { | |
2103 | struct ofp_queue_stats *reply; | |
2104 | ||
63f2140a | 2105 | reply = ofputil_append_stats_reply(sizeof *reply, &cbdata->replies); |
118c4676 | 2106 | reply->port_no = cbdata->ofport->opp.port_no; |
c1c9c9c4 BP |
2107 | memset(reply->pad, 0, sizeof reply->pad); |
2108 | reply->queue_id = htonl(queue_id); | |
c4617b3c BP |
2109 | put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes)); |
2110 | put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets)); | |
2111 | put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors)); | |
c1c9c9c4 BP |
2112 | } |
2113 | ||
2114 | static void | |
db9220c3 | 2115 | handle_queue_stats_dump_cb(uint32_t queue_id, |
c1c9c9c4 BP |
2116 | struct netdev_queue_stats *stats, |
2117 | void *cbdata_) | |
2118 | { | |
2119 | struct queue_stats_cbdata *cbdata = cbdata_; | |
2120 | ||
2121 | put_queue_stats(cbdata, queue_id, stats); | |
2122 | } | |
2123 | ||
2124 | static void | |
ca0f572c | 2125 | handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id, |
c1c9c9c4 BP |
2126 | struct queue_stats_cbdata *cbdata) |
2127 | { | |
ca0f572c | 2128 | cbdata->ofport = port; |
c1c9c9c4 BP |
2129 | if (queue_id == OFPQ_ALL) { |
2130 | netdev_dump_queue_stats(port->netdev, | |
2131 | handle_queue_stats_dump_cb, cbdata); | |
2132 | } else { | |
2133 | struct netdev_queue_stats stats; | |
2134 | ||
1ac788f6 BP |
2135 | if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) { |
2136 | put_queue_stats(cbdata, queue_id, &stats); | |
2137 | } | |
c1c9c9c4 BP |
2138 | } |
2139 | } | |
2140 | ||
2141 | static int | |
63f2140a BP |
2142 | handle_queue_stats_request(struct ofconn *ofconn, |
2143 | const struct ofp_queue_stats_request *qsr) | |
c1c9c9c4 | 2144 | { |
64ff1d96 | 2145 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
c1c9c9c4 BP |
2146 | struct queue_stats_cbdata cbdata; |
2147 | struct ofport *port; | |
2148 | unsigned int port_no; | |
2149 | uint32_t queue_id; | |
2150 | ||
c1c9c9c4 BP |
2151 | COVERAGE_INC(ofproto_queue_req); |
2152 | ||
63f2140a | 2153 | ofputil_start_stats_reply(&qsr->osm, &cbdata.replies); |
c1c9c9c4 BP |
2154 | |
2155 | port_no = ntohs(qsr->port_no); | |
2156 | queue_id = ntohl(qsr->queue_id); | |
2157 | if (port_no == OFPP_ALL) { | |
4e8e4213 | 2158 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
ca0f572c | 2159 | handle_queue_stats_for_port(port, queue_id, &cbdata); |
c1c9c9c4 | 2160 | } |
abe529af BP |
2161 | } else if (port_no < OFPP_MAX) { |
2162 | port = ofproto_get_port(ofproto, port_no); | |
c1c9c9c4 | 2163 | if (port) { |
ca0f572c | 2164 | handle_queue_stats_for_port(port, queue_id, &cbdata); |
c1c9c9c4 BP |
2165 | } |
2166 | } else { | |
63f2140a | 2167 | ofpbuf_list_delete(&cbdata.replies); |
c1c9c9c4 BP |
2168 | return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT); |
2169 | } | |
63f2140a | 2170 | ofconn_send_replies(ofconn, &cbdata.replies); |
c1c9c9c4 BP |
2171 | |
2172 | return 0; | |
2173 | } | |
7ee20df1 BP |
2174 | |
2175 | static bool | |
2176 | is_flow_deletion_pending(const struct ofproto *ofproto, | |
2177 | const struct cls_rule *cls_rule, | |
2178 | uint8_t table_id) | |
2179 | { | |
2180 | if (!hmap_is_empty(&ofproto->deletions)) { | |
2181 | struct ofoperation *op; | |
2182 | ||
2183 | HMAP_FOR_EACH_WITH_HASH (op, hmap_node, | |
2184 | cls_rule_hash(cls_rule, table_id), | |
2185 | &ofproto->deletions) { | |
2186 | if (cls_rule_equal(cls_rule, &op->rule->cr)) { | |
2187 | return true; | |
2188 | } | |
2189 | } | |
2190 | } | |
2191 | ||
2192 | return false; | |
2193 | } | |
2194 | ||
79eee1eb BP |
2195 | /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT |
2196 | * in which no matching flow already exists in the flow table. | |
2197 | * | |
2198 | * Adds the flow specified by 'ofm', which is followed by 'n_actions' | |
75a75043 BP |
2199 | * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow |
2200 | * error code as encoded by ofp_mkerr() on failure, or OFPROTO_POSTPONE if the | |
2201 | * operation cannot be initiated now but may be retried later. | |
79eee1eb BP |
2202 | * |
2203 | * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id, | |
2204 | * if any. */ | |
064af421 | 2205 | static int |
a9a2da38 | 2206 | add_flow(struct ofproto *ofproto, struct ofconn *ofconn, |
1b9d6420 | 2207 | const struct ofputil_flow_mod *fm, const struct ofp_header *request) |
064af421 | 2208 | { |
7ee20df1 BP |
2209 | struct classifier *table; |
2210 | struct ofopgroup *group; | |
2211 | struct rule *victim; | |
064af421 | 2212 | struct rule *rule; |
064af421 BP |
2213 | int error; |
2214 | ||
7ee20df1 | 2215 | /* Check for overlap, if requested. */ |
6c1491fb BP |
2216 | if (fm->flags & OFPFF_CHECK_OVERLAP) { |
2217 | struct classifier *cls; | |
2218 | ||
7ee20df1 | 2219 | FOR_EACH_MATCHING_TABLE (cls, fm->table_id, ofproto) { |
6c1491fb BP |
2220 | if (classifier_rule_overlaps(cls, &fm->cr)) { |
2221 | return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP); | |
2222 | } | |
2223 | } | |
49bdc010 JP |
2224 | } |
2225 | ||
7ee20df1 BP |
2226 | /* Pick table. */ |
2227 | if (fm->table_id == 0xff) { | |
2228 | uint8_t table_id; | |
13521ff5 | 2229 | if (ofproto->ofproto_class->rule_choose_table) { |
7ee20df1 BP |
2230 | error = ofproto->ofproto_class->rule_choose_table(ofproto, &fm->cr, |
2231 | &table_id); | |
2232 | if (error) { | |
2233 | return error; | |
2234 | } | |
2235 | assert(table_id < ofproto->n_tables); | |
2236 | table = &ofproto->tables[table_id]; | |
2237 | } else { | |
2238 | table = &ofproto->tables[0]; | |
2239 | } | |
2240 | } else if (fm->table_id < ofproto->n_tables) { | |
2241 | table = &ofproto->tables[fm->table_id]; | |
2242 | } else { | |
2243 | return ofp_mkerr_nicira(OFPET_FLOW_MOD_FAILED, NXFMFC_BAD_TABLE_ID); | |
064af421 BP |
2244 | } |
2245 | ||
7ee20df1 BP |
2246 | /* Serialize against pending deletion. */ |
2247 | if (is_flow_deletion_pending(ofproto, &fm->cr, table - ofproto->tables)) { | |
2248 | return OFPROTO_POSTPONE; | |
afe75089 | 2249 | } |
064af421 | 2250 | |
7ee20df1 BP |
2251 | /* Allocate new rule. */ |
2252 | rule = ofproto->ofproto_class->rule_alloc(); | |
2253 | if (!rule) { | |
2254 | VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)", | |
2255 | ofproto->name, strerror(error)); | |
2256 | return ENOMEM; | |
2257 | } | |
2258 | rule->ofproto = ofproto; | |
2259 | rule->cr = fm->cr; | |
2260 | rule->pending = NULL; | |
2261 | rule->flow_cookie = fm->cookie; | |
308881af | 2262 | rule->created = rule->modified = time_msec(); |
7ee20df1 BP |
2263 | rule->idle_timeout = fm->idle_timeout; |
2264 | rule->hard_timeout = fm->hard_timeout; | |
2265 | rule->table_id = table - ofproto->tables; | |
2266 | rule->send_flow_removed = (fm->flags & OFPFF_SEND_FLOW_REM) != 0; | |
2267 | rule->actions = ofputil_actions_clone(fm->actions, fm->n_actions); | |
2268 | rule->n_actions = fm->n_actions; | |
2269 | ||
2270 | /* Insert new rule. */ | |
2271 | victim = rule_from_cls_rule(classifier_replace(table, &rule->cr)); | |
2272 | if (victim && victim->pending) { | |
2273 | error = OFPROTO_POSTPONE; | |
2274 | } else { | |
7024dffe | 2275 | group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id); |
7ee20df1 BP |
2276 | ofoperation_create(group, rule, OFOPERATION_ADD); |
2277 | rule->pending->victim = victim; | |
2278 | ||
2279 | error = ofproto->ofproto_class->rule_construct(rule); | |
2280 | if (error) { | |
2281 | ofoperation_destroy(rule->pending); | |
2282 | } | |
2283 | ofopgroup_submit(group); | |
064af421 | 2284 | } |
79eee1eb | 2285 | |
7ee20df1 | 2286 | /* Back out if an error occurred. */ |
79eee1eb | 2287 | if (error) { |
7ee20df1 BP |
2288 | if (victim) { |
2289 | classifier_replace(table, &victim->cr); | |
2290 | } else { | |
2291 | classifier_remove(table, &rule->cr); | |
2292 | } | |
2293 | ofproto_rule_destroy__(rule); | |
79eee1eb | 2294 | } |
7ee20df1 | 2295 | return error; |
064af421 | 2296 | } |
79eee1eb BP |
2297 | \f |
2298 | /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */ | |
064af421 | 2299 | |
9ed18e46 BP |
2300 | /* Modifies the rules listed in 'rules', changing their actions to match those |
2301 | * in 'fm'. | |
79eee1eb | 2302 | * |
9ed18e46 BP |
2303 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, |
2304 | * if any. | |
2305 | * | |
2306 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
79eee1eb | 2307 | static int |
7024dffe BP |
2308 | modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn, |
2309 | const struct ofputil_flow_mod *fm, | |
7ee20df1 | 2310 | const struct ofp_header *request, struct list *rules) |
79eee1eb | 2311 | { |
7ee20df1 | 2312 | struct ofopgroup *group; |
9ed18e46 | 2313 | struct rule *rule; |
79eee1eb | 2314 | |
7024dffe | 2315 | group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id); |
9ed18e46 BP |
2316 | LIST_FOR_EACH (rule, ofproto_node, rules) { |
2317 | if (!ofputil_actions_equal(fm->actions, fm->n_actions, | |
2318 | rule->actions, rule->n_actions)) { | |
7ee20df1 BP |
2319 | ofoperation_create(group, rule, OFOPERATION_MODIFY); |
2320 | rule->pending->actions = rule->actions; | |
2321 | rule->pending->n_actions = rule->n_actions; | |
2322 | rule->actions = ofputil_actions_clone(fm->actions, fm->n_actions); | |
2323 | rule->n_actions = fm->n_actions; | |
2324 | rule->ofproto->ofproto_class->rule_modify_actions(rule); | |
308881af BP |
2325 | } else { |
2326 | rule->modified = time_msec(); | |
5ecc9d81 | 2327 | } |
9ed18e46 | 2328 | rule->flow_cookie = fm->cookie; |
5ecc9d81 | 2329 | } |
7ee20df1 | 2330 | ofopgroup_submit(group); |
79eee1eb | 2331 | |
7ee20df1 | 2332 | return 0; |
9ed18e46 BP |
2333 | } |
2334 | ||
2335 | /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as | |
2336 | * encoded by ofp_mkerr() on failure. | |
2337 | * | |
2338 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, | |
2339 | * if any. */ | |
2340 | static int | |
7024dffe | 2341 | modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn, |
1b9d6420 | 2342 | const struct ofputil_flow_mod *fm, |
7ee20df1 | 2343 | const struct ofp_header *request) |
9ed18e46 | 2344 | { |
9ed18e46 BP |
2345 | struct list rules; |
2346 | int error; | |
2347 | ||
7024dffe BP |
2348 | error = collect_rules_loose(ofproto, fm->table_id, &fm->cr, OFPP_NONE, |
2349 | &rules); | |
9ed18e46 | 2350 | return (error ? error |
7024dffe BP |
2351 | : list_is_empty(&rules) ? add_flow(ofproto, ofconn, fm, request) |
2352 | : modify_flows__(ofproto, ofconn, fm, request, &rules)); | |
79eee1eb BP |
2353 | } |
2354 | ||
2355 | /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error | |
2356 | * code as encoded by ofp_mkerr() on failure. | |
2357 | * | |
9ed18e46 | 2358 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, |
79eee1eb BP |
2359 | * if any. */ |
2360 | static int | |
7024dffe | 2361 | modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn, |
1b9d6420 | 2362 | const struct ofputil_flow_mod *fm, |
7ee20df1 | 2363 | const struct ofp_header *request) |
79eee1eb | 2364 | { |
9ed18e46 | 2365 | struct list rules; |
6c1491fb BP |
2366 | int error; |
2367 | ||
7024dffe BP |
2368 | error = collect_rules_strict(ofproto, fm->table_id, &fm->cr, OFPP_NONE, |
2369 | &rules); | |
9ed18e46 | 2370 | return (error ? error |
7024dffe BP |
2371 | : list_is_empty(&rules) ? add_flow(ofproto, ofconn, fm, request) |
2372 | : list_is_singleton(&rules) ? modify_flows__(ofproto, ofconn, | |
2373 | fm, request, &rules) | |
9ed18e46 | 2374 | : 0); |
79eee1eb | 2375 | } |
9ed18e46 BP |
2376 | \f |
2377 | /* OFPFC_DELETE implementation. */ | |
79eee1eb | 2378 | |
9ed18e46 BP |
2379 | /* Deletes the rules listed in 'rules'. |
2380 | * | |
2381 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
064af421 | 2382 | static int |
7024dffe BP |
2383 | delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn, |
2384 | const struct ofp_header *request, struct list *rules) | |
064af421 | 2385 | { |
9ed18e46 | 2386 | struct rule *rule, *next; |
7ee20df1 | 2387 | struct ofopgroup *group; |
79eee1eb | 2388 | |
7024dffe | 2389 | group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX); |
9ed18e46 BP |
2390 | LIST_FOR_EACH_SAFE (rule, next, ofproto_node, rules) { |
2391 | ofproto_rule_send_removed(rule, OFPRR_DELETE); | |
7ee20df1 BP |
2392 | |
2393 | ofoperation_create(group, rule, OFOPERATION_DELETE); | |
2394 | classifier_remove(&ofproto->tables[rule->table_id], &rule->cr); | |
2395 | rule->ofproto->ofproto_class->rule_destruct(rule); | |
79eee1eb | 2396 | } |
7ee20df1 | 2397 | ofopgroup_submit(group); |
79eee1eb | 2398 | |
9ed18e46 | 2399 | return 0; |
79eee1eb | 2400 | } |
79eee1eb BP |
2401 | |
2402 | /* Implements OFPFC_DELETE. */ | |
9ed18e46 | 2403 | static int |
7024dffe BP |
2404 | delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn, |
2405 | const struct ofputil_flow_mod *fm, | |
7ee20df1 | 2406 | const struct ofp_header *request) |
79eee1eb | 2407 | { |
9ed18e46 BP |
2408 | struct list rules; |
2409 | int error; | |
064af421 | 2410 | |
7024dffe | 2411 | error = collect_rules_loose(ofproto, fm->table_id, &fm->cr, fm->out_port, |
9ed18e46 BP |
2412 | &rules); |
2413 | return (error ? error | |
7024dffe BP |
2414 | : !list_is_empty(&rules) ? delete_flows__(ofproto, ofconn, request, |
2415 | &rules) | |
9ed18e46 | 2416 | : 0); |
064af421 BP |
2417 | } |
2418 | ||
79eee1eb | 2419 | /* Implements OFPFC_DELETE_STRICT. */ |
9ed18e46 | 2420 | static int |
7024dffe | 2421 | delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn, |
1b9d6420 | 2422 | const struct ofputil_flow_mod *fm, |
7ee20df1 | 2423 | const struct ofp_header *request) |
79eee1eb | 2424 | { |
9ed18e46 BP |
2425 | struct list rules; |
2426 | int error; | |
79eee1eb | 2427 | |
7024dffe | 2428 | error = collect_rules_strict(ofproto, fm->table_id, &fm->cr, fm->out_port, |
9ed18e46 BP |
2429 | &rules); |
2430 | return (error ? error | |
7024dffe BP |
2431 | : list_is_singleton(&rules) ? delete_flows__(ofproto, ofconn, |
2432 | request, &rules) | |
9ed18e46 | 2433 | : 0); |
abe529af BP |
2434 | } |
2435 | ||
2436 | static void | |
2437 | ofproto_rule_send_removed(struct rule *rule, uint8_t reason) | |
2438 | { | |
2439 | struct ofputil_flow_removed fr; | |
2440 | ||
2441 | if (rule_is_hidden(rule) || !rule->send_flow_removed) { | |
2442 | return; | |
2443 | } | |
2444 | ||
2445 | fr.rule = rule->cr; | |
2446 | fr.cookie = rule->flow_cookie; | |
2447 | fr.reason = reason; | |
2448 | calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec); | |
2449 | fr.idle_timeout = rule->idle_timeout; | |
2450 | rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count, | |
2451 | &fr.byte_count); | |
2452 | ||
2453 | connmgr_send_flow_removed(rule->ofproto->connmgr, &fr); | |
2454 | } | |
2455 | ||
2456 | /* Sends an OpenFlow "flow removed" message with the given 'reason' (either | |
2457 | * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its | |
2458 | * ofproto. | |
2459 | * | |
2460 | * ofproto implementation ->run() functions should use this function to expire | |
2461 | * OpenFlow flows. */ | |
2462 | void | |
2463 | ofproto_rule_expire(struct rule *rule, uint8_t reason) | |
2464 | { | |
7ee20df1 BP |
2465 | struct ofproto *ofproto = rule->ofproto; |
2466 | struct ofopgroup *group; | |
2467 | ||
abe529af | 2468 | assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT); |
7ee20df1 | 2469 | |
abe529af | 2470 | ofproto_rule_send_removed(rule, reason); |
7ee20df1 | 2471 | |
7024dffe | 2472 | group = ofopgroup_create_unattached(ofproto); |
7ee20df1 BP |
2473 | ofoperation_create(group, rule, OFOPERATION_DELETE); |
2474 | classifier_remove(&ofproto->tables[rule->table_id], &rule->cr); | |
2475 | rule->ofproto->ofproto_class->rule_destruct(rule); | |
2476 | ofopgroup_submit(group); | |
79eee1eb BP |
2477 | } |
2478 | \f | |
064af421 | 2479 | static int |
2e4f5fcf | 2480 | handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2481 | { |
a9a2da38 | 2482 | struct ofputil_flow_mod fm; |
064af421 BP |
2483 | int error; |
2484 | ||
3052b0c5 | 2485 | error = reject_slave_controller(ofconn, "flow_mod"); |
9deba63b BP |
2486 | if (error) { |
2487 | return error; | |
2488 | } | |
3052b0c5 | 2489 | |
00794817 | 2490 | error = ofputil_decode_flow_mod(&fm, oh, |
6c1491fb | 2491 | ofconn_get_flow_mod_table_id(ofconn)); |
064af421 BP |
2492 | if (error) { |
2493 | return error; | |
2494 | } | |
2495 | ||
2e4f5fcf BP |
2496 | /* We do not support the emergency flow cache. It will hopefully get |
2497 | * dropped from OpenFlow in the near future. */ | |
2498 | if (fm.flags & OFPFF_EMERG) { | |
49bdc010 JP |
2499 | /* There isn't a good fit for an error code, so just state that the |
2500 | * flow table is full. */ | |
2501 | return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL); | |
2502 | } | |
2503 | ||
75a75043 BP |
2504 | return handle_flow_mod__(ofconn_get_ofproto(ofconn), ofconn, &fm, oh); |
2505 | } | |
2506 | ||
2507 | static int | |
2508 | handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn, | |
2509 | const struct ofputil_flow_mod *fm, | |
2510 | const struct ofp_header *oh) | |
2511 | { | |
2512 | if (ofproto->n_pending >= 50) { | |
2513 | assert(!list_is_empty(&ofproto->pending)); | |
2514 | return OFPROTO_POSTPONE; | |
2515 | } | |
2516 | ||
2517 | switch (fm->command) { | |
3052b0c5 | 2518 | case OFPFC_ADD: |
75a75043 | 2519 | return add_flow(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
2520 | |
2521 | case OFPFC_MODIFY: | |
75a75043 | 2522 | return modify_flows_loose(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
2523 | |
2524 | case OFPFC_MODIFY_STRICT: | |
75a75043 | 2525 | return modify_flow_strict(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
2526 | |
2527 | case OFPFC_DELETE: | |
75a75043 | 2528 | return delete_flows_loose(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
2529 | |
2530 | case OFPFC_DELETE_STRICT: | |
75a75043 | 2531 | return delete_flow_strict(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
2532 | |
2533 | default: | |
75a75043 | 2534 | if (fm->command > 0xff) { |
6c1491fb BP |
2535 | VLOG_WARN_RL(&rl, "flow_mod has explicit table_id but " |
2536 | "flow_mod_table_id extension is not enabled"); | |
2537 | } | |
3052b0c5 BP |
2538 | return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND); |
2539 | } | |
2540 | } | |
2541 | ||
9deba63b | 2542 | static int |
d1e2cf21 | 2543 | handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh) |
9deba63b | 2544 | { |
d1e2cf21 | 2545 | struct nx_role_request *nrr = (struct nx_role_request *) oh; |
9deba63b BP |
2546 | struct nx_role_request *reply; |
2547 | struct ofpbuf *buf; | |
2548 | uint32_t role; | |
2549 | ||
1ce0a5fa | 2550 | if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) { |
19a87e36 | 2551 | VLOG_WARN_RL(&rl, "ignoring role request on service connection"); |
9deba63b BP |
2552 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM); |
2553 | } | |
2554 | ||
2555 | role = ntohl(nrr->role); | |
2556 | if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER | |
2557 | && role != NX_ROLE_SLAVE) { | |
2558 | VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role); | |
2559 | ||
2560 | /* There's no good error code for this. */ | |
2561 | return ofp_mkerr(OFPET_BAD_REQUEST, -1); | |
2562 | } | |
2563 | ||
7ee20df1 BP |
2564 | if (ofconn_get_role(ofconn) != role |
2565 | && ofconn_has_pending_opgroups(ofconn)) { | |
2566 | return OFPROTO_POSTPONE; | |
2567 | } | |
2568 | ||
1ce0a5fa | 2569 | ofconn_set_role(ofconn, role); |
9deba63b | 2570 | |
d1e2cf21 | 2571 | reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf); |
9deba63b | 2572 | reply->role = htonl(role); |
b0421aa2 | 2573 | ofconn_send_reply(ofconn, buf); |
9deba63b BP |
2574 | |
2575 | return 0; | |
2576 | } | |
2577 | ||
6c1491fb BP |
2578 | static int |
2579 | handle_nxt_flow_mod_table_id(struct ofconn *ofconn, | |
2580 | const struct ofp_header *oh) | |
2581 | { | |
2582 | const struct nxt_flow_mod_table_id *msg | |
2583 | = (const struct nxt_flow_mod_table_id *) oh; | |
2584 | ||
2585 | ofconn_set_flow_mod_table_id(ofconn, msg->set != 0); | |
2586 | return 0; | |
2587 | } | |
2588 | ||
09246b99 | 2589 | static int |
d1e2cf21 | 2590 | handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh) |
09246b99 | 2591 | { |
d1e2cf21 BP |
2592 | const struct nxt_set_flow_format *msg |
2593 | = (const struct nxt_set_flow_format *) oh; | |
09246b99 | 2594 | uint32_t format; |
09246b99 BP |
2595 | |
2596 | format = ntohl(msg->format); | |
7ee20df1 | 2597 | if (format != NXFF_OPENFLOW10 && format != NXFF_NXM) { |
09246b99 BP |
2598 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM); |
2599 | } | |
7ee20df1 BP |
2600 | |
2601 | if (format != ofconn_get_flow_format(ofconn) | |
2602 | && ofconn_has_pending_opgroups(ofconn)) { | |
2603 | /* Avoid sending async messages in surprising flow format. */ | |
2604 | return OFPROTO_POSTPONE; | |
2605 | } | |
2606 | ||
2607 | ofconn_set_flow_format(ofconn, format); | |
2608 | return 0; | |
09246b99 BP |
2609 | } |
2610 | ||
064af421 | 2611 | static int |
d1e2cf21 | 2612 | handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh) |
246e61ea JP |
2613 | { |
2614 | struct ofp_header *ob; | |
2615 | struct ofpbuf *buf; | |
2616 | ||
7ee20df1 BP |
2617 | if (ofconn_has_pending_opgroups(ofconn)) { |
2618 | return OFPROTO_POSTPONE; | |
2619 | } | |
2620 | ||
246e61ea | 2621 | ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf); |
b0421aa2 | 2622 | ofconn_send_reply(ofconn, buf); |
246e61ea JP |
2623 | return 0; |
2624 | } | |
2625 | ||
d1e2cf21 BP |
2626 | static int |
2627 | handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg) | |
064af421 | 2628 | { |
d1e2cf21 BP |
2629 | const struct ofp_header *oh = msg->data; |
2630 | const struct ofputil_msg_type *type; | |
064af421 BP |
2631 | int error; |
2632 | ||
d1e2cf21 BP |
2633 | error = ofputil_decode_msg_type(oh, &type); |
2634 | if (error) { | |
2635 | return error; | |
2636 | } | |
064af421 | 2637 | |
d1e2cf21 BP |
2638 | switch (ofputil_msg_type_code(type)) { |
2639 | /* OpenFlow requests. */ | |
2640 | case OFPUTIL_OFPT_ECHO_REQUEST: | |
2641 | return handle_echo_request(ofconn, oh); | |
064af421 | 2642 | |
d1e2cf21 BP |
2643 | case OFPUTIL_OFPT_FEATURES_REQUEST: |
2644 | return handle_features_request(ofconn, oh); | |
064af421 | 2645 | |
d1e2cf21 BP |
2646 | case OFPUTIL_OFPT_GET_CONFIG_REQUEST: |
2647 | return handle_get_config_request(ofconn, oh); | |
064af421 | 2648 | |
d1e2cf21 BP |
2649 | case OFPUTIL_OFPT_SET_CONFIG: |
2650 | return handle_set_config(ofconn, msg->data); | |
064af421 | 2651 | |
d1e2cf21 BP |
2652 | case OFPUTIL_OFPT_PACKET_OUT: |
2653 | return handle_packet_out(ofconn, oh); | |
064af421 | 2654 | |
d1e2cf21 BP |
2655 | case OFPUTIL_OFPT_PORT_MOD: |
2656 | return handle_port_mod(ofconn, oh); | |
064af421 | 2657 | |
d1e2cf21 | 2658 | case OFPUTIL_OFPT_FLOW_MOD: |
2e4f5fcf | 2659 | return handle_flow_mod(ofconn, oh); |
064af421 | 2660 | |
d1e2cf21 BP |
2661 | case OFPUTIL_OFPT_BARRIER_REQUEST: |
2662 | return handle_barrier_request(ofconn, oh); | |
064af421 | 2663 | |
d1e2cf21 BP |
2664 | /* OpenFlow replies. */ |
2665 | case OFPUTIL_OFPT_ECHO_REPLY: | |
2666 | return 0; | |
246e61ea | 2667 | |
d1e2cf21 | 2668 | /* Nicira extension requests. */ |
d1e2cf21 BP |
2669 | case OFPUTIL_NXT_ROLE_REQUEST: |
2670 | return handle_role_request(ofconn, oh); | |
2671 | ||
6c1491fb BP |
2672 | case OFPUTIL_NXT_FLOW_MOD_TABLE_ID: |
2673 | return handle_nxt_flow_mod_table_id(ofconn, oh); | |
d1e2cf21 BP |
2674 | |
2675 | case OFPUTIL_NXT_SET_FLOW_FORMAT: | |
2676 | return handle_nxt_set_flow_format(ofconn, oh); | |
2677 | ||
2678 | case OFPUTIL_NXT_FLOW_MOD: | |
2e4f5fcf | 2679 | return handle_flow_mod(ofconn, oh); |
d1e2cf21 | 2680 | |
349adfb2 | 2681 | /* Statistics requests. */ |
d1e2cf21 | 2682 | case OFPUTIL_OFPST_DESC_REQUEST: |
63f2140a | 2683 | return handle_desc_stats_request(ofconn, msg->data); |
d1e2cf21 BP |
2684 | |
2685 | case OFPUTIL_OFPST_FLOW_REQUEST: | |
349adfb2 | 2686 | case OFPUTIL_NXST_FLOW_REQUEST: |
63f2140a | 2687 | return handle_flow_stats_request(ofconn, msg->data); |
d1e2cf21 BP |
2688 | |
2689 | case OFPUTIL_OFPST_AGGREGATE_REQUEST: | |
76c93b22 | 2690 | case OFPUTIL_NXST_AGGREGATE_REQUEST: |
63f2140a | 2691 | return handle_aggregate_stats_request(ofconn, msg->data); |
d1e2cf21 BP |
2692 | |
2693 | case OFPUTIL_OFPST_TABLE_REQUEST: | |
63f2140a | 2694 | return handle_table_stats_request(ofconn, msg->data); |
d1e2cf21 BP |
2695 | |
2696 | case OFPUTIL_OFPST_PORT_REQUEST: | |
63f2140a | 2697 | return handle_port_stats_request(ofconn, msg->data); |
d1e2cf21 BP |
2698 | |
2699 | case OFPUTIL_OFPST_QUEUE_REQUEST: | |
63f2140a | 2700 | return handle_queue_stats_request(ofconn, msg->data); |
d1e2cf21 | 2701 | |
8f93e93c | 2702 | case OFPUTIL_MSG_INVALID: |
d1e2cf21 BP |
2703 | case OFPUTIL_OFPT_HELLO: |
2704 | case OFPUTIL_OFPT_ERROR: | |
2705 | case OFPUTIL_OFPT_FEATURES_REPLY: | |
2706 | case OFPUTIL_OFPT_GET_CONFIG_REPLY: | |
2707 | case OFPUTIL_OFPT_PACKET_IN: | |
2708 | case OFPUTIL_OFPT_FLOW_REMOVED: | |
2709 | case OFPUTIL_OFPT_PORT_STATUS: | |
2710 | case OFPUTIL_OFPT_BARRIER_REPLY: | |
2711 | case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST: | |
2712 | case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY: | |
2713 | case OFPUTIL_OFPST_DESC_REPLY: | |
2714 | case OFPUTIL_OFPST_FLOW_REPLY: | |
2715 | case OFPUTIL_OFPST_QUEUE_REPLY: | |
2716 | case OFPUTIL_OFPST_PORT_REPLY: | |
2717 | case OFPUTIL_OFPST_TABLE_REPLY: | |
2718 | case OFPUTIL_OFPST_AGGREGATE_REPLY: | |
d1e2cf21 BP |
2719 | case OFPUTIL_NXT_ROLE_REPLY: |
2720 | case OFPUTIL_NXT_FLOW_REMOVED: | |
2721 | case OFPUTIL_NXST_FLOW_REPLY: | |
2722 | case OFPUTIL_NXST_AGGREGATE_REPLY: | |
064af421 BP |
2723 | default: |
2724 | if (VLOG_IS_WARN_ENABLED()) { | |
2725 | char *s = ofp_to_string(oh, ntohs(oh->length), 2); | |
2726 | VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s); | |
2727 | free(s); | |
2728 | } | |
d1e2cf21 BP |
2729 | if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) { |
2730 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT); | |
2731 | } else { | |
2732 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE); | |
2733 | } | |
064af421 | 2734 | } |
d1e2cf21 | 2735 | } |
064af421 | 2736 | |
7ee20df1 | 2737 | static bool |
d1e2cf21 BP |
2738 | handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg) |
2739 | { | |
2740 | int error = handle_openflow__(ofconn, ofp_msg); | |
7ee20df1 | 2741 | if (error && error != OFPROTO_POSTPONE) { |
1be5ff75 | 2742 | ofconn_send_error(ofconn, ofp_msg->data, error); |
064af421 | 2743 | } |
d1e2cf21 | 2744 | COVERAGE_INC(ofproto_recv_openflow); |
7ee20df1 BP |
2745 | return error != OFPROTO_POSTPONE; |
2746 | } | |
2747 | \f | |
2748 | /* Asynchronous operations. */ | |
2749 | ||
2750 | /* Creates and returns a new ofopgroup that is not associated with any | |
2751 | * OpenFlow connection. | |
2752 | * | |
2753 | * The caller should add operations to the returned group with | |
2754 | * ofoperation_create() and then submit it with ofopgroup_submit(). */ | |
2755 | static struct ofopgroup * | |
7024dffe | 2756 | ofopgroup_create_unattached(struct ofproto *ofproto) |
7ee20df1 BP |
2757 | { |
2758 | struct ofopgroup *group = xzalloc(sizeof *group); | |
2759 | group->ofproto = ofproto; | |
2760 | list_init(&group->ofproto_node); | |
2761 | list_init(&group->ops); | |
2762 | list_init(&group->ofconn_node); | |
2763 | return group; | |
2764 | } | |
2765 | ||
7024dffe BP |
2766 | /* Creates and returns a new ofopgroup for 'ofproto'. |
2767 | * | |
2768 | * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow | |
2769 | * connection. The 'request' and 'buffer_id' arguments are ignored. | |
2770 | * | |
2771 | * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'. | |
2772 | * If the ofopgroup eventually fails, then the error reply will include | |
2773 | * 'request'. If the ofopgroup eventually succeeds, then the packet with | |
2774 | * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto. | |
7ee20df1 BP |
2775 | * |
2776 | * The caller should add operations to the returned group with | |
2777 | * ofoperation_create() and then submit it with ofopgroup_submit(). */ | |
2778 | static struct ofopgroup * | |
7024dffe BP |
2779 | ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn, |
2780 | const struct ofp_header *request, uint32_t buffer_id) | |
7ee20df1 | 2781 | { |
7024dffe BP |
2782 | struct ofopgroup *group = ofopgroup_create_unattached(ofproto); |
2783 | if (ofconn) { | |
2784 | size_t request_len = ntohs(request->length); | |
7ee20df1 | 2785 | |
7024dffe | 2786 | assert(ofconn_get_ofproto(ofconn) == ofproto); |
7ee20df1 | 2787 | |
7024dffe BP |
2788 | ofconn_add_opgroup(ofconn, &group->ofconn_node); |
2789 | group->ofconn = ofconn; | |
2790 | group->request = xmemdup(request, MIN(request_len, 64)); | |
2791 | group->buffer_id = buffer_id; | |
2792 | } | |
7ee20df1 BP |
2793 | return group; |
2794 | } | |
2795 | ||
2796 | /* Submits 'group' for processing. | |
2797 | * | |
2798 | * If 'group' contains no operations (e.g. none were ever added, or all of the | |
2799 | * ones that were added completed synchronously), then it is destroyed | |
2800 | * immediately. Otherwise it is added to the ofproto's list of pending | |
2801 | * groups. */ | |
2802 | static void | |
2803 | ofopgroup_submit(struct ofopgroup *group) | |
2804 | { | |
2805 | if (list_is_empty(&group->ops)) { | |
2806 | ofopgroup_destroy(group); | |
2807 | } else { | |
2808 | list_push_back(&group->ofproto->pending, &group->ofproto_node); | |
dd5616b3 | 2809 | group->ofproto->n_pending++; |
7ee20df1 BP |
2810 | } |
2811 | } | |
2812 | ||
2813 | static void | |
2814 | ofopgroup_destroy(struct ofopgroup *group) | |
2815 | { | |
2816 | assert(list_is_empty(&group->ops)); | |
2817 | if (!list_is_empty(&group->ofproto_node)) { | |
dd5616b3 BP |
2818 | assert(group->ofproto->n_pending > 0); |
2819 | group->ofproto->n_pending--; | |
7ee20df1 BP |
2820 | list_remove(&group->ofproto_node); |
2821 | } | |
2822 | if (!list_is_empty(&group->ofconn_node)) { | |
2823 | list_remove(&group->ofconn_node); | |
2824 | if (group->error) { | |
2825 | ofconn_send_error(group->ofconn, group->request, group->error); | |
2826 | } | |
2827 | connmgr_retry(group->ofproto->connmgr); | |
2828 | } | |
2829 | free(group->request); | |
2830 | free(group); | |
2831 | } | |
2832 | ||
2833 | /* Initiates a new operation on 'rule', of the specified 'type', within | |
2834 | * 'group'. Prior to calling, 'rule' must not have any pending operation. */ | |
2835 | static void | |
2836 | ofoperation_create(struct ofopgroup *group, struct rule *rule, | |
2837 | enum ofoperation_type type) | |
2838 | { | |
2839 | struct ofoperation *op; | |
2840 | ||
2841 | assert(!rule->pending); | |
2842 | ||
2843 | op = rule->pending = xzalloc(sizeof *op); | |
2844 | op->group = group; | |
2845 | list_push_back(&group->ops, &op->group_node); | |
2846 | op->rule = rule; | |
2847 | op->type = type; | |
2848 | op->status = -1; | |
2849 | op->flow_cookie = rule->flow_cookie; | |
2850 | ||
2851 | if (type == OFOPERATION_DELETE) { | |
2852 | hmap_insert(&op->group->ofproto->deletions, &op->hmap_node, | |
2853 | cls_rule_hash(&rule->cr, rule->table_id)); | |
2854 | } | |
2855 | } | |
2856 | ||
2857 | static void | |
2858 | ofoperation_destroy(struct ofoperation *op) | |
2859 | { | |
2860 | struct ofopgroup *group = op->group; | |
2861 | ||
2862 | if (op->rule) { | |
2863 | op->rule->pending = NULL; | |
2864 | } | |
2865 | if (op->type == OFOPERATION_DELETE) { | |
2866 | hmap_remove(&group->ofproto->deletions, &op->hmap_node); | |
2867 | } | |
2868 | list_remove(&op->group_node); | |
2869 | free(op->actions); | |
2870 | free(op); | |
2871 | ||
2872 | if (list_is_empty(&group->ops) && !list_is_empty(&group->ofproto_node)) { | |
2873 | ofopgroup_destroy(group); | |
2874 | } | |
2875 | } | |
2876 | ||
2877 | /* Indicates that 'op' completed with status 'error', which is either 0 to | |
2878 | * indicate success or an OpenFlow error code (constructed with | |
2879 | * e.g. ofp_mkerr()). | |
2880 | * | |
a6a62132 BP |
2881 | * If 'error' is 0, indicating success, the operation will be committed |
2882 | * permanently to the flow table. There is one interesting subcase: | |
2883 | * | |
2884 | * - If 'op' is an "add flow" operation that is replacing an existing rule in | |
2885 | * the flow table (the "victim" rule) by a new one, then the caller must | |
2886 | * have uninitialized any derived state in the victim rule, as in step 5 in | |
2887 | * the "Life Cycle" in ofproto/ofproto-provider.h. ofoperation_complete() | |
2888 | * performs steps 6 and 7 for the victim rule, most notably by calling its | |
2889 | * ->rule_dealloc() function. | |
2890 | * | |
2891 | * If 'error' is nonzero, then generally the operation will be rolled back: | |
2892 | * | |
2893 | * - If 'op' is an "add flow" operation, ofproto removes the new rule or | |
2894 | * restores the original rule. The caller must have uninitialized any | |
2895 | * derived state in the new rule, as in step 5 of in the "Life Cycle" in | |
2896 | * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and | |
2897 | * and 7 for the new rule, calling its ->rule_dealloc() function. | |
2898 | * | |
2899 | * - If 'op' is a "modify flow" operation, ofproto restores the original | |
2900 | * actions. | |
2901 | * | |
2902 | * - 'op' must not be a "delete flow" operation. Removing a rule is not | |
2903 | * allowed to fail. It must always succeed. | |
7ee20df1 | 2904 | * |
5bee6e26 JP |
2905 | * Please see the large comment in ofproto/ofproto-provider.h titled |
2906 | * "Asynchronous Operation Support" for more information. */ | |
7ee20df1 BP |
2907 | void |
2908 | ofoperation_complete(struct ofoperation *op, int error) | |
2909 | { | |
2910 | struct ofopgroup *group = op->group; | |
2911 | struct rule *rule = op->rule; | |
2912 | struct classifier *table = &rule->ofproto->tables[rule->table_id]; | |
2913 | ||
2914 | assert(rule->pending == op); | |
2915 | assert(op->status < 0); | |
2916 | assert(error >= 0); | |
2917 | ||
2918 | if (!error | |
2919 | && !group->error | |
2920 | && op->type != OFOPERATION_DELETE | |
2921 | && group->ofconn | |
2922 | && group->buffer_id != UINT32_MAX | |
2923 | && list_is_singleton(&op->group_node)) { | |
2924 | struct ofpbuf *packet; | |
2925 | uint16_t in_port; | |
2926 | ||
2927 | error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id, | |
2928 | &packet, &in_port); | |
2929 | if (packet) { | |
2930 | assert(!error); | |
2931 | error = rule_execute(rule, in_port, packet); | |
2932 | } | |
2933 | } | |
2934 | if (!group->error) { | |
2935 | group->error = error; | |
2936 | } | |
2937 | ||
2938 | switch (op->type) { | |
2939 | case OFOPERATION_ADD: | |
2940 | if (!error) { | |
2941 | if (op->victim) { | |
2942 | ofproto_rule_destroy__(op->victim); | |
2943 | } | |
2944 | } else { | |
2945 | if (op->victim) { | |
2946 | classifier_replace(table, &op->victim->cr); | |
2947 | op->victim = NULL; | |
2948 | } else { | |
2949 | classifier_remove(table, &rule->cr); | |
2950 | } | |
2951 | ofproto_rule_destroy__(rule); | |
2952 | } | |
2953 | op->victim = NULL; | |
2954 | break; | |
2955 | ||
2956 | case OFOPERATION_DELETE: | |
2957 | assert(!error); | |
2958 | ofproto_rule_destroy__(rule); | |
2959 | op->rule = NULL; | |
2960 | break; | |
2961 | ||
2962 | case OFOPERATION_MODIFY: | |
308881af BP |
2963 | if (!error) { |
2964 | rule->modified = time_msec(); | |
2965 | } else { | |
7ee20df1 BP |
2966 | free(rule->actions); |
2967 | rule->actions = op->actions; | |
2968 | rule->n_actions = op->n_actions; | |
2969 | op->actions = NULL; | |
2970 | } | |
2971 | break; | |
2972 | ||
2973 | default: | |
2974 | NOT_REACHED(); | |
2975 | } | |
2976 | ofoperation_destroy(op); | |
2977 | } | |
2978 | ||
2979 | struct rule * | |
2980 | ofoperation_get_victim(struct ofoperation *op) | |
2981 | { | |
2982 | assert(op->type == OFOPERATION_ADD); | |
2983 | return op->victim; | |
064af421 BP |
2984 | } |
2985 | \f | |
064af421 | 2986 | static uint64_t |
fa60c019 | 2987 | pick_datapath_id(const struct ofproto *ofproto) |
064af421 | 2988 | { |
fa60c019 | 2989 | const struct ofport *port; |
064af421 | 2990 | |
abe529af | 2991 | port = ofproto_get_port(ofproto, OFPP_LOCAL); |
fa60c019 BP |
2992 | if (port) { |
2993 | uint8_t ea[ETH_ADDR_LEN]; | |
2994 | int error; | |
2995 | ||
2996 | error = netdev_get_etheraddr(port->netdev, ea); | |
064af421 BP |
2997 | if (!error) { |
2998 | return eth_addr_to_uint64(ea); | |
2999 | } | |
3000 | VLOG_WARN("could not get MAC address for %s (%s)", | |
fa60c019 | 3001 | netdev_get_name(port->netdev), strerror(error)); |
064af421 | 3002 | } |
fa60c019 | 3003 | return ofproto->fallback_dpid; |
064af421 BP |
3004 | } |
3005 | ||
3006 | static uint64_t | |
3007 | pick_fallback_dpid(void) | |
3008 | { | |
3009 | uint8_t ea[ETH_ADDR_LEN]; | |
70150daf | 3010 | eth_addr_nicira_random(ea); |
064af421 BP |
3011 | return eth_addr_to_uint64(ea); |
3012 | } | |
3013 | \f | |
abe529af | 3014 | /* unixctl commands. */ |
7aa697dd | 3015 | |
abe529af | 3016 | struct ofproto * |
2ac6bedd | 3017 | ofproto_lookup(const char *name) |
7aa697dd | 3018 | { |
2ac6bedd | 3019 | struct ofproto *ofproto; |
7aa697dd | 3020 | |
2ac6bedd BP |
3021 | HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0), |
3022 | &all_ofprotos) { | |
3023 | if (!strcmp(ofproto->name, name)) { | |
3024 | return ofproto; | |
3025 | } | |
7aa697dd | 3026 | } |
2ac6bedd | 3027 | return NULL; |
7aa697dd BP |
3028 | } |
3029 | ||
3030 | static void | |
7aa697dd BP |
3031 | ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED, |
3032 | void *aux OVS_UNUSED) | |
7aa697dd | 3033 | { |
7aa697dd | 3034 | struct ofproto *ofproto; |
7aa697dd | 3035 | struct ds results; |
7aa697dd | 3036 | |
7aa697dd | 3037 | ds_init(&results); |
2ac6bedd BP |
3038 | HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) { |
3039 | ds_put_format(&results, "%s\n", ofproto->name); | |
7aa697dd | 3040 | } |
7aa697dd BP |
3041 | unixctl_command_reply(conn, 200, ds_cstr(&results)); |
3042 | ds_destroy(&results); | |
7aa697dd BP |
3043 | } |
3044 | ||
3045 | static void | |
3046 | ofproto_unixctl_init(void) | |
3047 | { | |
3048 | static bool registered; | |
3049 | if (registered) { | |
3050 | return; | |
3051 | } | |
3052 | registered = true; | |
3053 | ||
3054 | unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL); | |
064af421 | 3055 | } |