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