]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
cb22974d | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
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> | |
448a4b2f | 24 | #include <unistd.h> |
52a90c29 | 25 | #include "bitmap.h" |
10a24935 | 26 | #include "byte-order.h" |
064af421 | 27 | #include "classifier.h" |
19a87e36 | 28 | #include "connmgr.h" |
064af421 | 29 | #include "coverage.h" |
4f2cad2c | 30 | #include "dynamic-string.h" |
ca0f572c BP |
31 | #include "hash.h" |
32 | #include "hmap.h" | |
254750ce | 33 | #include "meta-flow.h" |
064af421 | 34 | #include "netdev.h" |
09246b99 | 35 | #include "nx-match.h" |
f25d0cf3 | 36 | #include "ofp-actions.h" |
90bf1e07 | 37 | #include "ofp-errors.h" |
982697a4 | 38 | #include "ofp-msgs.h" |
064af421 | 39 | #include "ofp-print.h" |
fa37b408 | 40 | #include "ofp-util.h" |
064af421 | 41 | #include "ofpbuf.h" |
5bee6e26 | 42 | #include "ofproto-provider.h" |
064af421 BP |
43 | #include "openflow/nicira-ext.h" |
44 | #include "openflow/openflow.h" | |
064af421 BP |
45 | #include "packets.h" |
46 | #include "pinsched.h" | |
47 | #include "pktbuf.h" | |
48 | #include "poll-loop.h" | |
254750ce | 49 | #include "random.h" |
064af421 | 50 | #include "shash.h" |
0d085684 | 51 | #include "simap.h" |
b3c01ed3 | 52 | #include "sset.h" |
064af421 | 53 | #include "timeval.h" |
c4617b3c | 54 | #include "unaligned.h" |
4f2cad2c | 55 | #include "unixctl.h" |
5136ce49 | 56 | #include "vlog.h" |
064af421 | 57 | |
d98e6007 | 58 | VLOG_DEFINE_THIS_MODULE(ofproto); |
064af421 | 59 | |
d76f09ea | 60 | COVERAGE_DEFINE(ofproto_error); |
d76f09ea | 61 | COVERAGE_DEFINE(ofproto_flush); |
d76f09ea | 62 | COVERAGE_DEFINE(ofproto_no_packet_in); |
d76f09ea BP |
63 | COVERAGE_DEFINE(ofproto_packet_out); |
64 | COVERAGE_DEFINE(ofproto_queue_req); | |
65 | COVERAGE_DEFINE(ofproto_recv_openflow); | |
66 | COVERAGE_DEFINE(ofproto_reinit_ports); | |
d76f09ea BP |
67 | COVERAGE_DEFINE(ofproto_uninstallable); |
68 | COVERAGE_DEFINE(ofproto_update_port); | |
69 | ||
7ee20df1 BP |
70 | enum ofproto_state { |
71 | S_OPENFLOW, /* Processing OpenFlow commands. */ | |
254750ce | 72 | S_EVICT, /* Evicting flows from over-limit tables. */ |
7ee20df1 BP |
73 | S_FLUSH, /* Deleting all flow table rules. */ |
74 | }; | |
75 | ||
76 | enum ofoperation_type { | |
77 | OFOPERATION_ADD, | |
78 | OFOPERATION_DELETE, | |
b277c7cc BP |
79 | OFOPERATION_MODIFY, |
80 | OFOPERATION_REPLACE | |
7ee20df1 BP |
81 | }; |
82 | ||
83 | /* A single OpenFlow request can execute any number of operations. The | |
84 | * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the | |
85 | * ofconn to which an error reply should be sent if necessary. | |
86 | * | |
87 | * ofproto initiates some operations internally. These operations are still | |
88 | * assigned to groups but will not have an associated ofconn. */ | |
89 | struct ofopgroup { | |
90 | struct ofproto *ofproto; /* Owning ofproto. */ | |
91 | struct list ofproto_node; /* In ofproto's "pending" list. */ | |
92 | struct list ops; /* List of "struct ofoperation"s. */ | |
e615b0a3 | 93 | int n_running; /* Number of ops still pending. */ |
7ee20df1 BP |
94 | |
95 | /* Data needed to send OpenFlow reply on failure or to send a buffered | |
96 | * packet on success. | |
97 | * | |
98 | * If list_is_empty(ofconn_node) then this ofopgroup never had an | |
99 | * associated ofconn or its ofconn's connection dropped after it initiated | |
100 | * the operation. In the latter case 'ofconn' is a wild pointer that | |
101 | * refers to freed memory, so the 'ofconn' member must be used only if | |
102 | * !list_is_empty(ofconn_node). | |
103 | */ | |
104 | struct list ofconn_node; /* In ofconn's list of pending opgroups. */ | |
105 | struct ofconn *ofconn; /* ofconn for reply (but see note above). */ | |
106 | struct ofp_header *request; /* Original request (truncated at 64 bytes). */ | |
107 | uint32_t buffer_id; /* Buffer id from original request. */ | |
7ee20df1 BP |
108 | }; |
109 | ||
7024dffe BP |
110 | static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *); |
111 | static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *, | |
112 | const struct ofp_header *, | |
113 | uint32_t buffer_id); | |
7ee20df1 | 114 | static void ofopgroup_submit(struct ofopgroup *); |
e615b0a3 | 115 | static void ofopgroup_complete(struct ofopgroup *); |
7ee20df1 BP |
116 | |
117 | /* A single flow table operation. */ | |
118 | struct ofoperation { | |
119 | struct ofopgroup *group; /* Owning group. */ | |
120 | struct list group_node; /* In ofopgroup's "ops" list. */ | |
121 | struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */ | |
122 | struct rule *rule; /* Rule being operated upon. */ | |
123 | enum ofoperation_type type; /* Type of operation. */ | |
08043761 | 124 | |
b277c7cc BP |
125 | /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions |
126 | * are changing. */ | |
08043761 BP |
127 | struct ofpact *ofpacts; |
128 | size_t ofpacts_len; | |
9cae45dc | 129 | uint32_t meter_id; |
08043761 | 130 | |
2b07c8b1 BP |
131 | /* OFOPERATION_DELETE. */ |
132 | enum ofp_flow_removed_reason reason; /* Reason flow was removed. */ | |
133 | ||
7ee20df1 | 134 | ovs_be64 flow_cookie; /* Rule's old flow cookie. */ |
b277c7cc BP |
135 | uint16_t idle_timeout; /* Rule's old idle timeout. */ |
136 | uint16_t hard_timeout; /* Rule's old hard timeout. */ | |
137 | bool send_flow_removed; /* Rule's old 'send_flow_removed'. */ | |
e615b0a3 | 138 | enum ofperr error; /* 0 if no error. */ |
7ee20df1 BP |
139 | }; |
140 | ||
a7d94793 BP |
141 | static struct ofoperation *ofoperation_create(struct ofopgroup *, |
142 | struct rule *, | |
2b07c8b1 BP |
143 | enum ofoperation_type, |
144 | enum ofp_flow_removed_reason); | |
7ee20df1 BP |
145 | static void ofoperation_destroy(struct ofoperation *); |
146 | ||
d0918789 BP |
147 | /* oftable. */ |
148 | static void oftable_init(struct oftable *); | |
149 | static void oftable_destroy(struct oftable *); | |
878ae780 | 150 | |
254750ce BP |
151 | static void oftable_set_name(struct oftable *, const char *name); |
152 | ||
153 | static void oftable_disable_eviction(struct oftable *); | |
154 | static void oftable_enable_eviction(struct oftable *, | |
155 | const struct mf_subfield *fields, | |
156 | size_t n_fields); | |
157 | ||
ad3efdcb | 158 | static void oftable_remove_rule(struct rule *rule) OVS_RELEASES(rule->evict); |
0b4f2078 EJ |
159 | static void oftable_remove_rule__(struct ofproto *ofproto, |
160 | struct classifier *cls, struct rule *rule) | |
ad3efdcb | 161 | OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->evict); |
b277c7cc | 162 | static void oftable_insert_rule(struct rule *); |
064af421 | 163 | |
254750ce BP |
164 | /* A set of rules within a single OpenFlow table (oftable) that have the same |
165 | * values for the oftable's eviction_fields. A rule to be evicted, when one is | |
166 | * needed, is taken from the eviction group that contains the greatest number | |
167 | * of rules. | |
168 | * | |
169 | * An oftable owns any number of eviction groups, each of which contains any | |
170 | * number of rules. | |
171 | * | |
172 | * Membership in an eviction group is imprecise, based on the hash of the | |
173 | * oftable's eviction_fields (in the eviction_group's id_node.hash member). | |
174 | * That is, if two rules have different eviction_fields, but those | |
175 | * eviction_fields hash to the same value, then they will belong to the same | |
176 | * eviction_group anyway. | |
177 | * | |
178 | * (When eviction is not enabled on an oftable, we don't track any eviction | |
179 | * groups, to save time and space.) */ | |
180 | struct eviction_group { | |
181 | struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */ | |
182 | struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */ | |
183 | struct heap rules; /* Contains "struct rule"s. */ | |
184 | }; | |
185 | ||
ad3efdcb EJ |
186 | static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep) |
187 | OVS_TRY_WRLOCK(true, (*rulep)->evict); | |
254750ce BP |
188 | static void ofproto_evict(struct ofproto *); |
189 | static uint32_t rule_eviction_priority(struct rule *); | |
b277c7cc BP |
190 | static void eviction_group_add_rule(struct rule *); |
191 | static void eviction_group_remove_rule(struct rule *); | |
f29152ca | 192 | |
d0918789 BP |
193 | /* ofport. */ |
194 | static void ofport_destroy__(struct ofport *); | |
195 | static void ofport_destroy(struct ofport *); | |
196 | ||
197 | static void update_port(struct ofproto *, const char *devname); | |
198 | static int init_ports(struct ofproto *); | |
199 | static void reinit_ports(struct ofproto *); | |
7ee20df1 | 200 | |
254750ce BP |
201 | /* rule. */ |
202 | static void ofproto_rule_destroy__(struct rule *); | |
203 | static void ofproto_rule_send_removed(struct rule *, uint8_t reason); | |
204 | static bool rule_is_modifiable(const struct rule *); | |
254750ce | 205 | |
d0918789 | 206 | /* OpenFlow. */ |
90bf1e07 | 207 | static enum ofperr add_flow(struct ofproto *, struct ofconn *, |
e3b56933 | 208 | struct ofputil_flow_mod *, |
90bf1e07 | 209 | const struct ofp_header *); |
b277c7cc BP |
210 | static enum ofperr modify_flows__(struct ofproto *, struct ofconn *, |
211 | struct ofputil_flow_mod *, | |
212 | const struct ofp_header *, struct list *); | |
ad3efdcb EJ |
213 | static void delete_flow__(struct rule *rule, struct ofopgroup *, |
214 | enum ofp_flow_removed_reason) | |
215 | OVS_RELEASES(rule->evict); | |
e03248b7 | 216 | static bool handle_openflow(struct ofconn *, const struct ofpbuf *); |
90bf1e07 | 217 | static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *, |
e3b56933 | 218 | struct ofputil_flow_mod *, |
d0918789 | 219 | const struct ofp_header *); |
65e0be10 BP |
220 | static void calc_duration(long long int start, long long int now, |
221 | uint32_t *sec, uint32_t *nsec); | |
f29152ca | 222 | |
d0918789 BP |
223 | /* ofproto. */ |
224 | static uint64_t pick_datapath_id(const struct ofproto *); | |
225 | static uint64_t pick_fallback_dpid(void); | |
226 | static void ofproto_destroy__(struct ofproto *); | |
ada3428f | 227 | static void update_mtu(struct ofproto *, struct ofport *); |
6b3f7f02 | 228 | static void meter_delete(struct ofproto *, uint32_t first, uint32_t last); |
f29152ca | 229 | |
d0918789 | 230 | /* unixctl. */ |
abe529af | 231 | static void ofproto_unixctl_init(void); |
f29152ca | 232 | |
abe529af BP |
233 | /* All registered ofproto classes, in probe order. */ |
234 | static const struct ofproto_class **ofproto_classes; | |
235 | static size_t n_ofproto_classes; | |
236 | static size_t allocated_ofproto_classes; | |
7aa697dd | 237 | |
380f49c4 | 238 | unsigned flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT; |
448a4b2f | 239 | unsigned n_handler_threads; |
7155fa52 | 240 | enum ofproto_flow_miss_model flow_miss_model = OFPROTO_HANDLE_MISS_AUTO; |
380f49c4 | 241 | |
f797957a | 242 | /* Map from datapath name to struct ofproto, for use by unixctl commands. */ |
abe529af | 243 | static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos); |
ebe482fd | 244 | |
e1b1d06a JP |
245 | /* Initial mappings of port to OpenFlow number mappings. */ |
246 | static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports); | |
247 | ||
abe529af | 248 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
f29152ca | 249 | |
40358701 GS |
250 | /* The default value of true waits for flow restore. */ |
251 | static bool flow_restore_wait = true; | |
252 | ||
b0408fca JP |
253 | /* Must be called to initialize the ofproto library. |
254 | * | |
255 | * The caller may pass in 'iface_hints', which contains an shash of | |
256 | * "iface_hint" elements indexed by the interface's name. The provider | |
257 | * may use these hints to describe the startup configuration in order to | |
258 | * reinitialize its state. The caller owns the provided data, so a | |
259 | * provider will make copies of anything required. An ofproto provider | |
260 | * will remove any existing state that is not described by the hint, and | |
261 | * may choose to remove it all. */ | |
262 | void | |
263 | ofproto_init(const struct shash *iface_hints) | |
abe529af | 264 | { |
e1b1d06a | 265 | struct shash_node *node; |
b0408fca | 266 | size_t i; |
f29152ca | 267 | |
b0408fca JP |
268 | ofproto_class_register(&ofproto_dpif_class); |
269 | ||
e1b1d06a JP |
270 | /* Make a local copy, since we don't own 'iface_hints' elements. */ |
271 | SHASH_FOR_EACH(node, iface_hints) { | |
272 | const struct iface_hint *orig_hint = node->data; | |
273 | struct iface_hint *new_hint = xmalloc(sizeof *new_hint); | |
274 | const char *br_type = ofproto_normalize_type(orig_hint->br_type); | |
275 | ||
276 | new_hint->br_name = xstrdup(orig_hint->br_name); | |
277 | new_hint->br_type = xstrdup(br_type); | |
278 | new_hint->ofp_port = orig_hint->ofp_port; | |
279 | ||
280 | shash_add(&init_ofp_ports, node->name, new_hint); | |
281 | } | |
282 | ||
b0408fca | 283 | for (i = 0; i < n_ofproto_classes; i++) { |
e1b1d06a | 284 | ofproto_classes[i]->init(&init_ofp_ports); |
abe529af BP |
285 | } |
286 | } | |
f29152ca | 287 | |
abe529af BP |
288 | /* 'type' should be a normalized datapath type, as returned by |
289 | * ofproto_normalize_type(). Returns the corresponding ofproto_class | |
290 | * structure, or a null pointer if there is none registered for 'type'. */ | |
291 | static const struct ofproto_class * | |
292 | ofproto_class_find__(const char *type) | |
293 | { | |
294 | size_t i; | |
f29152ca | 295 | |
abe529af BP |
296 | for (i = 0; i < n_ofproto_classes; i++) { |
297 | const struct ofproto_class *class = ofproto_classes[i]; | |
298 | struct sset types; | |
299 | bool found; | |
064af421 | 300 | |
abe529af BP |
301 | sset_init(&types); |
302 | class->enumerate_types(&types); | |
303 | found = sset_contains(&types, type); | |
304 | sset_destroy(&types); | |
bcf84111 | 305 | |
abe529af BP |
306 | if (found) { |
307 | return class; | |
308 | } | |
309 | } | |
310 | VLOG_WARN("unknown datapath type %s", type); | |
311 | return NULL; | |
312 | } | |
064af421 | 313 | |
abe529af BP |
314 | /* Registers a new ofproto class. After successful registration, new ofprotos |
315 | * of that type can be created using ofproto_create(). */ | |
316 | int | |
317 | ofproto_class_register(const struct ofproto_class *new_class) | |
318 | { | |
319 | size_t i; | |
7aa697dd | 320 | |
abe529af BP |
321 | for (i = 0; i < n_ofproto_classes; i++) { |
322 | if (ofproto_classes[i] == new_class) { | |
323 | return EEXIST; | |
324 | } | |
325 | } | |
064af421 | 326 | |
abe529af BP |
327 | if (n_ofproto_classes >= allocated_ofproto_classes) { |
328 | ofproto_classes = x2nrealloc(ofproto_classes, | |
329 | &allocated_ofproto_classes, | |
330 | sizeof *ofproto_classes); | |
331 | } | |
332 | ofproto_classes[n_ofproto_classes++] = new_class; | |
333 | return 0; | |
334 | } | |
064af421 | 335 | |
abe529af BP |
336 | /* Unregisters a datapath provider. 'type' must have been previously |
337 | * registered and not currently be in use by any ofprotos. After | |
338 | * unregistration new datapaths of that type cannot be opened using | |
339 | * ofproto_create(). */ | |
340 | int | |
341 | ofproto_class_unregister(const struct ofproto_class *class) | |
342 | { | |
343 | size_t i; | |
76ce9432 | 344 | |
abe529af BP |
345 | for (i = 0; i < n_ofproto_classes; i++) { |
346 | if (ofproto_classes[i] == class) { | |
347 | for (i++; i < n_ofproto_classes; i++) { | |
348 | ofproto_classes[i - 1] = ofproto_classes[i]; | |
349 | } | |
350 | n_ofproto_classes--; | |
351 | return 0; | |
352 | } | |
353 | } | |
354 | VLOG_WARN("attempted to unregister an ofproto class that is not " | |
355 | "registered"); | |
356 | return EAFNOSUPPORT; | |
357 | } | |
4a4cdb3b | 358 | |
f79e673f BP |
359 | /* Clears 'types' and enumerates all registered ofproto types into it. The |
360 | * caller must first initialize the sset. */ | |
361 | void | |
362 | ofproto_enumerate_types(struct sset *types) | |
363 | { | |
abe529af | 364 | size_t i; |
064af421 | 365 | |
abe529af BP |
366 | for (i = 0; i < n_ofproto_classes; i++) { |
367 | ofproto_classes[i]->enumerate_types(types); | |
368 | } | |
f79e673f | 369 | } |
064af421 | 370 | |
f79e673f BP |
371 | /* Returns the fully spelled out name for the given ofproto 'type'. |
372 | * | |
373 | * Normalized type string can be compared with strcmp(). Unnormalized type | |
374 | * string might be the same even if they have different spellings. */ | |
375 | const char * | |
376 | ofproto_normalize_type(const char *type) | |
377 | { | |
abe529af | 378 | return type && type[0] ? type : "system"; |
f79e673f | 379 | } |
064af421 | 380 | |
f79e673f BP |
381 | /* Clears 'names' and enumerates the names of all known created ofprotos with |
382 | * the given 'type'. The caller must first initialize the sset. Returns 0 if | |
383 | * successful, otherwise a positive errno value. | |
384 | * | |
385 | * Some kinds of datapaths might not be practically enumerable. This is not | |
386 | * considered an error. */ | |
387 | int | |
388 | ofproto_enumerate_names(const char *type, struct sset *names) | |
389 | { | |
abe529af BP |
390 | const struct ofproto_class *class = ofproto_class_find__(type); |
391 | return class ? class->enumerate_names(type, names) : EAFNOSUPPORT; | |
392 | } | |
7aa697dd | 393 | |
064af421 | 394 | int |
abe529af | 395 | ofproto_create(const char *datapath_name, const char *datapath_type, |
064af421 BP |
396 | struct ofproto **ofprotop) |
397 | { | |
abe529af BP |
398 | const struct ofproto_class *class; |
399 | struct ofproto *ofproto; | |
064af421 | 400 | int error; |
c2f0373a | 401 | int i; |
064af421 BP |
402 | |
403 | *ofprotop = NULL; | |
404 | ||
7aa697dd BP |
405 | ofproto_unixctl_init(); |
406 | ||
abe529af BP |
407 | datapath_type = ofproto_normalize_type(datapath_type); |
408 | class = ofproto_class_find__(datapath_type); | |
409 | if (!class) { | |
410 | VLOG_WARN("could not create datapath %s of unknown type %s", | |
411 | datapath_name, datapath_type); | |
412 | return EAFNOSUPPORT; | |
064af421 BP |
413 | } |
414 | ||
abe529af BP |
415 | ofproto = class->alloc(); |
416 | if (!ofproto) { | |
417 | VLOG_ERR("failed to allocate datapath %s of type %s", | |
418 | datapath_name, datapath_type); | |
419 | return ENOMEM; | |
420 | } | |
421 | ||
422 | /* Initialize. */ | |
423 | memset(ofproto, 0, sizeof *ofproto); | |
424 | ofproto->ofproto_class = class; | |
425 | ofproto->name = xstrdup(datapath_name); | |
426 | ofproto->type = xstrdup(datapath_type); | |
427 | hmap_insert(&all_ofprotos, &ofproto->hmap_node, | |
428 | hash_string(ofproto->name, 0)); | |
429 | ofproto->datapath_id = 0; | |
8402c74b | 430 | ofproto->forward_bpdu = false; |
abe529af | 431 | ofproto->fallback_dpid = pick_fallback_dpid(); |
061bfea4 BP |
432 | ofproto->mfr_desc = NULL; |
433 | ofproto->hw_desc = NULL; | |
434 | ofproto->sw_desc = NULL; | |
435 | ofproto->serial_desc = NULL; | |
436 | ofproto->dp_desc = NULL; | |
7257b535 | 437 | ofproto->frag_handling = OFPC_FRAG_NORMAL; |
abe529af BP |
438 | hmap_init(&ofproto->ports); |
439 | shash_init(&ofproto->port_by_name); | |
e1b1d06a | 440 | simap_init(&ofproto->ofp_requests); |
430dbb14 | 441 | ofproto->max_ports = ofp_to_u16(OFPP_MAX); |
6c1491fb BP |
442 | ofproto->tables = NULL; |
443 | ofproto->n_tables = 0; | |
98eaac36 | 444 | hindex_init(&ofproto->cookies); |
e503cc19 | 445 | list_init(&ofproto->expirable); |
834d6caf | 446 | ovs_mutex_init_recursive(&ofproto->expirable_mutex); |
abe529af | 447 | ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name); |
7ee20df1 BP |
448 | ofproto->state = S_OPENFLOW; |
449 | list_init(&ofproto->pending); | |
dd5616b3 | 450 | ofproto->n_pending = 0; |
7ee20df1 | 451 | hmap_init(&ofproto->deletions); |
a5b8d268 BP |
452 | ofproto->n_add = ofproto->n_delete = ofproto->n_modify = 0; |
453 | ofproto->first_op = ofproto->last_op = LLONG_MIN; | |
454 | ofproto->next_op_report = LLONG_MAX; | |
455 | ofproto->op_backoff = LLONG_MIN; | |
52a90c29 BP |
456 | ofproto->vlan_bitmap = NULL; |
457 | ofproto->vlans_changed = false; | |
ada3428f | 458 | ofproto->min_mtu = INT_MAX; |
abe529af | 459 | |
0f5f95a9 | 460 | error = ofproto->ofproto_class->construct(ofproto); |
19a87e36 | 461 | if (error) { |
abe529af | 462 | VLOG_ERR("failed to open datapath %s: %s", |
10a89ef0 | 463 | datapath_name, ovs_strerror(error)); |
abe529af | 464 | ofproto_destroy__(ofproto); |
19a87e36 BP |
465 | return error; |
466 | } | |
073e2a6f | 467 | |
e1b1d06a JP |
468 | /* The "max_ports" member should have been set by ->construct(ofproto). |
469 | * Port 0 is not a valid OpenFlow port, so mark that as unavailable. */ | |
430dbb14 | 470 | ofproto->ofp_port_ids = bitmap_allocate(ofproto->max_ports); |
e1b1d06a JP |
471 | bitmap_set1(ofproto->ofp_port_ids, 0); |
472 | ||
c2f0373a | 473 | /* Check that hidden tables, if any, are at the end. */ |
cb22974d | 474 | ovs_assert(ofproto->n_tables); |
c2f0373a BP |
475 | for (i = 0; i + 1 < ofproto->n_tables; i++) { |
476 | enum oftable_flags flags = ofproto->tables[i].flags; | |
477 | enum oftable_flags next_flags = ofproto->tables[i + 1].flags; | |
478 | ||
cb22974d | 479 | ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN); |
c2f0373a | 480 | } |
19a87e36 | 481 | |
abe529af | 482 | ofproto->datapath_id = pick_datapath_id(ofproto); |
abe529af | 483 | init_ports(ofproto); |
7aa697dd | 484 | |
9cae45dc JR |
485 | /* Initialize meters table. */ |
486 | if (ofproto->ofproto_class->meter_get_features) { | |
487 | ofproto->ofproto_class->meter_get_features(ofproto, | |
488 | &ofproto->meter_features); | |
489 | } else { | |
490 | memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features); | |
491 | } | |
492 | ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1) | |
493 | * sizeof(struct meter *)); | |
494 | ||
abe529af | 495 | *ofprotop = ofproto; |
064af421 BP |
496 | return 0; |
497 | } | |
498 | ||
91858960 BP |
499 | /* Must be called (only) by an ofproto implementation in its constructor |
500 | * function. See the large comment on 'construct' in struct ofproto_class for | |
501 | * details. */ | |
0f5f95a9 BP |
502 | void |
503 | ofproto_init_tables(struct ofproto *ofproto, int n_tables) | |
504 | { | |
505 | struct oftable *table; | |
506 | ||
cb22974d BP |
507 | ovs_assert(!ofproto->n_tables); |
508 | ovs_assert(n_tables >= 1 && n_tables <= 255); | |
0f5f95a9 BP |
509 | |
510 | ofproto->n_tables = n_tables; | |
511 | ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables); | |
512 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
513 | oftable_init(table); | |
514 | } | |
515 | } | |
516 | ||
91858960 BP |
517 | /* To be optionally called (only) by an ofproto implementation in its |
518 | * constructor function. See the large comment on 'construct' in struct | |
519 | * ofproto_class for details. | |
520 | * | |
521 | * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer | |
522 | * will then ensure that actions passed into the ofproto implementation will | |
523 | * not refer to OpenFlow ports numbered 'max_ports' or higher. If this | |
524 | * function is not called, there will be no such restriction. | |
525 | * | |
526 | * Reserved ports numbered OFPP_MAX and higher are special and not subject to | |
527 | * the 'max_ports' restriction. */ | |
528 | void | |
430dbb14 | 529 | ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports) |
91858960 | 530 | { |
430dbb14 | 531 | ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX)); |
91858960 BP |
532 | ofproto->max_ports = max_ports; |
533 | } | |
534 | ||
e825ace2 BP |
535 | uint64_t |
536 | ofproto_get_datapath_id(const struct ofproto *ofproto) | |
537 | { | |
538 | return ofproto->datapath_id; | |
539 | } | |
540 | ||
064af421 BP |
541 | void |
542 | ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id) | |
543 | { | |
544 | uint64_t old_dpid = p->datapath_id; | |
fa60c019 | 545 | p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p); |
064af421 | 546 | if (p->datapath_id != old_dpid) { |
76ce9432 BP |
547 | /* Force all active connections to reconnect, since there is no way to |
548 | * notify a controller that the datapath ID has changed. */ | |
fa05809b | 549 | ofproto_reconnect_controllers(p); |
064af421 BP |
550 | } |
551 | } | |
552 | ||
76ce9432 BP |
553 | void |
554 | ofproto_set_controllers(struct ofproto *p, | |
555 | const struct ofproto_controller *controllers, | |
1d9ffc17 | 556 | size_t n_controllers, uint32_t allowed_versions) |
76ce9432 | 557 | { |
1d9ffc17 SH |
558 | connmgr_set_controllers(p->connmgr, controllers, n_controllers, |
559 | allowed_versions); | |
064af421 BP |
560 | } |
561 | ||
31681a5d JP |
562 | void |
563 | ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode) | |
564 | { | |
19a87e36 | 565 | connmgr_set_fail_mode(p->connmgr, fail_mode); |
31681a5d JP |
566 | } |
567 | ||
fa05809b BP |
568 | /* Drops the connections between 'ofproto' and all of its controllers, forcing |
569 | * them to reconnect. */ | |
570 | void | |
571 | ofproto_reconnect_controllers(struct ofproto *ofproto) | |
572 | { | |
19a87e36 | 573 | connmgr_reconnect(ofproto->connmgr); |
917e50e1 BP |
574 | } |
575 | ||
576 | /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s | |
577 | * in-band control should guarantee access, in the same way that in-band | |
578 | * control guarantees access to OpenFlow controllers. */ | |
579 | void | |
580 | ofproto_set_extra_in_band_remotes(struct ofproto *ofproto, | |
581 | const struct sockaddr_in *extras, size_t n) | |
582 | { | |
19a87e36 | 583 | connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n); |
917e50e1 BP |
584 | } |
585 | ||
b1da6250 BP |
586 | /* Sets the OpenFlow queue used by flows set up by in-band control on |
587 | * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control | |
588 | * flows will use the default queue. */ | |
589 | void | |
590 | ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id) | |
591 | { | |
19a87e36 | 592 | connmgr_set_in_band_queue(ofproto->connmgr, queue_id); |
b1da6250 BP |
593 | } |
594 | ||
084f5290 SH |
595 | /* Sets the number of flows at which eviction from the kernel flow table |
596 | * will occur. */ | |
597 | void | |
380f49c4 | 598 | ofproto_set_flow_eviction_threshold(unsigned threshold) |
084f5290 | 599 | { |
380f49c4 EJ |
600 | flow_eviction_threshold = MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN, |
601 | threshold); | |
084f5290 SH |
602 | } |
603 | ||
7155fa52 JS |
604 | /* Sets the path for handling flow misses. */ |
605 | void | |
606 | ofproto_set_flow_miss_model(unsigned model) | |
607 | { | |
608 | flow_miss_model = model; | |
609 | } | |
610 | ||
b53055f4 | 611 | /* If forward_bpdu is true, the NORMAL action will forward frames with |
8402c74b SS |
612 | * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false, |
613 | * the NORMAL action will drop these frames. */ | |
614 | void | |
615 | ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu) | |
616 | { | |
617 | bool old_val = ofproto->forward_bpdu; | |
618 | ofproto->forward_bpdu = forward_bpdu; | |
619 | if (old_val != ofproto->forward_bpdu) { | |
620 | if (ofproto->ofproto_class->forward_bpdu_changed) { | |
621 | ofproto->ofproto_class->forward_bpdu_changed(ofproto); | |
622 | } | |
b53055f4 | 623 | } |
8402c74b SS |
624 | } |
625 | ||
e764773c | 626 | /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to |
c4069512 BP |
627 | * 'idle_time', in seconds, and the maximum number of MAC table entries to |
628 | * 'max_entries'. */ | |
e764773c | 629 | void |
c4069512 BP |
630 | ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time, |
631 | size_t max_entries) | |
e764773c | 632 | { |
c4069512 BP |
633 | if (ofproto->ofproto_class->set_mac_table_config) { |
634 | ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time, | |
635 | max_entries); | |
e764773c BP |
636 | } |
637 | } | |
638 | ||
448a4b2f AW |
639 | /* Sets number of upcall handler threads. The default is |
640 | * (number of online cores - 1). */ | |
641 | void | |
642 | ofproto_set_n_handler_threads(unsigned limit) | |
643 | { | |
644 | if (limit) { | |
645 | n_handler_threads = limit; | |
646 | } else { | |
647 | n_handler_threads = MAX(1, sysconf(_SC_NPROCESSORS_ONLN) - 1); | |
648 | } | |
649 | } | |
650 | ||
8b6ff729 BP |
651 | void |
652 | ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc) | |
653 | { | |
654 | free(p->dp_desc); | |
655 | p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL; | |
656 | } | |
657 | ||
064af421 | 658 | int |
81e2083f | 659 | ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops) |
064af421 | 660 | { |
19a87e36 | 661 | return connmgr_set_snoops(ofproto->connmgr, snoops); |
064af421 BP |
662 | } |
663 | ||
664 | int | |
0193b2af JG |
665 | ofproto_set_netflow(struct ofproto *ofproto, |
666 | const struct netflow_options *nf_options) | |
064af421 | 667 | { |
abe529af BP |
668 | if (nf_options && sset_is_empty(&nf_options->collectors)) { |
669 | nf_options = NULL; | |
670 | } | |
671 | ||
672 | if (ofproto->ofproto_class->set_netflow) { | |
673 | return ofproto->ofproto_class->set_netflow(ofproto, nf_options); | |
064af421 | 674 | } else { |
abe529af | 675 | return nf_options ? EOPNOTSUPP : 0; |
064af421 BP |
676 | } |
677 | } | |
678 | ||
abe529af | 679 | int |
72b06300 BP |
680 | ofproto_set_sflow(struct ofproto *ofproto, |
681 | const struct ofproto_sflow_options *oso) | |
682 | { | |
abe529af BP |
683 | if (oso && sset_is_empty(&oso->targets)) { |
684 | oso = NULL; | |
685 | } | |
72b06300 | 686 | |
abe529af BP |
687 | if (ofproto->ofproto_class->set_sflow) { |
688 | return ofproto->ofproto_class->set_sflow(ofproto, oso); | |
72b06300 | 689 | } else { |
abe529af | 690 | return oso ? EOPNOTSUPP : 0; |
72b06300 BP |
691 | } |
692 | } | |
29089a54 RL |
693 | |
694 | int | |
695 | ofproto_set_ipfix(struct ofproto *ofproto, | |
696 | const struct ofproto_ipfix_bridge_exporter_options *bo, | |
697 | const struct ofproto_ipfix_flow_exporter_options *fo, | |
698 | size_t n_fo) | |
699 | { | |
700 | if (ofproto->ofproto_class->set_ipfix) { | |
701 | return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo); | |
702 | } else { | |
703 | return (bo || fo) ? EOPNOTSUPP : 0; | |
704 | } | |
705 | } | |
40358701 GS |
706 | |
707 | void | |
708 | ofproto_set_flow_restore_wait(bool flow_restore_wait_db) | |
709 | { | |
710 | flow_restore_wait = flow_restore_wait_db; | |
711 | } | |
712 | ||
713 | bool | |
714 | ofproto_get_flow_restore_wait(void) | |
715 | { | |
716 | return flow_restore_wait; | |
717 | } | |
718 | ||
e7934396 | 719 | \f |
21f7563c JP |
720 | /* Spanning Tree Protocol (STP) configuration. */ |
721 | ||
722 | /* Configures STP on 'ofproto' using the settings defined in 's'. If | |
723 | * 's' is NULL, disables STP. | |
724 | * | |
725 | * Returns 0 if successful, otherwise a positive errno value. */ | |
726 | int | |
727 | ofproto_set_stp(struct ofproto *ofproto, | |
728 | const struct ofproto_stp_settings *s) | |
729 | { | |
730 | return (ofproto->ofproto_class->set_stp | |
731 | ? ofproto->ofproto_class->set_stp(ofproto, s) | |
732 | : EOPNOTSUPP); | |
733 | } | |
734 | ||
735 | /* Retrieves STP status of 'ofproto' and stores it in 's'. If the | |
736 | * 'enabled' member of 's' is false, then the other members are not | |
737 | * meaningful. | |
738 | * | |
739 | * Returns 0 if successful, otherwise a positive errno value. */ | |
740 | int | |
741 | ofproto_get_stp_status(struct ofproto *ofproto, | |
742 | struct ofproto_stp_status *s) | |
743 | { | |
744 | return (ofproto->ofproto_class->get_stp_status | |
745 | ? ofproto->ofproto_class->get_stp_status(ofproto, s) | |
746 | : EOPNOTSUPP); | |
747 | } | |
748 | ||
749 | /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined | |
750 | * in 's'. The caller is responsible for assigning STP port numbers | |
751 | * (using the 'port_num' member in the range of 1 through 255, inclusive) | |
752 | * and ensuring there are no duplicates. If the 's' is NULL, then STP | |
753 | * is disabled on the port. | |
754 | * | |
755 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
756 | int | |
4e022ec0 | 757 | ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port, |
21f7563c JP |
758 | const struct ofproto_port_stp_settings *s) |
759 | { | |
760 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
761 | if (!ofport) { | |
762 | VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16, | |
763 | ofproto->name, ofp_port); | |
764 | return ENODEV; | |
765 | } | |
766 | ||
767 | return (ofproto->ofproto_class->set_stp_port | |
768 | ? ofproto->ofproto_class->set_stp_port(ofport, s) | |
769 | : EOPNOTSUPP); | |
770 | } | |
771 | ||
772 | /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in | |
773 | * 's'. If the 'enabled' member in 's' is false, then the other members | |
774 | * are not meaningful. | |
775 | * | |
776 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
777 | int | |
4e022ec0 | 778 | ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port, |
21f7563c JP |
779 | struct ofproto_port_stp_status *s) |
780 | { | |
781 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
782 | if (!ofport) { | |
b0a5c43b JP |
783 | VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent " |
784 | "port %"PRIu16, ofproto->name, ofp_port); | |
21f7563c JP |
785 | return ENODEV; |
786 | } | |
787 | ||
788 | return (ofproto->ofproto_class->get_stp_port_status | |
789 | ? ofproto->ofproto_class->get_stp_port_status(ofport, s) | |
790 | : EOPNOTSUPP); | |
791 | } | |
792 | \f | |
8b36f51e EJ |
793 | /* Queue DSCP configuration. */ |
794 | ||
795 | /* Registers meta-data associated with the 'n_qdscp' Qualities of Service | |
796 | * 'queues' attached to 'ofport'. This data is not intended to be sufficient | |
797 | * to implement QoS. Instead, it is used to implement features which require | |
798 | * knowledge of what queues exist on a port, and some basic information about | |
799 | * them. | |
800 | * | |
801 | * Returns 0 if successful, otherwise a positive errno value. */ | |
802 | int | |
4e022ec0 | 803 | ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port, |
8b36f51e EJ |
804 | const struct ofproto_port_queue *queues, |
805 | size_t n_queues) | |
806 | { | |
807 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
808 | ||
809 | if (!ofport) { | |
810 | VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16, | |
811 | ofproto->name, ofp_port); | |
812 | return ENODEV; | |
813 | } | |
814 | ||
815 | return (ofproto->ofproto_class->set_queues | |
816 | ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues) | |
817 | : EOPNOTSUPP); | |
818 | } | |
819 | \f | |
e7934396 BP |
820 | /* Connectivity Fault Management configuration. */ |
821 | ||
892815f5 | 822 | /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */ |
e7934396 | 823 | void |
4e022ec0 | 824 | ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port) |
e7934396 | 825 | { |
abe529af BP |
826 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
827 | if (ofport && ofproto->ofproto_class->set_cfm) { | |
a5610457 | 828 | ofproto->ofproto_class->set_cfm(ofport, NULL); |
e7934396 BP |
829 | } |
830 | } | |
72b06300 | 831 | |
892815f5 | 832 | /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes |
93b8df38 EJ |
833 | * basic configuration from the configuration members in 'cfm', and the remote |
834 | * maintenance point ID from remote_mpid. Ignores the statistics members of | |
835 | * 'cfm'. | |
e7934396 | 836 | * |
892815f5 | 837 | * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */ |
e7934396 | 838 | void |
4e022ec0 | 839 | ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port, |
a5610457 | 840 | const struct cfm_settings *s) |
e7934396 BP |
841 | { |
842 | struct ofport *ofport; | |
abe529af | 843 | int error; |
e7934396 | 844 | |
abe529af | 845 | ofport = ofproto_get_port(ofproto, ofp_port); |
e7934396 | 846 | if (!ofport) { |
892815f5 BP |
847 | VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16, |
848 | ofproto->name, ofp_port); | |
e7934396 BP |
849 | return; |
850 | } | |
851 | ||
93b8df38 EJ |
852 | /* XXX: For configuration simplicity, we only support one remote_mpid |
853 | * outside of the CFM module. It's not clear if this is the correct long | |
854 | * term solution or not. */ | |
abe529af | 855 | error = (ofproto->ofproto_class->set_cfm |
a5610457 | 856 | ? ofproto->ofproto_class->set_cfm(ofport, s) |
abe529af BP |
857 | : EOPNOTSUPP); |
858 | if (error) { | |
859 | VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)", | |
860 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), | |
10a89ef0 | 861 | ovs_strerror(error)); |
e7934396 | 862 | } |
e7934396 | 863 | } |
e7934396 | 864 | |
ccc09689 EJ |
865 | /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if |
866 | * 'ofproto' does not have a port 'ofp_port'. */ | |
867 | void | |
4e022ec0 | 868 | ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port, |
ccc09689 EJ |
869 | const struct smap *cfg) |
870 | { | |
871 | struct ofport *ofport; | |
872 | int error; | |
873 | ||
874 | ofport = ofproto_get_port(ofproto, ofp_port); | |
875 | if (!ofport) { | |
876 | VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16, | |
877 | ofproto->name, ofp_port); | |
e8999bdc | 878 | return; |
ccc09689 EJ |
879 | } |
880 | ||
881 | error = (ofproto->ofproto_class->set_bfd | |
882 | ? ofproto->ofproto_class->set_bfd(ofport, cfg) | |
883 | : EOPNOTSUPP); | |
884 | if (error) { | |
885 | VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)", | |
886 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), | |
10a89ef0 | 887 | ovs_strerror(error)); |
ccc09689 EJ |
888 | } |
889 | } | |
890 | ||
891 | /* Populates 'status' with key value pairs indicating the status of the BFD | |
892 | * session on 'ofp_port'. This information is intended to be populated in the | |
893 | * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in | |
894 | * 'ofproto'. */ | |
895 | int | |
4e022ec0 | 896 | ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port, |
ccc09689 EJ |
897 | struct smap *status) |
898 | { | |
899 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
900 | return (ofport && ofproto->ofproto_class->get_bfd_status | |
901 | ? ofproto->ofproto_class->get_bfd_status(ofport, status) | |
902 | : EOPNOTSUPP); | |
903 | } | |
904 | ||
fa066f01 BP |
905 | /* Checks the status of LACP negotiation for 'ofp_port' within ofproto. |
906 | * Returns 1 if LACP partner information for 'ofp_port' is up-to-date, | |
907 | * 0 if LACP partner information is not current (generally indicating a | |
908 | * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */ | |
909 | int | |
4e022ec0 | 910 | ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port) |
fa066f01 | 911 | { |
abe529af BP |
912 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
913 | return (ofport && ofproto->ofproto_class->port_is_lacp_current | |
914 | ? ofproto->ofproto_class->port_is_lacp_current(ofport) | |
fa066f01 | 915 | : -1); |
e7934396 | 916 | } |
e7934396 | 917 | \f |
fa066f01 | 918 | /* Bundles. */ |
e7934396 | 919 | |
abe529af BP |
920 | /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'. |
921 | * A bundle is the same concept as a Port in OVSDB, that is, it consists of one | |
922 | * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN | |
923 | * configuration plus, if there is more than one slave, a bonding | |
924 | * configuration. | |
925 | * | |
926 | * If 'aux' is already registered then this function updates its configuration | |
927 | * to 's'. Otherwise, this function registers a new bundle. | |
928 | * | |
929 | * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL | |
930 | * port. */ | |
931 | int | |
932 | ofproto_bundle_register(struct ofproto *ofproto, void *aux, | |
933 | const struct ofproto_bundle_settings *s) | |
fa066f01 | 934 | { |
abe529af BP |
935 | return (ofproto->ofproto_class->bundle_set |
936 | ? ofproto->ofproto_class->bundle_set(ofproto, aux, s) | |
937 | : EOPNOTSUPP); | |
fa066f01 BP |
938 | } |
939 | ||
abe529af BP |
940 | /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'. |
941 | * If no such bundle has been registered, this has no effect. */ | |
942 | int | |
943 | ofproto_bundle_unregister(struct ofproto *ofproto, void *aux) | |
e7934396 | 944 | { |
abe529af | 945 | return ofproto_bundle_register(ofproto, aux, NULL); |
e7934396 | 946 | } |
fa066f01 | 947 | |
e7934396 | 948 | \f |
abe529af BP |
949 | /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'. |
950 | * If 'aux' is already registered then this function updates its configuration | |
c06bba01 | 951 | * to 's'. Otherwise, this function registers a new mirror. */ |
abe529af BP |
952 | int |
953 | ofproto_mirror_register(struct ofproto *ofproto, void *aux, | |
954 | const struct ofproto_mirror_settings *s) | |
064af421 | 955 | { |
abe529af BP |
956 | return (ofproto->ofproto_class->mirror_set |
957 | ? ofproto->ofproto_class->mirror_set(ofproto, aux, s) | |
958 | : EOPNOTSUPP); | |
064af421 BP |
959 | } |
960 | ||
abe529af BP |
961 | /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'. |
962 | * If no mirror has been registered, this has no effect. */ | |
963 | int | |
964 | ofproto_mirror_unregister(struct ofproto *ofproto, void *aux) | |
064af421 | 965 | { |
abe529af | 966 | return ofproto_mirror_register(ofproto, aux, NULL); |
064af421 BP |
967 | } |
968 | ||
9d24de3b JP |
969 | /* Retrieves statistics from mirror associated with client data pointer |
970 | * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and | |
971 | * 'bytes', respectively. If a particular counters is not supported, | |
972 | * the appropriate argument is set to UINT64_MAX. */ | |
973 | int | |
974 | ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux, | |
975 | uint64_t *packets, uint64_t *bytes) | |
976 | { | |
977 | if (!ofproto->ofproto_class->mirror_get_stats) { | |
978 | *packets = *bytes = UINT64_MAX; | |
979 | return EOPNOTSUPP; | |
980 | } | |
981 | ||
982 | return ofproto->ofproto_class->mirror_get_stats(ofproto, aux, | |
983 | packets, bytes); | |
984 | } | |
985 | ||
abe529af BP |
986 | /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on |
987 | * which all packets are flooded, instead of using MAC learning. If | |
988 | * 'flood_vlans' is NULL, then MAC learning applies to all VLANs. | |
989 | * | |
990 | * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL | |
991 | * port. */ | |
992 | int | |
993 | ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans) | |
abdfe474 | 994 | { |
abe529af BP |
995 | return (ofproto->ofproto_class->set_flood_vlans |
996 | ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans) | |
997 | : EOPNOTSUPP); | |
abdfe474 JP |
998 | } |
999 | ||
abe529af BP |
1000 | /* Returns true if 'aux' is a registered bundle that is currently in use as the |
1001 | * output for a mirror. */ | |
1002 | bool | |
b4affc74 | 1003 | ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux) |
abe529af BP |
1004 | { |
1005 | return (ofproto->ofproto_class->is_mirror_output_bundle | |
1006 | ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux) | |
1007 | : false); | |
1008 | } | |
1009 | \f | |
254750ce BP |
1010 | /* Configuration of OpenFlow tables. */ |
1011 | ||
1012 | /* Returns the number of OpenFlow tables in 'ofproto'. */ | |
1013 | int | |
1014 | ofproto_get_n_tables(const struct ofproto *ofproto) | |
1015 | { | |
1016 | return ofproto->n_tables; | |
1017 | } | |
1018 | ||
1019 | /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the | |
1020 | * settings from 's'. 'table_id' must be in the range 0 through the number of | |
1021 | * OpenFlow tables in 'ofproto' minus 1, inclusive. | |
1022 | * | |
1023 | * For read-only tables, only the name may be configured. */ | |
1024 | void | |
1025 | ofproto_configure_table(struct ofproto *ofproto, int table_id, | |
1026 | const struct ofproto_table_settings *s) | |
1027 | { | |
1028 | struct oftable *table; | |
1029 | ||
cb22974d | 1030 | ovs_assert(table_id >= 0 && table_id < ofproto->n_tables); |
254750ce BP |
1031 | table = &ofproto->tables[table_id]; |
1032 | ||
1033 | oftable_set_name(table, s->name); | |
1034 | ||
1035 | if (table->flags & OFTABLE_READONLY) { | |
1036 | return; | |
1037 | } | |
1038 | ||
1039 | if (s->groups) { | |
1040 | oftable_enable_eviction(table, s->groups, s->n_groups); | |
1041 | } else { | |
1042 | oftable_disable_eviction(table); | |
1043 | } | |
1044 | ||
1045 | table->max_flows = s->max_flows; | |
0b4f2078 | 1046 | ovs_rwlock_rdlock(&table->cls.rwlock); |
254750ce BP |
1047 | if (classifier_count(&table->cls) > table->max_flows |
1048 | && table->eviction_fields) { | |
1049 | /* 'table' contains more flows than allowed. We might not be able to | |
1050 | * evict them right away because of the asynchronous nature of flow | |
1051 | * table changes. Schedule eviction for later. */ | |
1052 | switch (ofproto->state) { | |
1053 | case S_OPENFLOW: | |
1054 | ofproto->state = S_EVICT; | |
1055 | break; | |
1056 | case S_EVICT: | |
1057 | case S_FLUSH: | |
1058 | /* We're already deleting flows, nothing more to do. */ | |
1059 | break; | |
1060 | } | |
1061 | } | |
0b4f2078 | 1062 | ovs_rwlock_unlock(&table->cls.rwlock); |
254750ce BP |
1063 | } |
1064 | \f | |
81e2083f BP |
1065 | bool |
1066 | ofproto_has_snoops(const struct ofproto *ofproto) | |
1067 | { | |
1068 | return connmgr_has_snoops(ofproto->connmgr); | |
1069 | } | |
1070 | ||
064af421 | 1071 | void |
81e2083f | 1072 | ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops) |
064af421 | 1073 | { |
19a87e36 | 1074 | connmgr_get_snoops(ofproto->connmgr, snoops); |
064af421 BP |
1075 | } |
1076 | ||
8037acb4 BP |
1077 | /* Deletes 'rule' from 'cls' within 'ofproto'. |
1078 | * | |
1079 | * The 'cls' argument is redundant (it is &ofproto->tables[rule->table_id].cls) | |
1080 | * but it allows Clang to do better checking. */ | |
7ee20df1 | 1081 | static void |
8037acb4 BP |
1082 | ofproto_delete_rule(struct ofproto *ofproto, struct classifier *cls, |
1083 | struct rule *rule) | |
1084 | OVS_REQ_WRLOCK(cls->rwlock) | |
7ee20df1 | 1085 | { |
7ee20df1 | 1086 | struct ofopgroup *group; |
8037acb4 BP |
1087 | |
1088 | ovs_assert(!rule->pending); | |
1089 | ovs_assert(cls == &ofproto->tables[rule->table_id].cls); | |
1090 | ||
1091 | group = ofopgroup_create_unattached(ofproto); | |
1092 | ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE); | |
1093 | ovs_rwlock_wrlock(&rule->evict); | |
1094 | oftable_remove_rule__(ofproto, cls, rule); | |
1095 | ofproto->ofproto_class->rule_delete(rule); | |
1096 | ofopgroup_submit(group); | |
1097 | } | |
1098 | ||
1099 | static void | |
1100 | ofproto_flush__(struct ofproto *ofproto) | |
1101 | { | |
d0918789 | 1102 | struct oftable *table; |
7ee20df1 BP |
1103 | |
1104 | if (ofproto->ofproto_class->flush) { | |
1105 | ofproto->ofproto_class->flush(ofproto); | |
1106 | } | |
1107 | ||
b772ded8 | 1108 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
7ee20df1 BP |
1109 | struct rule *rule, *next_rule; |
1110 | struct cls_cursor cursor; | |
1111 | ||
5c67e4af BP |
1112 | if (table->flags & OFTABLE_HIDDEN) { |
1113 | continue; | |
1114 | } | |
1115 | ||
0b4f2078 | 1116 | ovs_rwlock_wrlock(&table->cls.rwlock); |
d0918789 | 1117 | cls_cursor_init(&cursor, &table->cls, NULL); |
7ee20df1 BP |
1118 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) { |
1119 | if (!rule->pending) { | |
8037acb4 | 1120 | ofproto_delete_rule(ofproto, &table->cls, rule); |
7ee20df1 BP |
1121 | } |
1122 | } | |
0b4f2078 | 1123 | ovs_rwlock_unlock(&table->cls.rwlock); |
7ee20df1 | 1124 | } |
7ee20df1 BP |
1125 | } |
1126 | ||
abe529af BP |
1127 | static void |
1128 | ofproto_destroy__(struct ofproto *ofproto) | |
1129 | { | |
d0918789 | 1130 | struct oftable *table; |
6c1491fb | 1131 | |
cb22974d BP |
1132 | ovs_assert(list_is_empty(&ofproto->pending)); |
1133 | ovs_assert(!ofproto->n_pending); | |
7ee20df1 | 1134 | |
6b3f7f02 JR |
1135 | if (ofproto->meters) { |
1136 | meter_delete(ofproto, 1, ofproto->meter_features.max_meters); | |
1137 | free(ofproto->meters); | |
1138 | } | |
1139 | ||
abe529af | 1140 | connmgr_destroy(ofproto->connmgr); |
fa066f01 | 1141 | |
abe529af BP |
1142 | hmap_remove(&all_ofprotos, &ofproto->hmap_node); |
1143 | free(ofproto->name); | |
955a7127 | 1144 | free(ofproto->type); |
abe529af BP |
1145 | free(ofproto->mfr_desc); |
1146 | free(ofproto->hw_desc); | |
1147 | free(ofproto->sw_desc); | |
1148 | free(ofproto->serial_desc); | |
1149 | free(ofproto->dp_desc); | |
abe529af BP |
1150 | hmap_destroy(&ofproto->ports); |
1151 | shash_destroy(&ofproto->port_by_name); | |
e1b1d06a JP |
1152 | bitmap_free(ofproto->ofp_port_ids); |
1153 | simap_destroy(&ofproto->ofp_requests); | |
6c1491fb | 1154 | |
b772ded8 | 1155 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
d0918789 | 1156 | oftable_destroy(table); |
6c1491fb BP |
1157 | } |
1158 | free(ofproto->tables); | |
fa066f01 | 1159 | |
7ee20df1 BP |
1160 | hmap_destroy(&ofproto->deletions); |
1161 | ||
52a90c29 BP |
1162 | free(ofproto->vlan_bitmap); |
1163 | ||
83b16b98 | 1164 | ovs_mutex_destroy(&ofproto->expirable_mutex); |
abe529af BP |
1165 | ofproto->ofproto_class->dealloc(ofproto); |
1166 | } | |
fa066f01 | 1167 | |
064af421 BP |
1168 | void |
1169 | ofproto_destroy(struct ofproto *p) | |
1170 | { | |
ca0f572c | 1171 | struct ofport *ofport, *next_ofport; |
064af421 BP |
1172 | |
1173 | if (!p) { | |
1174 | return; | |
1175 | } | |
1176 | ||
7ee20df1 | 1177 | ofproto_flush__(p); |
4e8e4213 | 1178 | HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) { |
abe529af | 1179 | ofport_destroy(ofport); |
064af421 | 1180 | } |
064af421 | 1181 | |
abe529af BP |
1182 | p->ofproto_class->destruct(p); |
1183 | ofproto_destroy__(p); | |
064af421 BP |
1184 | } |
1185 | ||
abe529af BP |
1186 | /* Destroys the datapath with the respective 'name' and 'type'. With the Linux |
1187 | * kernel datapath, for example, this destroys the datapath in the kernel, and | |
1188 | * with the netdev-based datapath, it tears down the data structures that | |
1189 | * represent the datapath. | |
1190 | * | |
1191 | * The datapath should not be currently open as an ofproto. */ | |
064af421 | 1192 | int |
abe529af | 1193 | ofproto_delete(const char *name, const char *type) |
064af421 | 1194 | { |
abe529af BP |
1195 | const struct ofproto_class *class = ofproto_class_find__(type); |
1196 | return (!class ? EAFNOSUPPORT | |
1197 | : !class->del ? EACCES | |
1198 | : class->del(type, name)); | |
064af421 BP |
1199 | } |
1200 | ||
e9e28be3 BP |
1201 | static void |
1202 | process_port_change(struct ofproto *ofproto, int error, char *devname) | |
1203 | { | |
1204 | if (error == ENOBUFS) { | |
1205 | reinit_ports(ofproto); | |
1206 | } else if (!error) { | |
1207 | update_port(ofproto, devname); | |
1208 | free(devname); | |
1209 | } | |
1210 | } | |
1211 | ||
11a574a7 JP |
1212 | int |
1213 | ofproto_type_run(const char *datapath_type) | |
1214 | { | |
1215 | const struct ofproto_class *class; | |
1216 | int error; | |
1217 | ||
1218 | datapath_type = ofproto_normalize_type(datapath_type); | |
1219 | class = ofproto_class_find__(datapath_type); | |
1220 | ||
1221 | error = class->type_run ? class->type_run(datapath_type) : 0; | |
1222 | if (error && error != EAGAIN) { | |
1223 | VLOG_ERR_RL(&rl, "%s: type_run failed (%s)", | |
10a89ef0 | 1224 | datapath_type, ovs_strerror(error)); |
11a574a7 JP |
1225 | } |
1226 | return error; | |
1227 | } | |
1228 | ||
1229 | int | |
1230 | ofproto_type_run_fast(const char *datapath_type) | |
1231 | { | |
1232 | const struct ofproto_class *class; | |
1233 | int error; | |
1234 | ||
1235 | datapath_type = ofproto_normalize_type(datapath_type); | |
1236 | class = ofproto_class_find__(datapath_type); | |
1237 | ||
1238 | error = class->type_run_fast ? class->type_run_fast(datapath_type) : 0; | |
1239 | if (error && error != EAGAIN) { | |
1240 | VLOG_ERR_RL(&rl, "%s: type_run_fast failed (%s)", | |
10a89ef0 | 1241 | datapath_type, ovs_strerror(error)); |
11a574a7 JP |
1242 | } |
1243 | return error; | |
1244 | } | |
1245 | ||
1246 | void | |
1247 | ofproto_type_wait(const char *datapath_type) | |
1248 | { | |
1249 | const struct ofproto_class *class; | |
1250 | ||
1251 | datapath_type = ofproto_normalize_type(datapath_type); | |
1252 | class = ofproto_class_find__(datapath_type); | |
1253 | ||
1254 | if (class->type_wait) { | |
1255 | class->type_wait(datapath_type); | |
1256 | } | |
1257 | } | |
1258 | ||
064af421 | 1259 | int |
abe529af | 1260 | ofproto_run(struct ofproto *p) |
064af421 | 1261 | { |
7436ed80 BP |
1262 | struct sset changed_netdevs; |
1263 | const char *changed_netdev; | |
031d8bff | 1264 | struct ofport *ofport; |
064af421 | 1265 | int error; |
064af421 | 1266 | |
abe529af | 1267 | error = p->ofproto_class->run(p); |
5fcc0d00 | 1268 | if (error && error != EAGAIN) { |
10a89ef0 | 1269 | VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error)); |
149f577a JG |
1270 | } |
1271 | ||
5bf0e941 | 1272 | if (p->ofproto_class->port_poll) { |
7436ed80 BP |
1273 | char *devname; |
1274 | ||
5bf0e941 BP |
1275 | while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) { |
1276 | process_port_change(p, error, devname); | |
064af421 | 1277 | } |
e9e28be3 | 1278 | } |
031d8bff | 1279 | |
7436ed80 BP |
1280 | /* Update OpenFlow port status for any port whose netdev has changed. |
1281 | * | |
1282 | * Refreshing a given 'ofport' can cause an arbitrary ofport to be | |
1283 | * destroyed, so it's not safe to update ports directly from the | |
1284 | * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we | |
1285 | * need this two-phase approach. */ | |
1286 | sset_init(&changed_netdevs); | |
031d8bff EJ |
1287 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { |
1288 | unsigned int change_seq = netdev_change_seq(ofport->netdev); | |
1289 | if (ofport->change_seq != change_seq) { | |
1290 | ofport->change_seq = change_seq; | |
7436ed80 | 1291 | sset_add(&changed_netdevs, netdev_get_name(ofport->netdev)); |
031d8bff | 1292 | } |
064af421 | 1293 | } |
7436ed80 BP |
1294 | SSET_FOR_EACH (changed_netdev, &changed_netdevs) { |
1295 | update_port(p, changed_netdev); | |
1296 | } | |
1297 | sset_destroy(&changed_netdevs); | |
064af421 | 1298 | |
7ee20df1 BP |
1299 | switch (p->state) { |
1300 | case S_OPENFLOW: | |
1301 | connmgr_run(p->connmgr, handle_openflow); | |
1302 | break; | |
1303 | ||
254750ce BP |
1304 | case S_EVICT: |
1305 | connmgr_run(p->connmgr, NULL); | |
1306 | ofproto_evict(p); | |
1307 | if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) { | |
1308 | p->state = S_OPENFLOW; | |
1309 | } | |
1310 | break; | |
1311 | ||
7ee20df1 BP |
1312 | case S_FLUSH: |
1313 | connmgr_run(p->connmgr, NULL); | |
1314 | ofproto_flush__(p); | |
1315 | if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) { | |
1316 | connmgr_flushed(p->connmgr); | |
1317 | p->state = S_OPENFLOW; | |
1318 | } | |
1319 | break; | |
1320 | ||
1321 | default: | |
1322 | NOT_REACHED(); | |
1323 | } | |
064af421 | 1324 | |
a5b8d268 BP |
1325 | if (time_msec() >= p->next_op_report) { |
1326 | long long int ago = (time_msec() - p->first_op) / 1000; | |
1327 | long long int interval = (p->last_op - p->first_op) / 1000; | |
1328 | struct ds s; | |
1329 | ||
1330 | ds_init(&s); | |
1331 | ds_put_format(&s, "%d flow_mods ", | |
1332 | p->n_add + p->n_delete + p->n_modify); | |
1333 | if (interval == ago) { | |
1334 | ds_put_format(&s, "in the last %lld s", ago); | |
1335 | } else if (interval) { | |
1336 | ds_put_format(&s, "in the %lld s starting %lld s ago", | |
1337 | interval, ago); | |
1338 | } else { | |
1339 | ds_put_format(&s, "%lld s ago", ago); | |
1340 | } | |
1341 | ||
1342 | ds_put_cstr(&s, " ("); | |
1343 | if (p->n_add) { | |
1344 | ds_put_format(&s, "%d adds, ", p->n_add); | |
1345 | } | |
1346 | if (p->n_delete) { | |
1347 | ds_put_format(&s, "%d deletes, ", p->n_delete); | |
1348 | } | |
1349 | if (p->n_modify) { | |
1350 | ds_put_format(&s, "%d modifications, ", p->n_modify); | |
1351 | } | |
1352 | s.length -= 2; | |
1353 | ds_put_char(&s, ')'); | |
1354 | ||
fbfa2911 | 1355 | VLOG_INFO("%s: %s", p->name, ds_cstr(&s)); |
a5b8d268 BP |
1356 | ds_destroy(&s); |
1357 | ||
1358 | p->n_add = p->n_delete = p->n_modify = 0; | |
1359 | p->next_op_report = LLONG_MAX; | |
1360 | } | |
1361 | ||
5fcc0d00 BP |
1362 | return error; |
1363 | } | |
1364 | ||
1365 | /* Performs periodic activity required by 'ofproto' that needs to be done | |
1366 | * with the least possible latency. | |
1367 | * | |
1368 | * It makes sense to call this function a couple of times per poll loop, to | |
1369 | * provide a significant performance boost on some benchmarks with the | |
1370 | * ofproto-dpif implementation. */ | |
1371 | int | |
1372 | ofproto_run_fast(struct ofproto *p) | |
1373 | { | |
1374 | int error; | |
1375 | ||
1376 | error = p->ofproto_class->run_fast ? p->ofproto_class->run_fast(p) : 0; | |
1377 | if (error && error != EAGAIN) { | |
1378 | VLOG_ERR_RL(&rl, "%s: fastpath run failed (%s)", | |
10a89ef0 | 1379 | p->name, ovs_strerror(error)); |
5fcc0d00 BP |
1380 | } |
1381 | return error; | |
064af421 BP |
1382 | } |
1383 | ||
1384 | void | |
1385 | ofproto_wait(struct ofproto *p) | |
1386 | { | |
031d8bff EJ |
1387 | struct ofport *ofport; |
1388 | ||
abe529af | 1389 | p->ofproto_class->wait(p); |
5bf0e941 BP |
1390 | if (p->ofproto_class->port_poll_wait) { |
1391 | p->ofproto_class->port_poll_wait(p); | |
e7934396 | 1392 | } |
031d8bff EJ |
1393 | |
1394 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
1395 | if (ofport->change_seq != netdev_change_seq(ofport->netdev)) { | |
1396 | poll_immediate_wake(); | |
1397 | } | |
1398 | } | |
7ee20df1 BP |
1399 | |
1400 | switch (p->state) { | |
1401 | case S_OPENFLOW: | |
1402 | connmgr_wait(p->connmgr, true); | |
1403 | break; | |
1404 | ||
254750ce | 1405 | case S_EVICT: |
7ee20df1 BP |
1406 | case S_FLUSH: |
1407 | connmgr_wait(p->connmgr, false); | |
1408 | if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) { | |
1409 | poll_immediate_wake(); | |
1410 | } | |
1411 | break; | |
1412 | } | |
064af421 BP |
1413 | } |
1414 | ||
064af421 BP |
1415 | bool |
1416 | ofproto_is_alive(const struct ofproto *p) | |
1417 | { | |
19a87e36 | 1418 | return connmgr_has_controllers(p->connmgr); |
064af421 BP |
1419 | } |
1420 | ||
0d085684 BP |
1421 | /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with |
1422 | * memory_report(). */ | |
1423 | void | |
1424 | ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage) | |
1425 | { | |
1426 | const struct oftable *table; | |
1427 | unsigned int n_rules; | |
1428 | ||
1429 | simap_increase(usage, "ports", hmap_count(&ofproto->ports)); | |
1430 | simap_increase(usage, "ops", | |
1431 | ofproto->n_pending + hmap_count(&ofproto->deletions)); | |
1432 | ||
1433 | n_rules = 0; | |
1434 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
0b4f2078 | 1435 | ovs_rwlock_rdlock(&table->cls.rwlock); |
0d085684 | 1436 | n_rules += classifier_count(&table->cls); |
0b4f2078 | 1437 | ovs_rwlock_unlock(&table->cls.rwlock); |
0d085684 BP |
1438 | } |
1439 | simap_increase(usage, "rules", n_rules); | |
1440 | ||
1441 | if (ofproto->ofproto_class->get_memory_usage) { | |
1442 | ofproto->ofproto_class->get_memory_usage(ofproto, usage); | |
1443 | } | |
1444 | ||
1445 | connmgr_get_memory_usage(ofproto->connmgr, usage); | |
1446 | } | |
1447 | ||
bffc0589 | 1448 | void |
2cdcb898 | 1449 | ofproto_get_ofproto_controller_info(const struct ofproto *ofproto, |
bffc0589 AE |
1450 | struct shash *info) |
1451 | { | |
19a87e36 | 1452 | connmgr_get_controller_info(ofproto->connmgr, info); |
bffc0589 AE |
1453 | } |
1454 | ||
1455 | void | |
1456 | ofproto_free_ofproto_controller_info(struct shash *info) | |
1457 | { | |
72ba2ed3 | 1458 | connmgr_free_controller_info(info); |
bffc0589 AE |
1459 | } |
1460 | ||
b5827b24 BP |
1461 | /* Makes a deep copy of 'old' into 'port'. */ |
1462 | void | |
1463 | ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old) | |
1464 | { | |
1465 | port->name = xstrdup(old->name); | |
1466 | port->type = xstrdup(old->type); | |
1467 | port->ofp_port = old->ofp_port; | |
1468 | } | |
1469 | ||
1470 | /* Frees memory allocated to members of 'ofproto_port'. | |
3a6ccc8c | 1471 | * |
b5827b24 BP |
1472 | * Do not call this function on an ofproto_port obtained from |
1473 | * ofproto_port_dump_next(): that function retains ownership of the data in the | |
1474 | * ofproto_port. */ | |
1475 | void | |
1476 | ofproto_port_destroy(struct ofproto_port *ofproto_port) | |
1477 | { | |
1478 | free(ofproto_port->name); | |
1479 | free(ofproto_port->type); | |
1480 | } | |
1481 | ||
b5827b24 | 1482 | /* Initializes 'dump' to begin dumping the ports in an ofproto. |
3a6ccc8c | 1483 | * |
b5827b24 BP |
1484 | * This function provides no status indication. An error status for the entire |
1485 | * dump operation is provided when it is completed by calling | |
1486 | * ofproto_port_dump_done(). | |
1487 | */ | |
1488 | void | |
1489 | ofproto_port_dump_start(struct ofproto_port_dump *dump, | |
1490 | const struct ofproto *ofproto) | |
1491 | { | |
abe529af BP |
1492 | dump->ofproto = ofproto; |
1493 | dump->error = ofproto->ofproto_class->port_dump_start(ofproto, | |
1494 | &dump->state); | |
b5827b24 BP |
1495 | } |
1496 | ||
1497 | /* Attempts to retrieve another port from 'dump', which must have been created | |
1498 | * with ofproto_port_dump_start(). On success, stores a new ofproto_port into | |
1499 | * 'port' and returns true. On failure, returns false. | |
1500 | * | |
1501 | * Failure might indicate an actual error or merely that the last port has been | |
1502 | * dumped. An error status for the entire dump operation is provided when it | |
1503 | * is completed by calling ofproto_port_dump_done(). | |
1504 | * | |
1505 | * The ofproto owns the data stored in 'port'. It will remain valid until at | |
1506 | * least the next time 'dump' is passed to ofproto_port_dump_next() or | |
1507 | * ofproto_port_dump_done(). */ | |
1508 | bool | |
1509 | ofproto_port_dump_next(struct ofproto_port_dump *dump, | |
1510 | struct ofproto_port *port) | |
1511 | { | |
abe529af BP |
1512 | const struct ofproto *ofproto = dump->ofproto; |
1513 | ||
1514 | if (dump->error) { | |
1515 | return false; | |
1516 | } | |
b5827b24 | 1517 | |
abe529af BP |
1518 | dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state, |
1519 | port); | |
1520 | if (dump->error) { | |
1521 | ofproto->ofproto_class->port_dump_done(ofproto, dump->state); | |
1522 | return false; | |
b5827b24 | 1523 | } |
abe529af | 1524 | return true; |
b5827b24 BP |
1525 | } |
1526 | ||
1527 | /* Completes port table dump operation 'dump', which must have been created | |
1528 | * with ofproto_port_dump_start(). Returns 0 if the dump operation was | |
1529 | * error-free, otherwise a positive errno value describing the problem. */ | |
3a6ccc8c | 1530 | int |
b5827b24 | 1531 | ofproto_port_dump_done(struct ofproto_port_dump *dump) |
3a6ccc8c | 1532 | { |
abe529af BP |
1533 | const struct ofproto *ofproto = dump->ofproto; |
1534 | if (!dump->error) { | |
1535 | dump->error = ofproto->ofproto_class->port_dump_done(ofproto, | |
1536 | dump->state); | |
1537 | } | |
1538 | return dump->error == EOF ? 0 : dump->error; | |
b5827b24 BP |
1539 | } |
1540 | ||
0aeaabc8 JP |
1541 | /* Returns the type to pass to netdev_open() when a datapath of type |
1542 | * 'datapath_type' has a port of type 'port_type', for a few special | |
1543 | * cases when a netdev type differs from a port type. For example, when | |
1544 | * using the userspace datapath, a port of type "internal" needs to be | |
1545 | * opened as "tap". | |
1546 | * | |
1547 | * Returns either 'type' itself or a string literal, which must not be | |
1548 | * freed. */ | |
1549 | const char * | |
1550 | ofproto_port_open_type(const char *datapath_type, const char *port_type) | |
1551 | { | |
1552 | const struct ofproto_class *class; | |
1553 | ||
1554 | datapath_type = ofproto_normalize_type(datapath_type); | |
1555 | class = ofproto_class_find__(datapath_type); | |
1556 | if (!class) { | |
1557 | return port_type; | |
1558 | } | |
1559 | ||
1560 | return (class->port_open_type | |
1561 | ? class->port_open_type(datapath_type, port_type) | |
1562 | : port_type); | |
1563 | } | |
1564 | ||
81816a5f JP |
1565 | /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is |
1566 | * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as | |
1567 | * the port's OpenFlow port number. | |
1568 | * | |
1569 | * If successful, returns 0 and sets '*ofp_portp' to the new port's | |
1570 | * OpenFlow port number (if 'ofp_portp' is non-null). On failure, | |
1571 | * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if | |
1572 | * 'ofp_portp' is non-null). */ | |
b5827b24 BP |
1573 | int |
1574 | ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev, | |
4e022ec0 | 1575 | ofp_port_t *ofp_portp) |
b5827b24 | 1576 | { |
4e022ec0 | 1577 | ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE; |
3a6ccc8c BP |
1578 | int error; |
1579 | ||
e1b1d06a | 1580 | error = ofproto->ofproto_class->port_add(ofproto, netdev); |
1fa24dea | 1581 | if (!error) { |
e1b1d06a JP |
1582 | const char *netdev_name = netdev_get_name(netdev); |
1583 | ||
4e022ec0 AW |
1584 | simap_put(&ofproto->ofp_requests, netdev_name, |
1585 | ofp_to_u16(ofp_port)); | |
e1b1d06a | 1586 | update_port(ofproto, netdev_name); |
1fa24dea | 1587 | } |
b5827b24 | 1588 | if (ofp_portp) { |
e1b1d06a JP |
1589 | struct ofproto_port ofproto_port; |
1590 | ||
1591 | ofproto_port_query_by_name(ofproto, netdev_get_name(netdev), | |
1592 | &ofproto_port); | |
1593 | *ofp_portp = error ? OFPP_NONE : ofproto_port.ofp_port; | |
1594 | ofproto_port_destroy(&ofproto_port); | |
3a6ccc8c BP |
1595 | } |
1596 | return error; | |
1597 | } | |
1598 | ||
b5827b24 BP |
1599 | /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and |
1600 | * initializes '*port' appropriately; on failure, returns a positive errno | |
1601 | * value. | |
1602 | * | |
abe529af | 1603 | * The caller owns the data in 'ofproto_port' and must free it with |
b5827b24 BP |
1604 | * ofproto_port_destroy() when it is no longer needed. */ |
1605 | int | |
1606 | ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname, | |
1607 | struct ofproto_port *port) | |
a4e2e1f2 | 1608 | { |
b5827b24 BP |
1609 | int error; |
1610 | ||
abe529af BP |
1611 | error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port); |
1612 | if (error) { | |
1613 | memset(port, 0, sizeof *port); | |
b5827b24 BP |
1614 | } |
1615 | return error; | |
a4e2e1f2 EJ |
1616 | } |
1617 | ||
b5827b24 | 1618 | /* Deletes port number 'ofp_port' from the datapath for 'ofproto'. |
3a6ccc8c | 1619 | * Returns 0 if successful, otherwise a positive errno. */ |
064af421 | 1620 | int |
4e022ec0 | 1621 | ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port) |
064af421 | 1622 | { |
abe529af | 1623 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1264ec95 | 1624 | const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>"; |
e1b1d06a | 1625 | struct simap_node *ofp_request_node; |
3cf10406 | 1626 | int error; |
cdee00fd | 1627 | |
e1b1d06a JP |
1628 | ofp_request_node = simap_find(&ofproto->ofp_requests, name); |
1629 | if (ofp_request_node) { | |
1630 | simap_delete(&ofproto->ofp_requests, ofp_request_node); | |
1631 | } | |
1632 | ||
abe529af | 1633 | error = ofproto->ofproto_class->port_del(ofproto, ofp_port); |
892815f5 | 1634 | if (!error && ofport) { |
1264ec95 BP |
1635 | /* 'name' is the netdev's name and update_port() is going to close the |
1636 | * netdev. Just in case update_port() refers to 'name' after it | |
3a6ccc8c BP |
1637 | * destroys 'ofport', make a copy of it around the update_port() |
1638 | * call. */ | |
1639 | char *devname = xstrdup(name); | |
1640 | update_port(ofproto, devname); | |
1641 | free(devname); | |
3cf10406 BP |
1642 | } |
1643 | return error; | |
064af421 BP |
1644 | } |
1645 | ||
6c1491fb | 1646 | /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and |
fa8b054f BP |
1647 | * performs the 'n_actions' actions in 'actions'. The new flow will not |
1648 | * timeout. | |
1649 | * | |
1650 | * If cls_rule->priority is in the range of priorities supported by OpenFlow | |
1651 | * (0...65535, inclusive) then the flow will be visible to OpenFlow | |
1652 | * controllers; otherwise, it will be hidden. | |
1653 | * | |
f25d0cf3 | 1654 | * The caller retains ownership of 'cls_rule' and 'ofpacts'. |
6c1491fb BP |
1655 | * |
1656 | * This is a helper function for in-band control and fail-open. */ | |
064af421 | 1657 | void |
81a76618 BP |
1658 | ofproto_add_flow(struct ofproto *ofproto, const struct match *match, |
1659 | unsigned int priority, | |
f25d0cf3 | 1660 | const struct ofpact *ofpacts, size_t ofpacts_len) |
064af421 | 1661 | { |
7ee20df1 BP |
1662 | const struct rule *rule; |
1663 | ||
0b4f2078 | 1664 | ovs_rwlock_rdlock(&ofproto->tables[0].cls.rwlock); |
81a76618 BP |
1665 | rule = rule_from_cls_rule(classifier_find_match_exactly( |
1666 | &ofproto->tables[0].cls, match, priority)); | |
0b4f2078 | 1667 | ovs_rwlock_unlock(&ofproto->tables[0].cls.rwlock); |
f25d0cf3 BP |
1668 | if (!rule || !ofpacts_equal(rule->ofpacts, rule->ofpacts_len, |
1669 | ofpacts, ofpacts_len)) { | |
a9a2da38 | 1670 | struct ofputil_flow_mod fm; |
7ee20df1 BP |
1671 | |
1672 | memset(&fm, 0, sizeof fm); | |
81a76618 BP |
1673 | fm.match = *match; |
1674 | fm.priority = priority; | |
7ee20df1 | 1675 | fm.buffer_id = UINT32_MAX; |
f25d0cf3 BP |
1676 | fm.ofpacts = xmemdup(ofpacts, ofpacts_len); |
1677 | fm.ofpacts_len = ofpacts_len; | |
7ee20df1 | 1678 | add_flow(ofproto, NULL, &fm, NULL); |
f25d0cf3 | 1679 | free(fm.ofpacts); |
7ee20df1 | 1680 | } |
064af421 BP |
1681 | } |
1682 | ||
75a75043 | 1683 | /* Executes the flow modification specified in 'fm'. Returns 0 on success, an |
90bf1e07 BP |
1684 | * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the |
1685 | * operation cannot be initiated now but may be retried later. | |
75a75043 BP |
1686 | * |
1687 | * This is a helper function for in-band control and fail-open. */ | |
1688 | int | |
e3b56933 | 1689 | ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm) |
75a75043 BP |
1690 | { |
1691 | return handle_flow_mod__(ofproto, NULL, fm, NULL); | |
1692 | } | |
1693 | ||
6c1491fb BP |
1694 | /* Searches for a rule with matching criteria exactly equal to 'target' in |
1695 | * ofproto's table 0 and, if it finds one, deletes it. | |
1696 | * | |
1697 | * This is a helper function for in-band control and fail-open. */ | |
7ee20df1 | 1698 | bool |
81a76618 BP |
1699 | ofproto_delete_flow(struct ofproto *ofproto, |
1700 | const struct match *target, unsigned int priority) | |
064af421 | 1701 | { |
8037acb4 | 1702 | struct classifier *cls = &ofproto->tables[0].cls; |
064af421 BP |
1703 | struct rule *rule; |
1704 | ||
8037acb4 BP |
1705 | ovs_rwlock_rdlock(&cls->rwlock); |
1706 | rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target, | |
1707 | priority)); | |
1708 | ovs_rwlock_unlock(&cls->rwlock); | |
7ee20df1 BP |
1709 | if (!rule) { |
1710 | /* No such rule -> success. */ | |
1711 | return true; | |
1712 | } else if (rule->pending) { | |
1713 | /* An operation on the rule is already pending -> failure. | |
1714 | * Caller must retry later if it's important. */ | |
1715 | return false; | |
1716 | } else { | |
1717 | /* Initiate deletion -> success. */ | |
8037acb4 BP |
1718 | ovs_rwlock_wrlock(&cls->rwlock); |
1719 | ofproto_delete_rule(ofproto, cls, rule); | |
1720 | ovs_rwlock_unlock(&cls->rwlock); | |
1721 | ||
7ee20df1 | 1722 | return true; |
bcf84111 | 1723 | } |
5ecc9d81 | 1724 | |
142e1f5c BP |
1725 | } |
1726 | ||
7ee20df1 BP |
1727 | /* Starts the process of deleting all of the flows from all of ofproto's flow |
1728 | * tables and then reintroducing the flows required by in-band control and | |
1729 | * fail-open. The process will complete in a later call to ofproto_run(). */ | |
142e1f5c BP |
1730 | void |
1731 | ofproto_flush_flows(struct ofproto *ofproto) | |
1732 | { | |
7ee20df1 BP |
1733 | COVERAGE_INC(ofproto_flush); |
1734 | ofproto->state = S_FLUSH; | |
064af421 BP |
1735 | } |
1736 | \f | |
1737 | static void | |
1738 | reinit_ports(struct ofproto *p) | |
1739 | { | |
abe529af | 1740 | struct ofproto_port_dump dump; |
b3c01ed3 | 1741 | struct sset devnames; |
064af421 | 1742 | struct ofport *ofport; |
abe529af | 1743 | struct ofproto_port ofproto_port; |
b3c01ed3 | 1744 | const char *devname; |
064af421 | 1745 | |
898bf89d JP |
1746 | COVERAGE_INC(ofproto_reinit_ports); |
1747 | ||
b3c01ed3 | 1748 | sset_init(&devnames); |
4e8e4213 | 1749 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { |
1264ec95 | 1750 | sset_add(&devnames, netdev_get_name(ofport->netdev)); |
064af421 | 1751 | } |
abe529af BP |
1752 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { |
1753 | sset_add(&devnames, ofproto_port.name); | |
064af421 | 1754 | } |
064af421 | 1755 | |
b3c01ed3 BP |
1756 | SSET_FOR_EACH (devname, &devnames) { |
1757 | update_port(p, devname); | |
064af421 | 1758 | } |
b3c01ed3 | 1759 | sset_destroy(&devnames); |
064af421 BP |
1760 | } |
1761 | ||
4e022ec0 | 1762 | static ofp_port_t |
e1b1d06a JP |
1763 | alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name) |
1764 | { | |
4e022ec0 | 1765 | uint16_t port_idx; |
e1b1d06a | 1766 | |
4e022ec0 | 1767 | port_idx = simap_get(&ofproto->ofp_requests, netdev_name); |
430dbb14 | 1768 | port_idx = port_idx ? port_idx : UINT16_MAX; |
4e022ec0 | 1769 | |
430dbb14 | 1770 | if (port_idx >= ofproto->max_ports |
4e022ec0 | 1771 | || bitmap_is_set(ofproto->ofp_port_ids, port_idx)) { |
430dbb14 | 1772 | uint16_t end_port_no = ofproto->alloc_port_no; |
e1b1d06a | 1773 | |
e1b1d06a JP |
1774 | /* Search for a free OpenFlow port number. We try not to |
1775 | * immediately reuse them to prevent problems due to old | |
1776 | * flows. */ | |
6033d9d9 | 1777 | for (;;) { |
430dbb14 AW |
1778 | if (++ofproto->alloc_port_no >= ofproto->max_ports) { |
1779 | ofproto->alloc_port_no = 0; | |
6033d9d9 | 1780 | } |
430dbb14 AW |
1781 | if (!bitmap_is_set(ofproto->ofp_port_ids, |
1782 | ofproto->alloc_port_no)) { | |
1783 | port_idx = ofproto->alloc_port_no; | |
6033d9d9 | 1784 | break; |
e1b1d06a | 1785 | } |
430dbb14 | 1786 | if (ofproto->alloc_port_no == end_port_no) { |
6033d9d9 | 1787 | return OFPP_NONE; |
e1b1d06a JP |
1788 | } |
1789 | } | |
1790 | } | |
4e022ec0 AW |
1791 | bitmap_set1(ofproto->ofp_port_ids, port_idx); |
1792 | return u16_to_ofp(port_idx); | |
e1b1d06a JP |
1793 | } |
1794 | ||
1795 | static void | |
4e022ec0 | 1796 | dealloc_ofp_port(const struct ofproto *ofproto, ofp_port_t ofp_port) |
e1b1d06a | 1797 | { |
430dbb14 | 1798 | if (ofp_to_u16(ofp_port) < ofproto->max_ports) { |
4e022ec0 | 1799 | bitmap_set0(ofproto->ofp_port_ids, ofp_to_u16(ofp_port)); |
40fa9417 | 1800 | } |
e1b1d06a JP |
1801 | } |
1802 | ||
fbfa2911 BP |
1803 | /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null |
1804 | * pointer if the netdev cannot be opened. On success, also fills in | |
1805 | * 'opp'. */ | |
b33951b8 | 1806 | static struct netdev * |
e1b1d06a JP |
1807 | ofport_open(struct ofproto *ofproto, |
1808 | struct ofproto_port *ofproto_port, | |
9e1fd49b | 1809 | struct ofputil_phy_port *pp) |
064af421 BP |
1810 | { |
1811 | enum netdev_flags flags; | |
064af421 | 1812 | struct netdev *netdev; |
064af421 BP |
1813 | int error; |
1814 | ||
18812dff | 1815 | error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev); |
064af421 | 1816 | if (error) { |
fbfa2911 | 1817 | VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s " |
064af421 | 1818 | "cannot be opened (%s)", |
fbfa2911 | 1819 | ofproto->name, |
abe529af | 1820 | ofproto_port->name, ofproto_port->ofp_port, |
10a89ef0 | 1821 | ofproto_port->name, ovs_strerror(error)); |
064af421 BP |
1822 | return NULL; |
1823 | } | |
1824 | ||
e1b1d06a JP |
1825 | if (ofproto_port->ofp_port == OFPP_NONE) { |
1826 | if (!strcmp(ofproto->name, ofproto_port->name)) { | |
1827 | ofproto_port->ofp_port = OFPP_LOCAL; | |
1828 | } else { | |
1829 | ofproto_port->ofp_port = alloc_ofp_port(ofproto, | |
1830 | ofproto_port->name); | |
1831 | } | |
1832 | } | |
9e1fd49b BP |
1833 | pp->port_no = ofproto_port->ofp_port; |
1834 | netdev_get_etheraddr(netdev, pp->hw_addr); | |
1835 | ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name); | |
064af421 | 1836 | netdev_get_flags(netdev, &flags); |
9e1fd49b BP |
1837 | pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN; |
1838 | pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN; | |
1839 | netdev_get_features(netdev, &pp->curr, &pp->advertised, | |
1840 | &pp->supported, &pp->peer); | |
d02a5f8e BP |
1841 | pp->curr_speed = netdev_features_to_bps(pp->curr, 0); |
1842 | pp->max_speed = netdev_features_to_bps(pp->supported, 0); | |
118c4676 | 1843 | |
b33951b8 | 1844 | return netdev; |
064af421 BP |
1845 | } |
1846 | ||
b33951b8 | 1847 | /* Returns true if most fields of 'a' and 'b' are equal. Differences in name, |
9e1fd49b | 1848 | * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are |
b33951b8 BP |
1849 | * disregarded. */ |
1850 | static bool | |
9e1fd49b BP |
1851 | ofport_equal(const struct ofputil_phy_port *a, |
1852 | const struct ofputil_phy_port *b) | |
064af421 | 1853 | { |
9e1fd49b | 1854 | return (eth_addr_equals(a->hw_addr, b->hw_addr) |
064af421 | 1855 | && a->state == b->state |
9e1fd49b | 1856 | && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN) |
064af421 BP |
1857 | && a->curr == b->curr |
1858 | && a->advertised == b->advertised | |
1859 | && a->supported == b->supported | |
9e1fd49b BP |
1860 | && a->peer == b->peer |
1861 | && a->curr_speed == b->curr_speed | |
1862 | && a->max_speed == b->max_speed); | |
064af421 BP |
1863 | } |
1864 | ||
b33951b8 BP |
1865 | /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'. |
1866 | * The caller must ensure that 'p' does not have a conflicting ofport (that is, | |
1867 | * one with the same name or port number). */ | |
064af421 | 1868 | static void |
b33951b8 | 1869 | ofport_install(struct ofproto *p, |
9e1fd49b | 1870 | struct netdev *netdev, const struct ofputil_phy_port *pp) |
064af421 | 1871 | { |
b33951b8 BP |
1872 | const char *netdev_name = netdev_get_name(netdev); |
1873 | struct ofport *ofport; | |
abe529af | 1874 | int error; |
72b06300 | 1875 | |
b33951b8 | 1876 | /* Create ofport. */ |
abe529af BP |
1877 | ofport = p->ofproto_class->port_alloc(); |
1878 | if (!ofport) { | |
1879 | error = ENOMEM; | |
1880 | goto error; | |
1881 | } | |
0f7d71a5 | 1882 | ofport->ofproto = p; |
b33951b8 | 1883 | ofport->netdev = netdev; |
031d8bff | 1884 | ofport->change_seq = netdev_change_seq(netdev); |
9e1fd49b BP |
1885 | ofport->pp = *pp; |
1886 | ofport->ofp_port = pp->port_no; | |
65e0be10 | 1887 | ofport->created = time_msec(); |
b33951b8 BP |
1888 | |
1889 | /* Add port to 'p'. */ | |
4e022ec0 | 1890 | hmap_insert(&p->ports, &ofport->hmap_node, |
f9c0c3ec | 1891 | hash_ofp_port(ofport->ofp_port)); |
72b06300 | 1892 | shash_add(&p->port_by_name, netdev_name, ofport); |
abe529af | 1893 | |
ada3428f | 1894 | update_mtu(p, ofport); |
9197df76 | 1895 | |
abe529af BP |
1896 | /* Let the ofproto_class initialize its private data. */ |
1897 | error = p->ofproto_class->port_construct(ofport); | |
1898 | if (error) { | |
1899 | goto error; | |
1900 | } | |
9e1fd49b | 1901 | connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD); |
abe529af BP |
1902 | return; |
1903 | ||
1904 | error: | |
1905 | VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)", | |
10a89ef0 | 1906 | p->name, netdev_name, ovs_strerror(error)); |
abe529af BP |
1907 | if (ofport) { |
1908 | ofport_destroy__(ofport); | |
1909 | } else { | |
1910 | netdev_close(netdev); | |
72b06300 | 1911 | } |
064af421 BP |
1912 | } |
1913 | ||
b33951b8 | 1914 | /* Removes 'ofport' from 'p' and destroys it. */ |
064af421 | 1915 | static void |
0f7d71a5 | 1916 | ofport_remove(struct ofport *ofport) |
064af421 | 1917 | { |
9e1fd49b | 1918 | connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp, |
fa066f01 | 1919 | OFPPR_DELETE); |
abe529af | 1920 | ofport_destroy(ofport); |
b33951b8 BP |
1921 | } |
1922 | ||
1923 | /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and | |
1924 | * destroys it. */ | |
1925 | static void | |
1926 | ofport_remove_with_name(struct ofproto *ofproto, const char *name) | |
1927 | { | |
1928 | struct ofport *port = shash_find_data(&ofproto->port_by_name, name); | |
1929 | if (port) { | |
0f7d71a5 | 1930 | ofport_remove(port); |
b33951b8 BP |
1931 | } |
1932 | } | |
1933 | ||
9e1fd49b | 1934 | /* Updates 'port' with new 'pp' description. |
b33951b8 BP |
1935 | * |
1936 | * Does not handle a name or port number change. The caller must implement | |
1937 | * such a change as a delete followed by an add. */ | |
1938 | static void | |
9e1fd49b | 1939 | ofport_modified(struct ofport *port, struct ofputil_phy_port *pp) |
b33951b8 | 1940 | { |
9e1fd49b BP |
1941 | memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN); |
1942 | port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN) | |
1943 | | (pp->config & OFPUTIL_PC_PORT_DOWN)); | |
1944 | port->pp.state = pp->state; | |
1945 | port->pp.curr = pp->curr; | |
1946 | port->pp.advertised = pp->advertised; | |
1947 | port->pp.supported = pp->supported; | |
1948 | port->pp.peer = pp->peer; | |
1949 | port->pp.curr_speed = pp->curr_speed; | |
1950 | port->pp.max_speed = pp->max_speed; | |
b33951b8 | 1951 | |
9e1fd49b | 1952 | connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY); |
064af421 BP |
1953 | } |
1954 | ||
5a2dfd47 JP |
1955 | /* Update OpenFlow 'state' in 'port' and notify controller. */ |
1956 | void | |
9e1fd49b | 1957 | ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state) |
5a2dfd47 | 1958 | { |
9e1fd49b BP |
1959 | if (port->pp.state != state) { |
1960 | port->pp.state = state; | |
1961 | connmgr_send_port_status(port->ofproto->connmgr, &port->pp, | |
5a2dfd47 JP |
1962 | OFPPR_MODIFY); |
1963 | } | |
1964 | } | |
1965 | ||
abe529af | 1966 | void |
4e022ec0 | 1967 | ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port) |
e7934396 | 1968 | { |
abe529af BP |
1969 | struct ofport *port = ofproto_get_port(ofproto, ofp_port); |
1970 | if (port) { | |
52a90c29 BP |
1971 | if (port->ofproto->ofproto_class->set_realdev) { |
1972 | port->ofproto->ofproto_class->set_realdev(port, 0, 0); | |
1973 | } | |
b308140a JP |
1974 | if (port->ofproto->ofproto_class->set_stp_port) { |
1975 | port->ofproto->ofproto_class->set_stp_port(port, NULL); | |
1976 | } | |
abe529af | 1977 | if (port->ofproto->ofproto_class->set_cfm) { |
a5610457 | 1978 | port->ofproto->ofproto_class->set_cfm(port, NULL); |
abe529af BP |
1979 | } |
1980 | if (port->ofproto->ofproto_class->bundle_remove) { | |
1981 | port->ofproto->ofproto_class->bundle_remove(port); | |
e7934396 BP |
1982 | } |
1983 | } | |
1984 | } | |
1985 | ||
1986 | static void | |
abe529af | 1987 | ofport_destroy__(struct ofport *port) |
e7934396 | 1988 | { |
abe529af BP |
1989 | struct ofproto *ofproto = port->ofproto; |
1990 | const char *name = netdev_get_name(port->netdev); | |
fa066f01 | 1991 | |
abe529af BP |
1992 | hmap_remove(&ofproto->ports, &port->hmap_node); |
1993 | shash_delete(&ofproto->port_by_name, | |
1994 | shash_find(&ofproto->port_by_name, name)); | |
fa066f01 | 1995 | |
abe529af BP |
1996 | netdev_close(port->netdev); |
1997 | ofproto->ofproto_class->port_dealloc(port); | |
e7934396 BP |
1998 | } |
1999 | ||
064af421 | 2000 | static void |
abe529af | 2001 | ofport_destroy(struct ofport *port) |
064af421 | 2002 | { |
fa066f01 | 2003 | if (port) { |
e1b1d06a | 2004 | dealloc_ofp_port(port->ofproto, port->ofp_port); |
abe529af BP |
2005 | port->ofproto->ofproto_class->port_destruct(port); |
2006 | ofport_destroy__(port); | |
2007 | } | |
064af421 BP |
2008 | } |
2009 | ||
abe529af | 2010 | struct ofport * |
4e022ec0 | 2011 | ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port) |
ca0f572c BP |
2012 | { |
2013 | struct ofport *port; | |
2014 | ||
f9c0c3ec | 2015 | HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port), |
4e022ec0 | 2016 | &ofproto->ports) { |
abe529af | 2017 | if (port->ofp_port == ofp_port) { |
ca0f572c BP |
2018 | return port; |
2019 | } | |
2020 | } | |
2021 | return NULL; | |
2022 | } | |
2023 | ||
6527c598 PS |
2024 | int |
2025 | ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats) | |
2026 | { | |
2027 | struct ofproto *ofproto = port->ofproto; | |
2028 | int error; | |
2029 | ||
2030 | if (ofproto->ofproto_class->port_get_stats) { | |
2031 | error = ofproto->ofproto_class->port_get_stats(port, stats); | |
2032 | } else { | |
2033 | error = EOPNOTSUPP; | |
2034 | } | |
2035 | ||
2036 | return error; | |
2037 | } | |
2038 | ||
064af421 | 2039 | static void |
b33951b8 | 2040 | update_port(struct ofproto *ofproto, const char *name) |
064af421 | 2041 | { |
abe529af | 2042 | struct ofproto_port ofproto_port; |
9e1fd49b | 2043 | struct ofputil_phy_port pp; |
b33951b8 BP |
2044 | struct netdev *netdev; |
2045 | struct ofport *port; | |
064af421 BP |
2046 | |
2047 | COVERAGE_INC(ofproto_update_port); | |
c874dc6d | 2048 | |
b33951b8 | 2049 | /* Fetch 'name''s location and properties from the datapath. */ |
abe529af | 2050 | netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port) |
fbfa2911 | 2051 | ? ofport_open(ofproto, &ofproto_port, &pp) |
b33951b8 | 2052 | : NULL); |
4e022ec0 | 2053 | |
b33951b8 | 2054 | if (netdev) { |
abe529af | 2055 | port = ofproto_get_port(ofproto, ofproto_port.ofp_port); |
b33951b8 | 2056 | if (port && !strcmp(netdev_get_name(port->netdev), name)) { |
e65942a0 BP |
2057 | struct netdev *old_netdev = port->netdev; |
2058 | ||
b33951b8 | 2059 | /* 'name' hasn't changed location. Any properties changed? */ |
9e1fd49b BP |
2060 | if (!ofport_equal(&port->pp, &pp)) { |
2061 | ofport_modified(port, &pp); | |
abe529af BP |
2062 | } |
2063 | ||
ada3428f | 2064 | update_mtu(ofproto, port); |
9197df76 | 2065 | |
e65942a0 BP |
2066 | /* Install the newly opened netdev in case it has changed. |
2067 | * Don't close the old netdev yet in case port_modified has to | |
2068 | * remove a retained reference to it.*/ | |
abe529af | 2069 | port->netdev = netdev; |
031d8bff | 2070 | port->change_seq = netdev_change_seq(netdev); |
abe529af BP |
2071 | |
2072 | if (port->ofproto->ofproto_class->port_modified) { | |
2073 | port->ofproto->ofproto_class->port_modified(port); | |
b33951b8 | 2074 | } |
e65942a0 BP |
2075 | |
2076 | netdev_close(old_netdev); | |
b33951b8 BP |
2077 | } else { |
2078 | /* If 'port' is nonnull then its name differs from 'name' and thus | |
2079 | * we should delete it. If we think there's a port named 'name' | |
2080 | * then its port number must be wrong now so delete it too. */ | |
2081 | if (port) { | |
0f7d71a5 | 2082 | ofport_remove(port); |
b33951b8 BP |
2083 | } |
2084 | ofport_remove_with_name(ofproto, name); | |
9e1fd49b | 2085 | ofport_install(ofproto, netdev, &pp); |
c874dc6d | 2086 | } |
b33951b8 BP |
2087 | } else { |
2088 | /* Any port named 'name' is gone now. */ | |
2089 | ofport_remove_with_name(ofproto, name); | |
c874dc6d | 2090 | } |
abe529af | 2091 | ofproto_port_destroy(&ofproto_port); |
064af421 BP |
2092 | } |
2093 | ||
2094 | static int | |
2095 | init_ports(struct ofproto *p) | |
2096 | { | |
abe529af BP |
2097 | struct ofproto_port_dump dump; |
2098 | struct ofproto_port ofproto_port; | |
e1b1d06a | 2099 | struct shash_node *node, *next; |
abe529af BP |
2100 | |
2101 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { | |
e1b1d06a JP |
2102 | const char *name = ofproto_port.name; |
2103 | ||
2104 | if (shash_find(&p->port_by_name, name)) { | |
fbfa2911 | 2105 | VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath", |
e1b1d06a | 2106 | p->name, name); |
abe529af | 2107 | } else { |
9e1fd49b | 2108 | struct ofputil_phy_port pp; |
b33951b8 BP |
2109 | struct netdev *netdev; |
2110 | ||
e1b1d06a JP |
2111 | /* Check if an OpenFlow port number had been requested. */ |
2112 | node = shash_find(&init_ofp_ports, name); | |
2113 | if (node) { | |
2114 | const struct iface_hint *iface_hint = node->data; | |
4e022ec0 AW |
2115 | simap_put(&p->ofp_requests, name, |
2116 | ofp_to_u16(iface_hint->ofp_port)); | |
e1b1d06a JP |
2117 | } |
2118 | ||
fbfa2911 | 2119 | netdev = ofport_open(p, &ofproto_port, &pp); |
b33951b8 | 2120 | if (netdev) { |
9e1fd49b | 2121 | ofport_install(p, netdev, &pp); |
430dbb14 | 2122 | if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) { |
7c35397c | 2123 | p->alloc_port_no = MAX(p->alloc_port_no, |
430dbb14 | 2124 | ofp_to_u16(ofproto_port.ofp_port)); |
7c35397c | 2125 | } |
064af421 BP |
2126 | } |
2127 | } | |
2128 | } | |
b0ec0f27 | 2129 | |
e1b1d06a | 2130 | SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) { |
4f9e08a5 | 2131 | struct iface_hint *iface_hint = node->data; |
e1b1d06a JP |
2132 | |
2133 | if (!strcmp(iface_hint->br_name, p->name)) { | |
2134 | free(iface_hint->br_name); | |
2135 | free(iface_hint->br_type); | |
4f9e08a5 | 2136 | free(iface_hint); |
e1b1d06a JP |
2137 | shash_delete(&init_ofp_ports, node); |
2138 | } | |
2139 | } | |
2140 | ||
064af421 BP |
2141 | return 0; |
2142 | } | |
9197df76 JP |
2143 | |
2144 | /* Find the minimum MTU of all non-datapath devices attached to 'p'. | |
2145 | * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */ | |
2146 | static int | |
2147 | find_min_mtu(struct ofproto *p) | |
2148 | { | |
2149 | struct ofport *ofport; | |
2150 | int mtu = 0; | |
2151 | ||
2152 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
2153 | struct netdev *netdev = ofport->netdev; | |
2154 | int dev_mtu; | |
2155 | ||
2156 | /* Skip any internal ports, since that's what we're trying to | |
2157 | * set. */ | |
2158 | if (!strcmp(netdev_get_type(netdev), "internal")) { | |
2159 | continue; | |
2160 | } | |
2161 | ||
2162 | if (netdev_get_mtu(netdev, &dev_mtu)) { | |
2163 | continue; | |
2164 | } | |
2165 | if (!mtu || dev_mtu < mtu) { | |
2166 | mtu = dev_mtu; | |
2167 | } | |
2168 | } | |
2169 | ||
2170 | return mtu ? mtu: ETH_PAYLOAD_MAX; | |
2171 | } | |
2172 | ||
ada3428f PS |
2173 | /* Update MTU of all datapath devices on 'p' to the minimum of the |
2174 | * non-datapath ports in event of 'port' added or changed. */ | |
9197df76 | 2175 | static void |
ada3428f | 2176 | update_mtu(struct ofproto *p, struct ofport *port) |
9197df76 JP |
2177 | { |
2178 | struct ofport *ofport; | |
ada3428f PS |
2179 | struct netdev *netdev = port->netdev; |
2180 | int dev_mtu, old_min; | |
2181 | ||
2182 | if (netdev_get_mtu(netdev, &dev_mtu)) { | |
2183 | port->mtu = 0; | |
2184 | return; | |
2185 | } | |
2186 | if (!strcmp(netdev_get_type(port->netdev), "internal")) { | |
2187 | if (dev_mtu > p->min_mtu) { | |
2188 | if (!netdev_set_mtu(port->netdev, p->min_mtu)) { | |
2189 | dev_mtu = p->min_mtu; | |
2190 | } | |
2191 | } | |
2192 | port->mtu = dev_mtu; | |
2193 | return; | |
2194 | } | |
2195 | ||
2196 | /* For non-internal port find new min mtu. */ | |
2197 | old_min = p->min_mtu; | |
2198 | port->mtu = dev_mtu; | |
2199 | p->min_mtu = find_min_mtu(p); | |
2200 | if (p->min_mtu == old_min) { | |
2201 | return; | |
2202 | } | |
9197df76 JP |
2203 | |
2204 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
2205 | struct netdev *netdev = ofport->netdev; | |
2206 | ||
2207 | if (!strcmp(netdev_get_type(netdev), "internal")) { | |
ada3428f PS |
2208 | if (!netdev_set_mtu(netdev, p->min_mtu)) { |
2209 | ofport->mtu = p->min_mtu; | |
2210 | } | |
9197df76 JP |
2211 | } |
2212 | } | |
2213 | } | |
064af421 | 2214 | \f |
064af421 | 2215 | static void |
abe529af | 2216 | ofproto_rule_destroy__(struct rule *rule) |
064af421 | 2217 | { |
1eae3d33 | 2218 | if (rule) { |
8037acb4 | 2219 | rule->ofproto->ofproto_class->rule_destruct(rule); |
48d28ac1 | 2220 | cls_rule_destroy(&rule->cr); |
f25d0cf3 | 2221 | free(rule->ofpacts); |
a3779dbc | 2222 | ovs_mutex_destroy(&rule->timeout_mutex); |
ad3efdcb | 2223 | ovs_rwlock_destroy(&rule->evict); |
1eae3d33 BP |
2224 | rule->ofproto->ofproto_class->rule_dealloc(rule); |
2225 | } | |
064af421 BP |
2226 | } |
2227 | ||
7ee20df1 | 2228 | /* This function allows an ofproto implementation to destroy any rules that |
8037acb4 BP |
2229 | * remain when its ->destruct() function is called.. This function implements |
2230 | * steps 4.4 and 4.5 in the section titled "Rule Life Cycle" in | |
2231 | * ofproto-provider.h. | |
7ee20df1 BP |
2232 | * |
2233 | * This function should only be called from an ofproto implementation's | |
2234 | * ->destruct() function. It is not suitable elsewhere. */ | |
abe529af | 2235 | void |
8037acb4 BP |
2236 | ofproto_rule_delete(struct ofproto *ofproto, struct classifier *cls, |
2237 | struct rule *rule) | |
2238 | OVS_REQ_WRLOCK(cls->rwlock) | |
064af421 | 2239 | { |
8037acb4 | 2240 | ofproto_delete_rule(ofproto, cls, rule); |
064af421 BP |
2241 | } |
2242 | ||
bcf84111 | 2243 | /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action |
f25d0cf3 | 2244 | * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */ |
2b07c8b1 | 2245 | bool |
4e022ec0 | 2246 | ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port) |
064af421 | 2247 | { |
7f05e7ab | 2248 | return (port == OFPP_ANY |
f25d0cf3 | 2249 | || ofpacts_output_to_port(rule->ofpacts, rule->ofpacts_len, port)); |
064af421 BP |
2250 | } |
2251 | ||
2b07c8b1 BP |
2252 | /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or |
2253 | * OFPAT_ENQUEUE action that outputs to 'out_port'. */ | |
2254 | bool | |
4e022ec0 | 2255 | ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port) |
2b07c8b1 BP |
2256 | { |
2257 | if (ofproto_rule_has_out_port(op->rule, out_port)) { | |
2258 | return true; | |
2259 | } | |
2260 | ||
2261 | switch (op->type) { | |
2262 | case OFOPERATION_ADD: | |
2b07c8b1 BP |
2263 | case OFOPERATION_DELETE: |
2264 | return false; | |
2265 | ||
2266 | case OFOPERATION_MODIFY: | |
b277c7cc | 2267 | case OFOPERATION_REPLACE: |
2b07c8b1 BP |
2268 | return ofpacts_output_to_port(op->ofpacts, op->ofpacts_len, out_port); |
2269 | } | |
2270 | ||
2271 | NOT_REACHED(); | |
2272 | } | |
2273 | ||
bcf84111 | 2274 | /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s |
bcf84111 | 2275 | * statistics appropriately. 'packet' must have at least sizeof(struct |
31a9e63f | 2276 | * ofp10_packet_in) bytes of headroom. |
064af421 | 2277 | * |
bcf84111 BP |
2278 | * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited |
2279 | * with statistics for 'packet' either way. | |
2280 | * | |
2281 | * Takes ownership of 'packet'. */ | |
5bf0e941 | 2282 | static int |
4e022ec0 | 2283 | rule_execute(struct rule *rule, ofp_port_t in_port, struct ofpbuf *packet) |
eedc0097 | 2284 | { |
bcf84111 | 2285 | struct flow flow; |
4e022ec0 | 2286 | union flow_in_port in_port_; |
eedc0097 | 2287 | |
cb22974d | 2288 | ovs_assert(ofpbuf_headroom(packet) >= sizeof(struct ofp10_packet_in)); |
eedc0097 | 2289 | |
4e022ec0 AW |
2290 | in_port_.ofp_port = in_port; |
2291 | flow_extract(packet, 0, 0, NULL, &in_port_, &flow); | |
5bf0e941 | 2292 | return rule->ofproto->ofproto_class->rule_execute(rule, &flow, packet); |
350a665f BP |
2293 | } |
2294 | ||
abe529af BP |
2295 | /* Returns true if 'rule' should be hidden from the controller. |
2296 | * | |
2297 | * Rules with priority higher than UINT16_MAX are set up by ofproto itself | |
2298 | * (e.g. by in-band control) and are intentionally hidden from the | |
2299 | * controller. */ | |
2b07c8b1 BP |
2300 | bool |
2301 | ofproto_rule_is_hidden(const struct rule *rule) | |
b6c9e612 | 2302 | { |
abe529af | 2303 | return rule->cr.priority > UINT16_MAX; |
7b064a79 | 2304 | } |
5c67e4af BP |
2305 | |
2306 | static enum oftable_flags | |
2307 | rule_get_flags(const struct rule *rule) | |
2308 | { | |
2309 | return rule->ofproto->tables[rule->table_id].flags; | |
2310 | } | |
2311 | ||
2312 | static bool | |
2313 | rule_is_modifiable(const struct rule *rule) | |
2314 | { | |
2315 | return !(rule_get_flags(rule) & OFTABLE_READONLY); | |
2316 | } | |
fa066f01 | 2317 | \f |
90bf1e07 | 2318 | static enum ofperr |
d1e2cf21 | 2319 | handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2320 | { |
b0421aa2 | 2321 | ofconn_send_reply(ofconn, make_echo_reply(oh)); |
064af421 | 2322 | return 0; |
064af421 BP |
2323 | } |
2324 | ||
90bf1e07 | 2325 | static enum ofperr |
d1e2cf21 | 2326 | handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2327 | { |
64ff1d96 | 2328 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
9e1fd49b | 2329 | struct ofputil_switch_features features; |
064af421 | 2330 | struct ofport *port; |
6c1491fb | 2331 | bool arp_match_ip; |
9e1fd49b | 2332 | struct ofpbuf *b; |
c2f0373a BP |
2333 | int n_tables; |
2334 | int i; | |
064af421 | 2335 | |
9e1fd49b BP |
2336 | ofproto->ofproto_class->get_features(ofproto, &arp_match_ip, |
2337 | &features.actions); | |
cb22974d | 2338 | ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */ |
064af421 | 2339 | |
c2f0373a BP |
2340 | /* Count only non-hidden tables in the number of tables. (Hidden tables, |
2341 | * if present, are always at the end.) */ | |
2342 | n_tables = ofproto->n_tables; | |
2343 | for (i = 0; i < ofproto->n_tables; i++) { | |
2344 | if (ofproto->tables[i].flags & OFTABLE_HIDDEN) { | |
2345 | n_tables = i; | |
2346 | break; | |
2347 | } | |
2348 | } | |
2349 | ||
9e1fd49b BP |
2350 | features.datapath_id = ofproto->datapath_id; |
2351 | features.n_buffers = pktbuf_capacity(); | |
c2f0373a | 2352 | features.n_tables = n_tables; |
9e1fd49b BP |
2353 | features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS | |
2354 | OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS); | |
6c1491fb | 2355 | if (arp_match_ip) { |
9e1fd49b | 2356 | features.capabilities |= OFPUTIL_C_ARP_MATCH_IP; |
6c1491fb | 2357 | } |
2e1ae200 JR |
2358 | /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */ |
2359 | features.auxiliary_id = 0; | |
9e1fd49b BP |
2360 | b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn), |
2361 | oh->xid); | |
64ff1d96 | 2362 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
9e1fd49b | 2363 | ofputil_put_switch_features_port(&port->pp, b); |
064af421 | 2364 | } |
064af421 | 2365 | |
9e1fd49b | 2366 | ofconn_send_reply(ofconn, b); |
064af421 BP |
2367 | return 0; |
2368 | } | |
064af421 | 2369 | |
90bf1e07 | 2370 | static enum ofperr |
d1e2cf21 | 2371 | handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2372 | { |
64ff1d96 | 2373 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
064af421 | 2374 | struct ofp_switch_config *osc; |
f0fd1a17 | 2375 | enum ofp_config_flags flags; |
7257b535 | 2376 | struct ofpbuf *buf; |
959a2ecd | 2377 | |
064af421 | 2378 | /* Send reply. */ |
982697a4 BP |
2379 | buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0); |
2380 | osc = ofpbuf_put_uninit(buf, sizeof *osc); | |
f0fd1a17 | 2381 | flags = ofproto->frag_handling; |
2e1ae200 JR |
2382 | /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */ |
2383 | if (oh->version < OFP13_VERSION | |
2384 | && ofconn_get_invalid_ttl_to_controller(ofconn)) { | |
f0fd1a17 PS |
2385 | flags |= OFPC_INVALID_TTL_TO_CONTROLLER; |
2386 | } | |
2387 | osc->flags = htons(flags); | |
810605a2 | 2388 | osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn)); |
b0421aa2 | 2389 | ofconn_send_reply(ofconn, buf); |
2d70a31a | 2390 | |
064af421 BP |
2391 | return 0; |
2392 | } | |
064af421 | 2393 | |
90bf1e07 | 2394 | static enum ofperr |
982697a4 | 2395 | handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2396 | { |
982697a4 | 2397 | const struct ofp_switch_config *osc = ofpmsg_body(oh); |
64ff1d96 | 2398 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
d1e2cf21 | 2399 | uint16_t flags = ntohs(osc->flags); |
2d70a31a | 2400 | |
7257b535 | 2401 | if (ofconn_get_type(ofconn) != OFCONN_PRIMARY |
f4f1ea7e | 2402 | || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) { |
7257b535 BP |
2403 | enum ofp_config_flags cur = ofproto->frag_handling; |
2404 | enum ofp_config_flags next = flags & OFPC_FRAG_MASK; | |
2405 | ||
cb22974d | 2406 | ovs_assert((cur & OFPC_FRAG_MASK) == cur); |
7257b535 BP |
2407 | if (cur != next) { |
2408 | if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) { | |
2409 | ofproto->frag_handling = next; | |
2410 | } else { | |
2411 | VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s", | |
2412 | ofproto->name, | |
2413 | ofputil_frag_handling_to_string(next)); | |
2414 | } | |
064af421 BP |
2415 | } |
2416 | } | |
2e1ae200 | 2417 | /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */ |
f0fd1a17 | 2418 | ofconn_set_invalid_ttl_to_controller(ofconn, |
2e1ae200 JR |
2419 | (oh->version < OFP13_VERSION |
2420 | && flags & OFPC_INVALID_TTL_TO_CONTROLLER)); | |
ebe482fd | 2421 | |
810605a2 | 2422 | ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len)); |
0ad9b732 | 2423 | |
064af421 | 2424 | return 0; |
064af421 BP |
2425 | } |
2426 | ||
9deba63b | 2427 | /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow |
90bf1e07 BP |
2428 | * error message code for the caller to propagate upward. Otherwise, returns |
2429 | * 0. | |
2430 | * | |
2431 | * The log message mentions 'msg_type'. */ | |
2432 | static enum ofperr | |
2433 | reject_slave_controller(struct ofconn *ofconn) | |
9deba63b | 2434 | { |
1ce0a5fa | 2435 | if (ofconn_get_type(ofconn) == OFCONN_PRIMARY |
f4f1ea7e | 2436 | && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) { |
90bf1e07 | 2437 | return OFPERR_OFPBRC_EPERM; |
9deba63b BP |
2438 | } else { |
2439 | return 0; | |
2440 | } | |
2441 | } | |
2442 | ||
9cae45dc JR |
2443 | /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of |
2444 | * 'ofpacts'. If found, returns its meter ID; if not, returns 0. | |
2445 | * | |
2446 | * This function relies on the order of 'ofpacts' being correct (as checked by | |
2447 | * ofpacts_verify()). */ | |
2448 | static uint32_t | |
2449 | find_meter(const struct ofpact ofpacts[], size_t ofpacts_len) | |
2450 | { | |
2451 | const struct ofpact *a; | |
2452 | ||
2453 | OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) { | |
2454 | enum ovs_instruction_type inst; | |
2455 | ||
2456 | inst = ovs_instruction_type_from_ofpact_type(a->type); | |
2457 | if (a->type == OFPACT_METER) { | |
2458 | return ofpact_get_METER(a)->meter_id; | |
2459 | } else if (inst > OVSINST_OFPIT13_METER) { | |
2460 | break; | |
2461 | } | |
2462 | } | |
2463 | ||
2464 | return 0; | |
2465 | } | |
2466 | ||
e3b56933 JR |
2467 | /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are appropriate |
2468 | * for a packet with the prerequisites satisfied by 'flow' in table 'table_id'. | |
9cae45dc JR |
2469 | * 'flow' may be temporarily modified, but is restored at return. |
2470 | */ | |
2471 | static enum ofperr | |
2472 | ofproto_check_ofpacts(struct ofproto *ofproto, | |
2473 | const struct ofpact ofpacts[], size_t ofpacts_len, | |
e3b56933 | 2474 | struct flow *flow, uint8_t table_id) |
9cae45dc JR |
2475 | { |
2476 | enum ofperr error; | |
2477 | uint32_t mid; | |
2478 | ||
430dbb14 AW |
2479 | error = ofpacts_check(ofpacts, ofpacts_len, flow, |
2480 | u16_to_ofp(ofproto->max_ports), table_id); | |
9cae45dc JR |
2481 | if (error) { |
2482 | return error; | |
2483 | } | |
2484 | ||
2485 | mid = find_meter(ofpacts, ofpacts_len); | |
2486 | if (mid && ofproto_get_provider_meter_id(ofproto, mid) == UINT32_MAX) { | |
2487 | return OFPERR_OFPMMFC_INVALID_METER; | |
2488 | } | |
2489 | return 0; | |
2490 | } | |
2491 | ||
90bf1e07 | 2492 | static enum ofperr |
982697a4 | 2493 | handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2494 | { |
64ff1d96 | 2495 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
c6a93eb7 BP |
2496 | struct ofputil_packet_out po; |
2497 | struct ofpbuf *payload; | |
f25d0cf3 BP |
2498 | uint64_t ofpacts_stub[1024 / 8]; |
2499 | struct ofpbuf ofpacts; | |
ae412e7d | 2500 | struct flow flow; |
4e022ec0 | 2501 | union flow_in_port in_port_; |
90bf1e07 | 2502 | enum ofperr error; |
064af421 | 2503 | |
ac51afaf BP |
2504 | COVERAGE_INC(ofproto_packet_out); |
2505 | ||
76589937 | 2506 | error = reject_slave_controller(ofconn); |
9deba63b | 2507 | if (error) { |
f25d0cf3 | 2508 | goto exit; |
9deba63b BP |
2509 | } |
2510 | ||
c6a93eb7 | 2511 | /* Decode message. */ |
f25d0cf3 | 2512 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
982697a4 | 2513 | error = ofputil_decode_packet_out(&po, oh, &ofpacts); |
064af421 | 2514 | if (error) { |
f25d0cf3 | 2515 | goto exit_free_ofpacts; |
064af421 | 2516 | } |
430dbb14 | 2517 | if (ofp_to_u16(po.in_port) >= p->max_ports |
4e022ec0 | 2518 | && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) { |
2e1bfcb6 | 2519 | error = OFPERR_OFPBRC_BAD_PORT; |
91858960 BP |
2520 | goto exit_free_ofpacts; |
2521 | } | |
064af421 | 2522 | |
5cb7a798 | 2523 | |
ac51afaf | 2524 | /* Get payload. */ |
c6a93eb7 BP |
2525 | if (po.buffer_id != UINT32_MAX) { |
2526 | error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL); | |
2527 | if (error || !payload) { | |
f25d0cf3 | 2528 | goto exit_free_ofpacts; |
064af421 | 2529 | } |
064af421 | 2530 | } else { |
c6a93eb7 BP |
2531 | payload = xmalloc(sizeof *payload); |
2532 | ofpbuf_use_const(payload, po.packet, po.packet_len); | |
e1154f71 BP |
2533 | } |
2534 | ||
548de4dd | 2535 | /* Verify actions against packet, then send packet if successful. */ |
4e022ec0 AW |
2536 | in_port_.ofp_port = po.in_port; |
2537 | flow_extract(payload, 0, 0, NULL, &in_port_, &flow); | |
e3b56933 | 2538 | error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len, &flow, 0); |
548de4dd BP |
2539 | if (!error) { |
2540 | error = p->ofproto_class->packet_out(p, payload, &flow, | |
2541 | po.ofpacts, po.ofpacts_len); | |
2542 | } | |
c6a93eb7 | 2543 | ofpbuf_delete(payload); |
abe529af | 2544 | |
f25d0cf3 BP |
2545 | exit_free_ofpacts: |
2546 | ofpbuf_uninit(&ofpacts); | |
2547 | exit: | |
abe529af | 2548 | return error; |
064af421 BP |
2549 | } |
2550 | ||
2551 | static void | |
9e1fd49b BP |
2552 | update_port_config(struct ofport *port, |
2553 | enum ofputil_port_config config, | |
2554 | enum ofputil_port_config mask) | |
064af421 | 2555 | { |
9e1fd49b BP |
2556 | enum ofputil_port_config old_config = port->pp.config; |
2557 | enum ofputil_port_config toggle; | |
abe529af | 2558 | |
9e1fd49b BP |
2559 | toggle = (config ^ port->pp.config) & mask; |
2560 | if (toggle & OFPUTIL_PC_PORT_DOWN) { | |
2561 | if (config & OFPUTIL_PC_PORT_DOWN) { | |
4b609110 | 2562 | netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL); |
064af421 | 2563 | } else { |
4b609110 | 2564 | netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL); |
064af421 | 2565 | } |
9e1fd49b | 2566 | toggle &= ~OFPUTIL_PC_PORT_DOWN; |
064af421 | 2567 | } |
abe529af | 2568 | |
9e1fd49b BP |
2569 | port->pp.config ^= toggle; |
2570 | if (port->pp.config != old_config) { | |
abe529af | 2571 | port->ofproto->ofproto_class->port_reconfigured(port, old_config); |
064af421 BP |
2572 | } |
2573 | } | |
2574 | ||
90bf1e07 | 2575 | static enum ofperr |
d1e2cf21 | 2576 | handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2577 | { |
64ff1d96 | 2578 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
9e1fd49b | 2579 | struct ofputil_port_mod pm; |
064af421 | 2580 | struct ofport *port; |
9e1fd49b | 2581 | enum ofperr error; |
064af421 | 2582 | |
76589937 | 2583 | error = reject_slave_controller(ofconn); |
9deba63b BP |
2584 | if (error) { |
2585 | return error; | |
2586 | } | |
064af421 | 2587 | |
9e1fd49b BP |
2588 | error = ofputil_decode_port_mod(oh, &pm); |
2589 | if (error) { | |
2590 | return error; | |
2591 | } | |
2592 | ||
2593 | port = ofproto_get_port(p, pm.port_no); | |
064af421 | 2594 | if (!port) { |
90bf1e07 | 2595 | return OFPERR_OFPPMFC_BAD_PORT; |
9e1fd49b | 2596 | } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) { |
90bf1e07 | 2597 | return OFPERR_OFPPMFC_BAD_HW_ADDR; |
064af421 | 2598 | } else { |
9e1fd49b BP |
2599 | update_port_config(port, pm.config, pm.mask); |
2600 | if (pm.advertise) { | |
2601 | netdev_set_advertisements(port->netdev, pm.advertise); | |
064af421 BP |
2602 | } |
2603 | } | |
2604 | return 0; | |
2605 | } | |
2606 | ||
90bf1e07 | 2607 | static enum ofperr |
3269c562 | 2608 | handle_desc_stats_request(struct ofconn *ofconn, |
982697a4 | 2609 | const struct ofp_header *request) |
064af421 | 2610 | { |
061bfea4 BP |
2611 | static const char *default_mfr_desc = "Nicira, Inc."; |
2612 | static const char *default_hw_desc = "Open vSwitch"; | |
2613 | static const char *default_sw_desc = VERSION; | |
2614 | static const char *default_serial_desc = "None"; | |
2615 | static const char *default_dp_desc = "None"; | |
2616 | ||
64ff1d96 | 2617 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 BP |
2618 | struct ofp_desc_stats *ods; |
2619 | struct ofpbuf *msg; | |
2620 | ||
982697a4 BP |
2621 | msg = ofpraw_alloc_stats_reply(request, 0); |
2622 | ods = ofpbuf_put_zeros(msg, sizeof *ods); | |
061bfea4 BP |
2623 | ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc, |
2624 | sizeof ods->mfr_desc); | |
2625 | ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc, | |
2626 | sizeof ods->hw_desc); | |
2627 | ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc, | |
2628 | sizeof ods->sw_desc); | |
2629 | ovs_strlcpy(ods->serial_num, | |
2630 | p->serial_desc ? p->serial_desc : default_serial_desc, | |
2631 | sizeof ods->serial_num); | |
2632 | ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc, | |
2633 | sizeof ods->dp_desc); | |
b0421aa2 | 2634 | ofconn_send_reply(ofconn, msg); |
064af421 BP |
2635 | |
2636 | return 0; | |
2637 | } | |
2638 | ||
90bf1e07 | 2639 | static enum ofperr |
3269c562 | 2640 | handle_table_stats_request(struct ofconn *ofconn, |
982697a4 | 2641 | const struct ofp_header *request) |
064af421 | 2642 | { |
64ff1d96 | 2643 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
307975da | 2644 | struct ofp12_table_stats *ots; |
064af421 | 2645 | struct ofpbuf *msg; |
c2f0373a | 2646 | int n_tables; |
6c1491fb | 2647 | size_t i; |
064af421 | 2648 | |
307975da SH |
2649 | /* Set up default values. |
2650 | * | |
2651 | * ofp12_table_stats is used as a generic structure as | |
2652 | * it is able to hold all the fields for ofp10_table_stats | |
2653 | * and ofp11_table_stats (and of course itself). | |
2654 | */ | |
2655 | ots = xcalloc(p->n_tables, sizeof *ots); | |
6c1491fb BP |
2656 | for (i = 0; i < p->n_tables; i++) { |
2657 | ots[i].table_id = i; | |
35aa7a21 | 2658 | sprintf(ots[i].name, "table%zu", i); |
307975da SH |
2659 | ots[i].match = htonll(OFPXMT12_MASK); |
2660 | ots[i].wildcards = htonll(OFPXMT12_MASK); | |
e78b61f6 BP |
2661 | ots[i].write_actions = htonl(OFPAT11_OUTPUT); |
2662 | ots[i].apply_actions = htonl(OFPAT11_OUTPUT); | |
307975da SH |
2663 | ots[i].write_setfields = htonll(OFPXMT12_MASK); |
2664 | ots[i].apply_setfields = htonll(OFPXMT12_MASK); | |
2665 | ots[i].metadata_match = htonll(UINT64_MAX); | |
2666 | ots[i].metadata_write = htonll(UINT64_MAX); | |
2667 | ots[i].instructions = htonl(OFPIT11_ALL); | |
2668 | ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK); | |
6c1491fb | 2669 | ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */ |
0b4f2078 | 2670 | ovs_rwlock_rdlock(&p->tables[i].cls.rwlock); |
d0918789 | 2671 | ots[i].active_count = htonl(classifier_count(&p->tables[i].cls)); |
0b4f2078 | 2672 | ovs_rwlock_unlock(&p->tables[i].cls.rwlock); |
6c1491fb | 2673 | } |
064af421 | 2674 | |
6c1491fb | 2675 | p->ofproto_class->get_tables(p, ots); |
064af421 | 2676 | |
c2f0373a BP |
2677 | /* Post-process the tables, dropping hidden tables. */ |
2678 | n_tables = p->n_tables; | |
254750ce BP |
2679 | for (i = 0; i < p->n_tables; i++) { |
2680 | const struct oftable *table = &p->tables[i]; | |
2681 | ||
c2f0373a BP |
2682 | if (table->flags & OFTABLE_HIDDEN) { |
2683 | n_tables = i; | |
2684 | break; | |
2685 | } | |
2686 | ||
254750ce BP |
2687 | if (table->name) { |
2688 | ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name); | |
2689 | } | |
2690 | ||
2691 | if (table->max_flows < ntohl(ots[i].max_entries)) { | |
2692 | ots[i].max_entries = htonl(table->max_flows); | |
2693 | } | |
2694 | } | |
2695 | ||
c2f0373a | 2696 | msg = ofputil_encode_table_stats_reply(ots, n_tables, request); |
b0421aa2 | 2697 | ofconn_send_reply(ofconn, msg); |
307975da SH |
2698 | |
2699 | free(ots); | |
2700 | ||
064af421 BP |
2701 | return 0; |
2702 | } | |
2703 | ||
abaad8cf | 2704 | static void |
63f2140a | 2705 | append_port_stat(struct ofport *port, struct list *replies) |
abaad8cf | 2706 | { |
f8e4867e | 2707 | struct ofputil_port_stats ops = { .port_no = port->pp.port_no }; |
abaad8cf | 2708 | |
65e0be10 BP |
2709 | calc_duration(port->created, time_msec(), |
2710 | &ops.duration_sec, &ops.duration_nsec); | |
2711 | ||
d295e8e9 JP |
2712 | /* Intentionally ignore return value, since errors will set |
2713 | * 'stats' to all-1s, which is correct for OpenFlow, and | |
abaad8cf | 2714 | * netdev_get_stats() will log errors. */ |
f8e4867e SH |
2715 | ofproto_port_get_stats(port, &ops.stats); |
2716 | ||
2717 | ofputil_append_port_stat(replies, &ops); | |
abaad8cf JP |
2718 | } |
2719 | ||
90bf1e07 | 2720 | static enum ofperr |
63f2140a | 2721 | handle_port_stats_request(struct ofconn *ofconn, |
982697a4 | 2722 | const struct ofp_header *request) |
064af421 | 2723 | { |
64ff1d96 | 2724 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 | 2725 | struct ofport *port; |
63f2140a | 2726 | struct list replies; |
4e022ec0 | 2727 | ofp_port_t port_no; |
f8e4867e SH |
2728 | enum ofperr error; |
2729 | ||
2730 | error = ofputil_decode_port_stats_request(request, &port_no); | |
2731 | if (error) { | |
2732 | return error; | |
2733 | } | |
064af421 | 2734 | |
982697a4 | 2735 | ofpmp_init(&replies, request); |
7f05e7ab | 2736 | if (port_no != OFPP_ANY) { |
f8e4867e | 2737 | port = ofproto_get_port(p, port_no); |
abaad8cf | 2738 | if (port) { |
63f2140a | 2739 | append_port_stat(port, &replies); |
abaad8cf JP |
2740 | } |
2741 | } else { | |
4e8e4213 | 2742 | HMAP_FOR_EACH (port, hmap_node, &p->ports) { |
63f2140a | 2743 | append_port_stat(port, &replies); |
abaad8cf | 2744 | } |
064af421 BP |
2745 | } |
2746 | ||
63f2140a | 2747 | ofconn_send_replies(ofconn, &replies); |
064af421 BP |
2748 | return 0; |
2749 | } | |
2750 | ||
2be393ed JP |
2751 | static enum ofperr |
2752 | handle_port_desc_stats_request(struct ofconn *ofconn, | |
982697a4 | 2753 | const struct ofp_header *request) |
2be393ed JP |
2754 | { |
2755 | struct ofproto *p = ofconn_get_ofproto(ofconn); | |
2e3fa633 | 2756 | enum ofp_version version; |
2be393ed JP |
2757 | struct ofport *port; |
2758 | struct list replies; | |
2759 | ||
982697a4 | 2760 | ofpmp_init(&replies, request); |
2be393ed | 2761 | |
2e3fa633 | 2762 | version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn)); |
2be393ed | 2763 | HMAP_FOR_EACH (port, hmap_node, &p->ports) { |
2e3fa633 | 2764 | ofputil_append_port_desc_stats_reply(version, &port->pp, &replies); |
2be393ed JP |
2765 | } |
2766 | ||
2767 | ofconn_send_replies(ofconn, &replies); | |
2768 | return 0; | |
2769 | } | |
2770 | ||
98eaac36 JR |
2771 | static uint32_t |
2772 | hash_cookie(ovs_be64 cookie) | |
2773 | { | |
2774 | return hash_2words((OVS_FORCE uint64_t)cookie >> 32, | |
2775 | (OVS_FORCE uint64_t)cookie); | |
2776 | } | |
2777 | ||
2778 | static void | |
2779 | cookies_insert(struct ofproto *ofproto, struct rule *rule) | |
2780 | { | |
2781 | hindex_insert(&ofproto->cookies, &rule->cookie_node, | |
2782 | hash_cookie(rule->flow_cookie)); | |
2783 | } | |
2784 | ||
2785 | static void | |
2786 | cookies_remove(struct ofproto *ofproto, struct rule *rule) | |
2787 | { | |
2788 | hindex_remove(&ofproto->cookies, &rule->cookie_node); | |
2789 | } | |
2790 | ||
2791 | static void | |
2792 | ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule, | |
2793 | ovs_be64 new_cookie) | |
2794 | { | |
2795 | if (new_cookie != rule->flow_cookie) { | |
2796 | cookies_remove(ofproto, rule); | |
2797 | ||
2798 | rule->flow_cookie = new_cookie; | |
2799 | ||
2800 | cookies_insert(ofproto, rule); | |
2801 | } | |
2802 | } | |
2803 | ||
c6ebb8fb | 2804 | static void |
65e0be10 BP |
2805 | calc_duration(long long int start, long long int now, |
2806 | uint32_t *sec, uint32_t *nsec) | |
c6ebb8fb | 2807 | { |
f27f2134 | 2808 | long long int msecs = now - start; |
588cd7b5 BP |
2809 | *sec = msecs / 1000; |
2810 | *nsec = (msecs % 1000) * (1000 * 1000); | |
2811 | } | |
2812 | ||
48266274 BP |
2813 | /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns |
2814 | * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */ | |
90bf1e07 | 2815 | static enum ofperr |
48266274 BP |
2816 | check_table_id(const struct ofproto *ofproto, uint8_t table_id) |
2817 | { | |
2818 | return (table_id == 0xff || table_id < ofproto->n_tables | |
2819 | ? 0 | |
c22c56bd | 2820 | : OFPERR_OFPBRC_BAD_TABLE_ID); |
48266274 BP |
2821 | |
2822 | } | |
2823 | ||
5c67e4af | 2824 | static struct oftable * |
d4ce8a49 | 2825 | next_visible_table(const struct ofproto *ofproto, uint8_t table_id) |
5c67e4af BP |
2826 | { |
2827 | struct oftable *table; | |
2828 | ||
2829 | for (table = &ofproto->tables[table_id]; | |
2830 | table < &ofproto->tables[ofproto->n_tables]; | |
2831 | table++) { | |
2832 | if (!(table->flags & OFTABLE_HIDDEN)) { | |
2833 | return table; | |
2834 | } | |
2835 | } | |
2836 | ||
2837 | return NULL; | |
2838 | } | |
2839 | ||
d0918789 | 2840 | static struct oftable * |
d4ce8a49 | 2841 | first_matching_table(const struct ofproto *ofproto, uint8_t table_id) |
064af421 | 2842 | { |
6c1491fb | 2843 | if (table_id == 0xff) { |
5c67e4af | 2844 | return next_visible_table(ofproto, 0); |
6c1491fb BP |
2845 | } else if (table_id < ofproto->n_tables) { |
2846 | return &ofproto->tables[table_id]; | |
a02e5331 | 2847 | } else { |
6c1491fb | 2848 | return NULL; |
a02e5331 | 2849 | } |
064af421 BP |
2850 | } |
2851 | ||
d0918789 | 2852 | static struct oftable * |
d4ce8a49 BP |
2853 | next_matching_table(const struct ofproto *ofproto, |
2854 | const struct oftable *table, uint8_t table_id) | |
6c1491fb | 2855 | { |
5c67e4af BP |
2856 | return (table_id == 0xff |
2857 | ? next_visible_table(ofproto, (table - ofproto->tables) + 1) | |
6c1491fb BP |
2858 | : NULL); |
2859 | } | |
2860 | ||
d0918789 | 2861 | /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO: |
6c1491fb BP |
2862 | * |
2863 | * - If TABLE_ID is 0xff, this iterates over every classifier table in | |
5c67e4af | 2864 | * OFPROTO, skipping tables marked OFTABLE_HIDDEN. |
6c1491fb BP |
2865 | * |
2866 | * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates | |
5c67e4af BP |
2867 | * only once, for that table. (This can be used to access tables marked |
2868 | * OFTABLE_HIDDEN.) | |
6c1491fb | 2869 | * |
48266274 BP |
2870 | * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be |
2871 | * entered at all. (Perhaps you should have validated TABLE_ID with | |
2872 | * check_table_id().) | |
6c1491fb BP |
2873 | * |
2874 | * All parameters are evaluated multiple times. | |
2875 | */ | |
d0918789 BP |
2876 | #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \ |
2877 | for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \ | |
2878 | (TABLE) != NULL; \ | |
2879 | (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID)) | |
6c1491fb | 2880 | |
9ed18e46 BP |
2881 | /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if |
2882 | * 'table_id' is 0xff) that match 'match' in the "loose" way required for | |
2883 | * OpenFlow OFPFC_MODIFY and OFPFC_DELETE requests and puts them on list | |
2884 | * 'rules'. | |
2885 | * | |
7f05e7ab | 2886 | * If 'out_port' is anything other than OFPP_ANY, then only rules that output |
9ed18e46 BP |
2887 | * to 'out_port' are included. |
2888 | * | |
2889 | * Hidden rules are always omitted. | |
2890 | * | |
2891 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
90bf1e07 | 2892 | static enum ofperr |
9ed18e46 | 2893 | collect_rules_loose(struct ofproto *ofproto, uint8_t table_id, |
81a76618 | 2894 | const struct match *match, |
e729e793 | 2895 | ovs_be64 cookie, ovs_be64 cookie_mask, |
4e022ec0 | 2896 | ofp_port_t out_port, struct list *rules) |
9ed18e46 | 2897 | { |
d0918789 | 2898 | struct oftable *table; |
81a76618 | 2899 | struct cls_rule cr; |
90bf1e07 | 2900 | enum ofperr error; |
48266274 BP |
2901 | |
2902 | error = check_table_id(ofproto, table_id); | |
2903 | if (error) { | |
2904 | return error; | |
2905 | } | |
9ed18e46 BP |
2906 | |
2907 | list_init(rules); | |
81a76618 | 2908 | cls_rule_init(&cr, match, 0); |
98eaac36 JR |
2909 | |
2910 | if (cookie_mask == htonll(UINT64_MAX)) { | |
2911 | struct rule *rule; | |
2912 | ||
2913 | HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, hash_cookie(cookie), | |
2914 | &ofproto->cookies) { | |
2915 | if (table_id != rule->table_id && table_id != 0xff) { | |
2916 | continue; | |
2917 | } | |
2918 | if (ofproto_rule_is_hidden(rule)) { | |
2919 | continue; | |
2920 | } | |
2921 | if (cls_rule_is_loose_match(&rule->cr, &cr.match)) { | |
2922 | if (rule->pending) { | |
2923 | error = OFPROTO_POSTPONE; | |
2924 | goto exit; | |
2925 | } | |
2926 | if (rule->flow_cookie == cookie /* Hash collisions possible. */ | |
2927 | && ofproto_rule_has_out_port(rule, out_port)) { | |
2928 | list_push_back(rules, &rule->ofproto_node); | |
2929 | } | |
2930 | } | |
2931 | } | |
2932 | goto exit; | |
2933 | } | |
2934 | ||
d0918789 | 2935 | FOR_EACH_MATCHING_TABLE (table, table_id, ofproto) { |
9ed18e46 BP |
2936 | struct cls_cursor cursor; |
2937 | struct rule *rule; | |
2938 | ||
0b4f2078 | 2939 | ovs_rwlock_rdlock(&table->cls.rwlock); |
81a76618 | 2940 | cls_cursor_init(&cursor, &table->cls, &cr); |
9ed18e46 | 2941 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { |
7ee20df1 | 2942 | if (rule->pending) { |
0b4f2078 | 2943 | ovs_rwlock_unlock(&table->cls.rwlock); |
48d28ac1 BP |
2944 | error = OFPROTO_POSTPONE; |
2945 | goto exit; | |
7ee20df1 | 2946 | } |
2b07c8b1 BP |
2947 | if (!ofproto_rule_is_hidden(rule) |
2948 | && ofproto_rule_has_out_port(rule, out_port) | |
e729e793 | 2949 | && !((rule->flow_cookie ^ cookie) & cookie_mask)) { |
9ed18e46 BP |
2950 | list_push_back(rules, &rule->ofproto_node); |
2951 | } | |
2952 | } | |
0b4f2078 | 2953 | ovs_rwlock_unlock(&table->cls.rwlock); |
9ed18e46 | 2954 | } |
48d28ac1 BP |
2955 | |
2956 | exit: | |
2957 | cls_rule_destroy(&cr); | |
2958 | return error; | |
9ed18e46 BP |
2959 | } |
2960 | ||
2961 | /* Searches 'ofproto' for rules in table 'table_id' (or in all tables, if | |
2962 | * 'table_id' is 0xff) that match 'match' in the "strict" way required for | |
2963 | * OpenFlow OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests and puts them | |
2964 | * on list 'rules'. | |
2965 | * | |
7f05e7ab | 2966 | * If 'out_port' is anything other than OFPP_ANY, then only rules that output |
9ed18e46 BP |
2967 | * to 'out_port' are included. |
2968 | * | |
2969 | * Hidden rules are always omitted. | |
2970 | * | |
2971 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
90bf1e07 | 2972 | static enum ofperr |
9ed18e46 | 2973 | collect_rules_strict(struct ofproto *ofproto, uint8_t table_id, |
81a76618 | 2974 | const struct match *match, unsigned int priority, |
e729e793 | 2975 | ovs_be64 cookie, ovs_be64 cookie_mask, |
4e022ec0 | 2976 | ofp_port_t out_port, struct list *rules) |
9ed18e46 | 2977 | { |
d0918789 | 2978 | struct oftable *table; |
81a76618 | 2979 | struct cls_rule cr; |
48266274 BP |
2980 | int error; |
2981 | ||
2982 | error = check_table_id(ofproto, table_id); | |
2983 | if (error) { | |
2984 | return error; | |
2985 | } | |
9ed18e46 BP |
2986 | |
2987 | list_init(rules); | |
81a76618 | 2988 | cls_rule_init(&cr, match, priority); |
98eaac36 JR |
2989 | |
2990 | if (cookie_mask == htonll(UINT64_MAX)) { | |
2991 | struct rule *rule; | |
2992 | ||
2993 | HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, hash_cookie(cookie), | |
2994 | &ofproto->cookies) { | |
2995 | if (table_id != rule->table_id && table_id != 0xff) { | |
2996 | continue; | |
2997 | } | |
2998 | if (ofproto_rule_is_hidden(rule)) { | |
2999 | continue; | |
3000 | } | |
3001 | if (cls_rule_equal(&rule->cr, &cr)) { | |
3002 | if (rule->pending) { | |
3003 | error = OFPROTO_POSTPONE; | |
3004 | goto exit; | |
3005 | } | |
3006 | if (rule->flow_cookie == cookie /* Hash collisions possible. */ | |
3007 | && ofproto_rule_has_out_port(rule, out_port)) { | |
3008 | list_push_back(rules, &rule->ofproto_node); | |
3009 | } | |
3010 | } | |
3011 | } | |
3012 | goto exit; | |
3013 | } | |
3014 | ||
d0918789 | 3015 | FOR_EACH_MATCHING_TABLE (table, table_id, ofproto) { |
9ed18e46 BP |
3016 | struct rule *rule; |
3017 | ||
0b4f2078 | 3018 | ovs_rwlock_rdlock(&table->cls.rwlock); |
d0918789 | 3019 | rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, |
81a76618 | 3020 | &cr)); |
0b4f2078 | 3021 | ovs_rwlock_unlock(&table->cls.rwlock); |
9ed18e46 | 3022 | if (rule) { |
7ee20df1 | 3023 | if (rule->pending) { |
48d28ac1 BP |
3024 | error = OFPROTO_POSTPONE; |
3025 | goto exit; | |
7ee20df1 | 3026 | } |
2b07c8b1 BP |
3027 | if (!ofproto_rule_is_hidden(rule) |
3028 | && ofproto_rule_has_out_port(rule, out_port) | |
e729e793 | 3029 | && !((rule->flow_cookie ^ cookie) & cookie_mask)) { |
9ed18e46 BP |
3030 | list_push_back(rules, &rule->ofproto_node); |
3031 | } | |
3032 | } | |
3033 | } | |
48d28ac1 BP |
3034 | |
3035 | exit: | |
3036 | cls_rule_destroy(&cr); | |
9ed18e46 BP |
3037 | return 0; |
3038 | } | |
3039 | ||
f27f2134 BP |
3040 | /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and |
3041 | * forced into the range of a uint16_t. */ | |
3042 | static int | |
3043 | age_secs(long long int age_ms) | |
3044 | { | |
3045 | return (age_ms < 0 ? 0 | |
3046 | : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX | |
3047 | : (unsigned int) age_ms / 1000); | |
3048 | } | |
3049 | ||
90bf1e07 | 3050 | static enum ofperr |
63f2140a | 3051 | handle_flow_stats_request(struct ofconn *ofconn, |
982697a4 | 3052 | const struct ofp_header *request) |
064af421 | 3053 | { |
64ff1d96 | 3054 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 3055 | struct ofputil_flow_stats_request fsr; |
63f2140a | 3056 | struct list replies; |
9ed18e46 BP |
3057 | struct list rules; |
3058 | struct rule *rule; | |
90bf1e07 | 3059 | enum ofperr error; |
09246b99 | 3060 | |
982697a4 | 3061 | error = ofputil_decode_flow_stats_request(&fsr, request); |
09246b99 BP |
3062 | if (error) { |
3063 | return error; | |
3064 | } | |
3065 | ||
9ed18e46 | 3066 | error = collect_rules_loose(ofproto, fsr.table_id, &fsr.match, |
e729e793 | 3067 | fsr.cookie, fsr.cookie_mask, |
9ed18e46 BP |
3068 | fsr.out_port, &rules); |
3069 | if (error) { | |
3070 | return error; | |
3071 | } | |
5ecc9d81 | 3072 | |
982697a4 | 3073 | ofpmp_init(&replies, request); |
9ed18e46 | 3074 | LIST_FOR_EACH (rule, ofproto_node, &rules) { |
f27f2134 | 3075 | long long int now = time_msec(); |
9ed18e46 BP |
3076 | struct ofputil_flow_stats fs; |
3077 | ||
5cb7a798 | 3078 | minimatch_expand(&rule->cr.match, &fs.match); |
81a76618 | 3079 | fs.priority = rule->cr.priority; |
9ed18e46 BP |
3080 | fs.cookie = rule->flow_cookie; |
3081 | fs.table_id = rule->table_id; | |
65e0be10 | 3082 | calc_duration(rule->created, now, &fs.duration_sec, &fs.duration_nsec); |
f27f2134 BP |
3083 | fs.idle_age = age_secs(now - rule->used); |
3084 | fs.hard_age = age_secs(now - rule->modified); | |
9ed18e46 BP |
3085 | ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count, |
3086 | &fs.byte_count); | |
f25d0cf3 BP |
3087 | fs.ofpacts = rule->ofpacts; |
3088 | fs.ofpacts_len = rule->ofpacts_len; | |
a3779dbc EJ |
3089 | |
3090 | ovs_mutex_lock(&rule->timeout_mutex); | |
3091 | fs.idle_timeout = rule->idle_timeout; | |
3092 | fs.hard_timeout = rule->hard_timeout; | |
3093 | ovs_mutex_unlock(&rule->timeout_mutex); | |
3094 | ||
0fb88c18 | 3095 | fs.flags = 0; |
2e1ae200 | 3096 | if (rule->send_flow_removed) { |
0fb88c18 BP |
3097 | fs.flags |= OFPUTIL_FF_SEND_FLOW_REM; |
3098 | /* FIXME: Implement OFPUTIL_FF_NO_PKT_COUNTS and | |
3099 | OFPUTIL_FF_NO_BYT_COUNTS. */ | |
2e1ae200 | 3100 | } |
9ed18e46 | 3101 | ofputil_append_flow_stats_reply(&fs, &replies); |
3c4486a5 | 3102 | } |
63f2140a | 3103 | ofconn_send_replies(ofconn, &replies); |
5ecc9d81 | 3104 | |
09246b99 BP |
3105 | return 0; |
3106 | } | |
3107 | ||
4f2cad2c | 3108 | static void |
3394b5b6 | 3109 | flow_stats_ds(struct rule *rule, struct ds *results) |
4f2cad2c | 3110 | { |
4f2cad2c | 3111 | uint64_t packet_count, byte_count; |
4f2cad2c | 3112 | |
abe529af BP |
3113 | rule->ofproto->ofproto_class->rule_get_stats(rule, |
3114 | &packet_count, &byte_count); | |
4f2cad2c | 3115 | |
6c1491fb BP |
3116 | if (rule->table_id != 0) { |
3117 | ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id); | |
3118 | } | |
4f2cad2c JP |
3119 | ds_put_format(results, "duration=%llds, ", |
3120 | (time_msec() - rule->created) / 1000); | |
52ae00b3 | 3121 | ds_put_format(results, "priority=%u, ", rule->cr.priority); |
4f2cad2c JP |
3122 | ds_put_format(results, "n_packets=%"PRIu64", ", packet_count); |
3123 | ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count); | |
cb833cf6 | 3124 | cls_rule_format(&rule->cr, results); |
a5df0e72 | 3125 | ds_put_char(results, ','); |
aa30ae4e | 3126 | ofpacts_format(rule->ofpacts, rule->ofpacts_len, results); |
4f2cad2c JP |
3127 | ds_put_cstr(results, "\n"); |
3128 | } | |
3129 | ||
d295e8e9 | 3130 | /* Adds a pretty-printed description of all flows to 'results', including |
ee8b231c | 3131 | * hidden flows (e.g., set up by in-band control). */ |
4f2cad2c JP |
3132 | void |
3133 | ofproto_get_all_flows(struct ofproto *p, struct ds *results) | |
3134 | { | |
d0918789 | 3135 | struct oftable *table; |
6c1491fb | 3136 | |
d0918789 | 3137 | OFPROTO_FOR_EACH_TABLE (table, p) { |
6c1491fb BP |
3138 | struct cls_cursor cursor; |
3139 | struct rule *rule; | |
064af421 | 3140 | |
0b4f2078 | 3141 | ovs_rwlock_rdlock(&table->cls.rwlock); |
d0918789 | 3142 | cls_cursor_init(&cursor, &table->cls, NULL); |
6c1491fb BP |
3143 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { |
3144 | flow_stats_ds(rule, results); | |
3145 | } | |
0b4f2078 | 3146 | ovs_rwlock_unlock(&table->cls.rwlock); |
064af421 | 3147 | } |
064af421 BP |
3148 | } |
3149 | ||
b5827b24 BP |
3150 | /* Obtains the NetFlow engine type and engine ID for 'ofproto' into |
3151 | * '*engine_type' and '*engine_id', respectively. */ | |
3152 | void | |
3153 | ofproto_get_netflow_ids(const struct ofproto *ofproto, | |
3154 | uint8_t *engine_type, uint8_t *engine_id) | |
3155 | { | |
abe529af | 3156 | ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id); |
b5827b24 BP |
3157 | } |
3158 | ||
9a9e3786 BP |
3159 | /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns |
3160 | * true if the port's CFM status was successfully stored into '*status'. | |
3161 | * Returns false if the port did not have CFM configured, in which case | |
3162 | * '*status' is indeterminate. | |
3163 | * | |
13b1b2ae | 3164 | * The caller must provide and owns '*status', and must free 'status->rmps'. */ |
9a9e3786 | 3165 | bool |
4e022ec0 | 3166 | ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port, |
9a9e3786 | 3167 | struct ofproto_cfm_status *status) |
3967a833 MM |
3168 | { |
3169 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
9a9e3786 BP |
3170 | return (ofport |
3171 | && ofproto->ofproto_class->get_cfm_status | |
3172 | && ofproto->ofproto_class->get_cfm_status(ofport, status)); | |
3967a833 MM |
3173 | } |
3174 | ||
90bf1e07 | 3175 | static enum ofperr |
76c93b22 | 3176 | handle_aggregate_stats_request(struct ofconn *ofconn, |
982697a4 | 3177 | const struct ofp_header *oh) |
27d34fce | 3178 | { |
76c93b22 | 3179 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 3180 | struct ofputil_flow_stats_request request; |
76c93b22 | 3181 | struct ofputil_aggregate_stats stats; |
5e9d0469 | 3182 | bool unknown_packets, unknown_bytes; |
76c93b22 | 3183 | struct ofpbuf *reply; |
9ed18e46 BP |
3184 | struct list rules; |
3185 | struct rule *rule; | |
90bf1e07 | 3186 | enum ofperr error; |
27d34fce | 3187 | |
982697a4 | 3188 | error = ofputil_decode_flow_stats_request(&request, oh); |
76c93b22 BP |
3189 | if (error) { |
3190 | return error; | |
3191 | } | |
5ecc9d81 | 3192 | |
9ed18e46 | 3193 | error = collect_rules_loose(ofproto, request.table_id, &request.match, |
e729e793 | 3194 | request.cookie, request.cookie_mask, |
9ed18e46 BP |
3195 | request.out_port, &rules); |
3196 | if (error) { | |
3197 | return error; | |
3198 | } | |
3c4486a5 | 3199 | |
9ed18e46 | 3200 | memset(&stats, 0, sizeof stats); |
5e9d0469 | 3201 | unknown_packets = unknown_bytes = false; |
9ed18e46 BP |
3202 | LIST_FOR_EACH (rule, ofproto_node, &rules) { |
3203 | uint64_t packet_count; | |
3204 | uint64_t byte_count; | |
5ecc9d81 | 3205 | |
9ed18e46 BP |
3206 | ofproto->ofproto_class->rule_get_stats(rule, &packet_count, |
3207 | &byte_count); | |
5ecc9d81 | 3208 | |
5e9d0469 BP |
3209 | if (packet_count == UINT64_MAX) { |
3210 | unknown_packets = true; | |
3211 | } else { | |
3212 | stats.packet_count += packet_count; | |
3213 | } | |
3214 | ||
3215 | if (byte_count == UINT64_MAX) { | |
3216 | unknown_bytes = true; | |
3217 | } else { | |
3218 | stats.byte_count += byte_count; | |
3219 | } | |
3220 | ||
9ed18e46 | 3221 | stats.flow_count++; |
3c4486a5 | 3222 | } |
5e9d0469 BP |
3223 | if (unknown_packets) { |
3224 | stats.packet_count = UINT64_MAX; | |
3225 | } | |
3226 | if (unknown_bytes) { | |
3227 | stats.byte_count = UINT64_MAX; | |
3228 | } | |
27d34fce | 3229 | |
982697a4 | 3230 | reply = ofputil_encode_aggregate_stats_reply(&stats, oh); |
76c93b22 | 3231 | ofconn_send_reply(ofconn, reply); |
09246b99 BP |
3232 | |
3233 | return 0; | |
3234 | } | |
3235 | ||
c1c9c9c4 | 3236 | struct queue_stats_cbdata { |
ca0f572c | 3237 | struct ofport *ofport; |
63f2140a | 3238 | struct list replies; |
6dc34a0d | 3239 | long long int now; |
c1c9c9c4 BP |
3240 | }; |
3241 | ||
3242 | static void | |
db9220c3 | 3243 | put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id, |
c1c9c9c4 BP |
3244 | const struct netdev_queue_stats *stats) |
3245 | { | |
6dc34a0d | 3246 | struct ofputil_queue_stats oqs; |
c1c9c9c4 | 3247 | |
6dc34a0d BP |
3248 | oqs.port_no = cbdata->ofport->pp.port_no; |
3249 | oqs.queue_id = queue_id; | |
3250 | oqs.tx_bytes = stats->tx_bytes; | |
3251 | oqs.tx_packets = stats->tx_packets; | |
3252 | oqs.tx_errors = stats->tx_errors; | |
3253 | if (stats->created != LLONG_MIN) { | |
3254 | calc_duration(stats->created, cbdata->now, | |
3255 | &oqs.duration_sec, &oqs.duration_nsec); | |
3256 | } else { | |
3257 | oqs.duration_sec = oqs.duration_nsec = UINT32_MAX; | |
3258 | } | |
64626975 | 3259 | ofputil_append_queue_stat(&cbdata->replies, &oqs); |
c1c9c9c4 BP |
3260 | } |
3261 | ||
3262 | static void | |
db9220c3 | 3263 | handle_queue_stats_dump_cb(uint32_t queue_id, |
c1c9c9c4 BP |
3264 | struct netdev_queue_stats *stats, |
3265 | void *cbdata_) | |
3266 | { | |
3267 | struct queue_stats_cbdata *cbdata = cbdata_; | |
3268 | ||
3269 | put_queue_stats(cbdata, queue_id, stats); | |
3270 | } | |
3271 | ||
0414d158 | 3272 | static enum ofperr |
ca0f572c | 3273 | handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id, |
c1c9c9c4 BP |
3274 | struct queue_stats_cbdata *cbdata) |
3275 | { | |
ca0f572c | 3276 | cbdata->ofport = port; |
c1c9c9c4 BP |
3277 | if (queue_id == OFPQ_ALL) { |
3278 | netdev_dump_queue_stats(port->netdev, | |
3279 | handle_queue_stats_dump_cb, cbdata); | |
3280 | } else { | |
3281 | struct netdev_queue_stats stats; | |
3282 | ||
1ac788f6 BP |
3283 | if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) { |
3284 | put_queue_stats(cbdata, queue_id, &stats); | |
0414d158 BP |
3285 | } else { |
3286 | return OFPERR_OFPQOFC_BAD_QUEUE; | |
1ac788f6 | 3287 | } |
c1c9c9c4 | 3288 | } |
0414d158 | 3289 | return 0; |
c1c9c9c4 BP |
3290 | } |
3291 | ||
90bf1e07 | 3292 | static enum ofperr |
63f2140a | 3293 | handle_queue_stats_request(struct ofconn *ofconn, |
982697a4 | 3294 | const struct ofp_header *rq) |
c1c9c9c4 | 3295 | { |
64ff1d96 | 3296 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
c1c9c9c4 | 3297 | struct queue_stats_cbdata cbdata; |
0414d158 | 3298 | struct ofport *port; |
0414d158 | 3299 | enum ofperr error; |
64626975 | 3300 | struct ofputil_queue_stats_request oqsr; |
c1c9c9c4 | 3301 | |
c1c9c9c4 BP |
3302 | COVERAGE_INC(ofproto_queue_req); |
3303 | ||
982697a4 | 3304 | ofpmp_init(&cbdata.replies, rq); |
6dc34a0d | 3305 | cbdata.now = time_msec(); |
c1c9c9c4 | 3306 | |
64626975 SH |
3307 | error = ofputil_decode_queue_stats_request(rq, &oqsr); |
3308 | if (error) { | |
3309 | return error; | |
3310 | } | |
3311 | ||
7f05e7ab | 3312 | if (oqsr.port_no == OFPP_ANY) { |
0414d158 | 3313 | error = OFPERR_OFPQOFC_BAD_QUEUE; |
4e8e4213 | 3314 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
64626975 | 3315 | if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) { |
0414d158 BP |
3316 | error = 0; |
3317 | } | |
c1c9c9c4 | 3318 | } |
0414d158 | 3319 | } else { |
64626975 | 3320 | port = ofproto_get_port(ofproto, oqsr.port_no); |
0414d158 | 3321 | error = (port |
64626975 | 3322 | ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata) |
0414d158 BP |
3323 | : OFPERR_OFPQOFC_BAD_PORT); |
3324 | } | |
3325 | if (!error) { | |
3326 | ofconn_send_replies(ofconn, &cbdata.replies); | |
c1c9c9c4 | 3327 | } else { |
63f2140a | 3328 | ofpbuf_list_delete(&cbdata.replies); |
c1c9c9c4 | 3329 | } |
c1c9c9c4 | 3330 | |
0414d158 | 3331 | return error; |
c1c9c9c4 | 3332 | } |
7ee20df1 BP |
3333 | |
3334 | static bool | |
3335 | is_flow_deletion_pending(const struct ofproto *ofproto, | |
3336 | const struct cls_rule *cls_rule, | |
3337 | uint8_t table_id) | |
3338 | { | |
3339 | if (!hmap_is_empty(&ofproto->deletions)) { | |
3340 | struct ofoperation *op; | |
3341 | ||
3342 | HMAP_FOR_EACH_WITH_HASH (op, hmap_node, | |
3343 | cls_rule_hash(cls_rule, table_id), | |
3344 | &ofproto->deletions) { | |
3345 | if (cls_rule_equal(cls_rule, &op->rule->cr)) { | |
3346 | return true; | |
3347 | } | |
3348 | } | |
3349 | } | |
3350 | ||
3351 | return false; | |
3352 | } | |
3353 | ||
8037acb4 BP |
3354 | static enum ofperr |
3355 | evict_rule_from_table(struct ofproto *ofproto, struct oftable *table) | |
3356 | { | |
3357 | struct rule *rule; | |
3358 | size_t n_rules; | |
3359 | ||
3360 | ovs_rwlock_rdlock(&table->cls.rwlock); | |
3361 | n_rules = classifier_count(&table->cls); | |
3362 | ovs_rwlock_unlock(&table->cls.rwlock); | |
3363 | ||
3364 | if (n_rules < table->max_flows) { | |
3365 | return 0; | |
3366 | } else if (!choose_rule_to_evict(table, &rule)) { | |
3367 | return OFPERR_OFPFMFC_TABLE_FULL; | |
3368 | } else if (rule->pending) { | |
3369 | ovs_rwlock_unlock(&rule->evict); | |
3370 | return OFPROTO_POSTPONE; | |
3371 | } else { | |
3372 | struct ofopgroup *group; | |
3373 | ||
3374 | group = ofopgroup_create_unattached(ofproto); | |
3375 | delete_flow__(rule, group, OFPRR_EVICTION); | |
3376 | ofopgroup_submit(group); | |
3377 | ||
3378 | return 0; | |
3379 | } | |
3380 | } | |
3381 | ||
79eee1eb BP |
3382 | /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT |
3383 | * in which no matching flow already exists in the flow table. | |
3384 | * | |
3385 | * Adds the flow specified by 'ofm', which is followed by 'n_actions' | |
75a75043 | 3386 | * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow |
90bf1e07 BP |
3387 | * error code on failure, or OFPROTO_POSTPONE if the operation cannot be |
3388 | * initiated now but may be retried later. | |
79eee1eb | 3389 | * |
f25d0cf3 BP |
3390 | * Upon successful return, takes ownership of 'fm->ofpacts'. On failure, |
3391 | * ownership remains with the caller. | |
3392 | * | |
79eee1eb BP |
3393 | * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id, |
3394 | * if any. */ | |
90bf1e07 | 3395 | static enum ofperr |
a9a2da38 | 3396 | add_flow(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 3397 | struct ofputil_flow_mod *fm, const struct ofp_header *request) |
064af421 | 3398 | { |
d0918789 | 3399 | struct oftable *table; |
7ee20df1 | 3400 | struct ofopgroup *group; |
8037acb4 | 3401 | struct cls_rule cr; |
064af421 | 3402 | struct rule *rule; |
5a361239 | 3403 | uint8_t table_id; |
064af421 BP |
3404 | int error; |
3405 | ||
48266274 BP |
3406 | error = check_table_id(ofproto, fm->table_id); |
3407 | if (error) { | |
3408 | return error; | |
3409 | } | |
3410 | ||
7ee20df1 BP |
3411 | /* Pick table. */ |
3412 | if (fm->table_id == 0xff) { | |
13521ff5 | 3413 | if (ofproto->ofproto_class->rule_choose_table) { |
81a76618 BP |
3414 | error = ofproto->ofproto_class->rule_choose_table(ofproto, |
3415 | &fm->match, | |
7ee20df1 BP |
3416 | &table_id); |
3417 | if (error) { | |
3418 | return error; | |
3419 | } | |
cb22974d | 3420 | ovs_assert(table_id < ofproto->n_tables); |
7ee20df1 | 3421 | } else { |
5a361239 | 3422 | table_id = 0; |
7ee20df1 BP |
3423 | } |
3424 | } else if (fm->table_id < ofproto->n_tables) { | |
5a361239 | 3425 | table_id = fm->table_id; |
7ee20df1 | 3426 | } else { |
c22c56bd | 3427 | return OFPERR_OFPBRC_BAD_TABLE_ID; |
064af421 BP |
3428 | } |
3429 | ||
5a361239 JR |
3430 | table = &ofproto->tables[table_id]; |
3431 | ||
5c67e4af BP |
3432 | if (table->flags & OFTABLE_READONLY) { |
3433 | return OFPERR_OFPBRC_EPERM; | |
3434 | } | |
3435 | ||
8037acb4 BP |
3436 | cls_rule_init(&cr, &fm->match, fm->priority); |
3437 | ||
b277c7cc BP |
3438 | /* Transform "add" into "modify" if there's an existing identical flow. */ |
3439 | ovs_rwlock_rdlock(&table->cls.rwlock); | |
8037acb4 | 3440 | rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr)); |
b277c7cc BP |
3441 | ovs_rwlock_unlock(&table->cls.rwlock); |
3442 | if (rule) { | |
8037acb4 | 3443 | cls_rule_destroy(&cr); |
b277c7cc BP |
3444 | if (!rule_is_modifiable(rule)) { |
3445 | return OFPERR_OFPBRC_EPERM; | |
3446 | } else if (rule->pending) { | |
3447 | return OFPROTO_POSTPONE; | |
3448 | } else { | |
3449 | struct list rules; | |
3450 | ||
3451 | list_init(&rules); | |
3452 | list_push_back(&rules, &rule->ofproto_node); | |
3453 | fm->modify_cookie = true; | |
3454 | return modify_flows__(ofproto, ofconn, fm, request, &rules); | |
3455 | } | |
3456 | } | |
3457 | ||
e3b56933 JR |
3458 | /* Verify actions. */ |
3459 | error = ofproto_check_ofpacts(ofproto, fm->ofpacts, fm->ofpacts_len, | |
3460 | &fm->match.flow, table_id); | |
3461 | if (error) { | |
3462 | return error; | |
3463 | } | |
3464 | ||
7ee20df1 | 3465 | /* Serialize against pending deletion. */ |
8037acb4 BP |
3466 | if (is_flow_deletion_pending(ofproto, &cr, table_id)) { |
3467 | cls_rule_destroy(&cr); | |
7ee20df1 | 3468 | return OFPROTO_POSTPONE; |
afe75089 | 3469 | } |
064af421 | 3470 | |
81a76618 | 3471 | /* Check for overlap, if requested. */ |
0fb88c18 | 3472 | if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) { |
c09f755d BP |
3473 | bool overlaps; |
3474 | ||
3475 | ovs_rwlock_rdlock(&table->cls.rwlock); | |
8037acb4 | 3476 | overlaps = classifier_rule_overlaps(&table->cls, &cr); |
c09f755d BP |
3477 | ovs_rwlock_unlock(&table->cls.rwlock); |
3478 | ||
3479 | if (overlaps) { | |
8037acb4 | 3480 | cls_rule_destroy(&cr); |
c09f755d BP |
3481 | return OFPERR_OFPFMFC_OVERLAP; |
3482 | } | |
7ee20df1 | 3483 | } |
81a76618 | 3484 | |
8037acb4 BP |
3485 | /* If necessary, evict an existing rule to clear out space. */ |
3486 | error = evict_rule_from_table(ofproto, table); | |
3487 | if (error) { | |
3488 | cls_rule_destroy(&cr); | |
3489 | return error; | |
3490 | } | |
2e1ae200 | 3491 | |
8037acb4 BP |
3492 | /* Allocate new rule. */ |
3493 | rule = ofproto->ofproto_class->rule_alloc(); | |
3494 | if (!rule) { | |
3495 | cls_rule_destroy(&cr); | |
3496 | VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)", | |
3497 | ofproto->name, ovs_strerror(error)); | |
3498 | return ENOMEM; | |
3499 | } | |
3500 | ||
3501 | /* Initialize base state. */ | |
7ee20df1 | 3502 | rule->ofproto = ofproto; |
8037acb4 | 3503 | cls_rule_move(&rule->cr, &cr); |
7ee20df1 | 3504 | rule->pending = NULL; |
623e1caf | 3505 | rule->flow_cookie = fm->new_cookie; |
1745cd08 | 3506 | rule->created = rule->modified = rule->used = time_msec(); |
a3779dbc | 3507 | |
834d6caf | 3508 | ovs_mutex_init(&rule->timeout_mutex); |
a3779dbc | 3509 | ovs_mutex_lock(&rule->timeout_mutex); |
7ee20df1 BP |
3510 | rule->idle_timeout = fm->idle_timeout; |
3511 | rule->hard_timeout = fm->hard_timeout; | |
a3779dbc EJ |
3512 | ovs_mutex_unlock(&rule->timeout_mutex); |
3513 | ||
7ee20df1 | 3514 | rule->table_id = table - ofproto->tables; |
0fb88c18 | 3515 | rule->send_flow_removed = (fm->flags & OFPUTIL_FF_SEND_FLOW_REM) != 0; |
f25d0cf3 BP |
3516 | rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len); |
3517 | rule->ofpacts_len = fm->ofpacts_len; | |
9cae45dc JR |
3518 | rule->meter_id = find_meter(rule->ofpacts, rule->ofpacts_len); |
3519 | list_init(&rule->meter_list_node); | |
254750ce | 3520 | rule->eviction_group = NULL; |
e503cc19 | 3521 | list_init(&rule->expirable); |
2b07c8b1 BP |
3522 | rule->monitor_flags = 0; |
3523 | rule->add_seqno = 0; | |
3524 | rule->modify_seqno = 0; | |
ad3efdcb | 3525 | ovs_rwlock_init(&rule->evict); |
7ee20df1 | 3526 | |
8037acb4 | 3527 | /* Construct rule, initializing derived state. */ |
b277c7cc BP |
3528 | error = ofproto->ofproto_class->rule_construct(rule); |
3529 | if (error) { | |
8037acb4 BP |
3530 | ofproto_rule_destroy__(rule); |
3531 | return error; | |
064af421 | 3532 | } |
8037acb4 BP |
3533 | |
3534 | /* Insert rule. */ | |
3535 | oftable_insert_rule(rule); | |
3536 | ||
3537 | group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id); | |
3538 | ofoperation_create(group, rule, OFOPERATION_ADD, 0); | |
3539 | ofproto->ofproto_class->rule_insert(rule); | |
b277c7cc | 3540 | ofopgroup_submit(group); |
79eee1eb | 3541 | |
7ee20df1 | 3542 | return error; |
064af421 | 3543 | } |
79eee1eb BP |
3544 | \f |
3545 | /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */ | |
064af421 | 3546 | |
9ed18e46 BP |
3547 | /* Modifies the rules listed in 'rules', changing their actions to match those |
3548 | * in 'fm'. | |
79eee1eb | 3549 | * |
9ed18e46 BP |
3550 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, |
3551 | * if any. | |
3552 | * | |
3553 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
90bf1e07 | 3554 | static enum ofperr |
7024dffe | 3555 | modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 3556 | struct ofputil_flow_mod *fm, const struct ofp_header *request, |
9cae45dc | 3557 | struct list *rules) |
79eee1eb | 3558 | { |
b277c7cc | 3559 | enum ofoperation_type type; |
7ee20df1 | 3560 | struct ofopgroup *group; |
9ed18e46 | 3561 | struct rule *rule; |
5c67e4af | 3562 | enum ofperr error; |
79eee1eb | 3563 | |
b277c7cc | 3564 | type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY; |
7024dffe | 3565 | group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id); |
5c67e4af | 3566 | error = OFPERR_OFPBRC_EPERM; |
9ed18e46 | 3567 | LIST_FOR_EACH (rule, ofproto_node, rules) { |
08043761 BP |
3568 | struct ofoperation *op; |
3569 | bool actions_changed; | |
b277c7cc | 3570 | bool reset_counters; |
08043761 | 3571 | |
0fb88c18 | 3572 | /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */ |
2e1ae200 | 3573 | |
5c67e4af BP |
3574 | if (rule_is_modifiable(rule)) { |
3575 | /* At least one rule is modifiable, don't report EPERM error. */ | |
3576 | error = 0; | |
3577 | } else { | |
3578 | continue; | |
3579 | } | |
3580 | ||
e3b56933 JR |
3581 | /* Verify actions. */ |
3582 | error = ofpacts_check(fm->ofpacts, fm->ofpacts_len, &fm->match.flow, | |
430dbb14 | 3583 | u16_to_ofp(ofproto->max_ports), rule->table_id); |
e3b56933 JR |
3584 | if (error) { |
3585 | return error; | |
3586 | } | |
3587 | ||
08043761 BP |
3588 | actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len, |
3589 | rule->ofpacts, rule->ofpacts_len); | |
a7d94793 | 3590 | |
b277c7cc | 3591 | op = ofoperation_create(group, rule, type, 0); |
98eaac36 | 3592 | |
23342857 | 3593 | if (fm->modify_cookie && fm->new_cookie != htonll(UINT64_MAX)) { |
98eaac36 JR |
3594 | ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie); |
3595 | } | |
b277c7cc BP |
3596 | if (type == OFOPERATION_REPLACE) { |
3597 | ovs_mutex_lock(&rule->timeout_mutex); | |
3598 | rule->idle_timeout = fm->idle_timeout; | |
3599 | rule->hard_timeout = fm->hard_timeout; | |
3600 | ovs_mutex_unlock(&rule->timeout_mutex); | |
3601 | ||
3602 | rule->send_flow_removed = (fm->flags | |
3603 | & OFPUTIL_FF_SEND_FLOW_REM) != 0; | |
3604 | ||
3605 | if (fm->idle_timeout || fm->hard_timeout) { | |
3606 | if (!rule->eviction_group) { | |
3607 | eviction_group_add_rule(rule); | |
3608 | } | |
3609 | } else { | |
3610 | eviction_group_remove_rule(rule); | |
3611 | } | |
3612 | } | |
3613 | ||
3614 | reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0; | |
3615 | if (actions_changed || reset_counters) { | |
a7d94793 BP |
3616 | op->ofpacts = rule->ofpacts; |
3617 | op->ofpacts_len = rule->ofpacts_len; | |
9cae45dc | 3618 | op->meter_id = rule->meter_id; |
f25d0cf3 BP |
3619 | rule->ofpacts = xmemdup(fm->ofpacts, fm->ofpacts_len); |
3620 | rule->ofpacts_len = fm->ofpacts_len; | |
9cae45dc | 3621 | rule->meter_id = find_meter(rule->ofpacts, rule->ofpacts_len); |
b277c7cc BP |
3622 | rule->ofproto->ofproto_class->rule_modify_actions(rule, |
3623 | reset_counters); | |
308881af | 3624 | } else { |
08043761 | 3625 | ofoperation_complete(op, 0); |
623e1caf | 3626 | } |
5ecc9d81 | 3627 | } |
7ee20df1 | 3628 | ofopgroup_submit(group); |
79eee1eb | 3629 | |
5c67e4af | 3630 | return error; |
9ed18e46 BP |
3631 | } |
3632 | ||
9387b970 SH |
3633 | static enum ofperr |
3634 | modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn, | |
e3b56933 | 3635 | struct ofputil_flow_mod *fm, const struct ofp_header *request) |
9387b970 SH |
3636 | { |
3637 | if (fm->cookie_mask != htonll(0) || fm->new_cookie == htonll(UINT64_MAX)) { | |
3638 | return 0; | |
3639 | } | |
3640 | return add_flow(ofproto, ofconn, fm, request); | |
3641 | } | |
3642 | ||
90bf1e07 BP |
3643 | /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on |
3644 | * failure. | |
9ed18e46 BP |
3645 | * |
3646 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, | |
3647 | * if any. */ | |
90bf1e07 | 3648 | static enum ofperr |
7024dffe | 3649 | modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 3650 | struct ofputil_flow_mod *fm, |
7ee20df1 | 3651 | const struct ofp_header *request) |
9ed18e46 | 3652 | { |
9ed18e46 BP |
3653 | struct list rules; |
3654 | int error; | |
3655 | ||
81a76618 | 3656 | error = collect_rules_loose(ofproto, fm->table_id, &fm->match, |
e729e793 | 3657 | fm->cookie, fm->cookie_mask, |
7f05e7ab | 3658 | OFPP_ANY, &rules); |
623e1caf JP |
3659 | if (error) { |
3660 | return error; | |
3661 | } else if (list_is_empty(&rules)) { | |
9387b970 | 3662 | return modify_flows_add(ofproto, ofconn, fm, request); |
623e1caf JP |
3663 | } else { |
3664 | return modify_flows__(ofproto, ofconn, fm, request, &rules); | |
3665 | } | |
79eee1eb BP |
3666 | } |
3667 | ||
3668 | /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error | |
90bf1e07 | 3669 | * code on failure. |
79eee1eb | 3670 | * |
9ed18e46 | 3671 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, |
79eee1eb | 3672 | * if any. */ |
90bf1e07 | 3673 | static enum ofperr |
7024dffe | 3674 | modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 3675 | struct ofputil_flow_mod *fm, |
7ee20df1 | 3676 | const struct ofp_header *request) |
79eee1eb | 3677 | { |
9ed18e46 | 3678 | struct list rules; |
6c1491fb BP |
3679 | int error; |
3680 | ||
81a76618 BP |
3681 | error = collect_rules_strict(ofproto, fm->table_id, &fm->match, |
3682 | fm->priority, fm->cookie, fm->cookie_mask, | |
7f05e7ab | 3683 | OFPP_ANY, &rules); |
623e1caf JP |
3684 | |
3685 | if (error) { | |
3686 | return error; | |
3687 | } else if (list_is_empty(&rules)) { | |
9387b970 | 3688 | return modify_flows_add(ofproto, ofconn, fm, request); |
623e1caf JP |
3689 | } else { |
3690 | return list_is_singleton(&rules) ? modify_flows__(ofproto, ofconn, | |
3691 | fm, request, &rules) | |
3692 | : 0; | |
3693 | } | |
79eee1eb | 3694 | } |
9ed18e46 BP |
3695 | \f |
3696 | /* OFPFC_DELETE implementation. */ | |
79eee1eb | 3697 | |
254750ce | 3698 | static void |
ca311b2f JR |
3699 | delete_flow__(struct rule *rule, struct ofopgroup *group, |
3700 | enum ofp_flow_removed_reason reason) | |
254750ce BP |
3701 | { |
3702 | struct ofproto *ofproto = rule->ofproto; | |
3703 | ||
ca311b2f | 3704 | ofproto_rule_send_removed(rule, reason); |
254750ce | 3705 | |
ca311b2f | 3706 | ofoperation_create(group, rule, OFOPERATION_DELETE, reason); |
254750ce | 3707 | oftable_remove_rule(rule); |
8037acb4 | 3708 | ofproto->ofproto_class->rule_delete(rule); |
254750ce BP |
3709 | } |
3710 | ||
9ed18e46 BP |
3711 | /* Deletes the rules listed in 'rules'. |
3712 | * | |
3713 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
90bf1e07 | 3714 | static enum ofperr |
7024dffe | 3715 | delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn, |
ca311b2f JR |
3716 | const struct ofp_header *request, struct list *rules, |
3717 | enum ofp_flow_removed_reason reason) | |
064af421 | 3718 | { |
9ed18e46 | 3719 | struct rule *rule, *next; |
7ee20df1 | 3720 | struct ofopgroup *group; |
79eee1eb | 3721 | |
7024dffe | 3722 | group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX); |
9ed18e46 | 3723 | LIST_FOR_EACH_SAFE (rule, next, ofproto_node, rules) { |
ad3efdcb | 3724 | ovs_rwlock_wrlock(&rule->evict); |
ca311b2f | 3725 | delete_flow__(rule, group, reason); |
79eee1eb | 3726 | } |
7ee20df1 | 3727 | ofopgroup_submit(group); |
79eee1eb | 3728 | |
9ed18e46 | 3729 | return 0; |
79eee1eb | 3730 | } |
79eee1eb BP |
3731 | |
3732 | /* Implements OFPFC_DELETE. */ | |
90bf1e07 | 3733 | static enum ofperr |
7024dffe BP |
3734 | delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn, |
3735 | const struct ofputil_flow_mod *fm, | |
7ee20df1 | 3736 | const struct ofp_header *request) |
79eee1eb | 3737 | { |
9ed18e46 | 3738 | struct list rules; |
90bf1e07 | 3739 | enum ofperr error; |
064af421 | 3740 | |
81a76618 | 3741 | error = collect_rules_loose(ofproto, fm->table_id, &fm->match, |
e729e793 JP |
3742 | fm->cookie, fm->cookie_mask, |
3743 | fm->out_port, &rules); | |
9ed18e46 | 3744 | return (error ? error |
7024dffe | 3745 | : !list_is_empty(&rules) ? delete_flows__(ofproto, ofconn, request, |
ca311b2f | 3746 | &rules, OFPRR_DELETE) |
9ed18e46 | 3747 | : 0); |
064af421 BP |
3748 | } |
3749 | ||
79eee1eb | 3750 | /* Implements OFPFC_DELETE_STRICT. */ |
90bf1e07 | 3751 | static enum ofperr |
7024dffe | 3752 | delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn, |
1b9d6420 | 3753 | const struct ofputil_flow_mod *fm, |
7ee20df1 | 3754 | const struct ofp_header *request) |
79eee1eb | 3755 | { |
9ed18e46 | 3756 | struct list rules; |
90bf1e07 | 3757 | enum ofperr error; |
79eee1eb | 3758 | |
81a76618 BP |
3759 | error = collect_rules_strict(ofproto, fm->table_id, &fm->match, |
3760 | fm->priority, fm->cookie, fm->cookie_mask, | |
e729e793 | 3761 | fm->out_port, &rules); |
9ed18e46 | 3762 | return (error ? error |
7024dffe | 3763 | : list_is_singleton(&rules) ? delete_flows__(ofproto, ofconn, |
ca311b2f JR |
3764 | request, &rules, |
3765 | OFPRR_DELETE) | |
9ed18e46 | 3766 | : 0); |
abe529af BP |
3767 | } |
3768 | ||
3769 | static void | |
3770 | ofproto_rule_send_removed(struct rule *rule, uint8_t reason) | |
3771 | { | |
3772 | struct ofputil_flow_removed fr; | |
3773 | ||
2b07c8b1 | 3774 | if (ofproto_rule_is_hidden(rule) || !rule->send_flow_removed) { |
abe529af BP |
3775 | return; |
3776 | } | |
3777 | ||
5cb7a798 | 3778 | minimatch_expand(&rule->cr.match, &fr.match); |
81a76618 | 3779 | fr.priority = rule->cr.priority; |
abe529af BP |
3780 | fr.cookie = rule->flow_cookie; |
3781 | fr.reason = reason; | |
95216219 | 3782 | fr.table_id = rule->table_id; |
65e0be10 BP |
3783 | calc_duration(rule->created, time_msec(), |
3784 | &fr.duration_sec, &fr.duration_nsec); | |
a3779dbc | 3785 | ovs_mutex_lock(&rule->timeout_mutex); |
abe529af | 3786 | fr.idle_timeout = rule->idle_timeout; |
fa2bad0f | 3787 | fr.hard_timeout = rule->hard_timeout; |
a3779dbc | 3788 | ovs_mutex_unlock(&rule->timeout_mutex); |
abe529af BP |
3789 | rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count, |
3790 | &fr.byte_count); | |
3791 | ||
3792 | connmgr_send_flow_removed(rule->ofproto->connmgr, &fr); | |
3793 | } | |
3794 | ||
1745cd08 BP |
3795 | void |
3796 | ofproto_rule_update_used(struct rule *rule, long long int used) | |
3797 | { | |
3798 | if (used > rule->used) { | |
254750ce BP |
3799 | struct eviction_group *evg = rule->eviction_group; |
3800 | ||
1745cd08 | 3801 | rule->used = used; |
254750ce BP |
3802 | if (evg) { |
3803 | heap_change(&evg->rules, &rule->evg_node, | |
3804 | rule_eviction_priority(rule)); | |
3805 | } | |
1745cd08 BP |
3806 | } |
3807 | } | |
3808 | ||
abe529af BP |
3809 | /* Sends an OpenFlow "flow removed" message with the given 'reason' (either |
3810 | * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its | |
3811 | * ofproto. | |
3812 | * | |
e2a3d183 BP |
3813 | * 'rule' must not have a pending operation (that is, 'rule->pending' must be |
3814 | * NULL). | |
3815 | * | |
abe529af BP |
3816 | * ofproto implementation ->run() functions should use this function to expire |
3817 | * OpenFlow flows. */ | |
3818 | void | |
3819 | ofproto_rule_expire(struct rule *rule, uint8_t reason) | |
3820 | { | |
7ee20df1 | 3821 | struct ofproto *ofproto = rule->ofproto; |
8037acb4 | 3822 | struct classifier *cls = &ofproto->tables[rule->table_id].cls; |
7ee20df1 | 3823 | |
cb22974d | 3824 | ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT); |
abe529af | 3825 | ofproto_rule_send_removed(rule, reason); |
7ee20df1 | 3826 | |
8037acb4 BP |
3827 | ovs_rwlock_wrlock(&cls->rwlock); |
3828 | ofproto_delete_rule(ofproto, cls, rule); | |
3829 | ovs_rwlock_unlock(&cls->rwlock); | |
79eee1eb | 3830 | } |
994c9973 BP |
3831 | |
3832 | /* Reduces '*timeout' to no more than 'max'. A value of zero in either case | |
3833 | * means "infinite". */ | |
3834 | static void | |
3835 | reduce_timeout(uint16_t max, uint16_t *timeout) | |
3836 | { | |
3837 | if (max && (!*timeout || *timeout > max)) { | |
3838 | *timeout = max; | |
3839 | } | |
3840 | } | |
3841 | ||
3842 | /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle | |
3843 | * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to | |
3844 | * 'idle_timeout' seconds. Similarly for 'hard_timeout'. | |
3845 | * | |
3846 | * Suitable for implementing OFPACT_FIN_TIMEOUT. */ | |
3847 | void | |
3848 | ofproto_rule_reduce_timeouts(struct rule *rule, | |
3849 | uint16_t idle_timeout, uint16_t hard_timeout) | |
3850 | OVS_EXCLUDED(rule->ofproto->expirable_mutex, rule->timeout_mutex) | |
3851 | { | |
3852 | if (!idle_timeout && !hard_timeout) { | |
3853 | return; | |
3854 | } | |
3855 | ||
3856 | ovs_mutex_lock(&rule->ofproto->expirable_mutex); | |
3857 | if (list_is_empty(&rule->expirable)) { | |
3858 | list_insert(&rule->ofproto->expirable, &rule->expirable); | |
3859 | } | |
3860 | ovs_mutex_unlock(&rule->ofproto->expirable_mutex); | |
3861 | ||
3862 | ovs_mutex_lock(&rule->timeout_mutex); | |
3863 | reduce_timeout(idle_timeout, &rule->idle_timeout); | |
3864 | reduce_timeout(hard_timeout, &rule->hard_timeout); | |
3865 | ovs_mutex_unlock(&rule->timeout_mutex); | |
3866 | ||
3867 | if (!rule->eviction_group) { | |
3868 | eviction_group_add_rule(rule); | |
3869 | } | |
3870 | } | |
79eee1eb | 3871 | \f |
90bf1e07 | 3872 | static enum ofperr |
2e4f5fcf | 3873 | handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3874 | { |
a5b8d268 | 3875 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
a9a2da38 | 3876 | struct ofputil_flow_mod fm; |
f25d0cf3 BP |
3877 | uint64_t ofpacts_stub[1024 / 8]; |
3878 | struct ofpbuf ofpacts; | |
90bf1e07 | 3879 | enum ofperr error; |
a5b8d268 | 3880 | long long int now; |
064af421 | 3881 | |
76589937 | 3882 | error = reject_slave_controller(ofconn); |
9deba63b | 3883 | if (error) { |
f25d0cf3 | 3884 | goto exit; |
9deba63b | 3885 | } |
3052b0c5 | 3886 | |
f25d0cf3 BP |
3887 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
3888 | error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn), | |
3889 | &ofpacts); | |
548de4dd | 3890 | if (!error) { |
f1822532 | 3891 | error = handle_flow_mod__(ofproto, ofconn, &fm, oh); |
49bdc010 | 3892 | } |
a5b8d268 | 3893 | if (error) { |
f25d0cf3 | 3894 | goto exit_free_ofpacts; |
a5b8d268 | 3895 | } |
2e310801 | 3896 | |
a5b8d268 BP |
3897 | /* Record the operation for logging a summary report. */ |
3898 | switch (fm.command) { | |
3899 | case OFPFC_ADD: | |
3900 | ofproto->n_add++; | |
3901 | break; | |
3902 | ||
3903 | case OFPFC_MODIFY: | |
3904 | case OFPFC_MODIFY_STRICT: | |
3905 | ofproto->n_modify++; | |
3906 | break; | |
3907 | ||
3908 | case OFPFC_DELETE: | |
3909 | case OFPFC_DELETE_STRICT: | |
3910 | ofproto->n_delete++; | |
3911 | break; | |
3912 | } | |
3913 | ||
3914 | now = time_msec(); | |
3915 | if (ofproto->next_op_report == LLONG_MAX) { | |
3916 | ofproto->first_op = now; | |
3917 | ofproto->next_op_report = MAX(now + 10 * 1000, | |
3918 | ofproto->op_backoff); | |
3919 | ofproto->op_backoff = ofproto->next_op_report + 60 * 1000; | |
3920 | } | |
3921 | ofproto->last_op = now; | |
3922 | ||
f25d0cf3 BP |
3923 | exit_free_ofpacts: |
3924 | ofpbuf_uninit(&ofpacts); | |
3925 | exit: | |
3926 | return error; | |
75a75043 BP |
3927 | } |
3928 | ||
90bf1e07 | 3929 | static enum ofperr |
75a75043 | 3930 | handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 3931 | struct ofputil_flow_mod *fm, const struct ofp_header *oh) |
75a75043 BP |
3932 | { |
3933 | if (ofproto->n_pending >= 50) { | |
cb22974d | 3934 | ovs_assert(!list_is_empty(&ofproto->pending)); |
75a75043 BP |
3935 | return OFPROTO_POSTPONE; |
3936 | } | |
3937 | ||
3938 | switch (fm->command) { | |
3052b0c5 | 3939 | case OFPFC_ADD: |
75a75043 | 3940 | return add_flow(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
3941 | |
3942 | case OFPFC_MODIFY: | |
75a75043 | 3943 | return modify_flows_loose(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
3944 | |
3945 | case OFPFC_MODIFY_STRICT: | |
75a75043 | 3946 | return modify_flow_strict(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
3947 | |
3948 | case OFPFC_DELETE: | |
75a75043 | 3949 | return delete_flows_loose(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
3950 | |
3951 | case OFPFC_DELETE_STRICT: | |
75a75043 | 3952 | return delete_flow_strict(ofproto, ofconn, fm, oh); |
3052b0c5 BP |
3953 | |
3954 | default: | |
75a75043 | 3955 | if (fm->command > 0xff) { |
fbfa2911 BP |
3956 | VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but " |
3957 | "flow_mod_table_id extension is not enabled", | |
3958 | ofproto->name); | |
6c1491fb | 3959 | } |
90bf1e07 | 3960 | return OFPERR_OFPFMFC_BAD_COMMAND; |
3052b0c5 BP |
3961 | } |
3962 | } | |
3963 | ||
90bf1e07 | 3964 | static enum ofperr |
d1e2cf21 | 3965 | handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh) |
9deba63b | 3966 | { |
f4f1ea7e BP |
3967 | struct ofputil_role_request request; |
3968 | struct ofputil_role_request reply; | |
9deba63b | 3969 | struct ofpbuf *buf; |
6ea4776b JR |
3970 | enum ofperr error; |
3971 | ||
f4f1ea7e | 3972 | error = ofputil_decode_role_message(oh, &request); |
6ea4776b JR |
3973 | if (error) { |
3974 | return error; | |
3975 | } | |
9deba63b | 3976 | |
f4f1ea7e BP |
3977 | if (request.role != OFPCR12_ROLE_NOCHANGE) { |
3978 | if (ofconn_get_role(ofconn) != request.role | |
3979 | && ofconn_has_pending_opgroups(ofconn)) { | |
3980 | return OFPROTO_POSTPONE; | |
3981 | } | |
7ee20df1 | 3982 | |
f4f1ea7e BP |
3983 | if (request.have_generation_id |
3984 | && !ofconn_set_master_election_id(ofconn, request.generation_id)) { | |
3985 | return OFPERR_OFPRRFC_STALE; | |
6ea4776b | 3986 | } |
6ea4776b | 3987 | |
f4f1ea7e BP |
3988 | ofconn_set_role(ofconn, request.role); |
3989 | } | |
9deba63b | 3990 | |
f4f1ea7e | 3991 | reply.role = ofconn_get_role(ofconn); |
147cc9d3 BP |
3992 | reply.have_generation_id = ofconn_get_master_election_id( |
3993 | ofconn, &reply.generation_id); | |
f4f1ea7e | 3994 | buf = ofputil_encode_role_reply(oh, &reply); |
b0421aa2 | 3995 | ofconn_send_reply(ofconn, buf); |
9deba63b BP |
3996 | |
3997 | return 0; | |
3998 | } | |
3999 | ||
90bf1e07 | 4000 | static enum ofperr |
6c1491fb BP |
4001 | handle_nxt_flow_mod_table_id(struct ofconn *ofconn, |
4002 | const struct ofp_header *oh) | |
4003 | { | |
982697a4 | 4004 | const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh); |
27527aa0 BP |
4005 | enum ofputil_protocol cur, next; |
4006 | ||
4007 | cur = ofconn_get_protocol(ofconn); | |
4008 | next = ofputil_protocol_set_tid(cur, msg->set != 0); | |
4009 | ofconn_set_protocol(ofconn, next); | |
6c1491fb | 4010 | |
6c1491fb BP |
4011 | return 0; |
4012 | } | |
4013 | ||
90bf1e07 | 4014 | static enum ofperr |
d1e2cf21 | 4015 | handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh) |
09246b99 | 4016 | { |
982697a4 | 4017 | const struct nx_set_flow_format *msg = ofpmsg_body(oh); |
27527aa0 BP |
4018 | enum ofputil_protocol cur, next; |
4019 | enum ofputil_protocol next_base; | |
09246b99 | 4020 | |
27527aa0 BP |
4021 | next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format)); |
4022 | if (!next_base) { | |
90bf1e07 | 4023 | return OFPERR_OFPBRC_EPERM; |
09246b99 | 4024 | } |
7ee20df1 | 4025 | |
27527aa0 BP |
4026 | cur = ofconn_get_protocol(ofconn); |
4027 | next = ofputil_protocol_set_base(cur, next_base); | |
4028 | if (cur != next && ofconn_has_pending_opgroups(ofconn)) { | |
4029 | /* Avoid sending async messages in surprising protocol. */ | |
7ee20df1 BP |
4030 | return OFPROTO_POSTPONE; |
4031 | } | |
4032 | ||
27527aa0 | 4033 | ofconn_set_protocol(ofconn, next); |
7ee20df1 | 4034 | return 0; |
09246b99 BP |
4035 | } |
4036 | ||
90bf1e07 | 4037 | static enum ofperr |
54834960 EJ |
4038 | handle_nxt_set_packet_in_format(struct ofconn *ofconn, |
4039 | const struct ofp_header *oh) | |
4040 | { | |
982697a4 | 4041 | const struct nx_set_packet_in_format *msg = ofpmsg_body(oh); |
54834960 EJ |
4042 | uint32_t format; |
4043 | ||
54834960 | 4044 | format = ntohl(msg->format); |
a15f0eeb | 4045 | if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) { |
90bf1e07 | 4046 | return OFPERR_OFPBRC_EPERM; |
54834960 EJ |
4047 | } |
4048 | ||
4049 | if (format != ofconn_get_packet_in_format(ofconn) | |
4050 | && ofconn_has_pending_opgroups(ofconn)) { | |
4051 | /* Avoid sending async message in surprsing packet in format. */ | |
4052 | return OFPROTO_POSTPONE; | |
4053 | } | |
4054 | ||
4055 | ofconn_set_packet_in_format(ofconn, format); | |
4056 | return 0; | |
4057 | } | |
4058 | ||
80d5aefd BP |
4059 | static enum ofperr |
4060 | handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh) | |
4061 | { | |
982697a4 | 4062 | const struct nx_async_config *msg = ofpmsg_body(oh); |
80d5aefd BP |
4063 | uint32_t master[OAM_N_TYPES]; |
4064 | uint32_t slave[OAM_N_TYPES]; | |
4065 | ||
4066 | master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]); | |
4067 | master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]); | |
4068 | master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]); | |
4069 | ||
4070 | slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]); | |
4071 | slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]); | |
4072 | slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]); | |
4073 | ||
4074 | ofconn_set_async_config(ofconn, master, slave); | |
4550b647 MM |
4075 | if (ofconn_get_type(ofconn) == OFCONN_SERVICE && |
4076 | !ofconn_get_miss_send_len(ofconn)) { | |
4077 | ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN); | |
4078 | } | |
80d5aefd BP |
4079 | |
4080 | return 0; | |
4081 | } | |
4082 | ||
a7349929 BP |
4083 | static enum ofperr |
4084 | handle_nxt_set_controller_id(struct ofconn *ofconn, | |
4085 | const struct ofp_header *oh) | |
4086 | { | |
982697a4 | 4087 | const struct nx_controller_id *nci = ofpmsg_body(oh); |
a7349929 | 4088 | |
a7349929 BP |
4089 | if (!is_all_zeros(nci->zero, sizeof nci->zero)) { |
4090 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
4091 | } | |
4092 | ||
4093 | ofconn_set_controller_id(ofconn, ntohs(nci->controller_id)); | |
4094 | return 0; | |
4095 | } | |
4096 | ||
90bf1e07 | 4097 | static enum ofperr |
d1e2cf21 | 4098 | handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh) |
246e61ea | 4099 | { |
246e61ea JP |
4100 | struct ofpbuf *buf; |
4101 | ||
7ee20df1 BP |
4102 | if (ofconn_has_pending_opgroups(ofconn)) { |
4103 | return OFPROTO_POSTPONE; | |
4104 | } | |
4105 | ||
982697a4 BP |
4106 | buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION |
4107 | ? OFPRAW_OFPT10_BARRIER_REPLY | |
4108 | : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0); | |
b0421aa2 | 4109 | ofconn_send_reply(ofconn, buf); |
246e61ea JP |
4110 | return 0; |
4111 | } | |
4112 | ||
2b07c8b1 BP |
4113 | static void |
4114 | ofproto_compose_flow_refresh_update(const struct rule *rule, | |
4115 | enum nx_flow_monitor_flags flags, | |
4116 | struct list *msgs) | |
4117 | { | |
4118 | struct ofoperation *op = rule->pending; | |
4119 | struct ofputil_flow_update fu; | |
5cb7a798 | 4120 | struct match match; |
2b07c8b1 | 4121 | |
b277c7cc | 4122 | if (op && op->type == OFOPERATION_ADD) { |
2b07c8b1 BP |
4123 | /* We'll report the final flow when the operation completes. Reporting |
4124 | * it now would cause a duplicate report later. */ | |
4125 | return; | |
4126 | } | |
4127 | ||
4128 | fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD) | |
4129 | ? NXFME_ADDED : NXFME_MODIFIED); | |
4130 | fu.reason = 0; | |
a3779dbc | 4131 | ovs_mutex_lock(&rule->timeout_mutex); |
2b07c8b1 BP |
4132 | fu.idle_timeout = rule->idle_timeout; |
4133 | fu.hard_timeout = rule->hard_timeout; | |
a3779dbc | 4134 | ovs_mutex_unlock(&rule->timeout_mutex); |
2b07c8b1 BP |
4135 | fu.table_id = rule->table_id; |
4136 | fu.cookie = rule->flow_cookie; | |
5cb7a798 BP |
4137 | minimatch_expand(&rule->cr.match, &match); |
4138 | fu.match = &match; | |
6b140a4e | 4139 | fu.priority = rule->cr.priority; |
2b07c8b1 BP |
4140 | if (!(flags & NXFMF_ACTIONS)) { |
4141 | fu.ofpacts = NULL; | |
4142 | fu.ofpacts_len = 0; | |
4143 | } else if (!op) { | |
4144 | fu.ofpacts = rule->ofpacts; | |
4145 | fu.ofpacts_len = rule->ofpacts_len; | |
4146 | } else { | |
4147 | /* An operation is in progress. Use the previous version of the flow's | |
4148 | * actions, so that when the operation commits we report the change. */ | |
4149 | switch (op->type) { | |
4150 | case OFOPERATION_ADD: | |
b277c7cc | 4151 | NOT_REACHED(); |
2b07c8b1 BP |
4152 | |
4153 | case OFOPERATION_MODIFY: | |
b277c7cc | 4154 | case OFOPERATION_REPLACE: |
2b07c8b1 BP |
4155 | if (op->ofpacts) { |
4156 | fu.ofpacts = op->ofpacts; | |
4157 | fu.ofpacts_len = op->ofpacts_len; | |
4158 | } else { | |
4159 | fu.ofpacts = rule->ofpacts; | |
4160 | fu.ofpacts_len = rule->ofpacts_len; | |
4161 | } | |
4162 | break; | |
4163 | ||
4164 | case OFOPERATION_DELETE: | |
4165 | fu.ofpacts = rule->ofpacts; | |
4166 | fu.ofpacts_len = rule->ofpacts_len; | |
4167 | break; | |
4168 | ||
4169 | default: | |
4170 | NOT_REACHED(); | |
4171 | } | |
4172 | } | |
4173 | ||
4174 | if (list_is_empty(msgs)) { | |
4175 | ofputil_start_flow_update(msgs); | |
4176 | } | |
4177 | ofputil_append_flow_update(&fu, msgs); | |
4178 | } | |
4179 | ||
4180 | void | |
4181 | ofmonitor_compose_refresh_updates(struct list *rules, struct list *msgs) | |
4182 | { | |
4183 | struct rule *rule; | |
4184 | ||
4185 | LIST_FOR_EACH (rule, ofproto_node, rules) { | |
4186 | enum nx_flow_monitor_flags flags = rule->monitor_flags; | |
4187 | rule->monitor_flags = 0; | |
4188 | ||
4189 | ofproto_compose_flow_refresh_update(rule, flags, msgs); | |
4190 | } | |
4191 | } | |
4192 | ||
4193 | static void | |
4194 | ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m, | |
4195 | struct rule *rule, uint64_t seqno, | |
4196 | struct list *rules) | |
4197 | { | |
4198 | enum nx_flow_monitor_flags update; | |
4199 | ||
4200 | if (ofproto_rule_is_hidden(rule)) { | |
4201 | return; | |
4202 | } | |
4203 | ||
4204 | if (!(rule->pending | |
4205 | ? ofoperation_has_out_port(rule->pending, m->out_port) | |
4206 | : ofproto_rule_has_out_port(rule, m->out_port))) { | |
4207 | return; | |
4208 | } | |
4209 | ||
4210 | if (seqno) { | |
4211 | if (rule->add_seqno > seqno) { | |
4212 | update = NXFMF_ADD | NXFMF_MODIFY; | |
4213 | } else if (rule->modify_seqno > seqno) { | |
4214 | update = NXFMF_MODIFY; | |
4215 | } else { | |
4216 | return; | |
4217 | } | |
4218 | ||
4219 | if (!(m->flags & update)) { | |
4220 | return; | |
4221 | } | |
4222 | } else { | |
4223 | update = NXFMF_INITIAL; | |
4224 | } | |
4225 | ||
4226 | if (!rule->monitor_flags) { | |
4227 | list_push_back(rules, &rule->ofproto_node); | |
4228 | } | |
4229 | rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS); | |
4230 | } | |
4231 | ||
4232 | static void | |
4233 | ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m, | |
4234 | uint64_t seqno, | |
4235 | struct list *rules) | |
4236 | { | |
4237 | const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn); | |
4238 | const struct ofoperation *op; | |
4239 | const struct oftable *table; | |
81a76618 | 4240 | struct cls_rule target; |
2b07c8b1 | 4241 | |
5cb7a798 | 4242 | cls_rule_init_from_minimatch(&target, &m->match, 0); |
2b07c8b1 BP |
4243 | FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) { |
4244 | struct cls_cursor cursor; | |
4245 | struct rule *rule; | |
4246 | ||
0b4f2078 | 4247 | ovs_rwlock_rdlock(&table->cls.rwlock); |
81a76618 | 4248 | cls_cursor_init(&cursor, &table->cls, &target); |
2b07c8b1 | 4249 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { |
cb22974d | 4250 | ovs_assert(!rule->pending); /* XXX */ |
2b07c8b1 BP |
4251 | ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules); |
4252 | } | |
0b4f2078 | 4253 | ovs_rwlock_unlock(&table->cls.rwlock); |
2b07c8b1 BP |
4254 | } |
4255 | ||
4256 | HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) { | |
4257 | struct rule *rule = op->rule; | |
4258 | ||
4259 | if (((m->table_id == 0xff | |
4260 | ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN) | |
4261 | : m->table_id == rule->table_id)) | |
81a76618 | 4262 | && cls_rule_is_loose_match(&rule->cr, &target.match)) { |
2b07c8b1 BP |
4263 | ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules); |
4264 | } | |
4265 | } | |
48d28ac1 | 4266 | cls_rule_destroy(&target); |
2b07c8b1 BP |
4267 | } |
4268 | ||
4269 | static void | |
4270 | ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m, | |
4271 | struct list *rules) | |
4272 | { | |
4273 | if (m->flags & NXFMF_INITIAL) { | |
4274 | ofproto_collect_ofmonitor_refresh_rules(m, 0, rules); | |
4275 | } | |
4276 | } | |
4277 | ||
4278 | void | |
4279 | ofmonitor_collect_resume_rules(struct ofmonitor *m, | |
4280 | uint64_t seqno, struct list *rules) | |
4281 | { | |
4282 | ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules); | |
4283 | } | |
4284 | ||
4285 | static enum ofperr | |
982697a4 | 4286 | handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh) |
2b07c8b1 BP |
4287 | { |
4288 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
4289 | struct ofmonitor **monitors; | |
4290 | size_t n_monitors, allocated_monitors; | |
4291 | struct list replies; | |
4292 | enum ofperr error; | |
4293 | struct list rules; | |
4294 | struct ofpbuf b; | |
4295 | size_t i; | |
4296 | ||
4297 | error = 0; | |
982697a4 | 4298 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2b07c8b1 BP |
4299 | monitors = NULL; |
4300 | n_monitors = allocated_monitors = 0; | |
4301 | for (;;) { | |
4302 | struct ofputil_flow_monitor_request request; | |
4303 | struct ofmonitor *m; | |
4304 | int retval; | |
4305 | ||
4306 | retval = ofputil_decode_flow_monitor_request(&request, &b); | |
4307 | if (retval == EOF) { | |
4308 | break; | |
4309 | } else if (retval) { | |
4310 | error = retval; | |
4311 | goto error; | |
4312 | } | |
4313 | ||
4314 | if (request.table_id != 0xff | |
4315 | && request.table_id >= ofproto->n_tables) { | |
4316 | error = OFPERR_OFPBRC_BAD_TABLE_ID; | |
4317 | goto error; | |
4318 | } | |
4319 | ||
4320 | error = ofmonitor_create(&request, ofconn, &m); | |
4321 | if (error) { | |
4322 | goto error; | |
4323 | } | |
4324 | ||
4325 | if (n_monitors >= allocated_monitors) { | |
4326 | monitors = x2nrealloc(monitors, &allocated_monitors, | |
4327 | sizeof *monitors); | |
4328 | } | |
4329 | monitors[n_monitors++] = m; | |
4330 | } | |
4331 | ||
4332 | list_init(&rules); | |
4333 | for (i = 0; i < n_monitors; i++) { | |
4334 | ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules); | |
4335 | } | |
4336 | ||
982697a4 | 4337 | ofpmp_init(&replies, oh); |
2b07c8b1 BP |
4338 | ofmonitor_compose_refresh_updates(&rules, &replies); |
4339 | ofconn_send_replies(ofconn, &replies); | |
4340 | ||
4341 | free(monitors); | |
4342 | ||
4343 | return 0; | |
4344 | ||
4345 | error: | |
4346 | for (i = 0; i < n_monitors; i++) { | |
4347 | ofmonitor_destroy(monitors[i]); | |
4348 | } | |
4349 | free(monitors); | |
4350 | return error; | |
4351 | } | |
4352 | ||
4353 | static enum ofperr | |
4354 | handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh) | |
4355 | { | |
4356 | struct ofmonitor *m; | |
4357 | uint32_t id; | |
4358 | ||
4359 | id = ofputil_decode_flow_monitor_cancel(oh); | |
4360 | m = ofmonitor_lookup(ofconn, id); | |
4361 | if (!m) { | |
4362 | return OFPERR_NXBRC_FM_BAD_ID; | |
4363 | } | |
4364 | ||
4365 | ofmonitor_destroy(m); | |
4366 | return 0; | |
4367 | } | |
4368 | ||
9cae45dc JR |
4369 | /* Meters implementation. |
4370 | * | |
4371 | * Meter table entry, indexed by the OpenFlow meter_id. | |
4372 | * These are always dynamically allocated to allocate enough space for | |
4373 | * the bands. | |
4374 | * 'created' is used to compute the duration for meter stats. | |
4375 | * 'list rules' is needed so that we can delete the dependent rules when the | |
4376 | * meter table entry is deleted. | |
4377 | * 'provider_meter_id' is for the provider's private use. | |
4378 | */ | |
4379 | struct meter { | |
4380 | long long int created; /* Time created. */ | |
4381 | struct list rules; /* List of "struct rule_dpif"s. */ | |
4382 | ofproto_meter_id provider_meter_id; | |
4383 | uint16_t flags; /* Meter flags. */ | |
4384 | uint16_t n_bands; /* Number of meter bands. */ | |
4385 | struct ofputil_meter_band *bands; | |
4386 | }; | |
4387 | ||
4388 | /* | |
4389 | * This is used in instruction validation at flow set-up time, | |
4390 | * as flows may not use non-existing meters. | |
4391 | * This is also used by ofproto-providers to translate OpenFlow meter_ids | |
4392 | * in METER instructions to the corresponding provider meter IDs. | |
4393 | * Return value of UINT32_MAX signifies an invalid meter. | |
4394 | */ | |
4395 | uint32_t | |
4396 | ofproto_get_provider_meter_id(const struct ofproto * ofproto, | |
4397 | uint32_t of_meter_id) | |
4398 | { | |
4399 | if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) { | |
4400 | const struct meter *meter = ofproto->meters[of_meter_id]; | |
4401 | if (meter) { | |
4402 | return meter->provider_meter_id.uint32; | |
4403 | } | |
4404 | } | |
4405 | return UINT32_MAX; | |
4406 | } | |
4407 | ||
4408 | static void | |
4409 | meter_update(struct meter *meter, const struct ofputil_meter_config *config) | |
4410 | { | |
4411 | free(meter->bands); | |
4412 | ||
4413 | meter->flags = config->flags; | |
4414 | meter->n_bands = config->n_bands; | |
4415 | meter->bands = xmemdup(config->bands, | |
4416 | config->n_bands * sizeof *meter->bands); | |
4417 | } | |
4418 | ||
4419 | static struct meter * | |
4420 | meter_create(const struct ofputil_meter_config *config, | |
4421 | ofproto_meter_id provider_meter_id) | |
4422 | { | |
4423 | struct meter *meter; | |
4424 | ||
4425 | meter = xzalloc(sizeof *meter); | |
4426 | meter->provider_meter_id = provider_meter_id; | |
4427 | meter->created = time_msec(); | |
4428 | list_init(&meter->rules); | |
4429 | ||
4430 | meter_update(meter, config); | |
4431 | ||
4432 | return meter; | |
4433 | } | |
4434 | ||
6b3f7f02 JR |
4435 | static void |
4436 | meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last) | |
4437 | { | |
4438 | uint32_t mid; | |
4439 | for (mid = first; mid <= last; ++mid) { | |
4440 | struct meter *meter = ofproto->meters[mid]; | |
4441 | if (meter) { | |
4442 | ofproto->meters[mid] = NULL; | |
4443 | ofproto->ofproto_class->meter_del(ofproto, | |
4444 | meter->provider_meter_id); | |
4445 | free(meter->bands); | |
4446 | free(meter); | |
4447 | } | |
4448 | } | |
4449 | } | |
4450 | ||
9cae45dc JR |
4451 | static enum ofperr |
4452 | handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm) | |
4453 | { | |
4454 | ofproto_meter_id provider_meter_id = { UINT32_MAX }; | |
4455 | struct meter **meterp = &ofproto->meters[mm->meter.meter_id]; | |
4456 | enum ofperr error; | |
4457 | ||
4458 | if (*meterp) { | |
4459 | return OFPERR_OFPMMFC_METER_EXISTS; | |
4460 | } | |
4461 | ||
4462 | error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id, | |
4463 | &mm->meter); | |
4464 | if (!error) { | |
4465 | ovs_assert(provider_meter_id.uint32 != UINT32_MAX); | |
4466 | *meterp = meter_create(&mm->meter, provider_meter_id); | |
4467 | } | |
4468 | return 0; | |
4469 | } | |
4470 | ||
4471 | static enum ofperr | |
4472 | handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm) | |
4473 | { | |
4474 | struct meter *meter = ofproto->meters[mm->meter.meter_id]; | |
4475 | enum ofperr error; | |
4476 | ||
4477 | if (!meter) { | |
4478 | return OFPERR_OFPMMFC_UNKNOWN_METER; | |
4479 | } | |
4480 | ||
4481 | error = ofproto->ofproto_class->meter_set(ofproto, | |
4482 | &meter->provider_meter_id, | |
4483 | &mm->meter); | |
4484 | ovs_assert(meter->provider_meter_id.uint32 != UINT32_MAX); | |
4485 | if (!error) { | |
4486 | meter_update(meter, &mm->meter); | |
4487 | } | |
4488 | return error; | |
4489 | } | |
4490 | ||
4491 | static enum ofperr | |
4492 | handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh, | |
4493 | struct ofputil_meter_mod *mm) | |
4494 | { | |
4495 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
4496 | uint32_t meter_id = mm->meter.meter_id; | |
4497 | uint32_t first, last; | |
4498 | struct list rules; | |
4499 | ||
4500 | if (meter_id == OFPM13_ALL) { | |
4501 | first = 1; | |
4502 | last = ofproto->meter_features.max_meters; | |
4503 | } else { | |
4504 | if (!meter_id || meter_id > ofproto->meter_features.max_meters) { | |
4505 | return 0; | |
4506 | } | |
4507 | first = last = meter_id; | |
4508 | } | |
4509 | ||
4510 | /* First delete the rules that use this meter. If any of those rules are | |
4511 | * currently being modified, postpone the whole operation until later. */ | |
4512 | list_init(&rules); | |
4513 | for (meter_id = first; meter_id <= last; ++meter_id) { | |
4514 | struct meter *meter = ofproto->meters[meter_id]; | |
4515 | if (meter && !list_is_empty(&meter->rules)) { | |
4516 | struct rule *rule; | |
4517 | ||
4518 | LIST_FOR_EACH (rule, meter_list_node, &meter->rules) { | |
4519 | if (rule->pending) { | |
4520 | return OFPROTO_POSTPONE; | |
4521 | } | |
4522 | list_push_back(&rules, &rule->ofproto_node); | |
4523 | } | |
4524 | } | |
4525 | } | |
4526 | if (!list_is_empty(&rules)) { | |
4527 | delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE); | |
4528 | } | |
4529 | ||
4530 | /* Delete the meters. */ | |
6b3f7f02 | 4531 | meter_delete(ofproto, first, last); |
9cae45dc JR |
4532 | |
4533 | return 0; | |
4534 | } | |
4535 | ||
4536 | static enum ofperr | |
4537 | handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
4538 | { | |
4539 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
4540 | struct ofputil_meter_mod mm; | |
4541 | uint64_t bands_stub[256 / 8]; | |
4542 | struct ofpbuf bands; | |
4543 | uint32_t meter_id; | |
4544 | enum ofperr error; | |
4545 | ||
4546 | error = reject_slave_controller(ofconn); | |
4547 | if (error) { | |
4548 | return error; | |
4549 | } | |
4550 | ||
4551 | ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub); | |
4552 | ||
4553 | error = ofputil_decode_meter_mod(oh, &mm, &bands); | |
4554 | if (error) { | |
4555 | goto exit_free_bands; | |
4556 | } | |
4557 | ||
4558 | meter_id = mm.meter.meter_id; | |
4559 | ||
4560 | if (mm.command != OFPMC13_DELETE) { | |
4561 | /* Fails also when meters are not implemented by the provider. */ | |
4562 | if (!meter_id || meter_id > ofproto->meter_features.max_meters) { | |
4563 | error = OFPERR_OFPMMFC_INVALID_METER; | |
4564 | goto exit_free_bands; | |
4565 | } | |
4566 | if (mm.meter.n_bands > ofproto->meter_features.max_bands) { | |
4567 | error = OFPERR_OFPMMFC_OUT_OF_BANDS; | |
4568 | goto exit_free_bands; | |
4569 | } | |
4570 | } | |
4571 | ||
4572 | switch (mm.command) { | |
4573 | case OFPMC13_ADD: | |
4574 | error = handle_add_meter(ofproto, &mm); | |
4575 | break; | |
4576 | ||
4577 | case OFPMC13_MODIFY: | |
4578 | error = handle_modify_meter(ofproto, &mm); | |
4579 | break; | |
4580 | ||
4581 | case OFPMC13_DELETE: | |
4582 | error = handle_delete_meter(ofconn, oh, &mm); | |
4583 | break; | |
4584 | ||
4585 | default: | |
4586 | error = OFPERR_OFPMMFC_BAD_COMMAND; | |
4587 | break; | |
4588 | } | |
4589 | ||
4590 | exit_free_bands: | |
4591 | ofpbuf_uninit(&bands); | |
4592 | return error; | |
4593 | } | |
4594 | ||
4595 | static enum ofperr | |
4596 | handle_meter_features_request(struct ofconn *ofconn, | |
4597 | const struct ofp_header *request) | |
4598 | { | |
4599 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
4600 | struct ofputil_meter_features features; | |
4601 | struct ofpbuf *b; | |
4602 | ||
4603 | if (ofproto->ofproto_class->meter_get_features) { | |
4604 | ofproto->ofproto_class->meter_get_features(ofproto, &features); | |
4605 | } else { | |
4606 | memset(&features, 0, sizeof features); | |
4607 | } | |
4608 | b = ofputil_encode_meter_features_reply(&features, request); | |
4609 | ||
4610 | ofconn_send_reply(ofconn, b); | |
4611 | return 0; | |
4612 | } | |
4613 | ||
4614 | static enum ofperr | |
4615 | handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request, | |
4616 | enum ofptype type) | |
4617 | { | |
4618 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
4619 | struct list replies; | |
4620 | uint64_t bands_stub[256 / 8]; | |
4621 | struct ofpbuf bands; | |
4622 | uint32_t meter_id, first, last; | |
4623 | ||
4624 | ofputil_decode_meter_request(request, &meter_id); | |
4625 | ||
4626 | if (meter_id == OFPM13_ALL) { | |
4627 | first = 1; | |
4628 | last = ofproto->meter_features.max_meters; | |
4629 | } else { | |
4630 | if (!meter_id || meter_id > ofproto->meter_features.max_meters || | |
4631 | !ofproto->meters[meter_id]) { | |
4632 | return OFPERR_OFPMMFC_UNKNOWN_METER; | |
4633 | } | |
4634 | first = last = meter_id; | |
4635 | } | |
4636 | ||
4637 | ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub); | |
4638 | ofpmp_init(&replies, request); | |
4639 | ||
4640 | for (meter_id = first; meter_id <= last; ++meter_id) { | |
4641 | struct meter *meter = ofproto->meters[meter_id]; | |
4642 | if (!meter) { | |
4643 | continue; /* Skip non-existing meters. */ | |
4644 | } | |
261bd854 | 4645 | if (type == OFPTYPE_METER_STATS_REQUEST) { |
9cae45dc JR |
4646 | struct ofputil_meter_stats stats; |
4647 | ||
4648 | stats.meter_id = meter_id; | |
4649 | ||
4650 | /* Provider sets the packet and byte counts, we do the rest. */ | |
4651 | stats.flow_count = list_size(&meter->rules); | |
4652 | calc_duration(meter->created, time_msec(), | |
4653 | &stats.duration_sec, &stats.duration_nsec); | |
4654 | stats.n_bands = meter->n_bands; | |
4655 | ofpbuf_clear(&bands); | |
4656 | stats.bands | |
4657 | = ofpbuf_put_uninit(&bands, | |
4658 | meter->n_bands * sizeof *stats.bands); | |
4659 | ||
4660 | if (!ofproto->ofproto_class->meter_get(ofproto, | |
4661 | meter->provider_meter_id, | |
4662 | &stats)) { | |
4663 | ofputil_append_meter_stats(&replies, &stats); | |
4664 | } | |
4665 | } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */ | |
4666 | struct ofputil_meter_config config; | |
4667 | ||
4668 | config.meter_id = meter_id; | |
4669 | config.flags = meter->flags; | |
4670 | config.n_bands = meter->n_bands; | |
4671 | config.bands = meter->bands; | |
4672 | ofputil_append_meter_config(&replies, &config); | |
4673 | } | |
4674 | } | |
4675 | ||
4676 | ofconn_send_replies(ofconn, &replies); | |
4677 | ofpbuf_uninit(&bands); | |
4678 | return 0; | |
4679 | } | |
4680 | ||
90bf1e07 | 4681 | static enum ofperr |
d1e2cf21 | 4682 | handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg) |
064af421 | 4683 | { |
d1e2cf21 | 4684 | const struct ofp_header *oh = msg->data; |
982697a4 | 4685 | enum ofptype type; |
90bf1e07 | 4686 | enum ofperr error; |
064af421 | 4687 | |
982697a4 | 4688 | error = ofptype_decode(&type, oh); |
d1e2cf21 BP |
4689 | if (error) { |
4690 | return error; | |
4691 | } | |
064af421 | 4692 | |
982697a4 | 4693 | switch (type) { |
d1e2cf21 | 4694 | /* OpenFlow requests. */ |
982697a4 | 4695 | case OFPTYPE_ECHO_REQUEST: |
d1e2cf21 | 4696 | return handle_echo_request(ofconn, oh); |
064af421 | 4697 | |
982697a4 | 4698 | case OFPTYPE_FEATURES_REQUEST: |
d1e2cf21 | 4699 | return handle_features_request(ofconn, oh); |
064af421 | 4700 | |
982697a4 | 4701 | case OFPTYPE_GET_CONFIG_REQUEST: |
d1e2cf21 | 4702 | return handle_get_config_request(ofconn, oh); |
064af421 | 4703 | |
982697a4 BP |
4704 | case OFPTYPE_SET_CONFIG: |
4705 | return handle_set_config(ofconn, oh); | |
064af421 | 4706 | |
982697a4 BP |
4707 | case OFPTYPE_PACKET_OUT: |
4708 | return handle_packet_out(ofconn, oh); | |
064af421 | 4709 | |
982697a4 | 4710 | case OFPTYPE_PORT_MOD: |
d1e2cf21 | 4711 | return handle_port_mod(ofconn, oh); |
064af421 | 4712 | |
982697a4 | 4713 | case OFPTYPE_FLOW_MOD: |
2e4f5fcf | 4714 | return handle_flow_mod(ofconn, oh); |
064af421 | 4715 | |
9cae45dc JR |
4716 | case OFPTYPE_METER_MOD: |
4717 | return handle_meter_mod(ofconn, oh); | |
4718 | ||
982697a4 | 4719 | case OFPTYPE_BARRIER_REQUEST: |
d1e2cf21 | 4720 | return handle_barrier_request(ofconn, oh); |
064af421 | 4721 | |
6ea4776b JR |
4722 | case OFPTYPE_ROLE_REQUEST: |
4723 | return handle_role_request(ofconn, oh); | |
4724 | ||
d1e2cf21 | 4725 | /* OpenFlow replies. */ |
982697a4 | 4726 | case OFPTYPE_ECHO_REPLY: |
d1e2cf21 | 4727 | return 0; |
246e61ea | 4728 | |
d1e2cf21 | 4729 | /* Nicira extension requests. */ |
982697a4 | 4730 | case OFPTYPE_FLOW_MOD_TABLE_ID: |
6c1491fb | 4731 | return handle_nxt_flow_mod_table_id(ofconn, oh); |
d1e2cf21 | 4732 | |
982697a4 | 4733 | case OFPTYPE_SET_FLOW_FORMAT: |
d1e2cf21 BP |
4734 | return handle_nxt_set_flow_format(ofconn, oh); |
4735 | ||
982697a4 | 4736 | case OFPTYPE_SET_PACKET_IN_FORMAT: |
54834960 EJ |
4737 | return handle_nxt_set_packet_in_format(ofconn, oh); |
4738 | ||
982697a4 | 4739 | case OFPTYPE_SET_CONTROLLER_ID: |
a7349929 BP |
4740 | return handle_nxt_set_controller_id(ofconn, oh); |
4741 | ||
982697a4 | 4742 | case OFPTYPE_FLOW_AGE: |
f27f2134 BP |
4743 | /* Nothing to do. */ |
4744 | return 0; | |
4745 | ||
982697a4 | 4746 | case OFPTYPE_FLOW_MONITOR_CANCEL: |
2b07c8b1 BP |
4747 | return handle_flow_monitor_cancel(ofconn, oh); |
4748 | ||
982697a4 | 4749 | case OFPTYPE_SET_ASYNC_CONFIG: |
80d5aefd BP |
4750 | return handle_nxt_set_async_config(ofconn, oh); |
4751 | ||
349adfb2 | 4752 | /* Statistics requests. */ |
982697a4 BP |
4753 | case OFPTYPE_DESC_STATS_REQUEST: |
4754 | return handle_desc_stats_request(ofconn, oh); | |
4755 | ||
4756 | case OFPTYPE_FLOW_STATS_REQUEST: | |
4757 | return handle_flow_stats_request(ofconn, oh); | |
4758 | ||
4759 | case OFPTYPE_AGGREGATE_STATS_REQUEST: | |
4760 | return handle_aggregate_stats_request(ofconn, oh); | |
4761 | ||
4762 | case OFPTYPE_TABLE_STATS_REQUEST: | |
4763 | return handle_table_stats_request(ofconn, oh); | |
4764 | ||
4765 | case OFPTYPE_PORT_STATS_REQUEST: | |
4766 | return handle_port_stats_request(ofconn, oh); | |
4767 | ||
4768 | case OFPTYPE_QUEUE_STATS_REQUEST: | |
4769 | return handle_queue_stats_request(ofconn, oh); | |
4770 | ||
4771 | case OFPTYPE_PORT_DESC_STATS_REQUEST: | |
4772 | return handle_port_desc_stats_request(ofconn, oh); | |
4773 | ||
4774 | case OFPTYPE_FLOW_MONITOR_STATS_REQUEST: | |
4775 | return handle_flow_monitor_request(ofconn, oh); | |
4776 | ||
261bd854 BP |
4777 | case OFPTYPE_METER_STATS_REQUEST: |
4778 | case OFPTYPE_METER_CONFIG_STATS_REQUEST: | |
9cae45dc JR |
4779 | return handle_meter_request(ofconn, oh, type); |
4780 | ||
261bd854 | 4781 | case OFPTYPE_METER_FEATURES_STATS_REQUEST: |
9cae45dc JR |
4782 | return handle_meter_features_request(ofconn, oh); |
4783 | ||
2e1ae200 | 4784 | /* FIXME: Change the following once they are implemented: */ |
c545d38d | 4785 | case OFPTYPE_QUEUE_GET_CONFIG_REQUEST: |
2e1ae200 | 4786 | case OFPTYPE_GET_ASYNC_REQUEST: |
261bd854 BP |
4787 | case OFPTYPE_GROUP_STATS_REQUEST: |
4788 | case OFPTYPE_GROUP_DESC_STATS_REQUEST: | |
4789 | case OFPTYPE_GROUP_FEATURES_STATS_REQUEST: | |
4790 | case OFPTYPE_TABLE_FEATURES_STATS_REQUEST: | |
2e1ae200 JR |
4791 | return OFPERR_OFPBRC_BAD_TYPE; |
4792 | ||
982697a4 BP |
4793 | case OFPTYPE_HELLO: |
4794 | case OFPTYPE_ERROR: | |
4795 | case OFPTYPE_FEATURES_REPLY: | |
4796 | case OFPTYPE_GET_CONFIG_REPLY: | |
4797 | case OFPTYPE_PACKET_IN: | |
4798 | case OFPTYPE_FLOW_REMOVED: | |
4799 | case OFPTYPE_PORT_STATUS: | |
4800 | case OFPTYPE_BARRIER_REPLY: | |
c545d38d | 4801 | case OFPTYPE_QUEUE_GET_CONFIG_REPLY: |
982697a4 BP |
4802 | case OFPTYPE_DESC_STATS_REPLY: |
4803 | case OFPTYPE_FLOW_STATS_REPLY: | |
4804 | case OFPTYPE_QUEUE_STATS_REPLY: | |
4805 | case OFPTYPE_PORT_STATS_REPLY: | |
4806 | case OFPTYPE_TABLE_STATS_REPLY: | |
4807 | case OFPTYPE_AGGREGATE_STATS_REPLY: | |
4808 | case OFPTYPE_PORT_DESC_STATS_REPLY: | |
4809 | case OFPTYPE_ROLE_REPLY: | |
4810 | case OFPTYPE_FLOW_MONITOR_PAUSED: | |
4811 | case OFPTYPE_FLOW_MONITOR_RESUMED: | |
4812 | case OFPTYPE_FLOW_MONITOR_STATS_REPLY: | |
2e1ae200 | 4813 | case OFPTYPE_GET_ASYNC_REPLY: |
261bd854 BP |
4814 | case OFPTYPE_GROUP_STATS_REPLY: |
4815 | case OFPTYPE_GROUP_DESC_STATS_REPLY: | |
4816 | case OFPTYPE_GROUP_FEATURES_STATS_REPLY: | |
4817 | case OFPTYPE_METER_STATS_REPLY: | |
4818 | case OFPTYPE_METER_CONFIG_STATS_REPLY: | |
4819 | case OFPTYPE_METER_FEATURES_STATS_REPLY: | |
4820 | case OFPTYPE_TABLE_FEATURES_STATS_REPLY: | |
064af421 | 4821 | default: |
982697a4 | 4822 | return OFPERR_OFPBRC_BAD_TYPE; |
064af421 | 4823 | } |
d1e2cf21 | 4824 | } |
064af421 | 4825 | |
7ee20df1 | 4826 | static bool |
e03248b7 | 4827 | handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg) |
d1e2cf21 BP |
4828 | { |
4829 | int error = handle_openflow__(ofconn, ofp_msg); | |
7ee20df1 | 4830 | if (error && error != OFPROTO_POSTPONE) { |
1be5ff75 | 4831 | ofconn_send_error(ofconn, ofp_msg->data, error); |
064af421 | 4832 | } |
d1e2cf21 | 4833 | COVERAGE_INC(ofproto_recv_openflow); |
7ee20df1 BP |
4834 | return error != OFPROTO_POSTPONE; |
4835 | } | |
4836 | \f | |
4837 | /* Asynchronous operations. */ | |
4838 | ||
4839 | /* Creates and returns a new ofopgroup that is not associated with any | |
4840 | * OpenFlow connection. | |
4841 | * | |
4842 | * The caller should add operations to the returned group with | |
4843 | * ofoperation_create() and then submit it with ofopgroup_submit(). */ | |
4844 | static struct ofopgroup * | |
7024dffe | 4845 | ofopgroup_create_unattached(struct ofproto *ofproto) |
7ee20df1 BP |
4846 | { |
4847 | struct ofopgroup *group = xzalloc(sizeof *group); | |
4848 | group->ofproto = ofproto; | |
4849 | list_init(&group->ofproto_node); | |
4850 | list_init(&group->ops); | |
4851 | list_init(&group->ofconn_node); | |
4852 | return group; | |
4853 | } | |
4854 | ||
7024dffe BP |
4855 | /* Creates and returns a new ofopgroup for 'ofproto'. |
4856 | * | |
4857 | * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow | |
4858 | * connection. The 'request' and 'buffer_id' arguments are ignored. | |
4859 | * | |
4860 | * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'. | |
4861 | * If the ofopgroup eventually fails, then the error reply will include | |
4862 | * 'request'. If the ofopgroup eventually succeeds, then the packet with | |
4863 | * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto. | |
7ee20df1 BP |
4864 | * |
4865 | * The caller should add operations to the returned group with | |
4866 | * ofoperation_create() and then submit it with ofopgroup_submit(). */ | |
4867 | static struct ofopgroup * | |
7024dffe BP |
4868 | ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn, |
4869 | const struct ofp_header *request, uint32_t buffer_id) | |
7ee20df1 | 4870 | { |
7024dffe BP |
4871 | struct ofopgroup *group = ofopgroup_create_unattached(ofproto); |
4872 | if (ofconn) { | |
4873 | size_t request_len = ntohs(request->length); | |
7ee20df1 | 4874 | |
cb22974d | 4875 | ovs_assert(ofconn_get_ofproto(ofconn) == ofproto); |
7ee20df1 | 4876 | |
7024dffe BP |
4877 | ofconn_add_opgroup(ofconn, &group->ofconn_node); |
4878 | group->ofconn = ofconn; | |
4879 | group->request = xmemdup(request, MIN(request_len, 64)); | |
4880 | group->buffer_id = buffer_id; | |
4881 | } | |
7ee20df1 BP |
4882 | return group; |
4883 | } | |
4884 | ||
4885 | /* Submits 'group' for processing. | |
4886 | * | |
4887 | * If 'group' contains no operations (e.g. none were ever added, or all of the | |
4888 | * ones that were added completed synchronously), then it is destroyed | |
4889 | * immediately. Otherwise it is added to the ofproto's list of pending | |
4890 | * groups. */ | |
4891 | static void | |
4892 | ofopgroup_submit(struct ofopgroup *group) | |
4893 | { | |
e615b0a3 BP |
4894 | if (!group->n_running) { |
4895 | ofopgroup_complete(group); | |
7ee20df1 BP |
4896 | } else { |
4897 | list_push_back(&group->ofproto->pending, &group->ofproto_node); | |
dd5616b3 | 4898 | group->ofproto->n_pending++; |
7ee20df1 BP |
4899 | } |
4900 | } | |
4901 | ||
4902 | static void | |
e615b0a3 | 4903 | ofopgroup_complete(struct ofopgroup *group) |
7ee20df1 | 4904 | { |
e615b0a3 | 4905 | struct ofproto *ofproto = group->ofproto; |
2b07c8b1 BP |
4906 | |
4907 | struct ofconn *abbrev_ofconn; | |
4908 | ovs_be32 abbrev_xid; | |
4909 | ||
e615b0a3 BP |
4910 | struct ofoperation *op, *next_op; |
4911 | int error; | |
4912 | ||
cb22974d | 4913 | ovs_assert(!group->n_running); |
e615b0a3 BP |
4914 | |
4915 | error = 0; | |
4916 | LIST_FOR_EACH (op, group_node, &group->ops) { | |
4917 | if (op->error) { | |
4918 | error = op->error; | |
4919 | break; | |
4920 | } | |
4921 | } | |
4922 | ||
4923 | if (!error && group->ofconn && group->buffer_id != UINT32_MAX) { | |
4924 | LIST_FOR_EACH (op, group_node, &group->ops) { | |
4925 | if (op->type != OFOPERATION_DELETE) { | |
4926 | struct ofpbuf *packet; | |
4e022ec0 | 4927 | ofp_port_t in_port; |
e615b0a3 BP |
4928 | |
4929 | error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id, | |
4930 | &packet, &in_port); | |
4931 | if (packet) { | |
cb22974d | 4932 | ovs_assert(!error); |
e615b0a3 BP |
4933 | error = rule_execute(op->rule, in_port, packet); |
4934 | } | |
4935 | break; | |
4936 | } | |
4937 | } | |
4938 | } | |
4939 | ||
2b07c8b1 BP |
4940 | if (!error && !list_is_empty(&group->ofconn_node)) { |
4941 | abbrev_ofconn = group->ofconn; | |
4942 | abbrev_xid = group->request->xid; | |
4943 | } else { | |
4944 | abbrev_ofconn = NULL; | |
4945 | abbrev_xid = htonl(0); | |
4946 | } | |
e615b0a3 BP |
4947 | LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) { |
4948 | struct rule *rule = op->rule; | |
2b07c8b1 | 4949 | |
644e2a7c BP |
4950 | /* We generally want to report the change to active OpenFlow flow |
4951 | monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions: | |
4952 | ||
4953 | - The operation failed. | |
4954 | ||
4955 | - The affected rule is not visible to controllers. | |
4956 | ||
4957 | - The operation's only effect was to update rule->modified. */ | |
4958 | if (!(op->error | |
4959 | || ofproto_rule_is_hidden(rule) | |
4960 | || (op->type == OFOPERATION_MODIFY | |
4961 | && op->ofpacts | |
4962 | && rule->flow_cookie == op->flow_cookie))) { | |
2b07c8b1 BP |
4963 | /* Check that we can just cast from ofoperation_type to |
4964 | * nx_flow_update_event. */ | |
b277c7cc BP |
4965 | enum nx_flow_update_event event_type; |
4966 | ||
4967 | switch (op->type) { | |
4968 | case OFOPERATION_ADD: | |
4969 | case OFOPERATION_REPLACE: | |
4970 | event_type = NXFME_ADDED; | |
4971 | break; | |
4972 | ||
4973 | case OFOPERATION_DELETE: | |
4974 | event_type = NXFME_DELETED; | |
4975 | break; | |
4976 | ||
4977 | case OFOPERATION_MODIFY: | |
4978 | event_type = NXFME_MODIFIED; | |
4979 | break; | |
4980 | ||
4981 | default: | |
4982 | NOT_REACHED(); | |
4983 | } | |
4984 | ||
4985 | ofmonitor_report(ofproto->connmgr, rule, event_type, | |
2b07c8b1 BP |
4986 | op->reason, abbrev_ofconn, abbrev_xid); |
4987 | } | |
4988 | ||
e615b0a3 BP |
4989 | rule->pending = NULL; |
4990 | ||
4991 | switch (op->type) { | |
4992 | case OFOPERATION_ADD: | |
4993 | if (!op->error) { | |
5cb7a798 BP |
4994 | uint16_t vid_mask; |
4995 | ||
5cb7a798 BP |
4996 | vid_mask = minimask_get_vid_mask(&rule->cr.match.mask); |
4997 | if (vid_mask == VLAN_VID_MASK) { | |
e615b0a3 | 4998 | if (ofproto->vlan_bitmap) { |
5cb7a798 | 4999 | uint16_t vid = miniflow_get_vid(&rule->cr.match.flow); |
e615b0a3 BP |
5000 | if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) { |
5001 | bitmap_set1(ofproto->vlan_bitmap, vid); | |
5002 | ofproto->vlans_changed = true; | |
5003 | } | |
5004 | } else { | |
5005 | ofproto->vlans_changed = true; | |
5006 | } | |
5007 | } | |
5008 | } else { | |
b277c7cc BP |
5009 | ovs_rwlock_wrlock(&rule->evict); |
5010 | oftable_remove_rule(rule); | |
e615b0a3 BP |
5011 | ofproto_rule_destroy__(rule); |
5012 | } | |
5013 | break; | |
5014 | ||
5015 | case OFOPERATION_DELETE: | |
cb22974d | 5016 | ovs_assert(!op->error); |
e615b0a3 BP |
5017 | ofproto_rule_destroy__(rule); |
5018 | op->rule = NULL; | |
5019 | break; | |
5020 | ||
5021 | case OFOPERATION_MODIFY: | |
b277c7cc | 5022 | case OFOPERATION_REPLACE: |
e615b0a3 | 5023 | if (!op->error) { |
b277c7cc BP |
5024 | long long int now = time_msec(); |
5025 | ||
5026 | rule->modified = now; | |
5027 | if (op->type == OFOPERATION_REPLACE) { | |
5028 | rule->created = rule->used = now; | |
5029 | } | |
e615b0a3 | 5030 | } else { |
98eaac36 | 5031 | ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie); |
b277c7cc BP |
5032 | ovs_mutex_lock(&rule->timeout_mutex); |
5033 | rule->idle_timeout = op->idle_timeout; | |
5034 | rule->hard_timeout = op->hard_timeout; | |
5035 | ovs_mutex_unlock(&rule->timeout_mutex); | |
08043761 BP |
5036 | if (op->ofpacts) { |
5037 | free(rule->ofpacts); | |
5038 | rule->ofpacts = op->ofpacts; | |
5039 | rule->ofpacts_len = op->ofpacts_len; | |
5040 | op->ofpacts = NULL; | |
5041 | op->ofpacts_len = 0; | |
5042 | } | |
b277c7cc | 5043 | rule->send_flow_removed = op->send_flow_removed; |
e615b0a3 BP |
5044 | } |
5045 | break; | |
5046 | ||
5047 | default: | |
5048 | NOT_REACHED(); | |
5049 | } | |
5050 | ||
5051 | ofoperation_destroy(op); | |
5052 | } | |
5053 | ||
2b07c8b1 BP |
5054 | ofmonitor_flush(ofproto->connmgr); |
5055 | ||
7ee20df1 | 5056 | if (!list_is_empty(&group->ofproto_node)) { |
cb22974d | 5057 | ovs_assert(ofproto->n_pending > 0); |
e615b0a3 | 5058 | ofproto->n_pending--; |
7ee20df1 BP |
5059 | list_remove(&group->ofproto_node); |
5060 | } | |
5061 | if (!list_is_empty(&group->ofconn_node)) { | |
5062 | list_remove(&group->ofconn_node); | |
e615b0a3 BP |
5063 | if (error) { |
5064 | ofconn_send_error(group->ofconn, group->request, error); | |
7ee20df1 | 5065 | } |
e615b0a3 | 5066 | connmgr_retry(ofproto->connmgr); |
7ee20df1 BP |
5067 | } |
5068 | free(group->request); | |
5069 | free(group); | |
5070 | } | |
5071 | ||
5072 | /* Initiates a new operation on 'rule', of the specified 'type', within | |
a7d94793 BP |
5073 | * 'group'. Prior to calling, 'rule' must not have any pending operation. |
5074 | * | |
2b07c8b1 BP |
5075 | * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that |
5076 | * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0). | |
5077 | * | |
a7d94793 BP |
5078 | * Returns the newly created ofoperation (which is also available as |
5079 | * rule->pending). */ | |
5080 | static struct ofoperation * | |
7ee20df1 | 5081 | ofoperation_create(struct ofopgroup *group, struct rule *rule, |
2b07c8b1 BP |
5082 | enum ofoperation_type type, |
5083 | enum ofp_flow_removed_reason reason) | |
7ee20df1 | 5084 | { |
a5b8d268 | 5085 | struct ofproto *ofproto = group->ofproto; |
7ee20df1 BP |
5086 | struct ofoperation *op; |
5087 | ||
cb22974d | 5088 | ovs_assert(!rule->pending); |
7ee20df1 BP |
5089 | |
5090 | op = rule->pending = xzalloc(sizeof *op); | |
5091 | op->group = group; | |
5092 | list_push_back(&group->ops, &op->group_node); | |
5093 | op->rule = rule; | |
5094 | op->type = type; | |
2b07c8b1 | 5095 | op->reason = reason; |
7ee20df1 | 5096 | op->flow_cookie = rule->flow_cookie; |
b277c7cc BP |
5097 | ovs_mutex_lock(&rule->timeout_mutex); |
5098 | op->idle_timeout = rule->idle_timeout; | |
5099 | op->hard_timeout = rule->hard_timeout; | |
5100 | ovs_mutex_unlock(&rule->timeout_mutex); | |
5101 | op->send_flow_removed = rule->send_flow_removed; | |
7ee20df1 | 5102 | |
e615b0a3 BP |
5103 | group->n_running++; |
5104 | ||
7ee20df1 | 5105 | if (type == OFOPERATION_DELETE) { |
a5b8d268 | 5106 | hmap_insert(&ofproto->deletions, &op->hmap_node, |
7ee20df1 BP |
5107 | cls_rule_hash(&rule->cr, rule->table_id)); |
5108 | } | |
a7d94793 BP |
5109 | |
5110 | return op; | |
7ee20df1 BP |
5111 | } |
5112 | ||
5113 | static void | |
5114 | ofoperation_destroy(struct ofoperation *op) | |
5115 | { | |
5116 | struct ofopgroup *group = op->group; | |
5117 | ||
5118 | if (op->rule) { | |
5119 | op->rule->pending = NULL; | |
5120 | } | |
5121 | if (op->type == OFOPERATION_DELETE) { | |
5122 | hmap_remove(&group->ofproto->deletions, &op->hmap_node); | |
5123 | } | |
5124 | list_remove(&op->group_node); | |
f25d0cf3 | 5125 | free(op->ofpacts); |
7ee20df1 | 5126 | free(op); |
7ee20df1 BP |
5127 | } |
5128 | ||
5129 | /* Indicates that 'op' completed with status 'error', which is either 0 to | |
90bf1e07 | 5130 | * indicate success or an OpenFlow error code on failure. |
7ee20df1 | 5131 | * |
a6a62132 | 5132 | * If 'error' is 0, indicating success, the operation will be committed |
b277c7cc | 5133 | * permanently to the flow table. |
a6a62132 BP |
5134 | * |
5135 | * If 'error' is nonzero, then generally the operation will be rolled back: | |
5136 | * | |
5137 | * - If 'op' is an "add flow" operation, ofproto removes the new rule or | |
5138 | * restores the original rule. The caller must have uninitialized any | |
5139 | * derived state in the new rule, as in step 5 of in the "Life Cycle" in | |
5140 | * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and | |
5141 | * and 7 for the new rule, calling its ->rule_dealloc() function. | |
5142 | * | |
5143 | * - If 'op' is a "modify flow" operation, ofproto restores the original | |
5144 | * actions. | |
5145 | * | |
5146 | * - 'op' must not be a "delete flow" operation. Removing a rule is not | |
5147 | * allowed to fail. It must always succeed. | |
7ee20df1 | 5148 | * |
5bee6e26 JP |
5149 | * Please see the large comment in ofproto/ofproto-provider.h titled |
5150 | * "Asynchronous Operation Support" for more information. */ | |
7ee20df1 | 5151 | void |
90bf1e07 | 5152 | ofoperation_complete(struct ofoperation *op, enum ofperr error) |
7ee20df1 BP |
5153 | { |
5154 | struct ofopgroup *group = op->group; | |
7ee20df1 | 5155 | |
cb22974d BP |
5156 | ovs_assert(op->rule->pending == op); |
5157 | ovs_assert(group->n_running > 0); | |
5158 | ovs_assert(!error || op->type != OFOPERATION_DELETE); | |
9075907c | 5159 | |
e615b0a3 BP |
5160 | op->error = error; |
5161 | if (!--group->n_running && !list_is_empty(&group->ofproto_node)) { | |
5162 | ofopgroup_complete(group); | |
7ee20df1 | 5163 | } |
7ee20df1 | 5164 | } |
064af421 | 5165 | \f |
064af421 | 5166 | static uint64_t |
fa60c019 | 5167 | pick_datapath_id(const struct ofproto *ofproto) |
064af421 | 5168 | { |
fa60c019 | 5169 | const struct ofport *port; |
064af421 | 5170 | |
abe529af | 5171 | port = ofproto_get_port(ofproto, OFPP_LOCAL); |
fa60c019 BP |
5172 | if (port) { |
5173 | uint8_t ea[ETH_ADDR_LEN]; | |
5174 | int error; | |
5175 | ||
5176 | error = netdev_get_etheraddr(port->netdev, ea); | |
064af421 BP |
5177 | if (!error) { |
5178 | return eth_addr_to_uint64(ea); | |
5179 | } | |
fbfa2911 BP |
5180 | VLOG_WARN("%s: could not get MAC address for %s (%s)", |
5181 | ofproto->name, netdev_get_name(port->netdev), | |
10a89ef0 | 5182 | ovs_strerror(error)); |
064af421 | 5183 | } |
fa60c019 | 5184 | return ofproto->fallback_dpid; |
064af421 BP |
5185 | } |
5186 | ||
5187 | static uint64_t | |
5188 | pick_fallback_dpid(void) | |
5189 | { | |
5190 | uint8_t ea[ETH_ADDR_LEN]; | |
70150daf | 5191 | eth_addr_nicira_random(ea); |
064af421 BP |
5192 | return eth_addr_to_uint64(ea); |
5193 | } | |
5194 | \f | |
254750ce BP |
5195 | /* Table overflow policy. */ |
5196 | ||
ad3efdcb EJ |
5197 | /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep' |
5198 | * to NULL if the table is not configured to evict rules or if the table | |
5199 | * contains no evictable rules. (Rules with a readlock on their evict rwlock, | |
5200 | * or with no timeouts are not evictable.) */ | |
5201 | static bool | |
5202 | choose_rule_to_evict(struct oftable *table, struct rule **rulep) | |
254750ce BP |
5203 | { |
5204 | struct eviction_group *evg; | |
5205 | ||
ad3efdcb | 5206 | *rulep = NULL; |
254750ce | 5207 | if (!table->eviction_fields) { |
ad3efdcb | 5208 | return false; |
254750ce BP |
5209 | } |
5210 | ||
5211 | /* In the common case, the outer and inner loops here will each be entered | |
5212 | * exactly once: | |
5213 | * | |
5214 | * - The inner loop normally "return"s in its first iteration. If the | |
5215 | * eviction group has any evictable rules, then it always returns in | |
5216 | * some iteration. | |
5217 | * | |
5218 | * - The outer loop only iterates more than once if the largest eviction | |
5219 | * group has no evictable rules. | |
5220 | * | |
5221 | * - The outer loop can exit only if table's 'max_flows' is all filled up | |
afe2143d | 5222 | * by unevictable rules. */ |
254750ce BP |
5223 | HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) { |
5224 | struct rule *rule; | |
5225 | ||
5226 | HEAP_FOR_EACH (rule, evg_node, &evg->rules) { | |
ad3efdcb EJ |
5227 | if (!ovs_rwlock_trywrlock(&rule->evict)) { |
5228 | *rulep = rule; | |
5229 | return true; | |
254750ce BP |
5230 | } |
5231 | } | |
5232 | } | |
5233 | ||
ad3efdcb | 5234 | return false; |
254750ce BP |
5235 | } |
5236 | ||
5237 | /* Searches 'ofproto' for tables that have more flows than their configured | |
5238 | * maximum and that have flow eviction enabled, and evicts as many flows as | |
5239 | * necessary and currently feasible from them. | |
5240 | * | |
5241 | * This triggers only when an OpenFlow table has N flows in it and then the | |
5242 | * client configures a maximum number of flows less than N. */ | |
5243 | static void | |
5244 | ofproto_evict(struct ofproto *ofproto) | |
5245 | { | |
5246 | struct ofopgroup *group; | |
5247 | struct oftable *table; | |
5248 | ||
5249 | group = ofopgroup_create_unattached(ofproto); | |
5250 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
0b4f2078 | 5251 | while (table->eviction_fields) { |
254750ce | 5252 | struct rule *rule; |
0b4f2078 EJ |
5253 | size_t n_rules; |
5254 | ||
5255 | ovs_rwlock_rdlock(&table->cls.rwlock); | |
5256 | n_rules = classifier_count(&table->cls); | |
5257 | ovs_rwlock_unlock(&table->cls.rwlock); | |
5258 | ||
5259 | if (n_rules <= table->max_flows) { | |
5260 | break; | |
5261 | } | |
254750ce | 5262 | |
ad3efdcb EJ |
5263 | if (!choose_rule_to_evict(table, &rule)) { |
5264 | break; | |
5265 | } | |
5266 | ||
5267 | if (rule->pending) { | |
5268 | ovs_rwlock_unlock(&rule->evict); | |
254750ce BP |
5269 | break; |
5270 | } | |
5271 | ||
2b07c8b1 BP |
5272 | ofoperation_create(group, rule, |
5273 | OFOPERATION_DELETE, OFPRR_EVICTION); | |
254750ce | 5274 | oftable_remove_rule(rule); |
8037acb4 | 5275 | ofproto->ofproto_class->rule_delete(rule); |
254750ce BP |
5276 | } |
5277 | } | |
5278 | ofopgroup_submit(group); | |
5279 | } | |
5280 | \f | |
5281 | /* Eviction groups. */ | |
5282 | ||
5283 | /* Returns the priority to use for an eviction_group that contains 'n_rules' | |
5284 | * rules. The priority contains low-order random bits to ensure that eviction | |
5285 | * groups with the same number of rules are prioritized randomly. */ | |
5286 | static uint32_t | |
5287 | eviction_group_priority(size_t n_rules) | |
5288 | { | |
5289 | uint16_t size = MIN(UINT16_MAX, n_rules); | |
5290 | return (size << 16) | random_uint16(); | |
5291 | } | |
5292 | ||
5293 | /* Updates 'evg', an eviction_group within 'table', following a change that | |
5294 | * adds or removes rules in 'evg'. */ | |
5295 | static void | |
5296 | eviction_group_resized(struct oftable *table, struct eviction_group *evg) | |
5297 | { | |
5298 | heap_change(&table->eviction_groups_by_size, &evg->size_node, | |
5299 | eviction_group_priority(heap_count(&evg->rules))); | |
5300 | } | |
5301 | ||
5302 | /* Destroys 'evg', an eviction_group within 'table': | |
5303 | * | |
5304 | * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the | |
5305 | * rules themselves, just removes them from the eviction group.) | |
5306 | * | |
5307 | * - Removes 'evg' from 'table'. | |
5308 | * | |
5309 | * - Frees 'evg'. */ | |
5310 | static void | |
5311 | eviction_group_destroy(struct oftable *table, struct eviction_group *evg) | |
5312 | { | |
5313 | while (!heap_is_empty(&evg->rules)) { | |
5314 | struct rule *rule; | |
5315 | ||
5316 | rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node); | |
5317 | rule->eviction_group = NULL; | |
5318 | } | |
5319 | hmap_remove(&table->eviction_groups_by_id, &evg->id_node); | |
5320 | heap_remove(&table->eviction_groups_by_size, &evg->size_node); | |
5321 | heap_destroy(&evg->rules); | |
5322 | free(evg); | |
5323 | } | |
5324 | ||
5325 | /* Removes 'rule' from its eviction group, if any. */ | |
5326 | static void | |
5327 | eviction_group_remove_rule(struct rule *rule) | |
5328 | { | |
5329 | if (rule->eviction_group) { | |
5330 | struct oftable *table = &rule->ofproto->tables[rule->table_id]; | |
5331 | struct eviction_group *evg = rule->eviction_group; | |
5332 | ||
5333 | rule->eviction_group = NULL; | |
5334 | heap_remove(&evg->rules, &rule->evg_node); | |
5335 | if (heap_is_empty(&evg->rules)) { | |
5336 | eviction_group_destroy(table, evg); | |
5337 | } else { | |
5338 | eviction_group_resized(table, evg); | |
5339 | } | |
5340 | } | |
5341 | } | |
5342 | ||
5343 | /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and | |
5344 | * returns the hash value. */ | |
5345 | static uint32_t | |
5346 | eviction_group_hash_rule(struct rule *rule) | |
5347 | { | |
5348 | struct oftable *table = &rule->ofproto->tables[rule->table_id]; | |
5349 | const struct mf_subfield *sf; | |
5cb7a798 | 5350 | struct flow flow; |
254750ce BP |
5351 | uint32_t hash; |
5352 | ||
5353 | hash = table->eviction_group_id_basis; | |
5cb7a798 | 5354 | miniflow_expand(&rule->cr.match.flow, &flow); |
254750ce BP |
5355 | for (sf = table->eviction_fields; |
5356 | sf < &table->eviction_fields[table->n_eviction_fields]; | |
5357 | sf++) | |
5358 | { | |
5cb7a798 | 5359 | if (mf_are_prereqs_ok(sf->field, &flow)) { |
254750ce BP |
5360 | union mf_value value; |
5361 | ||
5cb7a798 | 5362 | mf_get_value(sf->field, &flow, &value); |
254750ce BP |
5363 | if (sf->ofs) { |
5364 | bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs); | |
5365 | } | |
5366 | if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) { | |
5367 | unsigned int start = sf->ofs + sf->n_bits; | |
5368 | bitwise_zero(&value, sf->field->n_bytes, start, | |
5369 | sf->field->n_bytes * 8 - start); | |
5370 | } | |
5371 | hash = hash_bytes(&value, sf->field->n_bytes, hash); | |
5372 | } else { | |
5373 | hash = hash_int(hash, 0); | |
5374 | } | |
5375 | } | |
5376 | ||
5377 | return hash; | |
5378 | } | |
5379 | ||
5380 | /* Returns an eviction group within 'table' with the given 'id', creating one | |
5381 | * if necessary. */ | |
5382 | static struct eviction_group * | |
5383 | eviction_group_find(struct oftable *table, uint32_t id) | |
5384 | { | |
5385 | struct eviction_group *evg; | |
5386 | ||
5387 | HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) { | |
5388 | return evg; | |
5389 | } | |
5390 | ||
5391 | evg = xmalloc(sizeof *evg); | |
5392 | hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id); | |
5393 | heap_insert(&table->eviction_groups_by_size, &evg->size_node, | |
5394 | eviction_group_priority(0)); | |
5395 | heap_init(&evg->rules); | |
5396 | ||
5397 | return evg; | |
5398 | } | |
5399 | ||
5400 | /* Returns an eviction priority for 'rule'. The return value should be | |
5401 | * interpreted so that higher priorities make a rule more attractive candidates | |
5402 | * for eviction. */ | |
5403 | static uint32_t | |
5404 | rule_eviction_priority(struct rule *rule) | |
5405 | { | |
5406 | long long int hard_expiration; | |
5407 | long long int idle_expiration; | |
5408 | long long int expiration; | |
5409 | uint32_t expiration_offset; | |
5410 | ||
5411 | /* Calculate time of expiration. */ | |
a3779dbc | 5412 | ovs_mutex_lock(&rule->timeout_mutex); |
254750ce BP |
5413 | hard_expiration = (rule->hard_timeout |
5414 | ? rule->modified + rule->hard_timeout * 1000 | |
5415 | : LLONG_MAX); | |
5416 | idle_expiration = (rule->idle_timeout | |
5417 | ? rule->used + rule->idle_timeout * 1000 | |
5418 | : LLONG_MAX); | |
5419 | expiration = MIN(hard_expiration, idle_expiration); | |
a3779dbc | 5420 | ovs_mutex_unlock(&rule->timeout_mutex); |
254750ce BP |
5421 | if (expiration == LLONG_MAX) { |
5422 | return 0; | |
5423 | } | |
5424 | ||
5425 | /* Calculate the time of expiration as a number of (approximate) seconds | |
5426 | * after program startup. | |
5427 | * | |
5428 | * This should work OK for program runs that last UINT32_MAX seconds or | |
5429 | * less. Therefore, please restart OVS at least once every 136 years. */ | |
5430 | expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10); | |
5431 | ||
5432 | /* Invert the expiration offset because we're using a max-heap. */ | |
5433 | return UINT32_MAX - expiration_offset; | |
5434 | } | |
5435 | ||
5436 | /* Adds 'rule' to an appropriate eviction group for its oftable's | |
5437 | * configuration. Does nothing if 'rule''s oftable doesn't have eviction | |
5438 | * enabled, or if 'rule' is a permanent rule (one that will never expire on its | |
5439 | * own). | |
5440 | * | |
5441 | * The caller must ensure that 'rule' is not already in an eviction group. */ | |
5442 | static void | |
5443 | eviction_group_add_rule(struct rule *rule) | |
5444 | { | |
5445 | struct ofproto *ofproto = rule->ofproto; | |
5446 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
a3779dbc | 5447 | bool has_timeout; |
254750ce | 5448 | |
a3779dbc EJ |
5449 | ovs_mutex_lock(&rule->timeout_mutex); |
5450 | has_timeout = rule->hard_timeout || rule->idle_timeout; | |
5451 | ovs_mutex_unlock(&rule->timeout_mutex); | |
5452 | ||
5453 | if (table->eviction_fields && has_timeout) { | |
254750ce BP |
5454 | struct eviction_group *evg; |
5455 | ||
5456 | evg = eviction_group_find(table, eviction_group_hash_rule(rule)); | |
5457 | ||
5458 | rule->eviction_group = evg; | |
5459 | heap_insert(&evg->rules, &rule->evg_node, | |
5460 | rule_eviction_priority(rule)); | |
5461 | eviction_group_resized(table, evg); | |
5462 | } | |
5463 | } | |
5464 | \f | |
d0918789 BP |
5465 | /* oftables. */ |
5466 | ||
5467 | /* Initializes 'table'. */ | |
5468 | static void | |
5469 | oftable_init(struct oftable *table) | |
5470 | { | |
5c67e4af | 5471 | memset(table, 0, sizeof *table); |
d0918789 | 5472 | classifier_init(&table->cls); |
ec1c5c7e | 5473 | table->max_flows = UINT_MAX; |
d0918789 BP |
5474 | } |
5475 | ||
254750ce | 5476 | /* Destroys 'table', including its classifier and eviction groups. |
d0918789 BP |
5477 | * |
5478 | * The caller is responsible for freeing 'table' itself. */ | |
5479 | static void | |
5480 | oftable_destroy(struct oftable *table) | |
5481 | { | |
0b4f2078 | 5482 | ovs_rwlock_rdlock(&table->cls.rwlock); |
cb22974d | 5483 | ovs_assert(classifier_is_empty(&table->cls)); |
0b4f2078 | 5484 | ovs_rwlock_unlock(&table->cls.rwlock); |
254750ce | 5485 | oftable_disable_eviction(table); |
d0918789 | 5486 | classifier_destroy(&table->cls); |
254750ce BP |
5487 | free(table->name); |
5488 | } | |
5489 | ||
5490 | /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty | |
5491 | * string, then 'table' will use its default name. | |
5492 | * | |
5493 | * This only affects the name exposed for a table exposed through the OpenFlow | |
5494 | * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */ | |
5495 | static void | |
5496 | oftable_set_name(struct oftable *table, const char *name) | |
5497 | { | |
5498 | if (name && name[0]) { | |
5499 | int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN); | |
5500 | if (!table->name || strncmp(name, table->name, len)) { | |
5501 | free(table->name); | |
5502 | table->name = xmemdup0(name, len); | |
5503 | } | |
5504 | } else { | |
5505 | free(table->name); | |
5506 | table->name = NULL; | |
5507 | } | |
5508 | } | |
5509 | ||
5510 | /* oftables support a choice of two policies when adding a rule would cause the | |
5511 | * number of flows in the table to exceed the configured maximum number: either | |
5512 | * they can refuse to add the new flow or they can evict some existing flow. | |
5513 | * This function configures the former policy on 'table'. */ | |
5514 | static void | |
5515 | oftable_disable_eviction(struct oftable *table) | |
5516 | { | |
5517 | if (table->eviction_fields) { | |
5518 | struct eviction_group *evg, *next; | |
5519 | ||
5520 | HMAP_FOR_EACH_SAFE (evg, next, id_node, | |
5521 | &table->eviction_groups_by_id) { | |
5522 | eviction_group_destroy(table, evg); | |
5523 | } | |
5524 | hmap_destroy(&table->eviction_groups_by_id); | |
5525 | heap_destroy(&table->eviction_groups_by_size); | |
5526 | ||
5527 | free(table->eviction_fields); | |
5528 | table->eviction_fields = NULL; | |
5529 | table->n_eviction_fields = 0; | |
5530 | } | |
5531 | } | |
5532 | ||
5533 | /* oftables support a choice of two policies when adding a rule would cause the | |
5534 | * number of flows in the table to exceed the configured maximum number: either | |
5535 | * they can refuse to add the new flow or they can evict some existing flow. | |
5536 | * This function configures the latter policy on 'table', with fairness based | |
5537 | * on the values of the 'n_fields' fields specified in 'fields'. (Specifying | |
5538 | * 'n_fields' as 0 disables fairness.) */ | |
5539 | static void | |
5540 | oftable_enable_eviction(struct oftable *table, | |
5541 | const struct mf_subfield *fields, size_t n_fields) | |
5542 | { | |
5543 | struct cls_cursor cursor; | |
5544 | struct rule *rule; | |
5545 | ||
5546 | if (table->eviction_fields | |
5547 | && n_fields == table->n_eviction_fields | |
5548 | && (!n_fields | |
5549 | || !memcmp(fields, table->eviction_fields, | |
5550 | n_fields * sizeof *fields))) { | |
5551 | /* No change. */ | |
5552 | return; | |
5553 | } | |
5554 | ||
5555 | oftable_disable_eviction(table); | |
5556 | ||
5557 | table->n_eviction_fields = n_fields; | |
5558 | table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields); | |
5559 | ||
5560 | table->eviction_group_id_basis = random_uint32(); | |
5561 | hmap_init(&table->eviction_groups_by_id); | |
5562 | heap_init(&table->eviction_groups_by_size); | |
5563 | ||
0b4f2078 | 5564 | ovs_rwlock_rdlock(&table->cls.rwlock); |
254750ce BP |
5565 | cls_cursor_init(&cursor, &table->cls, NULL); |
5566 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
5567 | eviction_group_add_rule(rule); | |
5568 | } | |
0b4f2078 | 5569 | ovs_rwlock_unlock(&table->cls.rwlock); |
d0918789 BP |
5570 | } |
5571 | ||
5572 | /* Removes 'rule' from the oftable that contains it. */ | |
5573 | static void | |
0b4f2078 | 5574 | oftable_remove_rule__(struct ofproto *ofproto, struct classifier *cls, |
47b52c71 BP |
5575 | struct rule *rule) |
5576 | OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->evict) | |
d0918789 | 5577 | { |
0b4f2078 | 5578 | classifier_remove(cls, &rule->cr); |
9cae45dc JR |
5579 | if (rule->meter_id) { |
5580 | list_remove(&rule->meter_list_node); | |
5581 | } | |
98eaac36 | 5582 | cookies_remove(ofproto, rule); |
254750ce | 5583 | eviction_group_remove_rule(rule); |
83b16b98 | 5584 | ovs_mutex_lock(&ofproto->expirable_mutex); |
e503cc19 SH |
5585 | if (!list_is_empty(&rule->expirable)) { |
5586 | list_remove(&rule->expirable); | |
5587 | } | |
83b16b98 | 5588 | ovs_mutex_unlock(&ofproto->expirable_mutex); |
9cae45dc JR |
5589 | if (!list_is_empty(&rule->meter_list_node)) { |
5590 | list_remove(&rule->meter_list_node); | |
5591 | } | |
c278bb82 | 5592 | ovs_rwlock_unlock(&rule->evict); |
d0918789 BP |
5593 | } |
5594 | ||
0b4f2078 EJ |
5595 | static void |
5596 | oftable_remove_rule(struct rule *rule) | |
5597 | { | |
5598 | struct ofproto *ofproto = rule->ofproto; | |
5599 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
5600 | ||
5601 | ovs_rwlock_wrlock(&table->cls.rwlock); | |
5602 | oftable_remove_rule__(ofproto, &table->cls, rule); | |
5603 | ovs_rwlock_unlock(&table->cls.rwlock); | |
5604 | } | |
5605 | ||
b277c7cc BP |
5606 | /* Inserts 'rule' into its oftable, which must not already contain any rule for |
5607 | * the same cls_rule. */ | |
5608 | static void | |
5609 | oftable_insert_rule(struct rule *rule) | |
d0918789 BP |
5610 | { |
5611 | struct ofproto *ofproto = rule->ofproto; | |
5612 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
a3779dbc EJ |
5613 | bool may_expire; |
5614 | ||
5615 | ovs_mutex_lock(&rule->timeout_mutex); | |
5616 | may_expire = rule->hard_timeout || rule->idle_timeout; | |
5617 | ovs_mutex_unlock(&rule->timeout_mutex); | |
e503cc19 SH |
5618 | |
5619 | if (may_expire) { | |
83b16b98 | 5620 | ovs_mutex_lock(&ofproto->expirable_mutex); |
e503cc19 | 5621 | list_insert(&ofproto->expirable, &rule->expirable); |
83b16b98 | 5622 | ovs_mutex_unlock(&ofproto->expirable_mutex); |
e503cc19 | 5623 | } |
98eaac36 | 5624 | cookies_insert(ofproto, rule); |
9cae45dc JR |
5625 | if (rule->meter_id) { |
5626 | struct meter *meter = ofproto->meters[rule->meter_id]; | |
5627 | list_insert(&meter->rules, &rule->meter_list_node); | |
5628 | } | |
0b4f2078 | 5629 | ovs_rwlock_wrlock(&table->cls.rwlock); |
b277c7cc | 5630 | classifier_insert(&table->cls, &rule->cr); |
0b4f2078 | 5631 | ovs_rwlock_unlock(&table->cls.rwlock); |
254750ce | 5632 | eviction_group_add_rule(rule); |
d0918789 BP |
5633 | } |
5634 | \f | |
abe529af | 5635 | /* unixctl commands. */ |
7aa697dd | 5636 | |
abe529af | 5637 | struct ofproto * |
2ac6bedd | 5638 | ofproto_lookup(const char *name) |
7aa697dd | 5639 | { |
2ac6bedd | 5640 | struct ofproto *ofproto; |
7aa697dd | 5641 | |
2ac6bedd BP |
5642 | HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0), |
5643 | &all_ofprotos) { | |
5644 | if (!strcmp(ofproto->name, name)) { | |
5645 | return ofproto; | |
5646 | } | |
7aa697dd | 5647 | } |
2ac6bedd | 5648 | return NULL; |
7aa697dd BP |
5649 | } |
5650 | ||
5651 | static void | |
0e15264f BP |
5652 | ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED, |
5653 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
7aa697dd | 5654 | { |
7aa697dd | 5655 | struct ofproto *ofproto; |
7aa697dd | 5656 | struct ds results; |
7aa697dd | 5657 | |
7aa697dd | 5658 | ds_init(&results); |
2ac6bedd BP |
5659 | HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) { |
5660 | ds_put_format(&results, "%s\n", ofproto->name); | |
7aa697dd | 5661 | } |
bde9f75d | 5662 | unixctl_command_reply(conn, ds_cstr(&results)); |
7aa697dd | 5663 | ds_destroy(&results); |
7aa697dd BP |
5664 | } |
5665 | ||
5666 | static void | |
5667 | ofproto_unixctl_init(void) | |
5668 | { | |
5669 | static bool registered; | |
5670 | if (registered) { | |
5671 | return; | |
5672 | } | |
5673 | registered = true; | |
5674 | ||
0e15264f BP |
5675 | unixctl_command_register("ofproto/list", "", 0, 0, |
5676 | ofproto_unixctl_list, NULL); | |
064af421 | 5677 | } |
52a90c29 BP |
5678 | \f |
5679 | /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.) | |
5680 | * | |
5681 | * This is deprecated. It is only for compatibility with broken device drivers | |
5682 | * in old versions of Linux that do not properly support VLANs when VLAN | |
5683 | * devices are not used. When broken device drivers are no longer in | |
5684 | * widespread use, we will delete these interfaces. */ | |
5685 | ||
5686 | /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched | |
5687 | * (exactly) by an OpenFlow rule in 'ofproto'. */ | |
5688 | void | |
5689 | ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap) | |
5690 | { | |
d0918789 | 5691 | const struct oftable *oftable; |
52a90c29 BP |
5692 | |
5693 | free(ofproto->vlan_bitmap); | |
5694 | ofproto->vlan_bitmap = bitmap_allocate(4096); | |
5695 | ofproto->vlans_changed = false; | |
5696 | ||
d0918789 | 5697 | OFPROTO_FOR_EACH_TABLE (oftable, ofproto) { |
52a90c29 BP |
5698 | const struct cls_table *table; |
5699 | ||
d0918789 | 5700 | HMAP_FOR_EACH (table, hmap_node, &oftable->cls.tables) { |
5cb7a798 | 5701 | if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) { |
52a90c29 BP |
5702 | const struct cls_rule *rule; |
5703 | ||
5704 | HMAP_FOR_EACH (rule, hmap_node, &table->rules) { | |
5cb7a798 | 5705 | uint16_t vid = miniflow_get_vid(&rule->match.flow); |
52a90c29 BP |
5706 | bitmap_set1(vlan_bitmap, vid); |
5707 | bitmap_set1(ofproto->vlan_bitmap, vid); | |
5708 | } | |
5709 | } | |
5710 | } | |
5711 | } | |
5712 | } | |
5713 | ||
5714 | /* Returns true if new VLANs have come into use by the flow table since the | |
5715 | * last call to ofproto_get_vlan_usage(). | |
5716 | * | |
5717 | * We don't track when old VLANs stop being used. */ | |
5718 | bool | |
5719 | ofproto_has_vlan_usage_changed(const struct ofproto *ofproto) | |
5720 | { | |
5721 | return ofproto->vlans_changed; | |
5722 | } | |
5723 | ||
5724 | /* Configures a VLAN splinter binding between the ports identified by OpenFlow | |
5725 | * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If | |
5726 | * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real | |
5727 | * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero, | |
5728 | * then the VLAN device is un-enslaved. */ | |
5729 | int | |
4e022ec0 AW |
5730 | ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port, |
5731 | ofp_port_t realdev_ofp_port, int vid) | |
52a90c29 BP |
5732 | { |
5733 | struct ofport *ofport; | |
5734 | int error; | |
5735 | ||
cb22974d | 5736 | ovs_assert(vlandev_ofp_port != realdev_ofp_port); |
52a90c29 BP |
5737 | |
5738 | ofport = ofproto_get_port(ofproto, vlandev_ofp_port); | |
5739 | if (!ofport) { | |
5740 | VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16, | |
5741 | ofproto->name, vlandev_ofp_port); | |
5742 | return EINVAL; | |
5743 | } | |
5744 | ||
5745 | if (!ofproto->ofproto_class->set_realdev) { | |
5746 | if (!vlandev_ofp_port) { | |
5747 | return 0; | |
5748 | } | |
5749 | VLOG_WARN("%s: vlan splinters not supported", ofproto->name); | |
5750 | return EOPNOTSUPP; | |
5751 | } | |
5752 | ||
5753 | error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid); | |
5754 | if (error) { | |
5755 | VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)", | |
5756 | ofproto->name, vlandev_ofp_port, | |
10a89ef0 | 5757 | netdev_get_name(ofport->netdev), ovs_strerror(error)); |
52a90c29 BP |
5758 | } |
5759 | return error; | |
5760 | } |