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