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