]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
04f48a68 | 2 | * Copyright (c) 2009-2017 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> | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <inttypes.h> | |
064af421 BP |
21 | #include <stdbool.h> |
22 | #include <stdlib.h> | |
448a4b2f | 23 | #include <unistd.h> |
b598f214 | 24 | |
52a90c29 | 25 | #include "bitmap.h" |
b598f214 | 26 | #include "bundles.h" |
10a24935 | 27 | #include "byte-order.h" |
064af421 | 28 | #include "classifier.h" |
da4a6191 | 29 | #include "connectivity.h" |
19a87e36 | 30 | #include "connmgr.h" |
064af421 | 31 | #include "coverage.h" |
b598f214 | 32 | #include "dp-packet.h" |
ca0f572c | 33 | #include "hash.h" |
ee89ea7b | 34 | #include "openvswitch/hmap.h" |
064af421 | 35 | #include "netdev.h" |
09246b99 | 36 | #include "nx-match.h" |
b598f214 | 37 | #include "ofproto.h" |
5bee6e26 | 38 | #include "ofproto-provider.h" |
064af421 BP |
39 | #include "openflow/nicira-ext.h" |
40 | #include "openflow/openflow.h" | |
d271907f BW |
41 | #include "openvswitch/dynamic-string.h" |
42 | #include "openvswitch/meta-flow.h" | |
b598f214 | 43 | #include "openvswitch/ofp-actions.h" |
d271907f BW |
44 | #include "openvswitch/ofp-errors.h" |
45 | #include "openvswitch/ofp-msgs.h" | |
25d436fb | 46 | #include "openvswitch/ofp-print.h" |
d271907f BW |
47 | #include "openvswitch/ofp-util.h" |
48 | #include "openvswitch/ofpbuf.h" | |
49 | #include "openvswitch/vlog.h" | |
06a0f3e2 | 50 | #include "ovs-rcu.h" |
064af421 BP |
51 | #include "packets.h" |
52 | #include "pinsched.h" | |
064af421 | 53 | #include "poll-loop.h" |
254750ce | 54 | #include "random.h" |
da4a6191 | 55 | #include "seq.h" |
ee89ea7b | 56 | #include "openvswitch/shash.h" |
0d085684 | 57 | #include "simap.h" |
e8f9a7bb | 58 | #include "smap.h" |
b3c01ed3 | 59 | #include "sset.h" |
064af421 | 60 | #include "timeval.h" |
9558d2a5 | 61 | #include "tun-metadata.h" |
c4617b3c | 62 | #include "unaligned.h" |
4f2cad2c | 63 | #include "unixctl.h" |
ee89ea7b | 64 | #include "util.h" |
064af421 | 65 | |
d98e6007 | 66 | VLOG_DEFINE_THIS_MODULE(ofproto); |
064af421 | 67 | |
d76f09ea | 68 | COVERAGE_DEFINE(ofproto_flush); |
d76f09ea BP |
69 | COVERAGE_DEFINE(ofproto_packet_out); |
70 | COVERAGE_DEFINE(ofproto_queue_req); | |
71 | COVERAGE_DEFINE(ofproto_recv_openflow); | |
72 | COVERAGE_DEFINE(ofproto_reinit_ports); | |
d76f09ea BP |
73 | COVERAGE_DEFINE(ofproto_update_port); |
74 | ||
f017d986 JR |
75 | /* Default fields to use for prefix tries in each flow table, unless something |
76 | * else is configured. */ | |
77 | const enum mf_field_id default_prefix_fields[2] = | |
78 | { MFF_IPV4_DST, MFF_IPV4_SRC }; | |
79 | ||
d0918789 BP |
80 | /* oftable. */ |
81 | static void oftable_init(struct oftable *); | |
82 | static void oftable_destroy(struct oftable *); | |
878ae780 | 83 | |
254750ce BP |
84 | static void oftable_set_name(struct oftable *, const char *name); |
85 | ||
6787a49f | 86 | static enum ofperr evict_rules_from_table(struct oftable *) |
834fe5cb | 87 | OVS_REQUIRES(ofproto_mutex); |
82c22d34 BP |
88 | static void oftable_configure_eviction(struct oftable *, |
89 | unsigned int eviction, | |
90 | const struct mf_subfield *fields, | |
91 | size_t n_fields) | |
7394b8fb | 92 | OVS_REQUIRES(ofproto_mutex); |
254750ce | 93 | |
82c22d34 BP |
94 | /* This is the only combination of OpenFlow eviction flags that OVS supports: a |
95 | * combination of OF1.4+ importance, the remaining lifetime of the flow, and | |
96 | * fairness based on user-specified fields. */ | |
97 | #define OFPROTO_EVICTION_FLAGS \ | |
98 | (OFPTMPEF14_OTHER | OFPTMPEF14_IMPORTANCE | OFPTMPEF14_LIFETIME) | |
99 | ||
254750ce BP |
100 | /* A set of rules within a single OpenFlow table (oftable) that have the same |
101 | * values for the oftable's eviction_fields. A rule to be evicted, when one is | |
102 | * needed, is taken from the eviction group that contains the greatest number | |
103 | * of rules. | |
104 | * | |
105 | * An oftable owns any number of eviction groups, each of which contains any | |
106 | * number of rules. | |
107 | * | |
108 | * Membership in an eviction group is imprecise, based on the hash of the | |
109 | * oftable's eviction_fields (in the eviction_group's id_node.hash member). | |
110 | * That is, if two rules have different eviction_fields, but those | |
111 | * eviction_fields hash to the same value, then they will belong to the same | |
112 | * eviction_group anyway. | |
113 | * | |
114 | * (When eviction is not enabled on an oftable, we don't track any eviction | |
115 | * groups, to save time and space.) */ | |
116 | struct eviction_group { | |
117 | struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */ | |
118 | struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */ | |
119 | struct heap rules; /* Contains "struct rule"s. */ | |
120 | }; | |
121 | ||
3d900aa7 BP |
122 | static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep) |
123 | OVS_REQUIRES(ofproto_mutex); | |
f70b94de BP |
124 | static uint64_t rule_eviction_priority(struct ofproto *ofproto, struct rule *) |
125 | OVS_REQUIRES(ofproto_mutex); | |
3d900aa7 BP |
126 | static void eviction_group_add_rule(struct rule *) |
127 | OVS_REQUIRES(ofproto_mutex); | |
128 | static void eviction_group_remove_rule(struct rule *) | |
129 | OVS_REQUIRES(ofproto_mutex); | |
f29152ca | 130 | |
a9b22b7f | 131 | static void rule_criteria_init(struct rule_criteria *, uint8_t table_id, |
eb391b76 | 132 | const struct match *match, int priority, |
44e0c35d | 133 | ovs_version_t version, |
a9b22b7f BP |
134 | ovs_be64 cookie, ovs_be64 cookie_mask, |
135 | ofp_port_t out_port, uint32_t out_group); | |
dd51dae2 BP |
136 | static void rule_criteria_require_rw(struct rule_criteria *, |
137 | bool can_write_readonly); | |
a9b22b7f BP |
138 | static void rule_criteria_destroy(struct rule_criteria *); |
139 | ||
35f48b8b BP |
140 | static enum ofperr collect_rules_loose(struct ofproto *, |
141 | const struct rule_criteria *, | |
142 | struct rule_collection *); | |
143 | ||
35f48b8b BP |
144 | struct learned_cookie { |
145 | union { | |
146 | /* In struct ofproto's 'learned_cookies' hmap. */ | |
147 | struct hmap_node hmap_node OVS_GUARDED_BY(ofproto_mutex); | |
148 | ||
149 | /* In 'dead_cookies' list when removed from hmap. */ | |
ca6ba700 | 150 | struct ovs_list list_node; |
35f48b8b BP |
151 | } u; |
152 | ||
153 | /* Key. */ | |
154 | ovs_be64 cookie OVS_GUARDED_BY(ofproto_mutex); | |
155 | uint8_t table_id OVS_GUARDED_BY(ofproto_mutex); | |
156 | ||
157 | /* Number of references from "learn" actions. | |
158 | * | |
159 | * When this drops to 0, all of the flows in 'table_id' with the specified | |
160 | * 'cookie' are deleted. */ | |
161 | int n OVS_GUARDED_BY(ofproto_mutex); | |
162 | }; | |
163 | ||
164 | static const struct ofpact_learn *next_learn_with_delete( | |
165 | const struct rule_actions *, const struct ofpact_learn *start); | |
166 | ||
167 | static void learned_cookies_inc(struct ofproto *, const struct rule_actions *) | |
168 | OVS_REQUIRES(ofproto_mutex); | |
169 | static void learned_cookies_dec(struct ofproto *, const struct rule_actions *, | |
ca6ba700 | 170 | struct ovs_list *dead_cookies) |
35f48b8b | 171 | OVS_REQUIRES(ofproto_mutex); |
ca6ba700 | 172 | static void learned_cookies_flush(struct ofproto *, struct ovs_list *dead_cookies) |
35f48b8b BP |
173 | OVS_REQUIRES(ofproto_mutex); |
174 | ||
d0918789 | 175 | /* ofport. */ |
15aaf599 | 176 | static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex); |
9aad5a5a | 177 | static void ofport_destroy(struct ofport *, bool del); |
3a414a0a DDP |
178 | static bool ofport_is_mtu_overridden(const struct ofproto *, |
179 | const struct ofport *); | |
d0918789 | 180 | |
01c77073 | 181 | static int update_port(struct ofproto *, const char *devname); |
d0918789 BP |
182 | static int init_ports(struct ofproto *); |
183 | static void reinit_ports(struct ofproto *); | |
7ee20df1 | 184 | |
fdcea803 GS |
185 | static long long int ofport_get_usage(const struct ofproto *, |
186 | ofp_port_t ofp_port); | |
187 | static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port, | |
188 | long long int last_used); | |
865b26a4 | 189 | static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port); |
fdcea803 GS |
190 | |
191 | /* Ofport usage. | |
192 | * | |
193 | * Keeps track of the currently used and recently used ofport values and is | |
194 | * used to prevent immediate recycling of ofport values. */ | |
195 | struct ofport_usage { | |
196 | struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */ | |
197 | ofp_port_t ofp_port; /* OpenFlow port number. */ | |
198 | long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX | |
199 | represents in-use ofports. */ | |
200 | }; | |
201 | ||
254750ce | 202 | /* rule. */ |
f695ebfa JR |
203 | static void ofproto_rule_send_removed(struct rule *) |
204 | OVS_EXCLUDED(ofproto_mutex); | |
834fe5cb | 205 | static bool rule_is_readonly(const struct rule *); |
bc5e6a91 JR |
206 | static void ofproto_rule_insert__(struct ofproto *, struct rule *) |
207 | OVS_REQUIRES(ofproto_mutex); | |
802f84ff JR |
208 | static void ofproto_rule_remove__(struct ofproto *, struct rule *) |
209 | OVS_REQUIRES(ofproto_mutex); | |
254750ce | 210 | |
0a0d9385 | 211 | /* The source of an OpenFlow request. |
baae3d02 | 212 | * |
0a0d9385 JR |
213 | * A table modification request can be generated externally, via OpenFlow, or |
214 | * internally through a function call. This structure indicates the source of | |
215 | * an OpenFlow-generated table modification. For an internal flow_mod, it | |
216 | * isn't meaningful and thus supplied as NULL. */ | |
217 | struct openflow_mod_requester { | |
baae3d02 | 218 | struct ofconn *ofconn; /* Connection on which flow_mod arrived. */ |
c84d8691 | 219 | const struct ofp_header *request; |
baae3d02 BP |
220 | }; |
221 | ||
d0918789 | 222 | /* OpenFlow. */ |
5bacd5cd JR |
223 | static enum ofperr ofproto_rule_create(struct ofproto *, struct cls_rule *, |
224 | uint8_t table_id, ovs_be64 new_cookie, | |
225 | uint16_t idle_timeout, | |
226 | uint16_t hard_timeout, | |
227 | enum ofputil_flow_mod_flags flags, | |
228 | uint16_t importance, | |
229 | const struct ofpact *ofpacts, | |
230 | size_t ofpacts_len, | |
5c7c16d8 YHW |
231 | uint64_t match_tlv_bitmap, |
232 | uint64_t ofpacts_tlv_bitmap, | |
39c94593 | 233 | struct rule **new_rule) |
5bacd5cd | 234 | OVS_NO_THREAD_SAFETY_ANALYSIS; |
39c94593 | 235 | |
5bacd5cd JR |
236 | static void replace_rule_start(struct ofproto *, struct ofproto_flow_mod *, |
237 | struct rule *old_rule, struct rule *new_rule) | |
dd27be82 | 238 | OVS_REQUIRES(ofproto_mutex); |
39c94593 | 239 | |
6787a49f JR |
240 | static void replace_rule_revert(struct ofproto *, struct rule *old_rule, |
241 | struct rule *new_rule) | |
39c94593 JR |
242 | OVS_REQUIRES(ofproto_mutex); |
243 | ||
81dee635 | 244 | static void replace_rule_finish(struct ofproto *, struct ofproto_flow_mod *, |
0a0d9385 | 245 | const struct openflow_mod_requester *, |
39c94593 JR |
246 | struct rule *old_rule, struct rule *new_rule, |
247 | struct ovs_list *dead_cookies) | |
dd27be82 | 248 | OVS_REQUIRES(ofproto_mutex); |
39c94593 | 249 | static void delete_flows__(struct rule_collection *, |
9ca4a86f | 250 | enum ofp_flow_removed_reason, |
0a0d9385 | 251 | const struct openflow_mod_requester *) |
15aaf599 | 252 | OVS_REQUIRES(ofproto_mutex); |
834fe5cb | 253 | |
db88b35c | 254 | static bool ofproto_group_exists(const struct ofproto *, uint32_t group_id); |
b20f4073 | 255 | static void handle_openflow(struct ofconn *, const struct ofpbuf *); |
5bacd5cd JR |
256 | static enum ofperr ofproto_flow_mod_init(struct ofproto *, |
257 | struct ofproto_flow_mod *, | |
2c7ee524 JR |
258 | const struct ofputil_flow_mod *fm, |
259 | struct rule *) | |
5bacd5cd | 260 | OVS_EXCLUDED(ofproto_mutex); |
8be00367 JR |
261 | static enum ofperr ofproto_flow_mod_start(struct ofproto *, |
262 | struct ofproto_flow_mod *) | |
1f42be1c | 263 | OVS_REQUIRES(ofproto_mutex); |
6dd3c787 JR |
264 | static void ofproto_flow_mod_revert(struct ofproto *, |
265 | struct ofproto_flow_mod *) | |
266 | OVS_REQUIRES(ofproto_mutex); | |
8be00367 JR |
267 | static void ofproto_flow_mod_finish(struct ofproto *, |
268 | struct ofproto_flow_mod *, | |
0a0d9385 | 269 | const struct openflow_mod_requester *) |
1f42be1c | 270 | OVS_REQUIRES(ofproto_mutex); |
baae3d02 | 271 | static enum ofperr handle_flow_mod__(struct ofproto *, |
7338102b | 272 | const struct ofputil_flow_mod *, |
0a0d9385 | 273 | const struct openflow_mod_requester *) |
15aaf599 | 274 | OVS_EXCLUDED(ofproto_mutex); |
65e0be10 BP |
275 | static void calc_duration(long long int start, long long int now, |
276 | uint32_t *sec, uint32_t *nsec); | |
f29152ca | 277 | |
d0918789 BP |
278 | /* ofproto. */ |
279 | static uint64_t pick_datapath_id(const struct ofproto *); | |
280 | static uint64_t pick_fallback_dpid(void); | |
281 | static void ofproto_destroy__(struct ofproto *); | |
ada3428f | 282 | static void update_mtu(struct ofproto *, struct ofport *); |
0670b5d0 | 283 | static void update_mtu_ofproto(struct ofproto *); |
6b3f7f02 | 284 | static void meter_delete(struct ofproto *, uint32_t first, uint32_t last); |
062fea06 | 285 | static void meter_insert_rule(struct rule *); |
f29152ca | 286 | |
d0918789 | 287 | /* unixctl. */ |
abe529af | 288 | static void ofproto_unixctl_init(void); |
f29152ca | 289 | |
abe529af BP |
290 | /* All registered ofproto classes, in probe order. */ |
291 | static const struct ofproto_class **ofproto_classes; | |
292 | static size_t n_ofproto_classes; | |
293 | static size_t allocated_ofproto_classes; | |
7aa697dd | 294 | |
15aaf599 BP |
295 | /* Global lock that protects all flow table operations. */ |
296 | struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER; | |
abe7b10f | 297 | |
e79a6c83 | 298 | unsigned ofproto_flow_limit = OFPROTO_FLOW_LIMIT_DEFAULT; |
72310b04 | 299 | unsigned ofproto_max_idle = OFPROTO_MAX_IDLE_DEFAULT; |
380f49c4 | 300 | |
e79a6c83 | 301 | size_t n_handlers, n_revalidators; |
6567010f | 302 | |
f797957a | 303 | /* Map from datapath name to struct ofproto, for use by unixctl commands. */ |
abe529af | 304 | static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos); |
ebe482fd | 305 | |
e1b1d06a JP |
306 | /* Initial mappings of port to OpenFlow number mappings. */ |
307 | static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports); | |
308 | ||
abe529af | 309 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
f29152ca | 310 | |
40358701 GS |
311 | /* The default value of true waits for flow restore. */ |
312 | static bool flow_restore_wait = true; | |
313 | ||
b0408fca JP |
314 | /* Must be called to initialize the ofproto library. |
315 | * | |
316 | * The caller may pass in 'iface_hints', which contains an shash of | |
317 | * "iface_hint" elements indexed by the interface's name. The provider | |
318 | * may use these hints to describe the startup configuration in order to | |
319 | * reinitialize its state. The caller owns the provided data, so a | |
320 | * provider will make copies of anything required. An ofproto provider | |
321 | * will remove any existing state that is not described by the hint, and | |
322 | * may choose to remove it all. */ | |
323 | void | |
324 | ofproto_init(const struct shash *iface_hints) | |
abe529af | 325 | { |
e1b1d06a | 326 | struct shash_node *node; |
b0408fca | 327 | size_t i; |
f29152ca | 328 | |
b0408fca JP |
329 | ofproto_class_register(&ofproto_dpif_class); |
330 | ||
e1b1d06a JP |
331 | /* Make a local copy, since we don't own 'iface_hints' elements. */ |
332 | SHASH_FOR_EACH(node, iface_hints) { | |
333 | const struct iface_hint *orig_hint = node->data; | |
334 | struct iface_hint *new_hint = xmalloc(sizeof *new_hint); | |
335 | const char *br_type = ofproto_normalize_type(orig_hint->br_type); | |
336 | ||
337 | new_hint->br_name = xstrdup(orig_hint->br_name); | |
338 | new_hint->br_type = xstrdup(br_type); | |
339 | new_hint->ofp_port = orig_hint->ofp_port; | |
340 | ||
341 | shash_add(&init_ofp_ports, node->name, new_hint); | |
342 | } | |
343 | ||
b0408fca | 344 | for (i = 0; i < n_ofproto_classes; i++) { |
e1b1d06a | 345 | ofproto_classes[i]->init(&init_ofp_ports); |
abe529af | 346 | } |
0fc1f5c0 HH |
347 | |
348 | ofproto_unixctl_init(); | |
abe529af | 349 | } |
f29152ca | 350 | |
abe529af BP |
351 | /* 'type' should be a normalized datapath type, as returned by |
352 | * ofproto_normalize_type(). Returns the corresponding ofproto_class | |
353 | * structure, or a null pointer if there is none registered for 'type'. */ | |
354 | static const struct ofproto_class * | |
355 | ofproto_class_find__(const char *type) | |
356 | { | |
357 | size_t i; | |
f29152ca | 358 | |
abe529af BP |
359 | for (i = 0; i < n_ofproto_classes; i++) { |
360 | const struct ofproto_class *class = ofproto_classes[i]; | |
361 | struct sset types; | |
362 | bool found; | |
064af421 | 363 | |
abe529af BP |
364 | sset_init(&types); |
365 | class->enumerate_types(&types); | |
366 | found = sset_contains(&types, type); | |
367 | sset_destroy(&types); | |
bcf84111 | 368 | |
abe529af BP |
369 | if (found) { |
370 | return class; | |
371 | } | |
372 | } | |
373 | VLOG_WARN("unknown datapath type %s", type); | |
374 | return NULL; | |
375 | } | |
064af421 | 376 | |
abe529af BP |
377 | /* Registers a new ofproto class. After successful registration, new ofprotos |
378 | * of that type can be created using ofproto_create(). */ | |
379 | int | |
380 | ofproto_class_register(const struct ofproto_class *new_class) | |
381 | { | |
382 | size_t i; | |
7aa697dd | 383 | |
abe529af BP |
384 | for (i = 0; i < n_ofproto_classes; i++) { |
385 | if (ofproto_classes[i] == new_class) { | |
386 | return EEXIST; | |
387 | } | |
388 | } | |
064af421 | 389 | |
abe529af BP |
390 | if (n_ofproto_classes >= allocated_ofproto_classes) { |
391 | ofproto_classes = x2nrealloc(ofproto_classes, | |
392 | &allocated_ofproto_classes, | |
393 | sizeof *ofproto_classes); | |
394 | } | |
395 | ofproto_classes[n_ofproto_classes++] = new_class; | |
396 | return 0; | |
397 | } | |
064af421 | 398 | |
abe529af BP |
399 | /* Unregisters a datapath provider. 'type' must have been previously |
400 | * registered and not currently be in use by any ofprotos. After | |
401 | * unregistration new datapaths of that type cannot be opened using | |
402 | * ofproto_create(). */ | |
403 | int | |
404 | ofproto_class_unregister(const struct ofproto_class *class) | |
405 | { | |
406 | size_t i; | |
76ce9432 | 407 | |
abe529af BP |
408 | for (i = 0; i < n_ofproto_classes; i++) { |
409 | if (ofproto_classes[i] == class) { | |
410 | for (i++; i < n_ofproto_classes; i++) { | |
411 | ofproto_classes[i - 1] = ofproto_classes[i]; | |
412 | } | |
413 | n_ofproto_classes--; | |
414 | return 0; | |
415 | } | |
416 | } | |
417 | VLOG_WARN("attempted to unregister an ofproto class that is not " | |
418 | "registered"); | |
419 | return EAFNOSUPPORT; | |
420 | } | |
4a4cdb3b | 421 | |
f79e673f BP |
422 | /* Clears 'types' and enumerates all registered ofproto types into it. The |
423 | * caller must first initialize the sset. */ | |
424 | void | |
425 | ofproto_enumerate_types(struct sset *types) | |
426 | { | |
abe529af | 427 | size_t i; |
064af421 | 428 | |
c799c306 | 429 | sset_clear(types); |
abe529af BP |
430 | for (i = 0; i < n_ofproto_classes; i++) { |
431 | ofproto_classes[i]->enumerate_types(types); | |
432 | } | |
f79e673f | 433 | } |
064af421 | 434 | |
f79e673f BP |
435 | /* Returns the fully spelled out name for the given ofproto 'type'. |
436 | * | |
437 | * Normalized type string can be compared with strcmp(). Unnormalized type | |
438 | * string might be the same even if they have different spellings. */ | |
439 | const char * | |
440 | ofproto_normalize_type(const char *type) | |
441 | { | |
abe529af | 442 | return type && type[0] ? type : "system"; |
f79e673f | 443 | } |
064af421 | 444 | |
f79e673f BP |
445 | /* Clears 'names' and enumerates the names of all known created ofprotos with |
446 | * the given 'type'. The caller must first initialize the sset. Returns 0 if | |
447 | * successful, otherwise a positive errno value. | |
448 | * | |
449 | * Some kinds of datapaths might not be practically enumerable. This is not | |
450 | * considered an error. */ | |
451 | int | |
452 | ofproto_enumerate_names(const char *type, struct sset *names) | |
453 | { | |
abe529af BP |
454 | const struct ofproto_class *class = ofproto_class_find__(type); |
455 | return class ? class->enumerate_names(type, names) : EAFNOSUPPORT; | |
239ad7d1 | 456 | } |
7aa697dd | 457 | |
39c94593 JR |
458 | static void |
459 | ofproto_bump_tables_version(struct ofproto *ofproto) | |
460 | { | |
461 | ++ofproto->tables_version; | |
462 | ofproto->ofproto_class->set_tables_version(ofproto, | |
463 | ofproto->tables_version); | |
464 | } | |
465 | ||
064af421 | 466 | int |
abe529af | 467 | ofproto_create(const char *datapath_name, const char *datapath_type, |
064af421 | 468 | struct ofproto **ofprotop) |
25010c68 | 469 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 470 | { |
abe529af BP |
471 | const struct ofproto_class *class; |
472 | struct ofproto *ofproto; | |
064af421 | 473 | int error; |
c2f0373a | 474 | int i; |
064af421 BP |
475 | |
476 | *ofprotop = NULL; | |
477 | ||
abe529af BP |
478 | datapath_type = ofproto_normalize_type(datapath_type); |
479 | class = ofproto_class_find__(datapath_type); | |
480 | if (!class) { | |
481 | VLOG_WARN("could not create datapath %s of unknown type %s", | |
482 | datapath_name, datapath_type); | |
483 | return EAFNOSUPPORT; | |
064af421 BP |
484 | } |
485 | ||
abe529af BP |
486 | ofproto = class->alloc(); |
487 | if (!ofproto) { | |
488 | VLOG_ERR("failed to allocate datapath %s of type %s", | |
489 | datapath_name, datapath_type); | |
490 | return ENOMEM; | |
491 | } | |
492 | ||
493 | /* Initialize. */ | |
abe7b10f | 494 | ovs_mutex_lock(&ofproto_mutex); |
abe529af BP |
495 | memset(ofproto, 0, sizeof *ofproto); |
496 | ofproto->ofproto_class = class; | |
497 | ofproto->name = xstrdup(datapath_name); | |
498 | ofproto->type = xstrdup(datapath_type); | |
499 | hmap_insert(&all_ofprotos, &ofproto->hmap_node, | |
500 | hash_string(ofproto->name, 0)); | |
501 | ofproto->datapath_id = 0; | |
8402c74b | 502 | ofproto->forward_bpdu = false; |
abe529af | 503 | ofproto->fallback_dpid = pick_fallback_dpid(); |
061bfea4 BP |
504 | ofproto->mfr_desc = NULL; |
505 | ofproto->hw_desc = NULL; | |
506 | ofproto->sw_desc = NULL; | |
507 | ofproto->serial_desc = NULL; | |
508 | ofproto->dp_desc = NULL; | |
ad99e2ed | 509 | ofproto->frag_handling = OFPUTIL_FRAG_NORMAL; |
abe529af | 510 | hmap_init(&ofproto->ports); |
fdcea803 | 511 | hmap_init(&ofproto->ofport_usage); |
abe529af | 512 | shash_init(&ofproto->port_by_name); |
e1b1d06a | 513 | simap_init(&ofproto->ofp_requests); |
430dbb14 | 514 | ofproto->max_ports = ofp_to_u16(OFPP_MAX); |
448c2fa8 | 515 | ofproto->eviction_group_timer = LLONG_MIN; |
6c1491fb BP |
516 | ofproto->tables = NULL; |
517 | ofproto->n_tables = 0; | |
44e0c35d | 518 | ofproto->tables_version = OVS_VERSION_MIN; |
98eaac36 | 519 | hindex_init(&ofproto->cookies); |
35f48b8b | 520 | hmap_init(&ofproto->learned_cookies); |
417e7e66 | 521 | ovs_list_init(&ofproto->expirable); |
abe529af | 522 | ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name); |
7478b5a2 | 523 | ofproto->min_mtu = INT_MAX; |
db88b35c | 524 | cmap_init(&ofproto->groups); |
abe7b10f | 525 | ovs_mutex_unlock(&ofproto_mutex); |
0ae01c64 | 526 | ofproto->ogf.types = 0xf; |
7cb279c2 SH |
527 | ofproto->ogf.capabilities = OFPGFC_CHAINING | OFPGFC_SELECT_LIVENESS | |
528 | OFPGFC_SELECT_WEIGHT; | |
08d1e234 BP |
529 | for (i = 0; i < 4; i++) { |
530 | ofproto->ogf.max_groups[i] = OFPG_MAX; | |
531 | ofproto->ogf.ofpacts[i] = (UINT64_C(1) << N_OFPACTS) - 1; | |
532 | } | |
8d8ab6c2 | 533 | ovsrcu_set(&ofproto->metadata_tab, tun_metadata_alloc(NULL)); |
abe529af | 534 | |
04f48a68 YHW |
535 | ovs_mutex_init(&ofproto->vl_mff_map.mutex); |
536 | cmap_init(&ofproto->vl_mff_map.cmap); | |
537 | ||
0f5f95a9 | 538 | error = ofproto->ofproto_class->construct(ofproto); |
19a87e36 | 539 | if (error) { |
abe529af | 540 | VLOG_ERR("failed to open datapath %s: %s", |
10a89ef0 | 541 | datapath_name, ovs_strerror(error)); |
25010c68 | 542 | ovs_mutex_lock(&ofproto_mutex); |
58f19539 | 543 | connmgr_destroy(ofproto->connmgr); |
25010c68 JR |
544 | ofproto->connmgr = NULL; |
545 | ovs_mutex_unlock(&ofproto_mutex); | |
abe529af | 546 | ofproto_destroy__(ofproto); |
19a87e36 BP |
547 | return error; |
548 | } | |
073e2a6f | 549 | |
c2f0373a | 550 | /* Check that hidden tables, if any, are at the end. */ |
cb22974d | 551 | ovs_assert(ofproto->n_tables); |
c2f0373a BP |
552 | for (i = 0; i + 1 < ofproto->n_tables; i++) { |
553 | enum oftable_flags flags = ofproto->tables[i].flags; | |
554 | enum oftable_flags next_flags = ofproto->tables[i + 1].flags; | |
555 | ||
cb22974d | 556 | ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN); |
c2f0373a | 557 | } |
19a87e36 | 558 | |
abe529af | 559 | ofproto->datapath_id = pick_datapath_id(ofproto); |
abe529af | 560 | init_ports(ofproto); |
7aa697dd | 561 | |
9cae45dc JR |
562 | /* Initialize meters table. */ |
563 | if (ofproto->ofproto_class->meter_get_features) { | |
564 | ofproto->ofproto_class->meter_get_features(ofproto, | |
565 | &ofproto->meter_features); | |
566 | } else { | |
567 | memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features); | |
568 | } | |
569 | ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1) | |
570 | * sizeof(struct meter *)); | |
571 | ||
621b8064 | 572 | /* Set the initial tables version. */ |
39c94593 | 573 | ofproto_bump_tables_version(ofproto); |
621b8064 | 574 | |
abe529af | 575 | *ofprotop = ofproto; |
064af421 BP |
576 | return 0; |
577 | } | |
578 | ||
91858960 BP |
579 | /* Must be called (only) by an ofproto implementation in its constructor |
580 | * function. See the large comment on 'construct' in struct ofproto_class for | |
581 | * details. */ | |
0f5f95a9 BP |
582 | void |
583 | ofproto_init_tables(struct ofproto *ofproto, int n_tables) | |
584 | { | |
585 | struct oftable *table; | |
586 | ||
cb22974d BP |
587 | ovs_assert(!ofproto->n_tables); |
588 | ovs_assert(n_tables >= 1 && n_tables <= 255); | |
0f5f95a9 BP |
589 | |
590 | ofproto->n_tables = n_tables; | |
591 | ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables); | |
592 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
593 | oftable_init(table); | |
594 | } | |
595 | } | |
596 | ||
91858960 BP |
597 | /* To be optionally called (only) by an ofproto implementation in its |
598 | * constructor function. See the large comment on 'construct' in struct | |
599 | * ofproto_class for details. | |
600 | * | |
601 | * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer | |
602 | * will then ensure that actions passed into the ofproto implementation will | |
603 | * not refer to OpenFlow ports numbered 'max_ports' or higher. If this | |
604 | * function is not called, there will be no such restriction. | |
605 | * | |
606 | * Reserved ports numbered OFPP_MAX and higher are special and not subject to | |
607 | * the 'max_ports' restriction. */ | |
608 | void | |
430dbb14 | 609 | ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports) |
91858960 | 610 | { |
430dbb14 | 611 | ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX)); |
91858960 BP |
612 | ofproto->max_ports = max_ports; |
613 | } | |
614 | ||
e825ace2 BP |
615 | uint64_t |
616 | ofproto_get_datapath_id(const struct ofproto *ofproto) | |
617 | { | |
618 | return ofproto->datapath_id; | |
619 | } | |
620 | ||
064af421 BP |
621 | void |
622 | ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id) | |
623 | { | |
624 | uint64_t old_dpid = p->datapath_id; | |
fa60c019 | 625 | p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p); |
064af421 | 626 | if (p->datapath_id != old_dpid) { |
76ce9432 BP |
627 | /* Force all active connections to reconnect, since there is no way to |
628 | * notify a controller that the datapath ID has changed. */ | |
fa05809b | 629 | ofproto_reconnect_controllers(p); |
064af421 BP |
630 | } |
631 | } | |
632 | ||
76ce9432 BP |
633 | void |
634 | ofproto_set_controllers(struct ofproto *p, | |
635 | const struct ofproto_controller *controllers, | |
1d9ffc17 | 636 | size_t n_controllers, uint32_t allowed_versions) |
76ce9432 | 637 | { |
1d9ffc17 SH |
638 | connmgr_set_controllers(p->connmgr, controllers, n_controllers, |
639 | allowed_versions); | |
064af421 BP |
640 | } |
641 | ||
31681a5d JP |
642 | void |
643 | ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode) | |
644 | { | |
19a87e36 | 645 | connmgr_set_fail_mode(p->connmgr, fail_mode); |
31681a5d JP |
646 | } |
647 | ||
fa05809b BP |
648 | /* Drops the connections between 'ofproto' and all of its controllers, forcing |
649 | * them to reconnect. */ | |
650 | void | |
651 | ofproto_reconnect_controllers(struct ofproto *ofproto) | |
652 | { | |
19a87e36 | 653 | connmgr_reconnect(ofproto->connmgr); |
917e50e1 BP |
654 | } |
655 | ||
656 | /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s | |
657 | * in-band control should guarantee access, in the same way that in-band | |
658 | * control guarantees access to OpenFlow controllers. */ | |
659 | void | |
660 | ofproto_set_extra_in_band_remotes(struct ofproto *ofproto, | |
661 | const struct sockaddr_in *extras, size_t n) | |
662 | { | |
19a87e36 | 663 | connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n); |
917e50e1 BP |
664 | } |
665 | ||
b1da6250 BP |
666 | /* Sets the OpenFlow queue used by flows set up by in-band control on |
667 | * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control | |
668 | * flows will use the default queue. */ | |
669 | void | |
670 | ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id) | |
671 | { | |
19a87e36 | 672 | connmgr_set_in_band_queue(ofproto->connmgr, queue_id); |
b1da6250 BP |
673 | } |
674 | ||
084f5290 SH |
675 | /* Sets the number of flows at which eviction from the kernel flow table |
676 | * will occur. */ | |
677 | void | |
e79a6c83 | 678 | ofproto_set_flow_limit(unsigned limit) |
084f5290 | 679 | { |
e79a6c83 | 680 | ofproto_flow_limit = limit; |
084f5290 SH |
681 | } |
682 | ||
72310b04 JS |
683 | /* Sets the maximum idle time for flows in the datapath before they are |
684 | * expired. */ | |
685 | void | |
686 | ofproto_set_max_idle(unsigned max_idle) | |
687 | { | |
688 | ofproto_max_idle = max_idle; | |
689 | } | |
690 | ||
b53055f4 | 691 | /* If forward_bpdu is true, the NORMAL action will forward frames with |
8402c74b SS |
692 | * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false, |
693 | * the NORMAL action will drop these frames. */ | |
694 | void | |
695 | ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu) | |
696 | { | |
697 | bool old_val = ofproto->forward_bpdu; | |
698 | ofproto->forward_bpdu = forward_bpdu; | |
699 | if (old_val != ofproto->forward_bpdu) { | |
700 | if (ofproto->ofproto_class->forward_bpdu_changed) { | |
701 | ofproto->ofproto_class->forward_bpdu_changed(ofproto); | |
702 | } | |
b53055f4 | 703 | } |
8402c74b SS |
704 | } |
705 | ||
e764773c | 706 | /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to |
c4069512 BP |
707 | * 'idle_time', in seconds, and the maximum number of MAC table entries to |
708 | * 'max_entries'. */ | |
e764773c | 709 | void |
c4069512 BP |
710 | ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time, |
711 | size_t max_entries) | |
e764773c | 712 | { |
c4069512 BP |
713 | if (ofproto->ofproto_class->set_mac_table_config) { |
714 | ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time, | |
715 | max_entries); | |
e764773c BP |
716 | } |
717 | } | |
718 | ||
7c38d0a5 FL |
719 | /* Multicast snooping configuration. */ |
720 | ||
721 | /* Configures multicast snooping on 'ofproto' using the settings | |
722 | * defined in 's'. If 's' is NULL, disables multicast snooping. | |
723 | * | |
724 | * Returns 0 if successful, otherwise a positive errno value. */ | |
725 | int | |
726 | ofproto_set_mcast_snooping(struct ofproto *ofproto, | |
727 | const struct ofproto_mcast_snooping_settings *s) | |
728 | { | |
729 | return (ofproto->ofproto_class->set_mcast_snooping | |
730 | ? ofproto->ofproto_class->set_mcast_snooping(ofproto, s) | |
731 | : EOPNOTSUPP); | |
732 | } | |
733 | ||
8e04a33f | 734 | /* Configures multicast snooping flood settings on 'ofp_port' of 'ofproto'. |
7c38d0a5 FL |
735 | * |
736 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
737 | int | |
8e04a33f FL |
738 | ofproto_port_set_mcast_snooping(struct ofproto *ofproto, void *aux, |
739 | const struct ofproto_mcast_snooping_port_settings *s) | |
7c38d0a5 FL |
740 | { |
741 | return (ofproto->ofproto_class->set_mcast_snooping_port | |
8e04a33f | 742 | ? ofproto->ofproto_class->set_mcast_snooping_port(ofproto, aux, s) |
7c38d0a5 FL |
743 | : EOPNOTSUPP); |
744 | } | |
745 | ||
f2eee189 | 746 | void |
d4f6865c | 747 | ofproto_type_set_config(const char *datapath_type, const struct smap *cfg) |
f2eee189 | 748 | { |
d4f6865c DDP |
749 | const struct ofproto_class *class; |
750 | ||
751 | datapath_type = ofproto_normalize_type(datapath_type); | |
752 | class = ofproto_class_find__(datapath_type); | |
753 | ||
754 | if (class->type_set_config) { | |
755 | class->type_set_config(datapath_type, cfg); | |
756 | } | |
f2eee189 AW |
757 | } |
758 | ||
448a4b2f | 759 | void |
30ea0da7 | 760 | ofproto_set_threads(int n_handlers_, int n_revalidators_) |
448a4b2f | 761 | { |
e79a6c83 EJ |
762 | int threads = MAX(count_cpu_cores(), 2); |
763 | ||
30ea0da7 GS |
764 | n_revalidators = MAX(n_revalidators_, 0); |
765 | n_handlers = MAX(n_handlers_, 0); | |
e79a6c83 EJ |
766 | |
767 | if (!n_revalidators) { | |
768 | n_revalidators = n_handlers | |
769 | ? MAX(threads - (int) n_handlers, 1) | |
770 | : threads / 4 + 1; | |
771 | } | |
772 | ||
773 | if (!n_handlers) { | |
774 | n_handlers = MAX(threads - (int) n_revalidators, 1); | |
775 | } | |
448a4b2f AW |
776 | } |
777 | ||
8b6ff729 BP |
778 | void |
779 | ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc) | |
780 | { | |
781 | free(p->dp_desc); | |
2225c0b9 | 782 | p->dp_desc = nullable_xstrdup(dp_desc); |
8b6ff729 BP |
783 | } |
784 | ||
064af421 | 785 | int |
81e2083f | 786 | ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops) |
064af421 | 787 | { |
19a87e36 | 788 | return connmgr_set_snoops(ofproto->connmgr, snoops); |
064af421 BP |
789 | } |
790 | ||
791 | int | |
0193b2af JG |
792 | ofproto_set_netflow(struct ofproto *ofproto, |
793 | const struct netflow_options *nf_options) | |
064af421 | 794 | { |
abe529af BP |
795 | if (nf_options && sset_is_empty(&nf_options->collectors)) { |
796 | nf_options = NULL; | |
797 | } | |
798 | ||
799 | if (ofproto->ofproto_class->set_netflow) { | |
800 | return ofproto->ofproto_class->set_netflow(ofproto, nf_options); | |
064af421 | 801 | } else { |
abe529af | 802 | return nf_options ? EOPNOTSUPP : 0; |
064af421 BP |
803 | } |
804 | } | |
805 | ||
abe529af | 806 | int |
72b06300 BP |
807 | ofproto_set_sflow(struct ofproto *ofproto, |
808 | const struct ofproto_sflow_options *oso) | |
809 | { | |
abe529af BP |
810 | if (oso && sset_is_empty(&oso->targets)) { |
811 | oso = NULL; | |
812 | } | |
72b06300 | 813 | |
abe529af BP |
814 | if (ofproto->ofproto_class->set_sflow) { |
815 | return ofproto->ofproto_class->set_sflow(ofproto, oso); | |
72b06300 | 816 | } else { |
abe529af | 817 | return oso ? EOPNOTSUPP : 0; |
72b06300 BP |
818 | } |
819 | } | |
29089a54 RL |
820 | |
821 | int | |
822 | ofproto_set_ipfix(struct ofproto *ofproto, | |
823 | const struct ofproto_ipfix_bridge_exporter_options *bo, | |
824 | const struct ofproto_ipfix_flow_exporter_options *fo, | |
825 | size_t n_fo) | |
826 | { | |
827 | if (ofproto->ofproto_class->set_ipfix) { | |
828 | return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo); | |
829 | } else { | |
830 | return (bo || fo) ? EOPNOTSUPP : 0; | |
831 | } | |
832 | } | |
40358701 | 833 | |
fb8f22c1 BY |
834 | static int |
835 | ofproto_get_ipfix_stats(struct ofproto *ofproto, | |
836 | bool bridge_ipfix, | |
837 | struct ovs_list *replies) | |
838 | { | |
839 | int error; | |
840 | ||
841 | if (ofproto->ofproto_class->get_ipfix_stats) { | |
842 | error = ofproto->ofproto_class->get_ipfix_stats(ofproto, | |
843 | bridge_ipfix, | |
844 | replies); | |
845 | } else { | |
846 | error = EOPNOTSUPP; | |
847 | } | |
848 | ||
849 | return error; | |
850 | } | |
851 | ||
852 | static enum ofperr | |
853 | handle_ipfix_bridge_stats_request(struct ofconn *ofconn, | |
854 | const struct ofp_header *request) | |
855 | { | |
856 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
857 | struct ovs_list replies; | |
858 | enum ofperr error; | |
859 | ||
860 | ofpmp_init(&replies, request); | |
861 | error = ofproto_get_ipfix_stats(ofproto, true, &replies); | |
862 | ||
863 | if (!error) { | |
864 | ofconn_send_replies(ofconn, &replies); | |
865 | } else { | |
866 | ofpbuf_list_delete(&replies); | |
867 | } | |
868 | ||
869 | return error; | |
870 | } | |
871 | ||
872 | static enum ofperr | |
873 | handle_ipfix_flow_stats_request(struct ofconn *ofconn, | |
874 | const struct ofp_header *request) | |
875 | { | |
876 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
877 | struct ovs_list replies; | |
878 | enum ofperr error; | |
879 | ||
880 | ofpmp_init(&replies, request); | |
881 | error = ofproto_get_ipfix_stats(ofproto, false, &replies); | |
882 | ||
883 | if (!error) { | |
884 | ofconn_send_replies(ofconn, &replies); | |
885 | } else { | |
886 | ofpbuf_list_delete(&replies); | |
887 | } | |
888 | ||
889 | return error; | |
890 | } | |
891 | ||
2a7c4805 JP |
892 | static enum ofperr |
893 | handle_nxt_ct_flush_zone(struct ofconn *ofconn, const struct ofp_header *oh) | |
894 | { | |
895 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
896 | const struct nx_zone_id *nzi = ofpmsg_body(oh); | |
897 | ||
898 | if (!is_all_zeros(nzi->zero, sizeof nzi->zero)) { | |
899 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
900 | } | |
901 | ||
902 | uint16_t zone = ntohs(nzi->zone_id); | |
903 | if (ofproto->ofproto_class->ct_flush) { | |
904 | ofproto->ofproto_class->ct_flush(ofproto, &zone); | |
905 | } else { | |
906 | return EOPNOTSUPP; | |
907 | } | |
908 | ||
909 | return 0; | |
910 | } | |
911 | ||
40358701 GS |
912 | void |
913 | ofproto_set_flow_restore_wait(bool flow_restore_wait_db) | |
914 | { | |
915 | flow_restore_wait = flow_restore_wait_db; | |
916 | } | |
917 | ||
918 | bool | |
919 | ofproto_get_flow_restore_wait(void) | |
920 | { | |
921 | return flow_restore_wait; | |
922 | } | |
923 | ||
e7934396 | 924 | \f |
21f7563c JP |
925 | /* Spanning Tree Protocol (STP) configuration. */ |
926 | ||
927 | /* Configures STP on 'ofproto' using the settings defined in 's'. If | |
928 | * 's' is NULL, disables STP. | |
929 | * | |
930 | * Returns 0 if successful, otherwise a positive errno value. */ | |
931 | int | |
932 | ofproto_set_stp(struct ofproto *ofproto, | |
933 | const struct ofproto_stp_settings *s) | |
934 | { | |
935 | return (ofproto->ofproto_class->set_stp | |
936 | ? ofproto->ofproto_class->set_stp(ofproto, s) | |
937 | : EOPNOTSUPP); | |
938 | } | |
939 | ||
940 | /* Retrieves STP status of 'ofproto' and stores it in 's'. If the | |
941 | * 'enabled' member of 's' is false, then the other members are not | |
942 | * meaningful. | |
943 | * | |
944 | * Returns 0 if successful, otherwise a positive errno value. */ | |
945 | int | |
946 | ofproto_get_stp_status(struct ofproto *ofproto, | |
947 | struct ofproto_stp_status *s) | |
948 | { | |
949 | return (ofproto->ofproto_class->get_stp_status | |
950 | ? ofproto->ofproto_class->get_stp_status(ofproto, s) | |
951 | : EOPNOTSUPP); | |
952 | } | |
953 | ||
954 | /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined | |
955 | * in 's'. The caller is responsible for assigning STP port numbers | |
956 | * (using the 'port_num' member in the range of 1 through 255, inclusive) | |
957 | * and ensuring there are no duplicates. If the 's' is NULL, then STP | |
958 | * is disabled on the port. | |
959 | * | |
960 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
961 | int | |
4e022ec0 | 962 | ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port, |
21f7563c JP |
963 | const struct ofproto_port_stp_settings *s) |
964 | { | |
965 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
966 | if (!ofport) { | |
94783c7c | 967 | VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu32, |
21f7563c JP |
968 | ofproto->name, ofp_port); |
969 | return ENODEV; | |
970 | } | |
971 | ||
972 | return (ofproto->ofproto_class->set_stp_port | |
973 | ? ofproto->ofproto_class->set_stp_port(ofport, s) | |
974 | : EOPNOTSUPP); | |
975 | } | |
976 | ||
977 | /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in | |
978 | * 's'. If the 'enabled' member in 's' is false, then the other members | |
979 | * are not meaningful. | |
980 | * | |
981 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
982 | int | |
4e022ec0 | 983 | ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port, |
21f7563c JP |
984 | struct ofproto_port_stp_status *s) |
985 | { | |
986 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
987 | if (!ofport) { | |
b0a5c43b | 988 | VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent " |
94783c7c | 989 | "port %"PRIu32, ofproto->name, ofp_port); |
21f7563c JP |
990 | return ENODEV; |
991 | } | |
992 | ||
993 | return (ofproto->ofproto_class->get_stp_port_status | |
994 | ? ofproto->ofproto_class->get_stp_port_status(ofport, s) | |
995 | : EOPNOTSUPP); | |
996 | } | |
fd28ce3a JS |
997 | |
998 | /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in | |
999 | * 's'. If the 'enabled' member in 's' is false, then the other members | |
1000 | * are not meaningful. | |
1001 | * | |
1002 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
1003 | int | |
1004 | ofproto_port_get_stp_stats(struct ofproto *ofproto, ofp_port_t ofp_port, | |
1005 | struct ofproto_port_stp_stats *s) | |
1006 | { | |
1007 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1008 | if (!ofport) { | |
1009 | VLOG_WARN_RL(&rl, "%s: cannot get STP stats on nonexistent " | |
94783c7c | 1010 | "port %"PRIu32, ofproto->name, ofp_port); |
fd28ce3a JS |
1011 | return ENODEV; |
1012 | } | |
1013 | ||
1014 | return (ofproto->ofproto_class->get_stp_port_stats | |
1015 | ? ofproto->ofproto_class->get_stp_port_stats(ofport, s) | |
1016 | : EOPNOTSUPP); | |
1017 | } | |
9efd308e DV |
1018 | |
1019 | /* Rapid Spanning Tree Protocol (RSTP) configuration. */ | |
1020 | ||
1021 | /* Configures RSTP on 'ofproto' using the settings defined in 's'. If | |
1022 | * 's' is NULL, disables RSTP. | |
1023 | * | |
1024 | * Returns 0 if successful, otherwise a positive errno value. */ | |
1025 | int | |
1026 | ofproto_set_rstp(struct ofproto *ofproto, | |
1027 | const struct ofproto_rstp_settings *s) | |
1028 | { | |
1029 | if (!ofproto->ofproto_class->set_rstp) { | |
1030 | return EOPNOTSUPP; | |
1031 | } | |
1032 | ofproto->ofproto_class->set_rstp(ofproto, s); | |
1033 | return 0; | |
1034 | } | |
1035 | ||
1036 | /* Retrieves RSTP status of 'ofproto' and stores it in 's'. If the | |
1037 | * 'enabled' member of 's' is false, then the other members are not | |
1038 | * meaningful. | |
1039 | * | |
1040 | * Returns 0 if successful, otherwise a positive errno value. */ | |
1041 | int | |
1042 | ofproto_get_rstp_status(struct ofproto *ofproto, | |
1043 | struct ofproto_rstp_status *s) | |
1044 | { | |
1045 | if (!ofproto->ofproto_class->get_rstp_status) { | |
1046 | return EOPNOTSUPP; | |
1047 | } | |
1048 | ofproto->ofproto_class->get_rstp_status(ofproto, s); | |
1049 | return 0; | |
1050 | } | |
1051 | ||
1052 | /* Configures RSTP on 'ofp_port' of 'ofproto' using the settings defined | |
1053 | * in 's'. The caller is responsible for assigning RSTP port numbers | |
1054 | * (using the 'port_num' member in the range of 1 through 255, inclusive) | |
1055 | * and ensuring there are no duplicates. If the 's' is NULL, then RSTP | |
1056 | * is disabled on the port. | |
1057 | * | |
1058 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
1059 | int | |
1060 | ofproto_port_set_rstp(struct ofproto *ofproto, ofp_port_t ofp_port, | |
1061 | const struct ofproto_port_rstp_settings *s) | |
1062 | { | |
1063 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1064 | if (!ofport) { | |
94783c7c | 1065 | VLOG_WARN("%s: cannot configure RSTP on nonexistent port %"PRIu32, |
9efd308e DV |
1066 | ofproto->name, ofp_port); |
1067 | return ENODEV; | |
1068 | } | |
1069 | ||
1070 | if (!ofproto->ofproto_class->set_rstp_port) { | |
1071 | return EOPNOTSUPP; | |
1072 | } | |
1073 | ofproto->ofproto_class->set_rstp_port(ofport, s); | |
1074 | return 0; | |
1075 | } | |
1076 | ||
1077 | /* Retrieves RSTP port status of 'ofp_port' on 'ofproto' and stores it in | |
1078 | * 's'. If the 'enabled' member in 's' is false, then the other members | |
1079 | * are not meaningful. | |
1080 | * | |
1081 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
1082 | int | |
1083 | ofproto_port_get_rstp_status(struct ofproto *ofproto, ofp_port_t ofp_port, | |
1084 | struct ofproto_port_rstp_status *s) | |
1085 | { | |
1086 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1087 | if (!ofport) { | |
1088 | VLOG_WARN_RL(&rl, "%s: cannot get RSTP status on nonexistent " | |
94783c7c | 1089 | "port %"PRIu32, ofproto->name, ofp_port); |
9efd308e DV |
1090 | return ENODEV; |
1091 | } | |
1092 | ||
1093 | if (!ofproto->ofproto_class->get_rstp_port_status) { | |
1094 | return EOPNOTSUPP; | |
1095 | } | |
1096 | ofproto->ofproto_class->get_rstp_port_status(ofport, s); | |
1097 | return 0; | |
1098 | } | |
21f7563c | 1099 | \f |
8b36f51e EJ |
1100 | /* Queue DSCP configuration. */ |
1101 | ||
1102 | /* Registers meta-data associated with the 'n_qdscp' Qualities of Service | |
1103 | * 'queues' attached to 'ofport'. This data is not intended to be sufficient | |
1104 | * to implement QoS. Instead, it is used to implement features which require | |
1105 | * knowledge of what queues exist on a port, and some basic information about | |
1106 | * them. | |
1107 | * | |
1108 | * Returns 0 if successful, otherwise a positive errno value. */ | |
1109 | int | |
4e022ec0 | 1110 | ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port, |
8b36f51e EJ |
1111 | const struct ofproto_port_queue *queues, |
1112 | size_t n_queues) | |
1113 | { | |
1114 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1115 | ||
1116 | if (!ofport) { | |
94783c7c | 1117 | VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu32, |
8b36f51e EJ |
1118 | ofproto->name, ofp_port); |
1119 | return ENODEV; | |
1120 | } | |
1121 | ||
1122 | return (ofproto->ofproto_class->set_queues | |
1123 | ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues) | |
1124 | : EOPNOTSUPP); | |
1125 | } | |
1126 | \f | |
0477baa9 DF |
1127 | /* LLDP configuration. */ |
1128 | void | |
1129 | ofproto_port_set_lldp(struct ofproto *ofproto, | |
1130 | ofp_port_t ofp_port, | |
1131 | const struct smap *cfg) | |
1132 | { | |
1133 | struct ofport *ofport; | |
1134 | int error; | |
1135 | ||
1136 | ofport = ofproto_get_port(ofproto, ofp_port); | |
1137 | if (!ofport) { | |
94783c7c | 1138 | VLOG_WARN("%s: cannot configure LLDP on nonexistent port %"PRIu32, |
0477baa9 DF |
1139 | ofproto->name, ofp_port); |
1140 | return; | |
1141 | } | |
1142 | error = (ofproto->ofproto_class->set_lldp | |
1143 | ? ofproto->ofproto_class->set_lldp(ofport, cfg) | |
1144 | : EOPNOTSUPP); | |
1145 | if (error) { | |
94783c7c | 1146 | VLOG_WARN("%s: lldp configuration on port %"PRIu32" (%s) failed (%s)", |
0477baa9 DF |
1147 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), |
1148 | ovs_strerror(error)); | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | int | |
1153 | ofproto_set_aa(struct ofproto *ofproto, void *aux OVS_UNUSED, | |
1154 | const struct aa_settings *s) | |
1155 | { | |
1156 | if (!ofproto->ofproto_class->set_aa) { | |
1157 | return EOPNOTSUPP; | |
1158 | } | |
1159 | ofproto->ofproto_class->set_aa(ofproto, s); | |
1160 | return 0; | |
1161 | } | |
1162 | ||
1163 | int | |
1164 | ofproto_aa_mapping_register(struct ofproto *ofproto, void *aux, | |
1165 | const struct aa_mapping_settings *s) | |
1166 | { | |
1167 | if (!ofproto->ofproto_class->aa_mapping_set) { | |
1168 | return EOPNOTSUPP; | |
1169 | } | |
1170 | ofproto->ofproto_class->aa_mapping_set(ofproto, aux, s); | |
1171 | return 0; | |
1172 | } | |
1173 | ||
1174 | int | |
1175 | ofproto_aa_mapping_unregister(struct ofproto *ofproto, void *aux) | |
1176 | { | |
1177 | if (!ofproto->ofproto_class->aa_mapping_unset) { | |
1178 | return EOPNOTSUPP; | |
1179 | } | |
1180 | ofproto->ofproto_class->aa_mapping_unset(ofproto, aux); | |
1181 | return 0; | |
1182 | } | |
1183 | ||
1184 | int | |
1185 | ofproto_aa_vlan_get_queued(struct ofproto *ofproto, | |
1186 | struct ovs_list *list) | |
1187 | { | |
1188 | if (!ofproto->ofproto_class->aa_vlan_get_queued) { | |
1189 | return EOPNOTSUPP; | |
1190 | } | |
1191 | ofproto->ofproto_class->aa_vlan_get_queued(ofproto, list); | |
1192 | return 0; | |
1193 | } | |
1194 | ||
1195 | unsigned int | |
1196 | ofproto_aa_vlan_get_queue_size(struct ofproto *ofproto) | |
1197 | { | |
1198 | if (!ofproto->ofproto_class->aa_vlan_get_queue_size) { | |
1199 | return EOPNOTSUPP; | |
1200 | } | |
1201 | return ofproto->ofproto_class->aa_vlan_get_queue_size(ofproto); | |
1202 | } | |
1203 | ||
e7934396 BP |
1204 | /* Connectivity Fault Management configuration. */ |
1205 | ||
892815f5 | 1206 | /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */ |
e7934396 | 1207 | void |
4e022ec0 | 1208 | ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port) |
e7934396 | 1209 | { |
abe529af BP |
1210 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1211 | if (ofport && ofproto->ofproto_class->set_cfm) { | |
a5610457 | 1212 | ofproto->ofproto_class->set_cfm(ofport, NULL); |
e7934396 BP |
1213 | } |
1214 | } | |
72b06300 | 1215 | |
892815f5 | 1216 | /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes |
93b8df38 EJ |
1217 | * basic configuration from the configuration members in 'cfm', and the remote |
1218 | * maintenance point ID from remote_mpid. Ignores the statistics members of | |
1219 | * 'cfm'. | |
e7934396 | 1220 | * |
892815f5 | 1221 | * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */ |
e7934396 | 1222 | void |
4e022ec0 | 1223 | ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port, |
a5610457 | 1224 | const struct cfm_settings *s) |
e7934396 BP |
1225 | { |
1226 | struct ofport *ofport; | |
abe529af | 1227 | int error; |
e7934396 | 1228 | |
abe529af | 1229 | ofport = ofproto_get_port(ofproto, ofp_port); |
e7934396 | 1230 | if (!ofport) { |
94783c7c | 1231 | VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32, |
892815f5 | 1232 | ofproto->name, ofp_port); |
e7934396 BP |
1233 | return; |
1234 | } | |
1235 | ||
93b8df38 EJ |
1236 | /* XXX: For configuration simplicity, we only support one remote_mpid |
1237 | * outside of the CFM module. It's not clear if this is the correct long | |
1238 | * term solution or not. */ | |
abe529af | 1239 | error = (ofproto->ofproto_class->set_cfm |
a5610457 | 1240 | ? ofproto->ofproto_class->set_cfm(ofport, s) |
abe529af BP |
1241 | : EOPNOTSUPP); |
1242 | if (error) { | |
94783c7c | 1243 | VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed (%s)", |
abe529af | 1244 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), |
10a89ef0 | 1245 | ovs_strerror(error)); |
e7934396 | 1246 | } |
e7934396 | 1247 | } |
e7934396 | 1248 | |
ccc09689 EJ |
1249 | /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if |
1250 | * 'ofproto' does not have a port 'ofp_port'. */ | |
1251 | void | |
4e022ec0 | 1252 | ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port, |
ccc09689 EJ |
1253 | const struct smap *cfg) |
1254 | { | |
1255 | struct ofport *ofport; | |
1256 | int error; | |
1257 | ||
1258 | ofport = ofproto_get_port(ofproto, ofp_port); | |
1259 | if (!ofport) { | |
94783c7c | 1260 | VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu32, |
ccc09689 | 1261 | ofproto->name, ofp_port); |
e8999bdc | 1262 | return; |
ccc09689 EJ |
1263 | } |
1264 | ||
1265 | error = (ofproto->ofproto_class->set_bfd | |
1266 | ? ofproto->ofproto_class->set_bfd(ofport, cfg) | |
1267 | : EOPNOTSUPP); | |
1268 | if (error) { | |
94783c7c | 1269 | VLOG_WARN("%s: bfd configuration on port %"PRIu32" (%s) failed (%s)", |
ccc09689 | 1270 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), |
10a89ef0 | 1271 | ovs_strerror(error)); |
ccc09689 EJ |
1272 | } |
1273 | } | |
1274 | ||
8f5514fe AW |
1275 | /* Checks the status change of BFD on 'ofport'. |
1276 | * | |
1277 | * Returns true if 'ofproto_class' does not support 'bfd_status_changed'. */ | |
1278 | bool | |
1279 | ofproto_port_bfd_status_changed(struct ofproto *ofproto, ofp_port_t ofp_port) | |
1280 | { | |
1281 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1282 | return (ofport && ofproto->ofproto_class->bfd_status_changed | |
1283 | ? ofproto->ofproto_class->bfd_status_changed(ofport) | |
1284 | : true); | |
1285 | } | |
1286 | ||
88bf179a | 1287 | /* Populates 'status' with the status of BFD on 'ofport'. Returns 0 on |
8f5514fe AW |
1288 | * success. Returns a positive errno otherwise. Has no effect if 'ofp_port' |
1289 | * is not an OpenFlow port in 'ofproto'. | |
88bf179a AW |
1290 | * |
1291 | * The caller must provide and own '*status'. */ | |
ccc09689 | 1292 | int |
4e022ec0 | 1293 | ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port, |
ccc09689 EJ |
1294 | struct smap *status) |
1295 | { | |
1296 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1297 | return (ofport && ofproto->ofproto_class->get_bfd_status | |
1298 | ? ofproto->ofproto_class->get_bfd_status(ofport, status) | |
1299 | : EOPNOTSUPP); | |
1300 | } | |
1301 | ||
fa066f01 BP |
1302 | /* Checks the status of LACP negotiation for 'ofp_port' within ofproto. |
1303 | * Returns 1 if LACP partner information for 'ofp_port' is up-to-date, | |
1304 | * 0 if LACP partner information is not current (generally indicating a | |
1305 | * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */ | |
1306 | int | |
4e022ec0 | 1307 | ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port) |
fa066f01 | 1308 | { |
abe529af BP |
1309 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1310 | return (ofport && ofproto->ofproto_class->port_is_lacp_current | |
1311 | ? ofproto->ofproto_class->port_is_lacp_current(ofport) | |
fa066f01 | 1312 | : -1); |
e7934396 | 1313 | } |
50b9699f NM |
1314 | |
1315 | int | |
1316 | ofproto_port_get_lacp_stats(const struct ofport *port, struct lacp_slave_stats *stats) | |
1317 | { | |
1318 | struct ofproto *ofproto = port->ofproto; | |
1319 | int error; | |
1320 | ||
1321 | if (ofproto->ofproto_class->port_get_lacp_stats) { | |
1322 | error = ofproto->ofproto_class->port_get_lacp_stats(port, stats); | |
1323 | } else { | |
1324 | error = EOPNOTSUPP; | |
1325 | } | |
1326 | ||
1327 | return error; | |
1328 | } | |
e7934396 | 1329 | \f |
fa066f01 | 1330 | /* Bundles. */ |
e7934396 | 1331 | |
abe529af BP |
1332 | /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'. |
1333 | * A bundle is the same concept as a Port in OVSDB, that is, it consists of one | |
1334 | * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN | |
1335 | * configuration plus, if there is more than one slave, a bonding | |
1336 | * configuration. | |
1337 | * | |
1338 | * If 'aux' is already registered then this function updates its configuration | |
1339 | * to 's'. Otherwise, this function registers a new bundle. | |
1340 | * | |
1341 | * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL | |
1342 | * port. */ | |
1343 | int | |
1344 | ofproto_bundle_register(struct ofproto *ofproto, void *aux, | |
1345 | const struct ofproto_bundle_settings *s) | |
fa066f01 | 1346 | { |
abe529af BP |
1347 | return (ofproto->ofproto_class->bundle_set |
1348 | ? ofproto->ofproto_class->bundle_set(ofproto, aux, s) | |
1349 | : EOPNOTSUPP); | |
fa066f01 BP |
1350 | } |
1351 | ||
abe529af BP |
1352 | /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'. |
1353 | * If no such bundle has been registered, this has no effect. */ | |
1354 | int | |
1355 | ofproto_bundle_unregister(struct ofproto *ofproto, void *aux) | |
e7934396 | 1356 | { |
abe529af | 1357 | return ofproto_bundle_register(ofproto, aux, NULL); |
e7934396 | 1358 | } |
fa066f01 | 1359 | |
e7934396 | 1360 | \f |
abe529af BP |
1361 | /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'. |
1362 | * If 'aux' is already registered then this function updates its configuration | |
c06bba01 | 1363 | * to 's'. Otherwise, this function registers a new mirror. */ |
abe529af BP |
1364 | int |
1365 | ofproto_mirror_register(struct ofproto *ofproto, void *aux, | |
1366 | const struct ofproto_mirror_settings *s) | |
064af421 | 1367 | { |
abe529af BP |
1368 | return (ofproto->ofproto_class->mirror_set |
1369 | ? ofproto->ofproto_class->mirror_set(ofproto, aux, s) | |
1370 | : EOPNOTSUPP); | |
064af421 BP |
1371 | } |
1372 | ||
abe529af BP |
1373 | /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'. |
1374 | * If no mirror has been registered, this has no effect. */ | |
1375 | int | |
1376 | ofproto_mirror_unregister(struct ofproto *ofproto, void *aux) | |
064af421 | 1377 | { |
abe529af | 1378 | return ofproto_mirror_register(ofproto, aux, NULL); |
064af421 BP |
1379 | } |
1380 | ||
9d24de3b JP |
1381 | /* Retrieves statistics from mirror associated with client data pointer |
1382 | * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and | |
1383 | * 'bytes', respectively. If a particular counters is not supported, | |
0477baa9 DF |
1384 | * the appropriate argument is set to UINT64_MAX. |
1385 | */ | |
9d24de3b JP |
1386 | int |
1387 | ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux, | |
1388 | uint64_t *packets, uint64_t *bytes) | |
1389 | { | |
1390 | if (!ofproto->ofproto_class->mirror_get_stats) { | |
1391 | *packets = *bytes = UINT64_MAX; | |
1392 | return EOPNOTSUPP; | |
1393 | } | |
1394 | ||
1395 | return ofproto->ofproto_class->mirror_get_stats(ofproto, aux, | |
1396 | packets, bytes); | |
1397 | } | |
1398 | ||
abe529af BP |
1399 | /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on |
1400 | * which all packets are flooded, instead of using MAC learning. If | |
1401 | * 'flood_vlans' is NULL, then MAC learning applies to all VLANs. | |
1402 | * | |
1403 | * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL | |
1404 | * port. */ | |
1405 | int | |
1406 | ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans) | |
abdfe474 | 1407 | { |
abe529af BP |
1408 | return (ofproto->ofproto_class->set_flood_vlans |
1409 | ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans) | |
1410 | : EOPNOTSUPP); | |
abdfe474 JP |
1411 | } |
1412 | ||
abe529af BP |
1413 | /* Returns true if 'aux' is a registered bundle that is currently in use as the |
1414 | * output for a mirror. */ | |
1415 | bool | |
b4affc74 | 1416 | ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux) |
abe529af BP |
1417 | { |
1418 | return (ofproto->ofproto_class->is_mirror_output_bundle | |
1419 | ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux) | |
1420 | : false); | |
1421 | } | |
1422 | \f | |
254750ce BP |
1423 | /* Configuration of OpenFlow tables. */ |
1424 | ||
1425 | /* Returns the number of OpenFlow tables in 'ofproto'. */ | |
1426 | int | |
1427 | ofproto_get_n_tables(const struct ofproto *ofproto) | |
1428 | { | |
1429 | return ofproto->n_tables; | |
1430 | } | |
1431 | ||
adcf00ba AZ |
1432 | /* Returns the number of Controller visible OpenFlow tables |
1433 | * in 'ofproto'. This number will exclude Hidden tables. | |
1434 | * This funtion's return value should be less or equal to that of | |
1435 | * ofproto_get_n_tables() . */ | |
1436 | uint8_t | |
1437 | ofproto_get_n_visible_tables(const struct ofproto *ofproto) | |
1438 | { | |
1439 | uint8_t n = ofproto->n_tables; | |
1440 | ||
1441 | /* Count only non-hidden tables in the number of tables. (Hidden tables, | |
1442 | * if present, are always at the end.) */ | |
1443 | while(n && (ofproto->tables[n - 1].flags & OFTABLE_HIDDEN)) { | |
1444 | n--; | |
1445 | } | |
1446 | ||
1447 | return n; | |
1448 | } | |
1449 | ||
254750ce BP |
1450 | /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the |
1451 | * settings from 's'. 'table_id' must be in the range 0 through the number of | |
1452 | * OpenFlow tables in 'ofproto' minus 1, inclusive. | |
1453 | * | |
1454 | * For read-only tables, only the name may be configured. */ | |
1455 | void | |
1456 | ofproto_configure_table(struct ofproto *ofproto, int table_id, | |
1457 | const struct ofproto_table_settings *s) | |
1458 | { | |
1459 | struct oftable *table; | |
1460 | ||
cb22974d | 1461 | ovs_assert(table_id >= 0 && table_id < ofproto->n_tables); |
254750ce BP |
1462 | table = &ofproto->tables[table_id]; |
1463 | ||
1464 | oftable_set_name(table, s->name); | |
1465 | ||
1466 | if (table->flags & OFTABLE_READONLY) { | |
1467 | return; | |
1468 | } | |
1469 | ||
f358a2cb JR |
1470 | if (classifier_set_prefix_fields(&table->cls, |
1471 | s->prefix_fields, s->n_prefix_fields)) { | |
1472 | /* XXX: Trigger revalidation. */ | |
1473 | } | |
834fe5cb BP |
1474 | |
1475 | ovs_mutex_lock(&ofproto_mutex); | |
82c22d34 BP |
1476 | unsigned int new_eviction = (s->enable_eviction |
1477 | ? table->eviction | EVICTION_CLIENT | |
1478 | : table->eviction & ~EVICTION_CLIENT); | |
1479 | oftable_configure_eviction(table, new_eviction, s->groups, s->n_groups); | |
7394b8fb | 1480 | table->max_flows = s->max_flows; |
6787a49f | 1481 | evict_rules_from_table(table); |
834fe5cb | 1482 | ovs_mutex_unlock(&ofproto_mutex); |
254750ce BP |
1483 | } |
1484 | \f | |
81e2083f BP |
1485 | bool |
1486 | ofproto_has_snoops(const struct ofproto *ofproto) | |
1487 | { | |
1488 | return connmgr_has_snoops(ofproto->connmgr); | |
1489 | } | |
1490 | ||
064af421 | 1491 | void |
81e2083f | 1492 | ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops) |
064af421 | 1493 | { |
19a87e36 | 1494 | connmgr_get_snoops(ofproto->connmgr, snoops); |
064af421 BP |
1495 | } |
1496 | ||
a73fcd71 | 1497 | /* Deletes 'rule' from 'ofproto'. |
8037acb4 | 1498 | * |
b74cb5d8 BP |
1499 | * Within an ofproto implementation, this function allows an ofproto |
1500 | * implementation to destroy any rules that remain when its ->destruct() | |
1501 | * function is called. This function is not suitable for use elsewhere in an | |
1502 | * ofproto implementation. | |
1503 | * | |
b74cb5d8 | 1504 | * This function implements steps 4.4 and 4.5 in the section titled "Rule Life |
8b81d1ef | 1505 | * Cycle" in ofproto-provider.h. */ |
b74cb5d8 | 1506 | void |
8b81d1ef | 1507 | ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule) |
15aaf599 | 1508 | OVS_EXCLUDED(ofproto_mutex) |
7ee20df1 | 1509 | { |
834fe5cb BP |
1510 | /* This skips the ofmonitor and flow-removed notifications because the |
1511 | * switch is being deleted and any OpenFlow channels have been or soon will | |
1512 | * be killed. */ | |
15aaf599 | 1513 | ovs_mutex_lock(&ofproto_mutex); |
39c94593 | 1514 | |
30d0452c | 1515 | if (rule->state == RULE_INSERTED) { |
39c94593 | 1516 | /* Make sure there is no postponed removal of the rule. */ |
44e0c35d | 1517 | ovs_assert(cls_rule_visible_in_version(&rule->cr, OVS_VERSION_MAX)); |
39c94593 JR |
1518 | |
1519 | if (!classifier_remove(&rule->ofproto->tables[rule->table_id].cls, | |
1520 | &rule->cr)) { | |
1521 | OVS_NOT_REACHED(); | |
1522 | } | |
1523 | ofproto_rule_remove__(rule->ofproto, rule); | |
1fc71871 JR |
1524 | if (ofproto->ofproto_class->rule_delete) { |
1525 | ofproto->ofproto_class->rule_delete(rule); | |
1526 | } | |
25010c68 JR |
1527 | |
1528 | /* This may not be the last reference to the rule. */ | |
39c94593 JR |
1529 | ofproto_rule_unref(rule); |
1530 | } | |
15aaf599 | 1531 | ovs_mutex_unlock(&ofproto_mutex); |
8037acb4 BP |
1532 | } |
1533 | ||
1534 | static void | |
1535 | ofproto_flush__(struct ofproto *ofproto) | |
15aaf599 | 1536 | OVS_EXCLUDED(ofproto_mutex) |
8037acb4 | 1537 | { |
d0918789 | 1538 | struct oftable *table; |
7ee20df1 | 1539 | |
3948503a | 1540 | /* This will flush all datapath flows. */ |
7ee20df1 BP |
1541 | if (ofproto->ofproto_class->flush) { |
1542 | ofproto->ofproto_class->flush(ofproto); | |
1543 | } | |
1544 | ||
3948503a JR |
1545 | /* XXX: There is a small race window here, where new datapath flows can be |
1546 | * created by upcall handlers based on the existing flow table. We can not | |
1547 | * call ofproto class flush while holding 'ofproto_mutex' to prevent this, | |
1548 | * as then we could deadlock on syncing with the handler threads waiting on | |
1549 | * the same mutex. */ | |
1550 | ||
15aaf599 | 1551 | ovs_mutex_lock(&ofproto_mutex); |
b772ded8 | 1552 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
802f84ff | 1553 | struct rule_collection rules; |
78c8df12 | 1554 | struct rule *rule; |
7ee20df1 | 1555 | |
5c67e4af BP |
1556 | if (table->flags & OFTABLE_HIDDEN) { |
1557 | continue; | |
1558 | } | |
1559 | ||
802f84ff JR |
1560 | rule_collection_init(&rules); |
1561 | ||
de4ad4a2 | 1562 | CLS_FOR_EACH (rule, cr, &table->cls) { |
802f84ff | 1563 | rule_collection_add(&rules, rule); |
7ee20df1 | 1564 | } |
802f84ff | 1565 | delete_flows__(&rules, OFPRR_DELETE, NULL); |
7ee20df1 | 1566 | } |
3948503a JR |
1567 | /* XXX: Concurrent handler threads may insert new learned flows based on |
1568 | * learn actions of the now deleted flows right after we release | |
1569 | * 'ofproto_mutex'. */ | |
15aaf599 | 1570 | ovs_mutex_unlock(&ofproto_mutex); |
7ee20df1 BP |
1571 | } |
1572 | ||
abe529af BP |
1573 | static void |
1574 | ofproto_destroy__(struct ofproto *ofproto) | |
15aaf599 | 1575 | OVS_EXCLUDED(ofproto_mutex) |
abe529af | 1576 | { |
d0918789 | 1577 | struct oftable *table; |
6c1491fb | 1578 | |
db88b35c | 1579 | cmap_destroy(&ofproto->groups); |
7395c052 | 1580 | |
abe529af | 1581 | hmap_remove(&all_ofprotos, &ofproto->hmap_node); |
8d8ab6c2 JG |
1582 | tun_metadata_free(ovsrcu_get_protected(struct tun_table *, |
1583 | &ofproto->metadata_tab)); | |
04f48a68 YHW |
1584 | |
1585 | ovs_mutex_lock(&ofproto->vl_mff_map.mutex); | |
5c7c16d8 | 1586 | mf_vl_mff_map_clear(&ofproto->vl_mff_map, true); |
04f48a68 YHW |
1587 | ovs_mutex_unlock(&ofproto->vl_mff_map.mutex); |
1588 | cmap_destroy(&ofproto->vl_mff_map.cmap); | |
1589 | ovs_mutex_destroy(&ofproto->vl_mff_map.mutex); | |
1590 | ||
abe529af | 1591 | free(ofproto->name); |
955a7127 | 1592 | free(ofproto->type); |
abe529af BP |
1593 | free(ofproto->mfr_desc); |
1594 | free(ofproto->hw_desc); | |
1595 | free(ofproto->sw_desc); | |
1596 | free(ofproto->serial_desc); | |
1597 | free(ofproto->dp_desc); | |
abe529af | 1598 | hmap_destroy(&ofproto->ports); |
fdcea803 | 1599 | hmap_destroy(&ofproto->ofport_usage); |
abe529af | 1600 | shash_destroy(&ofproto->port_by_name); |
e1b1d06a | 1601 | simap_destroy(&ofproto->ofp_requests); |
6c1491fb | 1602 | |
b772ded8 | 1603 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
d0918789 | 1604 | oftable_destroy(table); |
6c1491fb BP |
1605 | } |
1606 | free(ofproto->tables); | |
fa066f01 | 1607 | |
1406c79b BP |
1608 | ovs_assert(hindex_is_empty(&ofproto->cookies)); |
1609 | hindex_destroy(&ofproto->cookies); | |
1610 | ||
35f48b8b BP |
1611 | ovs_assert(hmap_is_empty(&ofproto->learned_cookies)); |
1612 | hmap_destroy(&ofproto->learned_cookies); | |
1613 | ||
abe529af BP |
1614 | ofproto->ofproto_class->dealloc(ofproto); |
1615 | } | |
fa066f01 | 1616 | |
39c94593 JR |
1617 | /* Destroying rules is doubly deferred, must have 'ofproto' around for them. |
1618 | * - 1st we defer the removal of the rules from the classifier | |
1619 | * - 2nd we defer the actual destruction of the rules. */ | |
1620 | static void | |
1621 | ofproto_destroy_defer__(struct ofproto *ofproto) | |
1622 | OVS_EXCLUDED(ofproto_mutex) | |
1623 | { | |
1624 | ovsrcu_postpone(ofproto_destroy__, ofproto); | |
1625 | } | |
1626 | ||
064af421 | 1627 | void |
9aad5a5a | 1628 | ofproto_destroy(struct ofproto *p, bool del) |
15aaf599 | 1629 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 1630 | { |
ca0f572c | 1631 | struct ofport *ofport, *next_ofport; |
4ec3d7c7 | 1632 | struct ofport_usage *usage; |
064af421 BP |
1633 | |
1634 | if (!p) { | |
1635 | return; | |
1636 | } | |
1637 | ||
717de9ff JR |
1638 | if (p->meters) { |
1639 | meter_delete(p, 1, p->meter_features.max_meters); | |
1640 | p->meter_features.max_meters = 0; | |
1641 | free(p->meters); | |
1642 | p->meters = NULL; | |
1643 | } | |
1644 | ||
7ee20df1 | 1645 | ofproto_flush__(p); |
4e8e4213 | 1646 | HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) { |
9aad5a5a | 1647 | ofport_destroy(ofport, del); |
064af421 | 1648 | } |
064af421 | 1649 | |
4ec3d7c7 | 1650 | HMAP_FOR_EACH_POP (usage, hmap_node, &p->ofport_usage) { |
fdcea803 GS |
1651 | free(usage); |
1652 | } | |
1653 | ||
abe529af | 1654 | p->ofproto_class->destruct(p); |
58f19539 DDP |
1655 | |
1656 | /* We should not postpone this because it involves deleting a listening | |
25010c68 JR |
1657 | * socket which we may want to reopen soon. 'connmgr' may be used by other |
1658 | * threads only if they take the ofproto_mutex and read a non-NULL | |
1659 | * 'ofproto->connmgr'. */ | |
1660 | ovs_mutex_lock(&ofproto_mutex); | |
58f19539 | 1661 | connmgr_destroy(p->connmgr); |
25010c68 JR |
1662 | p->connmgr = NULL; |
1663 | ovs_mutex_unlock(&ofproto_mutex); | |
58f19539 | 1664 | |
f416c8d6 | 1665 | /* Destroying rules is deferred, must have 'ofproto' around for them. */ |
39c94593 | 1666 | ovsrcu_postpone(ofproto_destroy_defer__, p); |
064af421 BP |
1667 | } |
1668 | ||
abe529af BP |
1669 | /* Destroys the datapath with the respective 'name' and 'type'. With the Linux |
1670 | * kernel datapath, for example, this destroys the datapath in the kernel, and | |
1671 | * with the netdev-based datapath, it tears down the data structures that | |
1672 | * represent the datapath. | |
1673 | * | |
1674 | * The datapath should not be currently open as an ofproto. */ | |
064af421 | 1675 | int |
abe529af | 1676 | ofproto_delete(const char *name, const char *type) |
064af421 | 1677 | { |
abe529af BP |
1678 | const struct ofproto_class *class = ofproto_class_find__(type); |
1679 | return (!class ? EAFNOSUPPORT | |
1680 | : !class->del ? EACCES | |
1681 | : class->del(type, name)); | |
064af421 BP |
1682 | } |
1683 | ||
e9e28be3 BP |
1684 | static void |
1685 | process_port_change(struct ofproto *ofproto, int error, char *devname) | |
1686 | { | |
1687 | if (error == ENOBUFS) { | |
1688 | reinit_ports(ofproto); | |
1689 | } else if (!error) { | |
1690 | update_port(ofproto, devname); | |
1691 | free(devname); | |
1692 | } | |
1693 | } | |
1694 | ||
11a574a7 JP |
1695 | int |
1696 | ofproto_type_run(const char *datapath_type) | |
1697 | { | |
1698 | const struct ofproto_class *class; | |
1699 | int error; | |
1700 | ||
1701 | datapath_type = ofproto_normalize_type(datapath_type); | |
1702 | class = ofproto_class_find__(datapath_type); | |
1703 | ||
1704 | error = class->type_run ? class->type_run(datapath_type) : 0; | |
1705 | if (error && error != EAGAIN) { | |
1706 | VLOG_ERR_RL(&rl, "%s: type_run failed (%s)", | |
10a89ef0 | 1707 | datapath_type, ovs_strerror(error)); |
11a574a7 JP |
1708 | } |
1709 | return error; | |
1710 | } | |
1711 | ||
11a574a7 JP |
1712 | void |
1713 | ofproto_type_wait(const char *datapath_type) | |
1714 | { | |
1715 | const struct ofproto_class *class; | |
1716 | ||
1717 | datapath_type = ofproto_normalize_type(datapath_type); | |
1718 | class = ofproto_class_find__(datapath_type); | |
1719 | ||
1720 | if (class->type_wait) { | |
1721 | class->type_wait(datapath_type); | |
1722 | } | |
1723 | } | |
1724 | ||
064af421 | 1725 | int |
abe529af | 1726 | ofproto_run(struct ofproto *p) |
064af421 | 1727 | { |
064af421 | 1728 | int error; |
da4a6191 | 1729 | uint64_t new_seq; |
064af421 | 1730 | |
abe529af | 1731 | error = p->ofproto_class->run(p); |
5fcc0d00 | 1732 | if (error && error != EAGAIN) { |
10a89ef0 | 1733 | VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error)); |
149f577a JG |
1734 | } |
1735 | ||
448c2fa8 EJ |
1736 | /* Restore the eviction group heap invariant occasionally. */ |
1737 | if (p->eviction_group_timer < time_msec()) { | |
1738 | size_t i; | |
1739 | ||
1740 | p->eviction_group_timer = time_msec() + 1000; | |
1741 | ||
1742 | for (i = 0; i < p->n_tables; i++) { | |
1743 | struct oftable *table = &p->tables[i]; | |
1744 | struct eviction_group *evg; | |
448c2fa8 EJ |
1745 | struct rule *rule; |
1746 | ||
82c22d34 | 1747 | if (!table->eviction) { |
448c2fa8 EJ |
1748 | continue; |
1749 | } | |
1750 | ||
d79e3d70 | 1751 | if (table->n_flows > 100000) { |
1a9bb326 EJ |
1752 | static struct vlog_rate_limit count_rl = |
1753 | VLOG_RATE_LIMIT_INIT(1, 1); | |
1754 | VLOG_WARN_RL(&count_rl, "Table %"PRIuSIZE" has an excessive" | |
d79e3d70 | 1755 | " number of rules: %d", i, table->n_flows); |
1a9bb326 EJ |
1756 | } |
1757 | ||
15aaf599 | 1758 | ovs_mutex_lock(&ofproto_mutex); |
5f0476ce | 1759 | CLS_FOR_EACH (rule, cr, &table->cls) { |
6d56c1f1 K |
1760 | if (rule->idle_timeout || rule->hard_timeout) { |
1761 | if (!rule->eviction_group) { | |
1762 | eviction_group_add_rule(rule); | |
1763 | } else { | |
1764 | heap_raw_change(&rule->evg_node, | |
1765 | rule_eviction_priority(p, rule)); | |
1766 | } | |
448c2fa8 EJ |
1767 | } |
1768 | } | |
6d56c1f1 K |
1769 | |
1770 | HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) { | |
1771 | heap_rebuild(&evg->rules); | |
1772 | } | |
15aaf599 | 1773 | ovs_mutex_unlock(&ofproto_mutex); |
448c2fa8 EJ |
1774 | } |
1775 | } | |
1776 | ||
5bf0e941 | 1777 | if (p->ofproto_class->port_poll) { |
7436ed80 BP |
1778 | char *devname; |
1779 | ||
5bf0e941 BP |
1780 | while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) { |
1781 | process_port_change(p, error, devname); | |
064af421 | 1782 | } |
e9e28be3 | 1783 | } |
031d8bff | 1784 | |
da4a6191 JS |
1785 | new_seq = seq_read(connectivity_seq_get()); |
1786 | if (new_seq != p->change_seq) { | |
1787 | struct sset devnames; | |
1788 | const char *devname; | |
1789 | struct ofport *ofport; | |
1790 | ||
1791 | /* Update OpenFlow port status for any port whose netdev has changed. | |
1792 | * | |
1793 | * Refreshing a given 'ofport' can cause an arbitrary ofport to be | |
1794 | * destroyed, so it's not safe to update ports directly from the | |
1795 | * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we | |
1796 | * need this two-phase approach. */ | |
1797 | sset_init(&devnames); | |
1798 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
61501798 AW |
1799 | uint64_t port_change_seq; |
1800 | ||
1801 | port_change_seq = netdev_get_change_seq(ofport->netdev); | |
1802 | if (ofport->change_seq != port_change_seq) { | |
1803 | ofport->change_seq = port_change_seq; | |
1804 | sset_add(&devnames, netdev_get_name(ofport->netdev)); | |
1805 | } | |
031d8bff | 1806 | } |
da4a6191 JS |
1807 | SSET_FOR_EACH (devname, &devnames) { |
1808 | update_port(p, devname); | |
1809 | } | |
1810 | sset_destroy(&devnames); | |
1811 | ||
1812 | p->change_seq = new_seq; | |
064af421 BP |
1813 | } |
1814 | ||
834fe5cb | 1815 | connmgr_run(p->connmgr, handle_openflow); |
064af421 | 1816 | |
5fcc0d00 BP |
1817 | return error; |
1818 | } | |
1819 | ||
064af421 BP |
1820 | void |
1821 | ofproto_wait(struct ofproto *p) | |
1822 | { | |
abe529af | 1823 | p->ofproto_class->wait(p); |
5bf0e941 BP |
1824 | if (p->ofproto_class->port_poll_wait) { |
1825 | p->ofproto_class->port_poll_wait(p); | |
e7934396 | 1826 | } |
da4a6191 | 1827 | seq_wait(connectivity_seq_get(), p->change_seq); |
834fe5cb | 1828 | connmgr_wait(p->connmgr); |
064af421 BP |
1829 | } |
1830 | ||
064af421 BP |
1831 | bool |
1832 | ofproto_is_alive(const struct ofproto *p) | |
1833 | { | |
19a87e36 | 1834 | return connmgr_has_controllers(p->connmgr); |
064af421 BP |
1835 | } |
1836 | ||
0d085684 BP |
1837 | /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with |
1838 | * memory_report(). */ | |
1839 | void | |
1840 | ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage) | |
1841 | { | |
1842 | const struct oftable *table; | |
1843 | unsigned int n_rules; | |
1844 | ||
1845 | simap_increase(usage, "ports", hmap_count(&ofproto->ports)); | |
15aaf599 | 1846 | |
0d085684 BP |
1847 | n_rules = 0; |
1848 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
d79e3d70 | 1849 | n_rules += table->n_flows; |
0d085684 BP |
1850 | } |
1851 | simap_increase(usage, "rules", n_rules); | |
1852 | ||
1853 | if (ofproto->ofproto_class->get_memory_usage) { | |
1854 | ofproto->ofproto_class->get_memory_usage(ofproto, usage); | |
1855 | } | |
1856 | ||
1857 | connmgr_get_memory_usage(ofproto->connmgr, usage); | |
1858 | } | |
1859 | ||
1c030aa5 EJ |
1860 | void |
1861 | ofproto_type_get_memory_usage(const char *datapath_type, struct simap *usage) | |
1862 | { | |
1863 | const struct ofproto_class *class; | |
1864 | ||
1865 | datapath_type = ofproto_normalize_type(datapath_type); | |
1866 | class = ofproto_class_find__(datapath_type); | |
1867 | ||
1868 | if (class && class->type_get_memory_usage) { | |
1869 | class->type_get_memory_usage(datapath_type, usage); | |
1870 | } | |
1871 | } | |
1872 | ||
bffc0589 | 1873 | void |
2cdcb898 | 1874 | ofproto_get_ofproto_controller_info(const struct ofproto *ofproto, |
bffc0589 AE |
1875 | struct shash *info) |
1876 | { | |
19a87e36 | 1877 | connmgr_get_controller_info(ofproto->connmgr, info); |
bffc0589 AE |
1878 | } |
1879 | ||
1880 | void | |
1881 | ofproto_free_ofproto_controller_info(struct shash *info) | |
1882 | { | |
72ba2ed3 | 1883 | connmgr_free_controller_info(info); |
bffc0589 AE |
1884 | } |
1885 | ||
b5827b24 BP |
1886 | /* Makes a deep copy of 'old' into 'port'. */ |
1887 | void | |
1888 | ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old) | |
1889 | { | |
1890 | port->name = xstrdup(old->name); | |
1891 | port->type = xstrdup(old->type); | |
1892 | port->ofp_port = old->ofp_port; | |
1893 | } | |
1894 | ||
1895 | /* Frees memory allocated to members of 'ofproto_port'. | |
3a6ccc8c | 1896 | * |
b5827b24 BP |
1897 | * Do not call this function on an ofproto_port obtained from |
1898 | * ofproto_port_dump_next(): that function retains ownership of the data in the | |
1899 | * ofproto_port. */ | |
1900 | void | |
1901 | ofproto_port_destroy(struct ofproto_port *ofproto_port) | |
1902 | { | |
1903 | free(ofproto_port->name); | |
1904 | free(ofproto_port->type); | |
1905 | } | |
1906 | ||
b5827b24 | 1907 | /* Initializes 'dump' to begin dumping the ports in an ofproto. |
3a6ccc8c | 1908 | * |
b5827b24 BP |
1909 | * This function provides no status indication. An error status for the entire |
1910 | * dump operation is provided when it is completed by calling | |
1911 | * ofproto_port_dump_done(). | |
1912 | */ | |
1913 | void | |
1914 | ofproto_port_dump_start(struct ofproto_port_dump *dump, | |
1915 | const struct ofproto *ofproto) | |
1916 | { | |
abe529af BP |
1917 | dump->ofproto = ofproto; |
1918 | dump->error = ofproto->ofproto_class->port_dump_start(ofproto, | |
1919 | &dump->state); | |
b5827b24 BP |
1920 | } |
1921 | ||
1922 | /* Attempts to retrieve another port from 'dump', which must have been created | |
1923 | * with ofproto_port_dump_start(). On success, stores a new ofproto_port into | |
1924 | * 'port' and returns true. On failure, returns false. | |
1925 | * | |
1926 | * Failure might indicate an actual error or merely that the last port has been | |
1927 | * dumped. An error status for the entire dump operation is provided when it | |
1928 | * is completed by calling ofproto_port_dump_done(). | |
1929 | * | |
1930 | * The ofproto owns the data stored in 'port'. It will remain valid until at | |
1931 | * least the next time 'dump' is passed to ofproto_port_dump_next() or | |
1932 | * ofproto_port_dump_done(). */ | |
1933 | bool | |
1934 | ofproto_port_dump_next(struct ofproto_port_dump *dump, | |
1935 | struct ofproto_port *port) | |
1936 | { | |
abe529af BP |
1937 | const struct ofproto *ofproto = dump->ofproto; |
1938 | ||
1939 | if (dump->error) { | |
1940 | return false; | |
1941 | } | |
b5827b24 | 1942 | |
abe529af BP |
1943 | dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state, |
1944 | port); | |
1945 | if (dump->error) { | |
1946 | ofproto->ofproto_class->port_dump_done(ofproto, dump->state); | |
1947 | return false; | |
b5827b24 | 1948 | } |
abe529af | 1949 | return true; |
b5827b24 BP |
1950 | } |
1951 | ||
1952 | /* Completes port table dump operation 'dump', which must have been created | |
1953 | * with ofproto_port_dump_start(). Returns 0 if the dump operation was | |
1954 | * error-free, otherwise a positive errno value describing the problem. */ | |
3a6ccc8c | 1955 | int |
b5827b24 | 1956 | ofproto_port_dump_done(struct ofproto_port_dump *dump) |
3a6ccc8c | 1957 | { |
abe529af BP |
1958 | const struct ofproto *ofproto = dump->ofproto; |
1959 | if (!dump->error) { | |
1960 | dump->error = ofproto->ofproto_class->port_dump_done(ofproto, | |
1961 | dump->state); | |
1962 | } | |
1963 | return dump->error == EOF ? 0 : dump->error; | |
b5827b24 BP |
1964 | } |
1965 | ||
0aeaabc8 JP |
1966 | /* Returns the type to pass to netdev_open() when a datapath of type |
1967 | * 'datapath_type' has a port of type 'port_type', for a few special | |
1968 | * cases when a netdev type differs from a port type. For example, when | |
1969 | * using the userspace datapath, a port of type "internal" needs to be | |
1970 | * opened as "tap". | |
1971 | * | |
1972 | * Returns either 'type' itself or a string literal, which must not be | |
1973 | * freed. */ | |
1974 | const char * | |
1975 | ofproto_port_open_type(const char *datapath_type, const char *port_type) | |
1976 | { | |
1977 | const struct ofproto_class *class; | |
1978 | ||
1979 | datapath_type = ofproto_normalize_type(datapath_type); | |
1980 | class = ofproto_class_find__(datapath_type); | |
1981 | if (!class) { | |
1982 | return port_type; | |
1983 | } | |
1984 | ||
1985 | return (class->port_open_type | |
1986 | ? class->port_open_type(datapath_type, port_type) | |
1987 | : port_type); | |
1988 | } | |
1989 | ||
81816a5f JP |
1990 | /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is |
1991 | * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as | |
1992 | * the port's OpenFlow port number. | |
1993 | * | |
1994 | * If successful, returns 0 and sets '*ofp_portp' to the new port's | |
1995 | * OpenFlow port number (if 'ofp_portp' is non-null). On failure, | |
1996 | * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if | |
1997 | * 'ofp_portp' is non-null). */ | |
b5827b24 BP |
1998 | int |
1999 | ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev, | |
4e022ec0 | 2000 | ofp_port_t *ofp_portp) |
b5827b24 | 2001 | { |
4e022ec0 | 2002 | ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE; |
3a6ccc8c BP |
2003 | int error; |
2004 | ||
e1b1d06a | 2005 | error = ofproto->ofproto_class->port_add(ofproto, netdev); |
1fa24dea | 2006 | if (!error) { |
e1b1d06a JP |
2007 | const char *netdev_name = netdev_get_name(netdev); |
2008 | ||
4e022ec0 AW |
2009 | simap_put(&ofproto->ofp_requests, netdev_name, |
2010 | ofp_to_u16(ofp_port)); | |
01c77073 | 2011 | error = update_port(ofproto, netdev_name); |
1fa24dea | 2012 | } |
b5827b24 | 2013 | if (ofp_portp) { |
17f69db5 BP |
2014 | *ofp_portp = OFPP_NONE; |
2015 | if (!error) { | |
2016 | struct ofproto_port ofproto_port; | |
2017 | ||
2018 | error = ofproto_port_query_by_name(ofproto, | |
2019 | netdev_get_name(netdev), | |
2020 | &ofproto_port); | |
2021 | if (!error) { | |
2022 | *ofp_portp = ofproto_port.ofp_port; | |
2023 | ofproto_port_destroy(&ofproto_port); | |
2024 | } | |
2025 | } | |
3a6ccc8c BP |
2026 | } |
2027 | return error; | |
2028 | } | |
2029 | ||
b5827b24 BP |
2030 | /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and |
2031 | * initializes '*port' appropriately; on failure, returns a positive errno | |
2032 | * value. | |
2033 | * | |
abe529af | 2034 | * The caller owns the data in 'ofproto_port' and must free it with |
b5827b24 BP |
2035 | * ofproto_port_destroy() when it is no longer needed. */ |
2036 | int | |
2037 | ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname, | |
2038 | struct ofproto_port *port) | |
a4e2e1f2 | 2039 | { |
b5827b24 BP |
2040 | int error; |
2041 | ||
abe529af BP |
2042 | error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port); |
2043 | if (error) { | |
2044 | memset(port, 0, sizeof *port); | |
b5827b24 BP |
2045 | } |
2046 | return error; | |
a4e2e1f2 EJ |
2047 | } |
2048 | ||
b5827b24 | 2049 | /* Deletes port number 'ofp_port' from the datapath for 'ofproto'. |
3a6ccc8c | 2050 | * Returns 0 if successful, otherwise a positive errno. */ |
064af421 | 2051 | int |
4e022ec0 | 2052 | ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port) |
064af421 | 2053 | { |
abe529af | 2054 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1264ec95 | 2055 | const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>"; |
e1b1d06a | 2056 | struct simap_node *ofp_request_node; |
3cf10406 | 2057 | int error; |
cdee00fd | 2058 | |
e1b1d06a JP |
2059 | ofp_request_node = simap_find(&ofproto->ofp_requests, name); |
2060 | if (ofp_request_node) { | |
2061 | simap_delete(&ofproto->ofp_requests, ofp_request_node); | |
2062 | } | |
2063 | ||
abe529af | 2064 | error = ofproto->ofproto_class->port_del(ofproto, ofp_port); |
892815f5 | 2065 | if (!error && ofport) { |
1264ec95 BP |
2066 | /* 'name' is the netdev's name and update_port() is going to close the |
2067 | * netdev. Just in case update_port() refers to 'name' after it | |
3a6ccc8c BP |
2068 | * destroys 'ofport', make a copy of it around the update_port() |
2069 | * call. */ | |
2070 | char *devname = xstrdup(name); | |
2071 | update_port(ofproto, devname); | |
2072 | free(devname); | |
3cf10406 BP |
2073 | } |
2074 | return error; | |
064af421 BP |
2075 | } |
2076 | ||
91364d18 IM |
2077 | /* Refreshes datapath configuration of port number 'ofp_port' in 'ofproto'. |
2078 | * | |
2079 | * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */ | |
2080 | void | |
2081 | ofproto_port_set_config(struct ofproto *ofproto, ofp_port_t ofp_port, | |
2082 | const struct smap *cfg) | |
2083 | { | |
2084 | struct ofport *ofport; | |
2085 | int error; | |
2086 | ||
2087 | ofport = ofproto_get_port(ofproto, ofp_port); | |
2088 | if (!ofport) { | |
94783c7c | 2089 | VLOG_WARN("%s: cannot configure datapath on nonexistent port %"PRIu32, |
91364d18 IM |
2090 | ofproto->name, ofp_port); |
2091 | return; | |
2092 | } | |
2093 | ||
2094 | error = (ofproto->ofproto_class->port_set_config | |
2095 | ? ofproto->ofproto_class->port_set_config(ofport, cfg) | |
2096 | : EOPNOTSUPP); | |
2097 | if (error) { | |
94783c7c | 2098 | VLOG_WARN("%s: datapath configuration on port %"PRIu32 |
91364d18 IM |
2099 | " (%s) failed (%s)", |
2100 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), | |
2101 | ovs_strerror(error)); | |
2102 | } | |
2103 | } | |
2104 | ||
2105 | ||
276f6518 SH |
2106 | static void |
2107 | flow_mod_init(struct ofputil_flow_mod *fm, | |
eb391b76 | 2108 | const struct match *match, int priority, |
276f6518 SH |
2109 | const struct ofpact *ofpacts, size_t ofpacts_len, |
2110 | enum ofp_flow_mod_command command) | |
2111 | { | |
39cc5c4a BP |
2112 | *fm = (struct ofputil_flow_mod) { |
2113 | .match = *match, | |
2114 | .priority = priority, | |
2115 | .table_id = 0, | |
2116 | .command = command, | |
2117 | .buffer_id = UINT32_MAX, | |
2118 | .out_port = OFPP_ANY, | |
2119 | .out_group = OFPG_ANY, | |
2120 | .ofpacts = CONST_CAST(struct ofpact *, ofpacts), | |
2121 | .ofpacts_len = ofpacts_len, | |
39cc5c4a | 2122 | }; |
276f6518 SH |
2123 | } |
2124 | ||
97f63e57 BP |
2125 | static int |
2126 | simple_flow_mod(struct ofproto *ofproto, | |
eb391b76 | 2127 | const struct match *match, int priority, |
97f63e57 BP |
2128 | const struct ofpact *ofpacts, size_t ofpacts_len, |
2129 | enum ofp_flow_mod_command command) | |
2130 | { | |
7338102b | 2131 | struct ofputil_flow_mod fm; |
97f63e57 | 2132 | |
7338102b | 2133 | flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command); |
15aaf599 | 2134 | |
7338102b | 2135 | return handle_flow_mod__(ofproto, &fm, NULL); |
97f63e57 BP |
2136 | } |
2137 | ||
6c1491fb | 2138 | /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and |
fa8b054f BP |
2139 | * performs the 'n_actions' actions in 'actions'. The new flow will not |
2140 | * timeout. | |
2141 | * | |
2142 | * If cls_rule->priority is in the range of priorities supported by OpenFlow | |
2143 | * (0...65535, inclusive) then the flow will be visible to OpenFlow | |
2144 | * controllers; otherwise, it will be hidden. | |
2145 | * | |
f25d0cf3 | 2146 | * The caller retains ownership of 'cls_rule' and 'ofpacts'. |
6c1491fb BP |
2147 | * |
2148 | * This is a helper function for in-band control and fail-open. */ | |
064af421 | 2149 | void |
81a76618 | 2150 | ofproto_add_flow(struct ofproto *ofproto, const struct match *match, |
eb391b76 | 2151 | int priority, |
f25d0cf3 | 2152 | const struct ofpact *ofpacts, size_t ofpacts_len) |
15aaf599 | 2153 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 2154 | { |
7ee20df1 | 2155 | const struct rule *rule; |
6f00e29b | 2156 | bool must_add; |
7ee20df1 | 2157 | |
97f63e57 BP |
2158 | /* First do a cheap check whether the rule we're looking for already exists |
2159 | * with the actions that we want. If it does, then we're done. */ | |
81a76618 | 2160 | rule = rule_from_cls_rule(classifier_find_match_exactly( |
2b7b1427 | 2161 | &ofproto->tables[0].cls, match, priority, |
44e0c35d | 2162 | OVS_VERSION_MAX)); |
6f00e29b | 2163 | if (rule) { |
dc723c44 | 2164 | const struct rule_actions *actions = rule_get_actions(rule); |
06a0f3e2 | 2165 | must_add = !ofpacts_equal(actions->ofpacts, actions->ofpacts_len, |
6f00e29b | 2166 | ofpacts, ofpacts_len); |
6f00e29b BP |
2167 | } else { |
2168 | must_add = true; | |
2169 | } | |
97f63e57 BP |
2170 | |
2171 | /* If there's no such rule or the rule doesn't have the actions we want, | |
2172 | * fall back to a executing a full flow mod. We can't optimize this at | |
2173 | * all because we didn't take enough locks above to ensure that the flow | |
2174 | * table didn't already change beneath us. */ | |
6f00e29b | 2175 | if (must_add) { |
97f63e57 BP |
2176 | simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len, |
2177 | OFPFC_MODIFY_STRICT); | |
7ee20df1 | 2178 | } |
064af421 BP |
2179 | } |
2180 | ||
d51c8b71 JR |
2181 | /* Executes the flow modification specified in 'fm'. Returns 0 on success, or |
2182 | * an OFPERR_* OpenFlow error code on failure. | |
75a75043 | 2183 | * |
2c7ee524 | 2184 | * This is a helper function for in-band control and fail-open. */ |
d51c8b71 | 2185 | enum ofperr |
7338102b | 2186 | ofproto_flow_mod(struct ofproto *ofproto, const struct ofputil_flow_mod *fm) |
15aaf599 | 2187 | OVS_EXCLUDED(ofproto_mutex) |
75a75043 | 2188 | { |
7338102b | 2189 | return handle_flow_mod__(ofproto, fm, NULL); |
75a75043 BP |
2190 | } |
2191 | ||
6c1491fb BP |
2192 | /* Searches for a rule with matching criteria exactly equal to 'target' in |
2193 | * ofproto's table 0 and, if it finds one, deletes it. | |
2194 | * | |
2195 | * This is a helper function for in-band control and fail-open. */ | |
b20f4073 | 2196 | void |
81a76618 | 2197 | ofproto_delete_flow(struct ofproto *ofproto, |
eb391b76 | 2198 | const struct match *target, int priority) |
25010c68 | 2199 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 2200 | { |
8037acb4 | 2201 | struct classifier *cls = &ofproto->tables[0].cls; |
064af421 BP |
2202 | struct rule *rule; |
2203 | ||
97f63e57 BP |
2204 | /* First do a cheap check whether the rule we're looking for has already |
2205 | * been deleted. If so, then we're done. */ | |
39c94593 | 2206 | rule = rule_from_cls_rule(classifier_find_match_exactly( |
44e0c35d | 2207 | cls, target, priority, OVS_VERSION_MAX)); |
834fe5cb BP |
2208 | if (!rule) { |
2209 | return; | |
bcf84111 | 2210 | } |
834fe5cb | 2211 | |
25010c68 JR |
2212 | struct rule_collection rules; |
2213 | ||
2214 | rule_collection_init(&rules); | |
2215 | rule_collection_add(&rules, rule); | |
2216 | delete_flows__(&rules, OFPRR_DELETE, NULL); | |
2217 | rule_collection_destroy(&rules); | |
142e1f5c BP |
2218 | } |
2219 | ||
834fe5cb BP |
2220 | /* Delete all of the flows from all of ofproto's flow tables, then reintroduce |
2221 | * the flows required by in-band control and fail-open. */ | |
142e1f5c BP |
2222 | void |
2223 | ofproto_flush_flows(struct ofproto *ofproto) | |
2224 | { | |
7ee20df1 | 2225 | COVERAGE_INC(ofproto_flush); |
834fe5cb BP |
2226 | ofproto_flush__(ofproto); |
2227 | connmgr_flushed(ofproto->connmgr); | |
064af421 BP |
2228 | } |
2229 | \f | |
2230 | static void | |
2231 | reinit_ports(struct ofproto *p) | |
2232 | { | |
abe529af | 2233 | struct ofproto_port_dump dump; |
b3c01ed3 | 2234 | struct sset devnames; |
064af421 | 2235 | struct ofport *ofport; |
abe529af | 2236 | struct ofproto_port ofproto_port; |
b3c01ed3 | 2237 | const char *devname; |
064af421 | 2238 | |
898bf89d JP |
2239 | COVERAGE_INC(ofproto_reinit_ports); |
2240 | ||
b3c01ed3 | 2241 | sset_init(&devnames); |
4e8e4213 | 2242 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { |
1264ec95 | 2243 | sset_add(&devnames, netdev_get_name(ofport->netdev)); |
064af421 | 2244 | } |
abe529af BP |
2245 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { |
2246 | sset_add(&devnames, ofproto_port.name); | |
064af421 | 2247 | } |
064af421 | 2248 | |
b3c01ed3 BP |
2249 | SSET_FOR_EACH (devname, &devnames) { |
2250 | update_port(p, devname); | |
064af421 | 2251 | } |
b3c01ed3 | 2252 | sset_destroy(&devnames); |
064af421 BP |
2253 | } |
2254 | ||
4e022ec0 | 2255 | static ofp_port_t |
e1b1d06a JP |
2256 | alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name) |
2257 | { | |
4e022ec0 | 2258 | uint16_t port_idx; |
e1b1d06a | 2259 | |
4e022ec0 | 2260 | port_idx = simap_get(&ofproto->ofp_requests, netdev_name); |
430dbb14 | 2261 | port_idx = port_idx ? port_idx : UINT16_MAX; |
4e022ec0 | 2262 | |
430dbb14 | 2263 | if (port_idx >= ofproto->max_ports |
fdcea803 GS |
2264 | || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) { |
2265 | uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no; | |
2266 | long long int last_used_at, lru = LLONG_MAX; | |
e1b1d06a | 2267 | |
e1b1d06a JP |
2268 | /* Search for a free OpenFlow port number. We try not to |
2269 | * immediately reuse them to prevent problems due to old | |
484c8355 BP |
2270 | * flows. |
2271 | * | |
2272 | * We limit the automatically assigned port numbers to the lower half | |
2273 | * of the port range, to reserve the upper half for assignment by | |
2274 | * controllers. */ | |
6033d9d9 | 2275 | for (;;) { |
484c8355 | 2276 | if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) { |
fdcea803 | 2277 | ofproto->alloc_port_no = 1; |
6033d9d9 | 2278 | } |
fdcea803 GS |
2279 | last_used_at = ofport_get_usage(ofproto, |
2280 | u16_to_ofp(ofproto->alloc_port_no)); | |
2281 | if (!last_used_at) { | |
430dbb14 | 2282 | port_idx = ofproto->alloc_port_no; |
6033d9d9 | 2283 | break; |
865b26a4 GS |
2284 | } else if ( last_used_at < time_msec() - 60*60*1000) { |
2285 | /* If the port with ofport 'ofproto->alloc_port_no' was deleted | |
2286 | * more than an hour ago, consider it usable. */ | |
2287 | ofport_remove_usage(ofproto, | |
2288 | u16_to_ofp(ofproto->alloc_port_no)); | |
2289 | port_idx = ofproto->alloc_port_no; | |
2290 | break; | |
fdcea803 GS |
2291 | } else if (last_used_at < lru) { |
2292 | lru = last_used_at; | |
2293 | lru_ofport = ofproto->alloc_port_no; | |
e1b1d06a | 2294 | } |
fdcea803 | 2295 | |
430dbb14 | 2296 | if (ofproto->alloc_port_no == end_port_no) { |
fdcea803 GS |
2297 | if (lru_ofport) { |
2298 | port_idx = lru_ofport; | |
2299 | break; | |
2300 | } | |
6033d9d9 | 2301 | return OFPP_NONE; |
e1b1d06a JP |
2302 | } |
2303 | } | |
2304 | } | |
fdcea803 | 2305 | ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX); |
4e022ec0 | 2306 | return u16_to_ofp(port_idx); |
e1b1d06a JP |
2307 | } |
2308 | ||
2309 | static void | |
fdcea803 | 2310 | dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port) |
e1b1d06a | 2311 | { |
430dbb14 | 2312 | if (ofp_to_u16(ofp_port) < ofproto->max_ports) { |
fdcea803 | 2313 | ofport_set_usage(ofproto, ofp_port, time_msec()); |
40fa9417 | 2314 | } |
e1b1d06a JP |
2315 | } |
2316 | ||
fbfa2911 BP |
2317 | /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null |
2318 | * pointer if the netdev cannot be opened. On success, also fills in | |
d7d92203 | 2319 | * '*pp'. */ |
b33951b8 | 2320 | static struct netdev * |
e1b1d06a JP |
2321 | ofport_open(struct ofproto *ofproto, |
2322 | struct ofproto_port *ofproto_port, | |
9e1fd49b | 2323 | struct ofputil_phy_port *pp) |
064af421 BP |
2324 | { |
2325 | enum netdev_flags flags; | |
064af421 | 2326 | struct netdev *netdev; |
064af421 BP |
2327 | int error; |
2328 | ||
18812dff | 2329 | error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev); |
064af421 | 2330 | if (error) { |
94783c7c | 2331 | VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu32") because netdev %s " |
064af421 | 2332 | "cannot be opened (%s)", |
fbfa2911 | 2333 | ofproto->name, |
abe529af | 2334 | ofproto_port->name, ofproto_port->ofp_port, |
10a89ef0 | 2335 | ofproto_port->name, ovs_strerror(error)); |
064af421 BP |
2336 | return NULL; |
2337 | } | |
2338 | ||
e1b1d06a JP |
2339 | if (ofproto_port->ofp_port == OFPP_NONE) { |
2340 | if (!strcmp(ofproto->name, ofproto_port->name)) { | |
2341 | ofproto_port->ofp_port = OFPP_LOCAL; | |
2342 | } else { | |
2343 | ofproto_port->ofp_port = alloc_ofp_port(ofproto, | |
2344 | ofproto_port->name); | |
2345 | } | |
2346 | } | |
9e1fd49b | 2347 | pp->port_no = ofproto_port->ofp_port; |
74ff3298 | 2348 | netdev_get_etheraddr(netdev, &pp->hw_addr); |
9e1fd49b | 2349 | ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name); |
064af421 | 2350 | netdev_get_flags(netdev, &flags); |
9e1fd49b BP |
2351 | pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN; |
2352 | pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN; | |
2353 | netdev_get_features(netdev, &pp->curr, &pp->advertised, | |
2354 | &pp->supported, &pp->peer); | |
cd65125d BP |
2355 | pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000; |
2356 | pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000; | |
118c4676 | 2357 | |
b33951b8 | 2358 | return netdev; |
064af421 BP |
2359 | } |
2360 | ||
b33951b8 | 2361 | /* Returns true if most fields of 'a' and 'b' are equal. Differences in name, |
d7d92203 | 2362 | * port number, and 'config' bits other than OFPUTIL_PC_PORT_DOWN are |
b33951b8 BP |
2363 | * disregarded. */ |
2364 | static bool | |
9e1fd49b BP |
2365 | ofport_equal(const struct ofputil_phy_port *a, |
2366 | const struct ofputil_phy_port *b) | |
064af421 | 2367 | { |
9e1fd49b | 2368 | return (eth_addr_equals(a->hw_addr, b->hw_addr) |
064af421 | 2369 | && a->state == b->state |
9e1fd49b | 2370 | && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN) |
064af421 BP |
2371 | && a->curr == b->curr |
2372 | && a->advertised == b->advertised | |
2373 | && a->supported == b->supported | |
9e1fd49b BP |
2374 | && a->peer == b->peer |
2375 | && a->curr_speed == b->curr_speed | |
2376 | && a->max_speed == b->max_speed); | |
064af421 BP |
2377 | } |
2378 | ||
b33951b8 BP |
2379 | /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'. |
2380 | * The caller must ensure that 'p' does not have a conflicting ofport (that is, | |
2381 | * one with the same name or port number). */ | |
01c77073 | 2382 | static int |
b33951b8 | 2383 | ofport_install(struct ofproto *p, |
9e1fd49b | 2384 | struct netdev *netdev, const struct ofputil_phy_port *pp) |
064af421 | 2385 | { |
b33951b8 BP |
2386 | const char *netdev_name = netdev_get_name(netdev); |
2387 | struct ofport *ofport; | |
abe529af | 2388 | int error; |
72b06300 | 2389 | |
b33951b8 | 2390 | /* Create ofport. */ |
abe529af BP |
2391 | ofport = p->ofproto_class->port_alloc(); |
2392 | if (!ofport) { | |
2393 | error = ENOMEM; | |
2394 | goto error; | |
2395 | } | |
0f7d71a5 | 2396 | ofport->ofproto = p; |
b33951b8 | 2397 | ofport->netdev = netdev; |
61501798 | 2398 | ofport->change_seq = netdev_get_change_seq(netdev); |
9e1fd49b BP |
2399 | ofport->pp = *pp; |
2400 | ofport->ofp_port = pp->port_no; | |
65e0be10 | 2401 | ofport->created = time_msec(); |
b33951b8 BP |
2402 | |
2403 | /* Add port to 'p'. */ | |
4e022ec0 | 2404 | hmap_insert(&p->ports, &ofport->hmap_node, |
f9c0c3ec | 2405 | hash_ofp_port(ofport->ofp_port)); |
72b06300 | 2406 | shash_add(&p->port_by_name, netdev_name, ofport); |
abe529af | 2407 | |
ada3428f | 2408 | update_mtu(p, ofport); |
9197df76 | 2409 | |
abe529af BP |
2410 | /* Let the ofproto_class initialize its private data. */ |
2411 | error = p->ofproto_class->port_construct(ofport); | |
2412 | if (error) { | |
2413 | goto error; | |
2414 | } | |
2a6f78e0 | 2415 | connmgr_send_port_status(p->connmgr, NULL, pp, OFPPR_ADD); |
01c77073 | 2416 | return 0; |
abe529af BP |
2417 | |
2418 | error: | |
2419 | VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)", | |
10a89ef0 | 2420 | p->name, netdev_name, ovs_strerror(error)); |
abe529af BP |
2421 | if (ofport) { |
2422 | ofport_destroy__(ofport); | |
2423 | } else { | |
2424 | netdev_close(netdev); | |
72b06300 | 2425 | } |
01c77073 | 2426 | return error; |
064af421 BP |
2427 | } |
2428 | ||
b33951b8 | 2429 | /* Removes 'ofport' from 'p' and destroys it. */ |
064af421 | 2430 | static void |
0f7d71a5 | 2431 | ofport_remove(struct ofport *ofport) |
064af421 | 2432 | { |
0670b5d0 | 2433 | struct ofproto *p = ofport->ofproto; |
3a414a0a | 2434 | bool is_mtu_overridden = ofport_is_mtu_overridden(p, ofport); |
0670b5d0 | 2435 | |
2a6f78e0 | 2436 | connmgr_send_port_status(ofport->ofproto->connmgr, NULL, &ofport->pp, |
fa066f01 | 2437 | OFPPR_DELETE); |
9aad5a5a | 2438 | ofport_destroy(ofport, true); |
3a414a0a | 2439 | if (!is_mtu_overridden) { |
0670b5d0 | 2440 | update_mtu_ofproto(p); |
2441 | } | |
b33951b8 BP |
2442 | } |
2443 | ||
2444 | /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and | |
2445 | * destroys it. */ | |
2446 | static void | |
2447 | ofport_remove_with_name(struct ofproto *ofproto, const char *name) | |
2448 | { | |
2449 | struct ofport *port = shash_find_data(&ofproto->port_by_name, name); | |
2450 | if (port) { | |
0f7d71a5 | 2451 | ofport_remove(port); |
b33951b8 BP |
2452 | } |
2453 | } | |
2454 | ||
9e1fd49b | 2455 | /* Updates 'port' with new 'pp' description. |
b33951b8 BP |
2456 | * |
2457 | * Does not handle a name or port number change. The caller must implement | |
2458 | * such a change as a delete followed by an add. */ | |
2459 | static void | |
9e1fd49b | 2460 | ofport_modified(struct ofport *port, struct ofputil_phy_port *pp) |
b33951b8 | 2461 | { |
74ff3298 | 2462 | port->pp.hw_addr = pp->hw_addr; |
9e1fd49b BP |
2463 | port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN) |
2464 | | (pp->config & OFPUTIL_PC_PORT_DOWN)); | |
53fd5c7c BP |
2465 | port->pp.state = ((port->pp.state & ~OFPUTIL_PS_LINK_DOWN) |
2466 | | (pp->state & OFPUTIL_PS_LINK_DOWN)); | |
9e1fd49b BP |
2467 | port->pp.curr = pp->curr; |
2468 | port->pp.advertised = pp->advertised; | |
2469 | port->pp.supported = pp->supported; | |
2470 | port->pp.peer = pp->peer; | |
2471 | port->pp.curr_speed = pp->curr_speed; | |
2472 | port->pp.max_speed = pp->max_speed; | |
b33951b8 | 2473 | |
2a6f78e0 BP |
2474 | connmgr_send_port_status(port->ofproto->connmgr, NULL, |
2475 | &port->pp, OFPPR_MODIFY); | |
064af421 BP |
2476 | } |
2477 | ||
5a2dfd47 JP |
2478 | /* Update OpenFlow 'state' in 'port' and notify controller. */ |
2479 | void | |
9e1fd49b | 2480 | ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state) |
5a2dfd47 | 2481 | { |
9e1fd49b BP |
2482 | if (port->pp.state != state) { |
2483 | port->pp.state = state; | |
2a6f78e0 BP |
2484 | connmgr_send_port_status(port->ofproto->connmgr, NULL, |
2485 | &port->pp, OFPPR_MODIFY); | |
5a2dfd47 JP |
2486 | } |
2487 | } | |
2488 | ||
abe529af | 2489 | void |
4e022ec0 | 2490 | ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port) |
e7934396 | 2491 | { |
abe529af BP |
2492 | struct ofport *port = ofproto_get_port(ofproto, ofp_port); |
2493 | if (port) { | |
b308140a JP |
2494 | if (port->ofproto->ofproto_class->set_stp_port) { |
2495 | port->ofproto->ofproto_class->set_stp_port(port, NULL); | |
2496 | } | |
9efd308e DV |
2497 | if (port->ofproto->ofproto_class->set_rstp_port) { |
2498 | port->ofproto->ofproto_class->set_rstp_port(port, NULL); | |
2499 | } | |
abe529af | 2500 | if (port->ofproto->ofproto_class->set_cfm) { |
a5610457 | 2501 | port->ofproto->ofproto_class->set_cfm(port, NULL); |
abe529af BP |
2502 | } |
2503 | if (port->ofproto->ofproto_class->bundle_remove) { | |
2504 | port->ofproto->ofproto_class->bundle_remove(port); | |
e7934396 BP |
2505 | } |
2506 | } | |
2507 | } | |
2508 | ||
2509 | static void | |
abe529af | 2510 | ofport_destroy__(struct ofport *port) |
e7934396 | 2511 | { |
abe529af BP |
2512 | struct ofproto *ofproto = port->ofproto; |
2513 | const char *name = netdev_get_name(port->netdev); | |
fa066f01 | 2514 | |
abe529af BP |
2515 | hmap_remove(&ofproto->ports, &port->hmap_node); |
2516 | shash_delete(&ofproto->port_by_name, | |
2517 | shash_find(&ofproto->port_by_name, name)); | |
fa066f01 | 2518 | |
abe529af BP |
2519 | netdev_close(port->netdev); |
2520 | ofproto->ofproto_class->port_dealloc(port); | |
e7934396 BP |
2521 | } |
2522 | ||
064af421 | 2523 | static void |
9aad5a5a | 2524 | ofport_destroy(struct ofport *port, bool del) |
064af421 | 2525 | { |
fa066f01 | 2526 | if (port) { |
e1b1d06a | 2527 | dealloc_ofp_port(port->ofproto, port->ofp_port); |
9aad5a5a | 2528 | port->ofproto->ofproto_class->port_destruct(port, del); |
abe529af BP |
2529 | ofport_destroy__(port); |
2530 | } | |
064af421 BP |
2531 | } |
2532 | ||
abe529af | 2533 | struct ofport * |
4e022ec0 | 2534 | ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port) |
ca0f572c BP |
2535 | { |
2536 | struct ofport *port; | |
2537 | ||
f9c0c3ec | 2538 | HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port), |
4e022ec0 | 2539 | &ofproto->ports) { |
abe529af | 2540 | if (port->ofp_port == ofp_port) { |
ca0f572c BP |
2541 | return port; |
2542 | } | |
2543 | } | |
2544 | return NULL; | |
2545 | } | |
2546 | ||
fdcea803 GS |
2547 | static long long int |
2548 | ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port) | |
2549 | { | |
2550 | struct ofport_usage *usage; | |
2551 | ||
2552 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2553 | &ofproto->ofport_usage) { | |
2554 | if (usage->ofp_port == ofp_port) { | |
2555 | return usage->last_used; | |
2556 | } | |
2557 | } | |
2558 | return 0; | |
2559 | } | |
2560 | ||
2561 | static void | |
2562 | ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port, | |
2563 | long long int last_used) | |
2564 | { | |
2565 | struct ofport_usage *usage; | |
2566 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2567 | &ofproto->ofport_usage) { | |
2568 | if (usage->ofp_port == ofp_port) { | |
2569 | usage->last_used = last_used; | |
2570 | return; | |
2571 | } | |
2572 | } | |
2573 | ovs_assert(last_used == LLONG_MAX); | |
2574 | ||
2575 | usage = xmalloc(sizeof *usage); | |
2576 | usage->ofp_port = ofp_port; | |
2577 | usage->last_used = last_used; | |
2578 | hmap_insert(&ofproto->ofport_usage, &usage->hmap_node, | |
2579 | hash_ofp_port(ofp_port)); | |
2580 | } | |
2581 | ||
865b26a4 GS |
2582 | static void |
2583 | ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port) | |
2584 | { | |
2585 | struct ofport_usage *usage; | |
2586 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2587 | &ofproto->ofport_usage) { | |
2588 | if (usage->ofp_port == ofp_port) { | |
2589 | hmap_remove(&ofproto->ofport_usage, &usage->hmap_node); | |
2590 | free(usage); | |
2591 | break; | |
2592 | } | |
2593 | } | |
2594 | } | |
2595 | ||
6527c598 PS |
2596 | int |
2597 | ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats) | |
2598 | { | |
2599 | struct ofproto *ofproto = port->ofproto; | |
2600 | int error; | |
2601 | ||
2602 | if (ofproto->ofproto_class->port_get_stats) { | |
2603 | error = ofproto->ofproto_class->port_get_stats(port, stats); | |
2604 | } else { | |
2605 | error = EOPNOTSUPP; | |
2606 | } | |
2607 | ||
2608 | return error; | |
2609 | } | |
2610 | ||
01c77073 | 2611 | static int |
b33951b8 | 2612 | update_port(struct ofproto *ofproto, const char *name) |
064af421 | 2613 | { |
abe529af | 2614 | struct ofproto_port ofproto_port; |
9e1fd49b | 2615 | struct ofputil_phy_port pp; |
b33951b8 BP |
2616 | struct netdev *netdev; |
2617 | struct ofport *port; | |
01c77073 | 2618 | int error = 0; |
064af421 BP |
2619 | |
2620 | COVERAGE_INC(ofproto_update_port); | |
c874dc6d | 2621 | |
b33951b8 | 2622 | /* Fetch 'name''s location and properties from the datapath. */ |
abe529af | 2623 | netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port) |
fbfa2911 | 2624 | ? ofport_open(ofproto, &ofproto_port, &pp) |
b33951b8 | 2625 | : NULL); |
4e022ec0 | 2626 | |
b33951b8 | 2627 | if (netdev) { |
abe529af | 2628 | port = ofproto_get_port(ofproto, ofproto_port.ofp_port); |
b33951b8 | 2629 | if (port && !strcmp(netdev_get_name(port->netdev), name)) { |
e65942a0 BP |
2630 | struct netdev *old_netdev = port->netdev; |
2631 | ||
b33951b8 | 2632 | /* 'name' hasn't changed location. Any properties changed? */ |
9e1fd49b BP |
2633 | if (!ofport_equal(&port->pp, &pp)) { |
2634 | ofport_modified(port, &pp); | |
abe529af BP |
2635 | } |
2636 | ||
ada3428f | 2637 | update_mtu(ofproto, port); |
9197df76 | 2638 | |
e65942a0 BP |
2639 | /* Install the newly opened netdev in case it has changed. |
2640 | * Don't close the old netdev yet in case port_modified has to | |
2641 | * remove a retained reference to it.*/ | |
abe529af | 2642 | port->netdev = netdev; |
61501798 | 2643 | port->change_seq = netdev_get_change_seq(netdev); |
abe529af BP |
2644 | |
2645 | if (port->ofproto->ofproto_class->port_modified) { | |
2646 | port->ofproto->ofproto_class->port_modified(port); | |
b33951b8 | 2647 | } |
e65942a0 BP |
2648 | |
2649 | netdev_close(old_netdev); | |
b33951b8 BP |
2650 | } else { |
2651 | /* If 'port' is nonnull then its name differs from 'name' and thus | |
2652 | * we should delete it. If we think there's a port named 'name' | |
2653 | * then its port number must be wrong now so delete it too. */ | |
2654 | if (port) { | |
0f7d71a5 | 2655 | ofport_remove(port); |
b33951b8 BP |
2656 | } |
2657 | ofport_remove_with_name(ofproto, name); | |
01c77073 | 2658 | error = ofport_install(ofproto, netdev, &pp); |
c874dc6d | 2659 | } |
b33951b8 BP |
2660 | } else { |
2661 | /* Any port named 'name' is gone now. */ | |
2662 | ofport_remove_with_name(ofproto, name); | |
c874dc6d | 2663 | } |
abe529af | 2664 | ofproto_port_destroy(&ofproto_port); |
01c77073 BP |
2665 | |
2666 | return error; | |
064af421 BP |
2667 | } |
2668 | ||
2669 | static int | |
2670 | init_ports(struct ofproto *p) | |
2671 | { | |
abe529af BP |
2672 | struct ofproto_port_dump dump; |
2673 | struct ofproto_port ofproto_port; | |
e1b1d06a | 2674 | struct shash_node *node, *next; |
abe529af BP |
2675 | |
2676 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { | |
e1b1d06a JP |
2677 | const char *name = ofproto_port.name; |
2678 | ||
2679 | if (shash_find(&p->port_by_name, name)) { | |
fbfa2911 | 2680 | VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath", |
e1b1d06a | 2681 | p->name, name); |
abe529af | 2682 | } else { |
9e1fd49b | 2683 | struct ofputil_phy_port pp; |
b33951b8 BP |
2684 | struct netdev *netdev; |
2685 | ||
e1b1d06a JP |
2686 | /* Check if an OpenFlow port number had been requested. */ |
2687 | node = shash_find(&init_ofp_ports, name); | |
2688 | if (node) { | |
2689 | const struct iface_hint *iface_hint = node->data; | |
4e022ec0 AW |
2690 | simap_put(&p->ofp_requests, name, |
2691 | ofp_to_u16(iface_hint->ofp_port)); | |
e1b1d06a JP |
2692 | } |
2693 | ||
fbfa2911 | 2694 | netdev = ofport_open(p, &ofproto_port, &pp); |
b33951b8 | 2695 | if (netdev) { |
9e1fd49b | 2696 | ofport_install(p, netdev, &pp); |
430dbb14 | 2697 | if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) { |
7c35397c | 2698 | p->alloc_port_no = MAX(p->alloc_port_no, |
430dbb14 | 2699 | ofp_to_u16(ofproto_port.ofp_port)); |
7c35397c | 2700 | } |
064af421 BP |
2701 | } |
2702 | } | |
2703 | } | |
b0ec0f27 | 2704 | |
e1b1d06a | 2705 | SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) { |
4f9e08a5 | 2706 | struct iface_hint *iface_hint = node->data; |
e1b1d06a JP |
2707 | |
2708 | if (!strcmp(iface_hint->br_name, p->name)) { | |
2709 | free(iface_hint->br_name); | |
2710 | free(iface_hint->br_type); | |
4f9e08a5 | 2711 | free(iface_hint); |
e1b1d06a JP |
2712 | shash_delete(&init_ofp_ports, node); |
2713 | } | |
2714 | } | |
2715 | ||
064af421 BP |
2716 | return 0; |
2717 | } | |
9197df76 | 2718 | |
3a414a0a | 2719 | static bool |
86fce577 | 2720 | ofport_is_internal(const struct ofproto *p, const struct ofport *port) |
0670b5d0 | 2721 | { |
86fce577 DDP |
2722 | return !strcmp(netdev_get_type(port->netdev), |
2723 | ofproto_port_open_type(p->type, "internal")); | |
0670b5d0 | 2724 | } |
2725 | ||
3a414a0a DDP |
2726 | /* If 'port' is internal and if the user didn't explicitly specify an mtu |
2727 | * through the database, we have to override it. */ | |
2728 | static bool | |
2729 | ofport_is_mtu_overridden(const struct ofproto *p, const struct ofport *port) | |
2730 | { | |
2731 | return ofport_is_internal(p, port) | |
2732 | && !netdev_mtu_is_user_config(port->netdev); | |
2733 | } | |
2734 | ||
2735 | /* Find the minimum MTU of all non-overridden devices attached to 'p'. | |
9197df76 JP |
2736 | * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */ |
2737 | static int | |
2738 | find_min_mtu(struct ofproto *p) | |
2739 | { | |
2740 | struct ofport *ofport; | |
2741 | int mtu = 0; | |
2742 | ||
2743 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
2744 | struct netdev *netdev = ofport->netdev; | |
2745 | int dev_mtu; | |
2746 | ||
3a414a0a DDP |
2747 | /* Skip any overridden port, since that's what we're trying to set. */ |
2748 | if (ofport_is_mtu_overridden(p, ofport)) { | |
9197df76 JP |
2749 | continue; |
2750 | } | |
2751 | ||
2752 | if (netdev_get_mtu(netdev, &dev_mtu)) { | |
2753 | continue; | |
2754 | } | |
2755 | if (!mtu || dev_mtu < mtu) { | |
2756 | mtu = dev_mtu; | |
2757 | } | |
2758 | } | |
2759 | ||
2760 | return mtu ? mtu: ETH_PAYLOAD_MAX; | |
2761 | } | |
2762 | ||
3a414a0a DDP |
2763 | /* Update MTU of all overridden devices on 'p' to the minimum of the |
2764 | * non-overridden ports in event of 'port' added or changed. */ | |
9197df76 | 2765 | static void |
ada3428f | 2766 | update_mtu(struct ofproto *p, struct ofport *port) |
9197df76 | 2767 | { |
ada3428f | 2768 | struct netdev *netdev = port->netdev; |
0670b5d0 | 2769 | int dev_mtu; |
ada3428f PS |
2770 | |
2771 | if (netdev_get_mtu(netdev, &dev_mtu)) { | |
2772 | port->mtu = 0; | |
2773 | return; | |
2774 | } | |
3a414a0a | 2775 | if (ofport_is_mtu_overridden(p, port)) { |
7478b5a2 | 2776 | if (dev_mtu > p->min_mtu) { |
3a414a0a DDP |
2777 | if (!netdev_set_mtu(port->netdev, p->min_mtu)) { |
2778 | dev_mtu = p->min_mtu; | |
2779 | } | |
ada3428f PS |
2780 | } |
2781 | port->mtu = dev_mtu; | |
2782 | return; | |
2783 | } | |
2784 | ||
ada3428f | 2785 | port->mtu = dev_mtu; |
3a414a0a DDP |
2786 | /* For non-overridden port find new min mtu. */ |
2787 | ||
0670b5d0 | 2788 | update_mtu_ofproto(p); |
2789 | } | |
2790 | ||
2791 | static void | |
2792 | update_mtu_ofproto(struct ofproto *p) | |
2793 | { | |
0670b5d0 | 2794 | struct ofport *ofport; |
2795 | int old_min = p->min_mtu; | |
2796 | ||
ada3428f PS |
2797 | p->min_mtu = find_min_mtu(p); |
2798 | if (p->min_mtu == old_min) { | |
2799 | return; | |
2800 | } | |
9197df76 JP |
2801 | |
2802 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
2803 | struct netdev *netdev = ofport->netdev; | |
2804 | ||
3a414a0a | 2805 | if (ofport_is_mtu_overridden(p, ofport)) { |
ada3428f PS |
2806 | if (!netdev_set_mtu(netdev, p->min_mtu)) { |
2807 | ofport->mtu = p->min_mtu; | |
2808 | } | |
9197df76 JP |
2809 | } |
2810 | } | |
2811 | } | |
064af421 | 2812 | \f |
f416c8d6 JR |
2813 | static void |
2814 | ofproto_rule_destroy__(struct rule *rule) | |
2815 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
2816 | { | |
2817 | cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr)); | |
2818 | rule_actions_destroy(rule_get_actions(rule)); | |
2819 | ovs_mutex_destroy(&rule->mutex); | |
2820 | rule->ofproto->ofproto_class->rule_dealloc(rule); | |
2821 | } | |
2822 | ||
2823 | static void | |
2824 | rule_destroy_cb(struct rule *rule) | |
f695ebfa | 2825 | OVS_NO_THREAD_SAFETY_ANALYSIS |
f416c8d6 | 2826 | { |
f695ebfa JR |
2827 | /* Send rule removed if needed. */ |
2828 | if (rule->flags & OFPUTIL_FF_SEND_FLOW_REM | |
2829 | && rule->removed_reason != OVS_OFPRR_NONE | |
2830 | && !rule_is_hidden(rule)) { | |
2831 | ofproto_rule_send_removed(rule); | |
2832 | } | |
f416c8d6 | 2833 | rule->ofproto->ofproto_class->rule_destruct(rule); |
5c7c16d8 YHW |
2834 | mf_vl_mff_unref(&rule->ofproto->vl_mff_map, rule->match_tlv_bitmap); |
2835 | mf_vl_mff_unref(&rule->ofproto->vl_mff_map, rule->ofpacts_tlv_bitmap); | |
f416c8d6 JR |
2836 | ofproto_rule_destroy__(rule); |
2837 | } | |
2838 | ||
a2143702 BP |
2839 | void |
2840 | ofproto_rule_ref(struct rule *rule) | |
064af421 | 2841 | { |
1eae3d33 | 2842 | if (rule) { |
37bec3d3 | 2843 | ovs_refcount_ref(&rule->ref_count); |
a2143702 BP |
2844 | } |
2845 | } | |
2846 | ||
f5d16e55 JR |
2847 | bool |
2848 | ofproto_rule_try_ref(struct rule *rule) | |
2849 | { | |
2850 | if (rule) { | |
2851 | return ovs_refcount_try_ref_rcu(&rule->ref_count); | |
2852 | } | |
2853 | return false; | |
2854 | } | |
2855 | ||
f416c8d6 JR |
2856 | /* Decrements 'rule''s ref_count and schedules 'rule' to be destroyed if the |
2857 | * ref_count reaches 0. | |
2858 | * | |
2859 | * Use of RCU allows short term use (between RCU quiescent periods) without | |
2860 | * keeping a reference. A reference must be taken if the rule needs to | |
2861 | * stay around accross the RCU quiescent periods. */ | |
a2143702 BP |
2862 | void |
2863 | ofproto_rule_unref(struct rule *rule) | |
2864 | { | |
24f83812 | 2865 | if (rule && ovs_refcount_unref_relaxed(&rule->ref_count) == 1) { |
2c7ee524 | 2866 | ovs_assert(rule->state != RULE_INSERTED); |
f416c8d6 | 2867 | ovsrcu_postpone(rule_destroy_cb, rule); |
1eae3d33 | 2868 | } |
064af421 BP |
2869 | } |
2870 | ||
39c94593 JR |
2871 | static void |
2872 | remove_rule_rcu__(struct rule *rule) | |
2873 | OVS_REQUIRES(ofproto_mutex) | |
2874 | { | |
2875 | struct ofproto *ofproto = rule->ofproto; | |
2876 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
2877 | ||
44e0c35d | 2878 | ovs_assert(!cls_rule_visible_in_version(&rule->cr, OVS_VERSION_MAX)); |
39c94593 JR |
2879 | if (!classifier_remove(&table->cls, &rule->cr)) { |
2880 | OVS_NOT_REACHED(); | |
2881 | } | |
1fc71871 JR |
2882 | if (ofproto->ofproto_class->rule_delete) { |
2883 | ofproto->ofproto_class->rule_delete(rule); | |
2884 | } | |
39c94593 JR |
2885 | ofproto_rule_unref(rule); |
2886 | } | |
2887 | ||
2888 | static void | |
2889 | remove_rule_rcu(struct rule *rule) | |
2890 | OVS_EXCLUDED(ofproto_mutex) | |
2891 | { | |
2892 | ovs_mutex_lock(&ofproto_mutex); | |
2893 | remove_rule_rcu__(rule); | |
2894 | ovs_mutex_unlock(&ofproto_mutex); | |
2895 | } | |
2896 | ||
2897 | /* Removes and deletes rules from a NULL-terminated array of rule pointers. */ | |
2898 | static void | |
2899 | remove_rules_rcu(struct rule **rules) | |
2900 | OVS_EXCLUDED(ofproto_mutex) | |
2901 | { | |
2902 | struct rule **orig_rules = rules; | |
2903 | ||
2904 | if (*rules) { | |
2905 | struct ofproto *ofproto = rules[0]->ofproto; | |
2906 | unsigned long tables[BITMAP_N_LONGS(256)]; | |
2907 | struct rule *rule; | |
2908 | size_t table_id; | |
2909 | ||
2910 | memset(tables, 0, sizeof tables); | |
2911 | ||
2912 | ovs_mutex_lock(&ofproto_mutex); | |
2913 | while ((rule = *rules++)) { | |
2914 | /* Defer once for each new table. This defers the subtable cleanup | |
2915 | * until later, so that when removing large number of flows the | |
2916 | * operation is faster. */ | |
2917 | if (!bitmap_is_set(tables, rule->table_id)) { | |
2918 | struct classifier *cls = &ofproto->tables[rule->table_id].cls; | |
2919 | ||
2920 | bitmap_set1(tables, rule->table_id); | |
2921 | classifier_defer(cls); | |
2922 | } | |
2923 | remove_rule_rcu__(rule); | |
2924 | } | |
2925 | ||
2926 | BITMAP_FOR_EACH_1(table_id, 256, tables) { | |
2927 | struct classifier *cls = &ofproto->tables[table_id].cls; | |
2928 | ||
2929 | classifier_publish(cls); | |
2930 | } | |
2931 | ovs_mutex_unlock(&ofproto_mutex); | |
2932 | } | |
2933 | ||
2934 | free(orig_rules); | |
2935 | } | |
2936 | ||
809c7548 RW |
2937 | void |
2938 | ofproto_group_ref(struct ofgroup *group) | |
2939 | { | |
2940 | if (group) { | |
2941 | ovs_refcount_ref(&group->ref_count); | |
2942 | } | |
2943 | } | |
2944 | ||
db88b35c JR |
2945 | bool |
2946 | ofproto_group_try_ref(struct ofgroup *group) | |
2947 | { | |
2948 | if (group) { | |
2949 | return ovs_refcount_try_ref_rcu(&group->ref_count); | |
2950 | } | |
2951 | return false; | |
2952 | } | |
2953 | ||
2954 | static void | |
2955 | group_destroy_cb(struct ofgroup *group) | |
2956 | { | |
2957 | group->ofproto->ofproto_class->group_destruct(group); | |
e8dba719 JR |
2958 | ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props *, |
2959 | &group->props)); | |
db88b35c JR |
2960 | ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list *, |
2961 | &group->buckets)); | |
2962 | group->ofproto->ofproto_class->group_dealloc(group); | |
2963 | } | |
2964 | ||
809c7548 RW |
2965 | void |
2966 | ofproto_group_unref(struct ofgroup *group) | |
cc099268 | 2967 | OVS_NO_THREAD_SAFETY_ANALYSIS |
809c7548 | 2968 | { |
db88b35c | 2969 | if (group && ovs_refcount_unref_relaxed(&group->ref_count) == 1) { |
fe59694b | 2970 | ovs_assert(rule_collection_n(&group->rules) == 0); |
db88b35c | 2971 | ovsrcu_postpone(group_destroy_cb, group); |
809c7548 RW |
2972 | } |
2973 | } | |
2974 | ||
5d08a275 JR |
2975 | static void |
2976 | remove_group_rcu__(struct ofgroup *group) | |
2977 | OVS_REQUIRES(ofproto_mutex) | |
2978 | { | |
2979 | struct ofproto *ofproto = group->ofproto; | |
2980 | ||
2981 | ovs_assert(!versions_visible_in_version(&group->versions, OVS_VERSION_MAX)); | |
2982 | ||
2983 | cmap_remove(&ofproto->groups, &group->cmap_node, | |
2984 | hash_int(group->group_id, 0)); | |
2985 | ofproto_group_unref(group); | |
2986 | } | |
2987 | ||
2988 | static void | |
2989 | remove_group_rcu(struct ofgroup *group) | |
2990 | OVS_EXCLUDED(ofproto_mutex) | |
2991 | { | |
2992 | ovs_mutex_lock(&ofproto_mutex); | |
2993 | remove_group_rcu__(group); | |
2994 | ovs_mutex_unlock(&ofproto_mutex); | |
2995 | } | |
2996 | ||
2997 | /* Removes and deletes groups from a NULL-terminated array of group | |
2998 | * pointers. */ | |
2999 | static void | |
3000 | remove_groups_rcu(struct ofgroup **groups) | |
3001 | OVS_EXCLUDED(ofproto_mutex) | |
3002 | { | |
3003 | ovs_mutex_lock(&ofproto_mutex); | |
3004 | for (struct ofgroup **g = groups; *g; g++) { | |
3005 | remove_group_rcu__(*g); | |
3006 | } | |
3007 | ovs_mutex_unlock(&ofproto_mutex); | |
3008 | free(groups); | |
3009 | } | |
3010 | ||
076caa2f JR |
3011 | static bool ofproto_fix_meter_action(const struct ofproto *, |
3012 | struct ofpact_meter *); | |
65efd2ab | 3013 | |
dc723c44 JR |
3014 | /* Creates and returns a new 'struct rule_actions', whose actions are a copy |
3015 | * of from the 'ofpacts_len' bytes of 'ofpacts'. */ | |
3016 | const struct rule_actions * | |
4c7562c5 | 3017 | rule_actions_create(const struct ofpact *ofpacts, size_t ofpacts_len) |
6f00e29b BP |
3018 | { |
3019 | struct rule_actions *actions; | |
3020 | ||
dc723c44 | 3021 | actions = xmalloc(sizeof *actions + ofpacts_len); |
6f00e29b | 3022 | actions->ofpacts_len = ofpacts_len; |
dc723c44 | 3023 | memcpy(actions->ofpacts, ofpacts, ofpacts_len); |
cc099268 JR |
3024 | actions->has_meter = ofpacts_get_meter(ofpacts, ofpacts_len) != 0; |
3025 | actions->has_groups = | |
3026 | (ofpact_find_type_flattened(ofpacts, OFPACT_GROUP, | |
3027 | ofpact_end(ofpacts, ofpacts_len)) | |
3028 | != NULL); | |
35f48b8b BP |
3029 | actions->has_learn_with_delete = (next_learn_with_delete(actions, NULL) |
3030 | != NULL); | |
3031 | ||
6f00e29b BP |
3032 | return actions; |
3033 | } | |
3034 | ||
dc723c44 | 3035 | /* Free the actions after the RCU quiescent period is reached. */ |
6f00e29b | 3036 | void |
dc723c44 | 3037 | rule_actions_destroy(const struct rule_actions *actions) |
6f00e29b | 3038 | { |
06a0f3e2 | 3039 | if (actions) { |
dc723c44 | 3040 | ovsrcu_postpone(free, CONST_CAST(struct rule_actions *, actions)); |
6f00e29b BP |
3041 | } |
3042 | } | |
3043 | ||
bcf84111 | 3044 | /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action |
f25d0cf3 | 3045 | * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */ |
cdbdeeda | 3046 | bool |
4e022ec0 | 3047 | ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port) |
15aaf599 | 3048 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 3049 | { |
06a0f3e2 BP |
3050 | if (port == OFPP_ANY) { |
3051 | return true; | |
3052 | } else { | |
3053 | const struct rule_actions *actions = rule_get_actions(rule); | |
3054 | return ofpacts_output_to_port(actions->ofpacts, | |
3055 | actions->ofpacts_len, port); | |
3056 | } | |
064af421 BP |
3057 | } |
3058 | ||
7395c052 | 3059 | /* Returns true if 'rule' has group and equals group_id. */ |
74e79b7c | 3060 | static bool |
7395c052 | 3061 | ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id) |
15aaf599 | 3062 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 3063 | { |
06a0f3e2 BP |
3064 | if (group_id == OFPG_ANY) { |
3065 | return true; | |
3066 | } else { | |
3067 | const struct rule_actions *actions = rule_get_actions(rule); | |
3068 | return ofpacts_output_to_group(actions->ofpacts, | |
3069 | actions->ofpacts_len, group_id); | |
3070 | } | |
7395c052 NZ |
3071 | } |
3072 | ||
adcf00ba | 3073 | static bool |
834fe5cb | 3074 | rule_is_readonly(const struct rule *rule) |
5c67e4af | 3075 | { |
834fe5cb BP |
3076 | const struct oftable *table = &rule->ofproto->tables[rule->table_id]; |
3077 | return (table->flags & OFTABLE_READONLY) != 0; | |
5c67e4af | 3078 | } |
fa066f01 | 3079 | \f |
35f48b8b BP |
3080 | static uint32_t |
3081 | hash_learned_cookie(ovs_be64 cookie_, uint8_t table_id) | |
3082 | { | |
3083 | uint64_t cookie = (OVS_FORCE uint64_t) cookie_; | |
3084 | return hash_3words(cookie, cookie >> 32, table_id); | |
3085 | } | |
3086 | ||
3087 | static void | |
3088 | learned_cookies_update_one__(struct ofproto *ofproto, | |
3089 | const struct ofpact_learn *learn, | |
ca6ba700 | 3090 | int delta, struct ovs_list *dead_cookies) |
35f48b8b BP |
3091 | OVS_REQUIRES(ofproto_mutex) |
3092 | { | |
3093 | uint32_t hash = hash_learned_cookie(learn->cookie, learn->table_id); | |
3094 | struct learned_cookie *c; | |
3095 | ||
3096 | HMAP_FOR_EACH_WITH_HASH (c, u.hmap_node, hash, &ofproto->learned_cookies) { | |
3097 | if (c->cookie == learn->cookie && c->table_id == learn->table_id) { | |
3098 | c->n += delta; | |
3099 | ovs_assert(c->n >= 0); | |
3100 | ||
3101 | if (!c->n) { | |
3102 | hmap_remove(&ofproto->learned_cookies, &c->u.hmap_node); | |
417e7e66 | 3103 | ovs_list_push_back(dead_cookies, &c->u.list_node); |
35f48b8b BP |
3104 | } |
3105 | ||
3106 | return; | |
3107 | } | |
3108 | } | |
3109 | ||
3110 | ovs_assert(delta > 0); | |
3111 | c = xmalloc(sizeof *c); | |
3112 | hmap_insert(&ofproto->learned_cookies, &c->u.hmap_node, hash); | |
3113 | c->cookie = learn->cookie; | |
3114 | c->table_id = learn->table_id; | |
3115 | c->n = delta; | |
3116 | } | |
3117 | ||
3118 | static const struct ofpact_learn * | |
3119 | next_learn_with_delete(const struct rule_actions *actions, | |
3120 | const struct ofpact_learn *start) | |
3121 | { | |
3122 | const struct ofpact *pos; | |
3123 | ||
3124 | for (pos = start ? ofpact_next(&start->ofpact) : actions->ofpacts; | |
3125 | pos < ofpact_end(actions->ofpacts, actions->ofpacts_len); | |
3126 | pos = ofpact_next(pos)) { | |
3127 | if (pos->type == OFPACT_LEARN) { | |
3128 | const struct ofpact_learn *learn = ofpact_get_LEARN(pos); | |
3129 | if (learn->flags & NX_LEARN_F_DELETE_LEARNED) { | |
3130 | return learn; | |
3131 | } | |
3132 | } | |
3133 | } | |
3134 | ||
3135 | return NULL; | |
3136 | } | |
3137 | ||
3138 | static void | |
3139 | learned_cookies_update__(struct ofproto *ofproto, | |
3140 | const struct rule_actions *actions, | |
ca6ba700 | 3141 | int delta, struct ovs_list *dead_cookies) |
35f48b8b BP |
3142 | OVS_REQUIRES(ofproto_mutex) |
3143 | { | |
3144 | if (actions->has_learn_with_delete) { | |
3145 | const struct ofpact_learn *learn; | |
3146 | ||
3147 | for (learn = next_learn_with_delete(actions, NULL); learn; | |
3148 | learn = next_learn_with_delete(actions, learn)) { | |
3149 | learned_cookies_update_one__(ofproto, learn, delta, dead_cookies); | |
3150 | } | |
3151 | } | |
3152 | } | |
3153 | ||
3154 | static void | |
3155 | learned_cookies_inc(struct ofproto *ofproto, | |
3156 | const struct rule_actions *actions) | |
3157 | OVS_REQUIRES(ofproto_mutex) | |
3158 | { | |
3159 | learned_cookies_update__(ofproto, actions, +1, NULL); | |
3160 | } | |
3161 | ||
3162 | static void | |
3163 | learned_cookies_dec(struct ofproto *ofproto, | |
3164 | const struct rule_actions *actions, | |
ca6ba700 | 3165 | struct ovs_list *dead_cookies) |
35f48b8b BP |
3166 | OVS_REQUIRES(ofproto_mutex) |
3167 | { | |
3168 | learned_cookies_update__(ofproto, actions, -1, dead_cookies); | |
3169 | } | |
3170 | ||
3171 | static void | |
ca6ba700 | 3172 | learned_cookies_flush(struct ofproto *ofproto, struct ovs_list *dead_cookies) |
35f48b8b BP |
3173 | OVS_REQUIRES(ofproto_mutex) |
3174 | { | |
5f03c983 | 3175 | struct learned_cookie *c; |
35f48b8b | 3176 | |
5f03c983 | 3177 | LIST_FOR_EACH_POP (c, u.list_node, dead_cookies) { |
35f48b8b BP |
3178 | struct rule_criteria criteria; |
3179 | struct rule_collection rules; | |
3180 | struct match match; | |
3181 | ||
3182 | match_init_catchall(&match); | |
44e0c35d | 3183 | rule_criteria_init(&criteria, c->table_id, &match, 0, OVS_VERSION_MAX, |
35f48b8b BP |
3184 | c->cookie, OVS_BE64_MAX, OFPP_ANY, OFPG_ANY); |
3185 | rule_criteria_require_rw(&criteria, false); | |
3186 | collect_rules_loose(ofproto, &criteria, &rules); | |
35f48b8b | 3187 | rule_criteria_destroy(&criteria); |
39c94593 | 3188 | delete_flows__(&rules, OFPRR_DELETE, NULL); |
35f48b8b | 3189 | |
35f48b8b BP |
3190 | free(c); |
3191 | } | |
3192 | } | |
3193 | \f | |
90bf1e07 | 3194 | static enum ofperr |
d1e2cf21 | 3195 | handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3196 | { |
b0421aa2 | 3197 | ofconn_send_reply(ofconn, make_echo_reply(oh)); |
064af421 | 3198 | return 0; |
064af421 BP |
3199 | } |
3200 | ||
3c1bb396 BP |
3201 | static void |
3202 | query_tables(struct ofproto *ofproto, | |
3203 | struct ofputil_table_features **featuresp, | |
3204 | struct ofputil_table_stats **statsp) | |
3205 | { | |
178742f9 BP |
3206 | struct mf_bitmap rw_fields = oxm_writable_fields(); |
3207 | struct mf_bitmap match = oxm_matchable_fields(); | |
3208 | struct mf_bitmap mask = oxm_maskable_fields(); | |
3c1bb396 BP |
3209 | |
3210 | struct ofputil_table_features *features; | |
3211 | struct ofputil_table_stats *stats; | |
3212 | int i; | |
3213 | ||
3c1bb396 BP |
3214 | features = *featuresp = xcalloc(ofproto->n_tables, sizeof *features); |
3215 | for (i = 0; i < ofproto->n_tables; i++) { | |
3216 | struct ofputil_table_features *f = &features[i]; | |
3217 | ||
3218 | f->table_id = i; | |
3219 | sprintf(f->name, "table%d", i); | |
3220 | f->metadata_match = OVS_BE64_MAX; | |
3221 | f->metadata_write = OVS_BE64_MAX; | |
afcea9ea | 3222 | atomic_read_relaxed(&ofproto->tables[i].miss_config, &f->miss_config); |
3c1bb396 BP |
3223 | f->max_entries = 1000000; |
3224 | ||
5dc7516b BP |
3225 | bool more_tables = false; |
3226 | for (int j = i + 1; j < ofproto->n_tables; j++) { | |
3227 | if (!(ofproto->tables[j].flags & OFTABLE_HIDDEN)) { | |
3228 | bitmap_set1(f->nonmiss.next, j); | |
3229 | more_tables = true; | |
3230 | } | |
3231 | } | |
3c1bb396 | 3232 | f->nonmiss.instructions = (1u << N_OVS_INSTRUCTIONS) - 1; |
5dc7516b | 3233 | if (!more_tables) { |
3c1bb396 BP |
3234 | f->nonmiss.instructions &= ~(1u << OVSINST_OFPIT11_GOTO_TABLE); |
3235 | } | |
3236 | f->nonmiss.write.ofpacts = (UINT64_C(1) << N_OFPACTS) - 1; | |
3237 | f->nonmiss.write.set_fields = rw_fields; | |
3238 | f->nonmiss.apply = f->nonmiss.write; | |
3239 | f->miss = f->nonmiss; | |
3240 | ||
3241 | f->match = match; | |
3242 | f->mask = mask; | |
3243 | f->wildcard = match; | |
3244 | } | |
3245 | ||
3246 | if (statsp) { | |
3247 | stats = *statsp = xcalloc(ofproto->n_tables, sizeof *stats); | |
3248 | for (i = 0; i < ofproto->n_tables; i++) { | |
3249 | struct ofputil_table_stats *s = &stats[i]; | |
3c1bb396 BP |
3250 | |
3251 | s->table_id = i; | |
d79e3d70 | 3252 | s->active_count = ofproto->tables[i].n_flows; |
3fbbcba7 BP |
3253 | if (i == 0) { |
3254 | s->active_count -= connmgr_count_hidden_rules( | |
3255 | ofproto->connmgr); | |
3256 | } | |
3c1bb396 BP |
3257 | } |
3258 | } else { | |
3259 | stats = NULL; | |
3260 | } | |
3261 | ||
3262 | ofproto->ofproto_class->query_tables(ofproto, features, stats); | |
3263 | ||
3264 | for (i = 0; i < ofproto->n_tables; i++) { | |
3265 | const struct oftable *table = &ofproto->tables[i]; | |
3266 | struct ofputil_table_features *f = &features[i]; | |
3267 | ||
3268 | if (table->name) { | |
3269 | ovs_strzcpy(f->name, table->name, sizeof f->name); | |
3270 | } | |
3271 | ||
3272 | if (table->max_flows < f->max_entries) { | |
3273 | f->max_entries = table->max_flows; | |
3274 | } | |
3275 | } | |
3276 | } | |
3277 | ||
3278 | static void | |
3279 | query_switch_features(struct ofproto *ofproto, | |
3280 | bool *arp_match_ip, uint64_t *ofpacts) | |
3281 | { | |
3282 | struct ofputil_table_features *features, *f; | |
3283 | ||
3284 | *arp_match_ip = false; | |
3285 | *ofpacts = 0; | |
3286 | ||
3287 | query_tables(ofproto, &features, NULL); | |
3288 | for (f = features; f < &features[ofproto->n_tables]; f++) { | |
3289 | *ofpacts |= f->nonmiss.apply.ofpacts | f->miss.apply.ofpacts; | |
3290 | if (bitmap_is_set(f->match.bm, MFF_ARP_SPA) || | |
3291 | bitmap_is_set(f->match.bm, MFF_ARP_TPA)) { | |
3292 | *arp_match_ip = true; | |
3293 | } | |
3294 | } | |
3295 | free(features); | |
3296 | ||
3297 | /* Sanity check. */ | |
3298 | ovs_assert(*ofpacts & (UINT64_C(1) << OFPACT_OUTPUT)); | |
3299 | } | |
3300 | ||
90bf1e07 | 3301 | static enum ofperr |
d1e2cf21 | 3302 | handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3303 | { |
64ff1d96 | 3304 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
9e1fd49b | 3305 | struct ofputil_switch_features features; |
064af421 | 3306 | struct ofport *port; |
6c1491fb | 3307 | bool arp_match_ip; |
9e1fd49b | 3308 | struct ofpbuf *b; |
064af421 | 3309 | |
3c1bb396 | 3310 | query_switch_features(ofproto, &arp_match_ip, &features.ofpacts); |
064af421 | 3311 | |
9e1fd49b | 3312 | features.datapath_id = ofproto->datapath_id; |
c184807c | 3313 | features.n_buffers = 0; |
adcf00ba | 3314 | features.n_tables = ofproto_get_n_visible_tables(ofproto); |
9e1fd49b | 3315 | features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS | |
4efe455d | 3316 | OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS | |
50b73fe1 | 3317 | OFPUTIL_C_GROUP_STATS | OFPUTIL_C_BUNDLES); |
6c1491fb | 3318 | if (arp_match_ip) { |
9e1fd49b | 3319 | features.capabilities |= OFPUTIL_C_ARP_MATCH_IP; |
6c1491fb | 3320 | } |
2e1ae200 JR |
3321 | /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */ |
3322 | features.auxiliary_id = 0; | |
9e1fd49b BP |
3323 | b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn), |
3324 | oh->xid); | |
64ff1d96 | 3325 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
9e1fd49b | 3326 | ofputil_put_switch_features_port(&port->pp, b); |
064af421 | 3327 | } |
064af421 | 3328 | |
9e1fd49b | 3329 | ofconn_send_reply(ofconn, b); |
064af421 BP |
3330 | return 0; |
3331 | } | |
064af421 | 3332 | |
90bf1e07 | 3333 | static enum ofperr |
d1e2cf21 | 3334 | handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3335 | { |
ad99e2ed BP |
3336 | struct ofputil_switch_config config; |
3337 | config.frag = ofconn_get_ofproto(ofconn)->frag_handling; | |
3338 | config.invalid_ttl_to_controller | |
3339 | = ofconn_get_invalid_ttl_to_controller(ofconn); | |
3340 | config.miss_send_len = ofconn_get_miss_send_len(ofconn); | |
3341 | ||
3342 | ofconn_send_reply(ofconn, ofputil_encode_get_config_reply(oh, &config)); | |
2d70a31a | 3343 | |
064af421 BP |
3344 | return 0; |
3345 | } | |
064af421 | 3346 | |
90bf1e07 | 3347 | static enum ofperr |
982697a4 | 3348 | handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3349 | { |
64ff1d96 | 3350 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
ad99e2ed BP |
3351 | struct ofputil_switch_config config; |
3352 | enum ofperr error; | |
3353 | ||
3354 | error = ofputil_decode_set_config(oh, &config); | |
3355 | if (error) { | |
3356 | return error; | |
3357 | } | |
2d70a31a | 3358 | |
7257b535 | 3359 | if (ofconn_get_type(ofconn) != OFCONN_PRIMARY |
f4f1ea7e | 3360 | || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) { |
ad99e2ed BP |
3361 | enum ofputil_frag_handling cur = ofproto->frag_handling; |
3362 | enum ofputil_frag_handling next = config.frag; | |
7257b535 | 3363 | |
7257b535 BP |
3364 | if (cur != next) { |
3365 | if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) { | |
3366 | ofproto->frag_handling = next; | |
3367 | } else { | |
3368 | VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s", | |
3369 | ofproto->name, | |
3370 | ofputil_frag_handling_to_string(next)); | |
3371 | } | |
064af421 BP |
3372 | } |
3373 | } | |
ebe482fd | 3374 | |
ad99e2ed BP |
3375 | if (config.invalid_ttl_to_controller >= 0) { |
3376 | ofconn_set_invalid_ttl_to_controller(ofconn, | |
3377 | config.invalid_ttl_to_controller); | |
3378 | } | |
3379 | ||
3380 | ofconn_set_miss_send_len(ofconn, config.miss_send_len); | |
0ad9b732 | 3381 | |
064af421 | 3382 | return 0; |
064af421 BP |
3383 | } |
3384 | ||
9deba63b | 3385 | /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow |
90bf1e07 BP |
3386 | * error message code for the caller to propagate upward. Otherwise, returns |
3387 | * 0. | |
3388 | * | |
3389 | * The log message mentions 'msg_type'. */ | |
3390 | static enum ofperr | |
3391 | reject_slave_controller(struct ofconn *ofconn) | |
9deba63b | 3392 | { |
1ce0a5fa | 3393 | if (ofconn_get_type(ofconn) == OFCONN_PRIMARY |
f4f1ea7e | 3394 | && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) { |
20e4ec1a | 3395 | return OFPERR_OFPBRC_IS_SLAVE; |
9deba63b BP |
3396 | } else { |
3397 | return 0; | |
3398 | } | |
3399 | } | |
3400 | ||
7e9f8266 BP |
3401 | /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate |
3402 | * for 'ofproto': | |
3403 | * | |
3404 | * - If they use a meter, then 'ofproto' has that meter configured. | |
076caa2f | 3405 | * Updates the meter action with ofproto's datapath's provider_meter_id. |
7e9f8266 BP |
3406 | * |
3407 | * - If they use any groups, then 'ofproto' has that group configured. | |
3408 | * | |
1f4a8933 JR |
3409 | * Returns 0 if successful, otherwise an OpenFlow error. Caller must hold |
3410 | * 'ofproto_mutex' for the result to be valid also after this function | |
3411 | * returns. */ | |
89108874 | 3412 | enum ofperr |
9cae45dc | 3413 | ofproto_check_ofpacts(struct ofproto *ofproto, |
7e9f8266 | 3414 | const struct ofpact ofpacts[], size_t ofpacts_len) |
1f4a8933 | 3415 | OVS_REQUIRES(ofproto_mutex) |
9cae45dc | 3416 | { |
076caa2f | 3417 | const struct ofpact *a; |
9cae45dc | 3418 | |
076caa2f JR |
3419 | OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) { |
3420 | if (a->type == OFPACT_METER && | |
3421 | !ofproto_fix_meter_action(ofproto, ofpact_get_METER(a))) { | |
3422 | return OFPERR_OFPMMFC_INVALID_METER; | |
3423 | } | |
9cae45dc | 3424 | |
076caa2f JR |
3425 | if (a->type == OFPACT_GROUP |
3426 | && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) { | |
7e9f8266 | 3427 | return OFPERR_OFPBAC_BAD_OUT_GROUP; |
84d68566 SH |
3428 | } |
3429 | } | |
3430 | ||
9cae45dc JR |
3431 | return 0; |
3432 | } | |
3433 | ||
1f4a8933 JR |
3434 | void |
3435 | ofproto_packet_out_uninit(struct ofproto_packet_out *opo) | |
3436 | { | |
3437 | dp_packet_delete(opo->packet); | |
3438 | opo->packet = NULL; | |
3439 | free(opo->flow); | |
3440 | opo->flow = NULL; | |
3441 | free(opo->ofpacts); | |
3442 | opo->ofpacts = NULL; | |
3443 | opo->ofpacts_len = 0; | |
3444 | ovs_assert(!opo->aux); | |
3445 | } | |
3446 | ||
3447 | /* Takes ownership of po->ofpacts, which must have been malloc'ed. */ | |
3448 | static enum ofperr | |
3449 | ofproto_packet_out_init(struct ofproto *ofproto, | |
3450 | struct ofconn *ofconn, | |
3451 | struct ofproto_packet_out *opo, | |
3452 | const struct ofputil_packet_out *po) | |
3453 | { | |
3454 | enum ofperr error; | |
67210a55 | 3455 | struct match match; |
1f4a8933 JR |
3456 | |
3457 | if (ofp_to_u16(po->in_port) >= ofproto->max_ports | |
3458 | && ofp_to_u16(po->in_port) < ofp_to_u16(OFPP_MAX)) { | |
3459 | return OFPERR_OFPBRC_BAD_PORT; | |
3460 | } | |
3461 | ||
3462 | /* Get payload. */ | |
3463 | if (po->buffer_id != UINT32_MAX) { | |
3464 | return OFPERR_OFPBRC_BUFFER_UNKNOWN; | |
3465 | } | |
3466 | ||
3467 | /* Ensure that the L3 header is 32-bit aligned. */ | |
3468 | opo->packet = dp_packet_clone_data_with_headroom(po->packet, | |
3469 | po->packet_len, 2); | |
3470 | /* Store struct flow. */ | |
3471 | opo->flow = xmalloc(sizeof *opo->flow); | |
3472 | flow_extract(opo->packet, opo->flow); | |
3473 | opo->flow->in_port.ofp_port = po->in_port; | |
3474 | ||
3475 | /* Check actions like for flow mods. We pass a 'table_id' of 0 to | |
3476 | * ofproto_check_consistency(), which isn't strictly correct because these | |
3477 | * actions aren't in any table. This is OK as 'table_id' is only used to | |
3478 | * check instructions (e.g., goto-table), which can't appear on the action | |
3479 | * list of a packet-out. */ | |
67210a55 JR |
3480 | match_wc_init(&match, opo->flow); |
3481 | error = ofpacts_check_consistency(po->ofpacts, po->ofpacts_len, &match, | |
1f4a8933 JR |
3482 | u16_to_ofp(ofproto->max_ports), 0, |
3483 | ofproto->n_tables, | |
3484 | ofconn_get_protocol(ofconn)); | |
3485 | if (error) { | |
3486 | dp_packet_delete(opo->packet); | |
3487 | free(opo->flow); | |
3488 | return error; | |
3489 | } | |
3490 | ||
3491 | opo->ofpacts = po->ofpacts; | |
3492 | opo->ofpacts_len = po->ofpacts_len; | |
3493 | ||
3494 | opo->aux = NULL; | |
3495 | return 0; | |
3496 | } | |
3497 | ||
3498 | static enum ofperr | |
3499 | ofproto_packet_out_start(struct ofproto *ofproto, | |
3500 | struct ofproto_packet_out *opo) | |
3501 | OVS_REQUIRES(ofproto_mutex) | |
3502 | { | |
3503 | enum ofperr error; | |
3504 | ||
3505 | error = ofproto_check_ofpacts(ofproto, opo->ofpacts, opo->ofpacts_len); | |
3506 | if (error) { | |
3507 | return error; | |
3508 | } | |
3509 | ||
3510 | return ofproto->ofproto_class->packet_xlate(ofproto, opo); | |
3511 | } | |
3512 | ||
6dd3c787 JR |
3513 | static void |
3514 | ofproto_packet_out_revert(struct ofproto *ofproto, | |
3515 | struct ofproto_packet_out *opo) | |
3516 | OVS_REQUIRES(ofproto_mutex) | |
3517 | { | |
3518 | ofproto->ofproto_class->packet_xlate_revert(ofproto, opo); | |
3519 | } | |
3520 | ||
1f4a8933 JR |
3521 | static void |
3522 | ofproto_packet_out_finish(struct ofproto *ofproto, | |
3523 | struct ofproto_packet_out *opo) | |
3524 | OVS_REQUIRES(ofproto_mutex) | |
3525 | { | |
3526 | ofproto->ofproto_class->packet_execute(ofproto, opo); | |
3527 | } | |
3528 | ||
90bf1e07 | 3529 | static enum ofperr |
982697a4 | 3530 | handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh) |
1f4a8933 | 3531 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 3532 | { |
64ff1d96 | 3533 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
c6a93eb7 | 3534 | struct ofputil_packet_out po; |
1f4a8933 | 3535 | struct ofproto_packet_out opo; |
f25d0cf3 BP |
3536 | uint64_t ofpacts_stub[1024 / 8]; |
3537 | struct ofpbuf ofpacts; | |
90bf1e07 | 3538 | enum ofperr error; |
064af421 | 3539 | |
ac51afaf BP |
3540 | COVERAGE_INC(ofproto_packet_out); |
3541 | ||
76589937 | 3542 | error = reject_slave_controller(ofconn); |
9deba63b | 3543 | if (error) { |
1f4a8933 | 3544 | return error; |
9deba63b BP |
3545 | } |
3546 | ||
c6a93eb7 | 3547 | /* Decode message. */ |
f25d0cf3 | 3548 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
982697a4 | 3549 | error = ofputil_decode_packet_out(&po, oh, &ofpacts); |
064af421 | 3550 | if (error) { |
1f4a8933 JR |
3551 | ofpbuf_uninit(&ofpacts); |
3552 | return error; | |
91858960 | 3553 | } |
064af421 | 3554 | |
1f4a8933 JR |
3555 | po.ofpacts = ofpbuf_steal_data(&ofpacts); /* Move to heap. */ |
3556 | error = ofproto_packet_out_init(p, ofconn, &opo, &po); | |
3557 | if (error) { | |
3558 | free(po.ofpacts); | |
3559 | return error; | |
e1154f71 | 3560 | } |
89108874 | 3561 | |
1f4a8933 JR |
3562 | ovs_mutex_lock(&ofproto_mutex); |
3563 | opo.version = p->tables_version; | |
3564 | error = ofproto_packet_out_start(p, &opo); | |
548de4dd | 3565 | if (!error) { |
1f4a8933 | 3566 | ofproto_packet_out_finish(p, &opo); |
548de4dd | 3567 | } |
1f4a8933 | 3568 | ovs_mutex_unlock(&ofproto_mutex); |
abe529af | 3569 | |
1f4a8933 | 3570 | ofproto_packet_out_uninit(&opo); |
abe529af | 3571 | return error; |
064af421 BP |
3572 | } |
3573 | ||
77ab5fd2 BP |
3574 | static enum ofperr |
3575 | handle_nxt_resume(struct ofconn *ofconn, const struct ofp_header *oh) | |
3576 | { | |
3577 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
3578 | struct ofputil_packet_in_private pin; | |
3579 | enum ofperr error; | |
3580 | ||
8d8ab6c2 | 3581 | error = ofputil_decode_packet_in_private(oh, false, |
3cddeff0 YHW |
3582 | ofproto_get_tun_tab(ofproto), |
3583 | &ofproto->vl_mff_map, &pin, NULL, | |
3584 | NULL); | |
77ab5fd2 BP |
3585 | if (error) { |
3586 | return error; | |
3587 | } | |
3588 | ||
3589 | error = (ofproto->ofproto_class->nxt_resume | |
3590 | ? ofproto->ofproto_class->nxt_resume(ofproto, &pin) | |
3591 | : OFPERR_NXR_NOT_SUPPORTED); | |
3592 | ||
3593 | ofputil_packet_in_private_destroy(&pin); | |
3594 | ||
3595 | return error; | |
3596 | } | |
3597 | ||
064af421 | 3598 | static void |
2a6f78e0 | 3599 | update_port_config(struct ofconn *ofconn, struct ofport *port, |
9e1fd49b BP |
3600 | enum ofputil_port_config config, |
3601 | enum ofputil_port_config mask) | |
064af421 | 3602 | { |
2a6f78e0 | 3603 | enum ofputil_port_config toggle = (config ^ port->pp.config) & mask; |
abe529af | 3604 | |
2a6f78e0 BP |
3605 | if (toggle & OFPUTIL_PC_PORT_DOWN |
3606 | && (config & OFPUTIL_PC_PORT_DOWN | |
3607 | ? netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL) | |
3608 | : netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL))) { | |
3609 | /* We tried to bring the port up or down, but it failed, so don't | |
3610 | * update the "down" bit. */ | |
9e1fd49b | 3611 | toggle &= ~OFPUTIL_PC_PORT_DOWN; |
064af421 | 3612 | } |
abe529af | 3613 | |
2a6f78e0 BP |
3614 | if (toggle) { |
3615 | enum ofputil_port_config old_config = port->pp.config; | |
3616 | port->pp.config ^= toggle; | |
abe529af | 3617 | port->ofproto->ofproto_class->port_reconfigured(port, old_config); |
2a6f78e0 BP |
3618 | connmgr_send_port_status(port->ofproto->connmgr, ofconn, &port->pp, |
3619 | OFPPR_MODIFY); | |
064af421 BP |
3620 | } |
3621 | } | |
3622 | ||
90bf1e07 | 3623 | static enum ofperr |
1c38055d JR |
3624 | port_mod_start(struct ofconn *ofconn, struct ofputil_port_mod *pm, |
3625 | struct ofport **port) | |
064af421 | 3626 | { |
64ff1d96 | 3627 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
1c38055d JR |
3628 | |
3629 | *port = ofproto_get_port(p, pm->port_no); | |
3630 | if (!*port) { | |
3631 | return OFPERR_OFPPMFC_BAD_PORT; | |
3632 | } | |
3633 | if (!eth_addr_equals((*port)->pp.hw_addr, pm->hw_addr)) { | |
3634 | return OFPERR_OFPPMFC_BAD_HW_ADDR; | |
3635 | } | |
3636 | return 0; | |
3637 | } | |
3638 | ||
3639 | static void | |
3640 | port_mod_finish(struct ofconn *ofconn, struct ofputil_port_mod *pm, | |
3641 | struct ofport *port) | |
3642 | { | |
3643 | update_port_config(ofconn, port, pm->config, pm->mask); | |
3644 | if (pm->advertise) { | |
3645 | netdev_set_advertisements(port->netdev, pm->advertise); | |
3646 | } | |
3647 | } | |
3648 | ||
3649 | static enum ofperr | |
3650 | handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
3651 | { | |
9e1fd49b | 3652 | struct ofputil_port_mod pm; |
064af421 | 3653 | struct ofport *port; |
9e1fd49b | 3654 | enum ofperr error; |
064af421 | 3655 | |
76589937 | 3656 | error = reject_slave_controller(ofconn); |
9deba63b BP |
3657 | if (error) { |
3658 | return error; | |
3659 | } | |
064af421 | 3660 | |
18cc69d9 | 3661 | error = ofputil_decode_port_mod(oh, &pm, false); |
9e1fd49b BP |
3662 | if (error) { |
3663 | return error; | |
3664 | } | |
3665 | ||
1c38055d JR |
3666 | error = port_mod_start(ofconn, &pm, &port); |
3667 | if (!error) { | |
3668 | port_mod_finish(ofconn, &pm, port); | |
064af421 | 3669 | } |
1c38055d | 3670 | return error; |
064af421 BP |
3671 | } |
3672 | ||
90bf1e07 | 3673 | static enum ofperr |
3269c562 | 3674 | handle_desc_stats_request(struct ofconn *ofconn, |
982697a4 | 3675 | const struct ofp_header *request) |
064af421 | 3676 | { |
061bfea4 BP |
3677 | static const char *default_mfr_desc = "Nicira, Inc."; |
3678 | static const char *default_hw_desc = "Open vSwitch"; | |
3679 | static const char *default_sw_desc = VERSION; | |
3680 | static const char *default_serial_desc = "None"; | |
3681 | static const char *default_dp_desc = "None"; | |
3682 | ||
64ff1d96 | 3683 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 BP |
3684 | struct ofp_desc_stats *ods; |
3685 | struct ofpbuf *msg; | |
3686 | ||
982697a4 BP |
3687 | msg = ofpraw_alloc_stats_reply(request, 0); |
3688 | ods = ofpbuf_put_zeros(msg, sizeof *ods); | |
061bfea4 BP |
3689 | ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc, |
3690 | sizeof ods->mfr_desc); | |
3691 | ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc, | |
3692 | sizeof ods->hw_desc); | |
3693 | ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc, | |
3694 | sizeof ods->sw_desc); | |
3695 | ovs_strlcpy(ods->serial_num, | |
3696 | p->serial_desc ? p->serial_desc : default_serial_desc, | |
3697 | sizeof ods->serial_num); | |
3698 | ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc, | |
3699 | sizeof ods->dp_desc); | |
b0421aa2 | 3700 | ofconn_send_reply(ofconn, msg); |
064af421 BP |
3701 | |
3702 | return 0; | |
3703 | } | |
3704 | ||
90bf1e07 | 3705 | static enum ofperr |
3269c562 | 3706 | handle_table_stats_request(struct ofconn *ofconn, |
982697a4 | 3707 | const struct ofp_header *request) |
064af421 | 3708 | { |
3c1bb396 BP |
3709 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
3710 | struct ofputil_table_features *features; | |
08d1e234 | 3711 | struct ofputil_table_stats *stats; |
3c1bb396 | 3712 | struct ofpbuf *reply; |
6c1491fb | 3713 | size_t i; |
064af421 | 3714 | |
3c1bb396 | 3715 | query_tables(ofproto, &features, &stats); |
254750ce | 3716 | |
3c1bb396 BP |
3717 | reply = ofputil_encode_table_stats_reply(request); |
3718 | for (i = 0; i < ofproto->n_tables; i++) { | |
3719 | if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) { | |
3720 | ofputil_append_table_stats_reply(reply, &stats[i], &features[i]); | |
254750ce BP |
3721 | } |
3722 | } | |
3c1bb396 | 3723 | ofconn_send_reply(ofconn, reply); |
254750ce | 3724 | |
3c1bb396 | 3725 | free(features); |
08d1e234 | 3726 | free(stats); |
307975da | 3727 | |
064af421 BP |
3728 | return 0; |
3729 | } | |
3730 | ||
3c4e10fb BP |
3731 | static enum ofperr |
3732 | handle_table_features_request(struct ofconn *ofconn, | |
3733 | const struct ofp_header *request) | |
3734 | { | |
3735 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
0a2869d5 BP |
3736 | struct ofpbuf msg = ofpbuf_const_initializer(request, |
3737 | ntohs(request->length)); | |
3c4e10fb | 3738 | ofpraw_pull_assert(&msg); |
6fd6ed71 | 3739 | if (msg.size || ofpmp_more(request)) { |
3c4e10fb BP |
3740 | return OFPERR_OFPTFFC_EPERM; |
3741 | } | |
3742 | ||
0a2869d5 | 3743 | struct ofputil_table_features *features; |
3c4e10fb BP |
3744 | query_tables(ofproto, &features, NULL); |
3745 | ||
0a2869d5 | 3746 | struct ovs_list replies; |
3c4e10fb | 3747 | ofpmp_init(&replies, request); |
0a2869d5 | 3748 | for (size_t i = 0; i < ofproto->n_tables; i++) { |
3c4e10fb BP |
3749 | if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) { |
3750 | ofputil_append_table_features_reply(&features[i], &replies); | |
3751 | } | |
3752 | } | |
3753 | ofconn_send_replies(ofconn, &replies); | |
3754 | ||
3755 | free(features); | |
3756 | ||
3757 | return 0; | |
3758 | } | |
3759 | ||
6c6eedc5 SJ |
3760 | /* Returns the vacancy of 'oftable', a number that ranges from 0 (if the table |
3761 | * is full) to 100 (if the table is empty). | |
3762 | * | |
3763 | * A table without a limit on flows is considered to be empty. */ | |
3764 | static uint8_t | |
3765 | oftable_vacancy(const struct oftable *t) | |
3766 | { | |
3767 | return (!t->max_flows ? 100 | |
3768 | : t->n_flows >= t->max_flows ? 0 | |
3769 | : (t->max_flows - t->n_flows) * 100.0 / t->max_flows); | |
3770 | } | |
3771 | ||
bab86012 SJ |
3772 | static void |
3773 | query_table_desc__(struct ofputil_table_desc *td, | |
3774 | struct ofproto *ofproto, uint8_t table_id) | |
3775 | { | |
6c6eedc5 | 3776 | const struct oftable *t = &ofproto->tables[table_id]; |
bab86012 SJ |
3777 | |
3778 | td->table_id = table_id; | |
6c6eedc5 | 3779 | td->eviction = (t->eviction & EVICTION_OPENFLOW |
bab86012 SJ |
3780 | ? OFPUTIL_TABLE_EVICTION_ON |
3781 | : OFPUTIL_TABLE_EVICTION_OFF); | |
3782 | td->eviction_flags = OFPROTO_EVICTION_FLAGS; | |
6c6eedc5 | 3783 | td->vacancy = (t->vacancy_event |
bab86012 SJ |
3784 | ? OFPUTIL_TABLE_VACANCY_ON |
3785 | : OFPUTIL_TABLE_VACANCY_OFF); | |
6c6eedc5 SJ |
3786 | td->table_vacancy.vacancy_down = t->vacancy_down; |
3787 | td->table_vacancy.vacancy_up = t->vacancy_up; | |
3788 | td->table_vacancy.vacancy = oftable_vacancy(t); | |
bab86012 SJ |
3789 | } |
3790 | ||
03c72922 BP |
3791 | /* This function queries the database for dumping table-desc. */ |
3792 | static void | |
3793 | query_tables_desc(struct ofproto *ofproto, struct ofputil_table_desc **descp) | |
3794 | { | |
3795 | struct ofputil_table_desc *table_desc; | |
3796 | size_t i; | |
3797 | ||
3798 | table_desc = *descp = xcalloc(ofproto->n_tables, sizeof *table_desc); | |
3799 | for (i = 0; i < ofproto->n_tables; i++) { | |
3800 | struct ofputil_table_desc *td = &table_desc[i]; | |
bab86012 | 3801 | query_table_desc__(td, ofproto, i); |
03c72922 BP |
3802 | } |
3803 | } | |
3804 | ||
3805 | /* Function to handle dump-table-desc request. */ | |
3806 | static enum ofperr | |
3807 | handle_table_desc_request(struct ofconn *ofconn, | |
3808 | const struct ofp_header *request) | |
3809 | { | |
3810 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
3811 | struct ofputil_table_desc *table_desc; | |
3812 | struct ovs_list replies; | |
3813 | size_t i; | |
3814 | ||
3815 | query_tables_desc(ofproto, &table_desc); | |
3816 | ofpmp_init(&replies, request); | |
3817 | for (i = 0; i < ofproto->n_tables; i++) { | |
3818 | if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) { | |
3819 | ofputil_append_table_desc_reply(&table_desc[i], &replies, | |
3820 | request->version); | |
3821 | } | |
3822 | } | |
3823 | ofconn_send_replies(ofconn, &replies); | |
3824 | free(table_desc); | |
3825 | return 0; | |
3826 | } | |
3827 | ||
6c6eedc5 SJ |
3828 | /* This function determines and sends the vacancy event, based on the value |
3829 | * of current vacancy and threshold vacancy. If the current vacancy is less | |
3830 | * than or equal to vacancy_down, vacancy up events must be enabled, and when | |
3831 | * the current vacancy is greater or equal to vacancy_up, vacancy down events | |
3832 | * must be enabled. */ | |
3833 | static void | |
3834 | send_table_status(struct ofproto *ofproto, uint8_t table_id) | |
3835 | { | |
3836 | struct oftable *t = &ofproto->tables[table_id]; | |
3837 | if (!t->vacancy_event) { | |
3838 | return; | |
3839 | } | |
3840 | ||
3841 | uint8_t vacancy = oftable_vacancy(t); | |
3842 | enum ofp14_table_reason event; | |
3843 | if (vacancy < t->vacancy_down) { | |
3844 | event = OFPTR_VACANCY_DOWN; | |
3845 | } else if (vacancy > t->vacancy_up) { | |
3846 | event = OFPTR_VACANCY_UP; | |
3847 | } else { | |
3848 | return; | |
3849 | } | |
3850 | ||
3851 | if (event == t->vacancy_event) { | |
3852 | struct ofputil_table_desc td; | |
3853 | query_table_desc__(&td, ofproto, table_id); | |
3854 | connmgr_send_table_status(ofproto->connmgr, &td, event); | |
3855 | ||
3856 | t->vacancy_event = (event == OFPTR_VACANCY_DOWN | |
3857 | ? OFPTR_VACANCY_UP | |
3858 | : OFPTR_VACANCY_DOWN); | |
3859 | } | |
3860 | } | |
3861 | ||
abaad8cf | 3862 | static void |
ca6ba700 | 3863 | append_port_stat(struct ofport *port, struct ovs_list *replies) |
abaad8cf | 3864 | { |
f8e4867e | 3865 | struct ofputil_port_stats ops = { .port_no = port->pp.port_no }; |
abaad8cf | 3866 | |
65e0be10 BP |
3867 | calc_duration(port->created, time_msec(), |
3868 | &ops.duration_sec, &ops.duration_nsec); | |
3869 | ||
d295e8e9 JP |
3870 | /* Intentionally ignore return value, since errors will set |
3871 | * 'stats' to all-1s, which is correct for OpenFlow, and | |
abaad8cf | 3872 | * netdev_get_stats() will log errors. */ |
f8e4867e SH |
3873 | ofproto_port_get_stats(port, &ops.stats); |
3874 | ||
3875 | ofputil_append_port_stat(replies, &ops); | |
abaad8cf JP |
3876 | } |
3877 | ||
70ae4f93 BP |
3878 | static void |
3879 | handle_port_request(struct ofconn *ofconn, | |
3880 | const struct ofp_header *request, ofp_port_t port_no, | |
ca6ba700 | 3881 | void (*cb)(struct ofport *, struct ovs_list *replies)) |
064af421 | 3882 | { |
70ae4f93 | 3883 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
064af421 | 3884 | struct ofport *port; |
ca6ba700 | 3885 | struct ovs_list replies; |
064af421 | 3886 | |
982697a4 | 3887 | ofpmp_init(&replies, request); |
7f05e7ab | 3888 | if (port_no != OFPP_ANY) { |
70ae4f93 | 3889 | port = ofproto_get_port(ofproto, port_no); |
abaad8cf | 3890 | if (port) { |
70ae4f93 | 3891 | cb(port, &replies); |
abaad8cf JP |
3892 | } |
3893 | } else { | |
70ae4f93 BP |
3894 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
3895 | cb(port, &replies); | |
abaad8cf | 3896 | } |
064af421 BP |
3897 | } |
3898 | ||
63f2140a | 3899 | ofconn_send_replies(ofconn, &replies); |
70ae4f93 BP |
3900 | } |
3901 | ||
3902 | static enum ofperr | |
3903 | handle_port_stats_request(struct ofconn *ofconn, | |
3904 | const struct ofp_header *request) | |
3905 | { | |
3906 | ofp_port_t port_no; | |
3907 | enum ofperr error; | |
3908 | ||
3909 | error = ofputil_decode_port_stats_request(request, &port_no); | |
3910 | if (!error) { | |
3911 | handle_port_request(ofconn, request, port_no, append_port_stat); | |
3912 | } | |
3913 | return error; | |
3914 | } | |
3915 | ||
3916 | static void | |
ca6ba700 | 3917 | append_port_desc(struct ofport *port, struct ovs_list *replies) |
70ae4f93 BP |
3918 | { |
3919 | ofputil_append_port_desc_stats_reply(&port->pp, replies); | |
064af421 BP |
3920 | } |
3921 | ||
2be393ed JP |
3922 | static enum ofperr |
3923 | handle_port_desc_stats_request(struct ofconn *ofconn, | |
982697a4 | 3924 | const struct ofp_header *request) |
2be393ed | 3925 | { |
70ae4f93 BP |
3926 | ofp_port_t port_no; |
3927 | enum ofperr error; | |
2be393ed | 3928 | |
70ae4f93 BP |
3929 | error = ofputil_decode_port_desc_stats_request(request, &port_no); |
3930 | if (!error) { | |
3931 | handle_port_request(ofconn, request, port_no, append_port_desc); | |
2be393ed | 3932 | } |
70ae4f93 | 3933 | return error; |
2be393ed JP |
3934 | } |
3935 | ||
98eaac36 JR |
3936 | static uint32_t |
3937 | hash_cookie(ovs_be64 cookie) | |
3938 | { | |
965607c8 | 3939 | return hash_uint64((OVS_FORCE uint64_t)cookie); |
98eaac36 JR |
3940 | } |
3941 | ||
3942 | static void | |
3943 | cookies_insert(struct ofproto *ofproto, struct rule *rule) | |
2c916028 | 3944 | OVS_REQUIRES(ofproto_mutex) |
98eaac36 JR |
3945 | { |
3946 | hindex_insert(&ofproto->cookies, &rule->cookie_node, | |
3947 | hash_cookie(rule->flow_cookie)); | |
3948 | } | |
3949 | ||
3950 | static void | |
3951 | cookies_remove(struct ofproto *ofproto, struct rule *rule) | |
2c916028 | 3952 | OVS_REQUIRES(ofproto_mutex) |
98eaac36 JR |
3953 | { |
3954 | hindex_remove(&ofproto->cookies, &rule->cookie_node); | |
3955 | } | |
3956 | ||
c6ebb8fb | 3957 | static void |
65e0be10 BP |
3958 | calc_duration(long long int start, long long int now, |
3959 | uint32_t *sec, uint32_t *nsec) | |
c6ebb8fb | 3960 | { |
f27f2134 | 3961 | long long int msecs = now - start; |
588cd7b5 BP |
3962 | *sec = msecs / 1000; |
3963 | *nsec = (msecs % 1000) * (1000 * 1000); | |
3964 | } | |
3965 | ||
48266274 | 3966 | /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns |
554764d1 SH |
3967 | * true if 'table_id' is OK, false otherwise. */ |
3968 | static bool | |
48266274 BP |
3969 | check_table_id(const struct ofproto *ofproto, uint8_t table_id) |
3970 | { | |
554764d1 | 3971 | return table_id == OFPTT_ALL || table_id < ofproto->n_tables; |
48266274 BP |
3972 | } |
3973 | ||
5c67e4af | 3974 | static struct oftable * |
d4ce8a49 | 3975 | next_visible_table(const struct ofproto *ofproto, uint8_t table_id) |
5c67e4af BP |
3976 | { |
3977 | struct oftable *table; | |
3978 | ||
3979 | for (table = &ofproto->tables[table_id]; | |
3980 | table < &ofproto->tables[ofproto->n_tables]; | |
3981 | table++) { | |
3982 | if (!(table->flags & OFTABLE_HIDDEN)) { | |
3983 | return table; | |
3984 | } | |
3985 | } | |
3986 | ||
3987 | return NULL; | |
3988 | } | |
3989 | ||
d0918789 | 3990 | static struct oftable * |
d4ce8a49 | 3991 | first_matching_table(const struct ofproto *ofproto, uint8_t table_id) |
064af421 | 3992 | { |
6c1491fb | 3993 | if (table_id == 0xff) { |
5c67e4af | 3994 | return next_visible_table(ofproto, 0); |
6c1491fb BP |
3995 | } else if (table_id < ofproto->n_tables) { |
3996 | return &ofproto->tables[table_id]; | |
a02e5331 | 3997 | } else { |
6c1491fb | 3998 | return NULL; |
a02e5331 | 3999 | } |
064af421 BP |
4000 | } |
4001 | ||
d0918789 | 4002 | static struct oftable * |
d4ce8a49 BP |
4003 | next_matching_table(const struct ofproto *ofproto, |
4004 | const struct oftable *table, uint8_t table_id) | |
6c1491fb | 4005 | { |
5c67e4af BP |
4006 | return (table_id == 0xff |
4007 | ? next_visible_table(ofproto, (table - ofproto->tables) + 1) | |
6c1491fb BP |
4008 | : NULL); |
4009 | } | |
4010 | ||
d0918789 | 4011 | /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO: |
6c1491fb BP |
4012 | * |
4013 | * - If TABLE_ID is 0xff, this iterates over every classifier table in | |
5c67e4af | 4014 | * OFPROTO, skipping tables marked OFTABLE_HIDDEN. |
6c1491fb BP |
4015 | * |
4016 | * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates | |
5c67e4af BP |
4017 | * only once, for that table. (This can be used to access tables marked |
4018 | * OFTABLE_HIDDEN.) | |
6c1491fb | 4019 | * |
48266274 BP |
4020 | * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be |
4021 | * entered at all. (Perhaps you should have validated TABLE_ID with | |
4022 | * check_table_id().) | |
6c1491fb BP |
4023 | * |
4024 | * All parameters are evaluated multiple times. | |
4025 | */ | |
d0918789 BP |
4026 | #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \ |
4027 | for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \ | |
4028 | (TABLE) != NULL; \ | |
4029 | (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID)) | |
6c1491fb | 4030 | |
a9b22b7f BP |
4031 | /* Initializes 'criteria' in a straightforward way based on the other |
4032 | * parameters. | |
4033 | * | |
dd51dae2 BP |
4034 | * By default, the criteria include flows that are read-only, on the assumption |
4035 | * that the collected flows won't be modified. Call rule_criteria_require_rw() | |
4036 | * if flows will be modified. | |
4037 | * | |
a9b22b7f BP |
4038 | * For "loose" matching, the 'priority' parameter is unimportant and may be |
4039 | * supplied as 0. */ | |
4040 | static void | |
4041 | rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id, | |
18721c4a | 4042 | const struct match *match, int priority, |
44e0c35d | 4043 | ovs_version_t version, ovs_be64 cookie, |
18721c4a JR |
4044 | ovs_be64 cookie_mask, ofp_port_t out_port, |
4045 | uint32_t out_group) | |
a9b22b7f BP |
4046 | { |
4047 | criteria->table_id = table_id; | |
bd53aa17 JR |
4048 | cls_rule_init(&criteria->cr, match, priority); |
4049 | criteria->version = version; | |
a9b22b7f BP |
4050 | criteria->cookie = cookie; |
4051 | criteria->cookie_mask = cookie_mask; | |
4052 | criteria->out_port = out_port; | |
4053 | criteria->out_group = out_group; | |
dd51dae2 BP |
4054 | |
4055 | /* We ordinarily want to skip hidden rules, but there has to be a way for | |
4056 | * code internal to OVS to modify and delete them, so if the criteria | |
4057 | * specify a priority that can only be for a hidden flow, then allow hidden | |
4058 | * rules to be selected. (This doesn't allow OpenFlow clients to meddle | |
4059 | * with hidden flows because OpenFlow uses only a 16-bit field to specify | |
4060 | * priority.) */ | |
4061 | criteria->include_hidden = priority > UINT16_MAX; | |
4062 | ||
4063 | /* We assume that the criteria are being used to collect flows for reading | |
4064 | * but not modification. Thus, we should collect read-only flows. */ | |
4065 | criteria->include_readonly = true; | |
4066 | } | |
4067 | ||
4068 | /* By default, criteria initialized by rule_criteria_init() will match flows | |
4069 | * that are read-only, on the assumption that the collected flows won't be | |
4070 | * modified. Call this function to match only flows that are be modifiable. | |
4071 | * | |
4072 | * Specify 'can_write_readonly' as false in ordinary circumstances, true if the | |
4073 | * caller has special privileges that allow it to modify even "read-only" | |
4074 | * flows. */ | |
4075 | static void | |
4076 | rule_criteria_require_rw(struct rule_criteria *criteria, | |
4077 | bool can_write_readonly) | |
4078 | { | |
4079 | criteria->include_readonly = can_write_readonly; | |
a9b22b7f BP |
4080 | } |
4081 | ||
4082 | static void | |
4083 | rule_criteria_destroy(struct rule_criteria *criteria) | |
4084 | { | |
4085 | cls_rule_destroy(&criteria->cr); | |
5bacd5cd | 4086 | criteria->version = OVS_VERSION_NOT_REMOVED; /* Mark as destroyed. */ |
a9b22b7f BP |
4087 | } |
4088 | ||
39c94593 JR |
4089 | /* Schedules postponed removal of rules, destroys 'rules'. */ |
4090 | static void | |
cc099268 | 4091 | remove_rules_postponed(struct rule_collection *rules) |
39c94593 JR |
4092 | OVS_REQUIRES(ofproto_mutex) |
4093 | { | |
fe59694b JR |
4094 | if (rule_collection_n(rules) > 0) { |
4095 | if (rule_collection_n(rules) == 1) { | |
4096 | ovsrcu_postpone(remove_rule_rcu, rule_collection_rules(rules)[0]); | |
4097 | rule_collection_init(rules); | |
39c94593 JR |
4098 | } else { |
4099 | ovsrcu_postpone(remove_rules_rcu, rule_collection_detach(rules)); | |
4100 | } | |
4101 | } | |
4102 | } | |
4103 | ||
5d08a275 JR |
4104 | /* Schedules postponed removal of groups, destroys 'groups'. */ |
4105 | static void | |
4106 | remove_groups_postponed(struct group_collection *groups) | |
4107 | OVS_REQUIRES(ofproto_mutex) | |
4108 | { | |
4109 | if (group_collection_n(groups) > 0) { | |
4110 | if (group_collection_n(groups) == 1) { | |
4111 | ovsrcu_postpone(remove_group_rcu, | |
4112 | group_collection_groups(groups)[0]); | |
4113 | group_collection_init(groups); | |
4114 | } else { | |
4115 | ovsrcu_postpone(remove_groups_rcu, | |
4116 | group_collection_detach(groups)); | |
4117 | } | |
4118 | } | |
4119 | } | |
4120 | ||
dd51dae2 BP |
4121 | /* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This |
4122 | * function verifies most of the criteria in 'c' itself, but the caller must | |
4123 | * check 'c->cr' itself. | |
4124 | * | |
2b7b1427 | 4125 | * Rules that have already been marked for removal are not collected. |
ce59413f | 4126 | * |
dd51dae2 | 4127 | * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and |
b20f4073 BP |
4128 | * 'c' only includes modifiable rules). */ |
4129 | static void | |
a9b22b7f | 4130 | collect_rule(struct rule *rule, const struct rule_criteria *c, |
dd51dae2 | 4131 | struct rule_collection *rules, size_t *n_readonly) |
15aaf599 | 4132 | OVS_REQUIRES(ofproto_mutex) |
6c1d7642 | 4133 | { |
dd51dae2 BP |
4134 | if ((c->table_id == rule->table_id || c->table_id == 0xff) |
4135 | && ofproto_rule_has_out_port(rule, c->out_port) | |
4136 | && ofproto_rule_has_out_group(rule, c->out_group) | |
4137 | && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask) | |
ce59413f | 4138 | && (!rule_is_hidden(rule) || c->include_hidden) |
bd53aa17 | 4139 | && cls_rule_visible_in_version(&rule->cr, c->version)) { |
dd51dae2 | 4140 | /* Rule matches all the criteria... */ |
834fe5cb | 4141 | if (!rule_is_readonly(rule) || c->include_readonly) { |
dd51dae2 | 4142 | /* ...add it. */ |
a8e547c1 | 4143 | rule_collection_add(rules, rule); |
dd51dae2 BP |
4144 | } else { |
4145 | /* ...except it's read-only. */ | |
4146 | ++*n_readonly; | |
6c1d7642 | 4147 | } |
6c1d7642 BP |
4148 | } |
4149 | } | |
4150 | ||
a9b22b7f BP |
4151 | /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches |
4152 | * on classifiers rules are done in the "loose" way required for OpenFlow | |
4153 | * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list | |
9ed18e46 BP |
4154 | * 'rules'. |
4155 | * | |
9ed18e46 | 4156 | * Returns 0 on success, otherwise an OpenFlow error code. */ |
90bf1e07 | 4157 | static enum ofperr |
a9b22b7f | 4158 | collect_rules_loose(struct ofproto *ofproto, |
a8e547c1 BP |
4159 | const struct rule_criteria *criteria, |
4160 | struct rule_collection *rules) | |
15aaf599 | 4161 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 4162 | { |
d0918789 | 4163 | struct oftable *table; |
554764d1 | 4164 | enum ofperr error = 0; |
dd51dae2 | 4165 | size_t n_readonly = 0; |
48266274 | 4166 | |
a8e547c1 BP |
4167 | rule_collection_init(rules); |
4168 | ||
554764d1 SH |
4169 | if (!check_table_id(ofproto, criteria->table_id)) { |
4170 | error = OFPERR_OFPBRC_BAD_TABLE_ID; | |
a8e547c1 | 4171 | goto exit; |
48266274 | 4172 | } |
9ed18e46 | 4173 | |
b8266395 | 4174 | if (criteria->cookie_mask == OVS_BE64_MAX) { |
98eaac36 JR |
4175 | struct rule *rule; |
4176 | ||
a9b22b7f BP |
4177 | HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, |
4178 | hash_cookie(criteria->cookie), | |
98eaac36 | 4179 | &ofproto->cookies) { |
a9b22b7f | 4180 | if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) { |
b20f4073 | 4181 | collect_rule(rule, criteria, rules, &n_readonly); |
98eaac36 JR |
4182 | } |
4183 | } | |
6c1d7642 | 4184 | } else { |
a9b22b7f | 4185 | FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) { |
6c1d7642 | 4186 | struct rule *rule; |
9ed18e46 | 4187 | |
bd53aa17 JR |
4188 | CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &criteria->cr, |
4189 | criteria->version) { | |
b20f4073 | 4190 | collect_rule(rule, criteria, rules, &n_readonly); |
9ed18e46 BP |
4191 | } |
4192 | } | |
4193 | } | |
48d28ac1 | 4194 | |
a8e547c1 | 4195 | exit: |
fe59694b | 4196 | if (!error && !rule_collection_n(rules) && n_readonly) { |
dd51dae2 BP |
4197 | /* We didn't find any rules to modify. We did find some read-only |
4198 | * rules that we're not allowed to modify, so report that. */ | |
4199 | error = OFPERR_OFPBRC_EPERM; | |
4200 | } | |
a8e547c1 BP |
4201 | if (error) { |
4202 | rule_collection_destroy(rules); | |
4203 | } | |
48d28ac1 | 4204 | return error; |
9ed18e46 BP |
4205 | } |
4206 | ||
a9b22b7f BP |
4207 | /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches |
4208 | * on classifiers rules are done in the "strict" way required for OpenFlow | |
4209 | * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected | |
4210 | * rules on list 'rules'. | |
9ed18e46 | 4211 | * |
9ed18e46 | 4212 | * Returns 0 on success, otherwise an OpenFlow error code. */ |
90bf1e07 | 4213 | static enum ofperr |
a9b22b7f | 4214 | collect_rules_strict(struct ofproto *ofproto, |
a8e547c1 BP |
4215 | const struct rule_criteria *criteria, |
4216 | struct rule_collection *rules) | |
15aaf599 | 4217 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 4218 | { |
d0918789 | 4219 | struct oftable *table; |
dd51dae2 | 4220 | size_t n_readonly = 0; |
d51c8b71 | 4221 | enum ofperr error = 0; |
48266274 | 4222 | |
a8e547c1 BP |
4223 | rule_collection_init(rules); |
4224 | ||
554764d1 SH |
4225 | if (!check_table_id(ofproto, criteria->table_id)) { |
4226 | error = OFPERR_OFPBRC_BAD_TABLE_ID; | |
a8e547c1 | 4227 | goto exit; |
48266274 | 4228 | } |
9ed18e46 | 4229 | |
b8266395 | 4230 | if (criteria->cookie_mask == OVS_BE64_MAX) { |
98eaac36 JR |
4231 | struct rule *rule; |
4232 | ||
a9b22b7f BP |
4233 | HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, |
4234 | hash_cookie(criteria->cookie), | |
98eaac36 | 4235 | &ofproto->cookies) { |
a9b22b7f | 4236 | if (cls_rule_equal(&rule->cr, &criteria->cr)) { |
b20f4073 | 4237 | collect_rule(rule, criteria, rules, &n_readonly); |
98eaac36 JR |
4238 | } |
4239 | } | |
6c1d7642 | 4240 | } else { |
a9b22b7f | 4241 | FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) { |
6c1d7642 | 4242 | struct rule *rule; |
9ed18e46 | 4243 | |
a9b22b7f | 4244 | rule = rule_from_cls_rule(classifier_find_rule_exactly( |
bd53aa17 JR |
4245 | &table->cls, &criteria->cr, |
4246 | criteria->version)); | |
6c1d7642 | 4247 | if (rule) { |
b20f4073 | 4248 | collect_rule(rule, criteria, rules, &n_readonly); |
9ed18e46 BP |
4249 | } |
4250 | } | |
4251 | } | |
48d28ac1 | 4252 | |
a8e547c1 | 4253 | exit: |
fe59694b | 4254 | if (!error && !rule_collection_n(rules) && n_readonly) { |
b20f4073 BP |
4255 | /* We didn't find any rules to modify. We did find some read-only |
4256 | * rules that we're not allowed to modify, so report that. */ | |
4257 | error = OFPERR_OFPBRC_EPERM; | |
4258 | } | |
a8e547c1 BP |
4259 | if (error) { |
4260 | rule_collection_destroy(rules); | |
4261 | } | |
6c1d7642 | 4262 | return error; |
9ed18e46 BP |
4263 | } |
4264 | ||
f27f2134 BP |
4265 | /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and |
4266 | * forced into the range of a uint16_t. */ | |
4267 | static int | |
4268 | age_secs(long long int age_ms) | |
4269 | { | |
4270 | return (age_ms < 0 ? 0 | |
4271 | : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX | |
4272 | : (unsigned int) age_ms / 1000); | |
4273 | } | |
4274 | ||
90bf1e07 | 4275 | static enum ofperr |
63f2140a | 4276 | handle_flow_stats_request(struct ofconn *ofconn, |
982697a4 | 4277 | const struct ofp_header *request) |
15aaf599 | 4278 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 4279 | { |
64ff1d96 | 4280 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 4281 | struct ofputil_flow_stats_request fsr; |
a9b22b7f | 4282 | struct rule_criteria criteria; |
a8e547c1 | 4283 | struct rule_collection rules; |
ca6ba700 | 4284 | struct ovs_list replies; |
90bf1e07 | 4285 | enum ofperr error; |
09246b99 | 4286 | |
8d8ab6c2 | 4287 | error = ofputil_decode_flow_stats_request(&fsr, request, |
3cddeff0 YHW |
4288 | ofproto_get_tun_tab(ofproto), |
4289 | &ofproto->vl_mff_map); | |
09246b99 BP |
4290 | if (error) { |
4291 | return error; | |
4292 | } | |
4293 | ||
44e0c35d | 4294 | rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, OVS_VERSION_MAX, |
2b7b1427 JR |
4295 | fsr.cookie, fsr.cookie_mask, fsr.out_port, |
4296 | fsr.out_group); | |
15aaf599 BP |
4297 | |
4298 | ovs_mutex_lock(&ofproto_mutex); | |
a9b22b7f BP |
4299 | error = collect_rules_loose(ofproto, &criteria, &rules); |
4300 | rule_criteria_destroy(&criteria); | |
15aaf599 BP |
4301 | if (!error) { |
4302 | rule_collection_ref(&rules); | |
4303 | } | |
4304 | ovs_mutex_unlock(&ofproto_mutex); | |
4305 | ||
9ed18e46 BP |
4306 | if (error) { |
4307 | return error; | |
4308 | } | |
5ecc9d81 | 4309 | |
982697a4 | 4310 | ofpmp_init(&replies, request); |
fe59694b JR |
4311 | struct rule *rule; |
4312 | RULE_COLLECTION_FOR_EACH (rule, &rules) { | |
f27f2134 | 4313 | long long int now = time_msec(); |
9ed18e46 | 4314 | struct ofputil_flow_stats fs; |
15aaf599 | 4315 | long long int created, used, modified; |
dc723c44 | 4316 | const struct rule_actions *actions; |
15aaf599 | 4317 | enum ofputil_flow_mod_flags flags; |
a3779dbc | 4318 | |
d10976b7 | 4319 | ovs_mutex_lock(&rule->mutex); |
15aaf599 | 4320 | fs.cookie = rule->flow_cookie; |
a3779dbc EJ |
4321 | fs.idle_timeout = rule->idle_timeout; |
4322 | fs.hard_timeout = rule->hard_timeout; | |
ca26eb44 | 4323 | fs.importance = rule->importance; |
15aaf599 | 4324 | created = rule->created; |
15aaf599 | 4325 | modified = rule->modified; |
06a0f3e2 | 4326 | actions = rule_get_actions(rule); |
15aaf599 | 4327 | flags = rule->flags; |
d10976b7 | 4328 | ovs_mutex_unlock(&rule->mutex); |
a3779dbc | 4329 | |
dc437090 JR |
4330 | ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count, |
4331 | &fs.byte_count, &used); | |
4332 | ||
15aaf599 BP |
4333 | minimatch_expand(&rule->cr.match, &fs.match); |
4334 | fs.table_id = rule->table_id; | |
4335 | calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec); | |
4336 | fs.priority = rule->cr.priority; | |
4337 | fs.idle_age = age_secs(now - used); | |
4338 | fs.hard_age = age_secs(now - modified); | |
15aaf599 BP |
4339 | fs.ofpacts = actions->ofpacts; |
4340 | fs.ofpacts_len = actions->ofpacts_len; | |
3f517bcd | 4341 | |
15aaf599 | 4342 | fs.flags = flags; |
8d8ab6c2 JG |
4343 | ofputil_append_flow_stats_reply(&fs, &replies, |
4344 | ofproto_get_tun_tab(ofproto)); | |
3c4486a5 | 4345 | } |
15aaf599 BP |
4346 | |
4347 | rule_collection_unref(&rules); | |
a8e547c1 BP |
4348 | rule_collection_destroy(&rules); |
4349 | ||
63f2140a | 4350 | ofconn_send_replies(ofconn, &replies); |
5ecc9d81 | 4351 | |
09246b99 BP |
4352 | return 0; |
4353 | } | |
4354 | ||
4f2cad2c | 4355 | static void |
8d8ab6c2 | 4356 | flow_stats_ds(struct ofproto *ofproto, struct rule *rule, struct ds *results) |
4f2cad2c | 4357 | { |
4f2cad2c | 4358 | uint64_t packet_count, byte_count; |
dc723c44 | 4359 | const struct rule_actions *actions; |
dc437090 | 4360 | long long int created, used; |
4f2cad2c | 4361 | |
dc437090 JR |
4362 | rule->ofproto->ofproto_class->rule_get_stats(rule, &packet_count, |
4363 | &byte_count, &used); | |
4f2cad2c | 4364 | |
15aaf599 | 4365 | ovs_mutex_lock(&rule->mutex); |
06a0f3e2 | 4366 | actions = rule_get_actions(rule); |
15aaf599 BP |
4367 | created = rule->created; |
4368 | ovs_mutex_unlock(&rule->mutex); | |
4369 | ||
6c1491fb BP |
4370 | if (rule->table_id != 0) { |
4371 | ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id); | |
4372 | } | |
15aaf599 | 4373 | ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000); |
4f2cad2c JP |
4374 | ds_put_format(results, "n_packets=%"PRIu64", ", packet_count); |
4375 | ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count); | |
8d8ab6c2 | 4376 | cls_rule_format(&rule->cr, ofproto_get_tun_tab(ofproto), results); |
a5df0e72 | 4377 | ds_put_char(results, ','); |
15aaf599 | 4378 | |
455ecd77 | 4379 | ds_put_cstr(results, "actions="); |
15aaf599 BP |
4380 | ofpacts_format(actions->ofpacts, actions->ofpacts_len, results); |
4381 | ||
4f2cad2c JP |
4382 | ds_put_cstr(results, "\n"); |
4383 | } | |
4384 | ||
d295e8e9 | 4385 | /* Adds a pretty-printed description of all flows to 'results', including |
ee8b231c | 4386 | * hidden flows (e.g., set up by in-band control). */ |
4f2cad2c JP |
4387 | void |
4388 | ofproto_get_all_flows(struct ofproto *p, struct ds *results) | |
4389 | { | |
d0918789 | 4390 | struct oftable *table; |
6c1491fb | 4391 | |
d0918789 | 4392 | OFPROTO_FOR_EACH_TABLE (table, p) { |
6c1491fb | 4393 | struct rule *rule; |
064af421 | 4394 | |
5f0476ce | 4395 | CLS_FOR_EACH (rule, cr, &table->cls) { |
8d8ab6c2 | 4396 | flow_stats_ds(p, rule, results); |
6c1491fb | 4397 | } |
064af421 | 4398 | } |
064af421 BP |
4399 | } |
4400 | ||
b5827b24 BP |
4401 | /* Obtains the NetFlow engine type and engine ID for 'ofproto' into |
4402 | * '*engine_type' and '*engine_id', respectively. */ | |
4403 | void | |
4404 | ofproto_get_netflow_ids(const struct ofproto *ofproto, | |
4405 | uint8_t *engine_type, uint8_t *engine_id) | |
4406 | { | |
abe529af | 4407 | ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id); |
b5827b24 BP |
4408 | } |
4409 | ||
8f5514fe AW |
4410 | /* Checks the status change of CFM on 'ofport'. |
4411 | * | |
4412 | * Returns true if 'ofproto_class' does not support 'cfm_status_changed'. */ | |
4413 | bool | |
4414 | ofproto_port_cfm_status_changed(struct ofproto *ofproto, ofp_port_t ofp_port) | |
4415 | { | |
4416 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
4417 | return (ofport && ofproto->ofproto_class->cfm_status_changed | |
4418 | ? ofproto->ofproto_class->cfm_status_changed(ofport) | |
4419 | : true); | |
4420 | } | |
4421 | ||
88bf179a AW |
4422 | /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. |
4423 | * Returns 0 if the port's CFM status was successfully stored into | |
4424 | * '*status'. Returns positive errno if the port did not have CFM | |
8f5514fe | 4425 | * configured. |
9a9e3786 | 4426 | * |
88bf179a AW |
4427 | * The caller must provide and own '*status', and must free 'status->rmps'. |
4428 | * '*status' is indeterminate if the return value is non-zero. */ | |
4429 | int | |
4e022ec0 | 4430 | ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port, |
685acfd9 | 4431 | struct cfm_status *status) |
3967a833 MM |
4432 | { |
4433 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
88bf179a AW |
4434 | return (ofport && ofproto->ofproto_class->get_cfm_status |
4435 | ? ofproto->ofproto_class->get_cfm_status(ofport, status) | |
4436 | : EOPNOTSUPP); | |
3967a833 MM |
4437 | } |
4438 | ||
90bf1e07 | 4439 | static enum ofperr |
76c93b22 | 4440 | handle_aggregate_stats_request(struct ofconn *ofconn, |
982697a4 | 4441 | const struct ofp_header *oh) |
15aaf599 | 4442 | OVS_EXCLUDED(ofproto_mutex) |
27d34fce | 4443 | { |
76c93b22 | 4444 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 4445 | struct ofputil_flow_stats_request request; |
76c93b22 | 4446 | struct ofputil_aggregate_stats stats; |
5e9d0469 | 4447 | bool unknown_packets, unknown_bytes; |
a9b22b7f | 4448 | struct rule_criteria criteria; |
a8e547c1 | 4449 | struct rule_collection rules; |
76c93b22 | 4450 | struct ofpbuf *reply; |
90bf1e07 | 4451 | enum ofperr error; |
27d34fce | 4452 | |
8d8ab6c2 | 4453 | error = ofputil_decode_flow_stats_request(&request, oh, |
3cddeff0 YHW |
4454 | ofproto_get_tun_tab(ofproto), |
4455 | &ofproto->vl_mff_map); | |
76c93b22 BP |
4456 | if (error) { |
4457 | return error; | |
4458 | } | |
5ecc9d81 | 4459 | |
a9b22b7f | 4460 | rule_criteria_init(&criteria, request.table_id, &request.match, 0, |
44e0c35d | 4461 | OVS_VERSION_MAX, request.cookie, request.cookie_mask, |
a9b22b7f | 4462 | request.out_port, request.out_group); |
15aaf599 BP |
4463 | |
4464 | ovs_mutex_lock(&ofproto_mutex); | |
a9b22b7f BP |
4465 | error = collect_rules_loose(ofproto, &criteria, &rules); |
4466 | rule_criteria_destroy(&criteria); | |
15aaf599 BP |
4467 | if (!error) { |
4468 | rule_collection_ref(&rules); | |
4469 | } | |
4470 | ovs_mutex_unlock(&ofproto_mutex); | |
4471 | ||
9ed18e46 BP |
4472 | if (error) { |
4473 | return error; | |
4474 | } | |
3c4486a5 | 4475 | |
9ed18e46 | 4476 | memset(&stats, 0, sizeof stats); |
5e9d0469 | 4477 | unknown_packets = unknown_bytes = false; |
fe59694b JR |
4478 | |
4479 | struct rule *rule; | |
4480 | RULE_COLLECTION_FOR_EACH (rule, &rules) { | |
9ed18e46 BP |
4481 | uint64_t packet_count; |
4482 | uint64_t byte_count; | |
dc437090 | 4483 | long long int used; |
5ecc9d81 | 4484 | |
9ed18e46 | 4485 | ofproto->ofproto_class->rule_get_stats(rule, &packet_count, |
dc437090 | 4486 | &byte_count, &used); |
5ecc9d81 | 4487 | |
5e9d0469 BP |
4488 | if (packet_count == UINT64_MAX) { |
4489 | unknown_packets = true; | |
4490 | } else { | |
4491 | stats.packet_count += packet_count; | |
4492 | } | |
4493 | ||
4494 | if (byte_count == UINT64_MAX) { | |
4495 | unknown_bytes = true; | |
4496 | } else { | |
4497 | stats.byte_count += byte_count; | |
4498 | } | |
4499 | ||
9ed18e46 | 4500 | stats.flow_count++; |
3c4486a5 | 4501 | } |
5e9d0469 BP |
4502 | if (unknown_packets) { |
4503 | stats.packet_count = UINT64_MAX; | |
4504 | } | |
4505 | if (unknown_bytes) { | |
4506 | stats.byte_count = UINT64_MAX; | |
4507 | } | |
27d34fce | 4508 | |
15aaf599 | 4509 | rule_collection_unref(&rules); |
a8e547c1 BP |
4510 | rule_collection_destroy(&rules); |
4511 | ||
982697a4 | 4512 | reply = ofputil_encode_aggregate_stats_reply(&stats, oh); |
76c93b22 | 4513 | ofconn_send_reply(ofconn, reply); |
09246b99 BP |
4514 | |
4515 | return 0; | |
4516 | } | |
4517 | ||
c1c9c9c4 | 4518 | struct queue_stats_cbdata { |
ca0f572c | 4519 | struct ofport *ofport; |
ca6ba700 | 4520 | struct ovs_list replies; |
6dc34a0d | 4521 | long long int now; |
c1c9c9c4 BP |
4522 | }; |
4523 | ||
4524 | static void | |
db9220c3 | 4525 | put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id, |
c1c9c9c4 BP |
4526 | const struct netdev_queue_stats *stats) |
4527 | { | |
6dc34a0d | 4528 | struct ofputil_queue_stats oqs; |
c1c9c9c4 | 4529 | |
6dc34a0d BP |
4530 | oqs.port_no = cbdata->ofport->pp.port_no; |
4531 | oqs.queue_id = queue_id; | |
4532 | oqs.tx_bytes = stats->tx_bytes; | |
4533 | oqs.tx_packets = stats->tx_packets; | |
4534 | oqs.tx_errors = stats->tx_errors; | |
4535 | if (stats->created != LLONG_MIN) { | |
4536 | calc_duration(stats->created, cbdata->now, | |
4537 | &oqs.duration_sec, &oqs.duration_nsec); | |
4538 | } else { | |
4539 | oqs.duration_sec = oqs.duration_nsec = UINT32_MAX; | |
4540 | } | |
64626975 | 4541 | ofputil_append_queue_stat(&cbdata->replies, &oqs); |
c1c9c9c4 BP |
4542 | } |
4543 | ||
4544 | static void | |
db9220c3 | 4545 | handle_queue_stats_dump_cb(uint32_t queue_id, |
c1c9c9c4 BP |
4546 | struct netdev_queue_stats *stats, |
4547 | void *cbdata_) | |
4548 | { | |
4549 | struct queue_stats_cbdata *cbdata = cbdata_; | |
4550 | ||
4551 | put_queue_stats(cbdata, queue_id, stats); | |
4552 | } | |
4553 | ||
0414d158 | 4554 | static enum ofperr |
ca0f572c | 4555 | handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id, |
c1c9c9c4 BP |
4556 | struct queue_stats_cbdata *cbdata) |
4557 | { | |
ca0f572c | 4558 | cbdata->ofport = port; |
c1c9c9c4 BP |
4559 | if (queue_id == OFPQ_ALL) { |
4560 | netdev_dump_queue_stats(port->netdev, | |
4561 | handle_queue_stats_dump_cb, cbdata); | |
4562 | } else { | |
4563 | struct netdev_queue_stats stats; | |
4564 | ||
1ac788f6 BP |
4565 | if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) { |
4566 | put_queue_stats(cbdata, queue_id, &stats); | |
0414d158 BP |
4567 | } else { |
4568 | return OFPERR_OFPQOFC_BAD_QUEUE; | |
1ac788f6 | 4569 | } |
c1c9c9c4 | 4570 | } |
0414d158 | 4571 | return 0; |
c1c9c9c4 BP |
4572 | } |
4573 | ||
90bf1e07 | 4574 | static enum ofperr |
63f2140a | 4575 | handle_queue_stats_request(struct ofconn *ofconn, |
982697a4 | 4576 | const struct ofp_header *rq) |
c1c9c9c4 | 4577 | { |
64ff1d96 | 4578 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
c1c9c9c4 | 4579 | struct queue_stats_cbdata cbdata; |
0414d158 | 4580 | struct ofport *port; |
0414d158 | 4581 | enum ofperr error; |
64626975 | 4582 | struct ofputil_queue_stats_request oqsr; |
c1c9c9c4 | 4583 | |
c1c9c9c4 BP |
4584 | COVERAGE_INC(ofproto_queue_req); |
4585 | ||
982697a4 | 4586 | ofpmp_init(&cbdata.replies, rq); |
6dc34a0d | 4587 | cbdata.now = time_msec(); |
c1c9c9c4 | 4588 | |
64626975 SH |
4589 | error = ofputil_decode_queue_stats_request(rq, &oqsr); |
4590 | if (error) { | |
4591 | return error; | |
4592 | } | |
4593 | ||
7f05e7ab | 4594 | if (oqsr.port_no == OFPP_ANY) { |
0414d158 | 4595 | error = OFPERR_OFPQOFC_BAD_QUEUE; |
4e8e4213 | 4596 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
64626975 | 4597 | if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) { |
0414d158 BP |
4598 | error = 0; |
4599 | } | |
c1c9c9c4 | 4600 | } |
0414d158 | 4601 | } else { |
64626975 | 4602 | port = ofproto_get_port(ofproto, oqsr.port_no); |
0414d158 | 4603 | error = (port |
64626975 | 4604 | ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata) |
0414d158 BP |
4605 | : OFPERR_OFPQOFC_BAD_PORT); |
4606 | } | |
4607 | if (!error) { | |
4608 | ofconn_send_replies(ofconn, &cbdata.replies); | |
c1c9c9c4 | 4609 | } else { |
63f2140a | 4610 | ofpbuf_list_delete(&cbdata.replies); |
c1c9c9c4 | 4611 | } |
c1c9c9c4 | 4612 | |
0414d158 | 4613 | return error; |
c1c9c9c4 | 4614 | } |
7ee20df1 | 4615 | |
2e31a9b8 | 4616 | static enum ofperr |
6787a49f | 4617 | evict_rules_from_table(struct oftable *table) |
15aaf599 | 4618 | OVS_REQUIRES(ofproto_mutex) |
2e31a9b8 | 4619 | { |
802f84ff JR |
4620 | enum ofperr error = 0; |
4621 | struct rule_collection rules; | |
6787a49f | 4622 | unsigned int count = table->n_flows; |
802f84ff JR |
4623 | unsigned int max_flows = table->max_flows; |
4624 | ||
4625 | rule_collection_init(&rules); | |
4626 | ||
4627 | while (count-- > max_flows) { | |
2e31a9b8 | 4628 | struct rule *rule; |
8037acb4 | 4629 | |
2e31a9b8 | 4630 | if (!choose_rule_to_evict(table, &rule)) { |
802f84ff JR |
4631 | error = OFPERR_OFPFMFC_TABLE_FULL; |
4632 | break; | |
2e31a9b8 | 4633 | } else { |
802f84ff JR |
4634 | eviction_group_remove_rule(rule); |
4635 | rule_collection_add(&rules, rule); | |
2e31a9b8 | 4636 | } |
8037acb4 | 4637 | } |
802f84ff | 4638 | delete_flows__(&rules, OFPRR_EVICTION, NULL); |
2e31a9b8 | 4639 | |
802f84ff | 4640 | return error; |
8037acb4 BP |
4641 | } |
4642 | ||
18080541 BP |
4643 | static void |
4644 | get_conjunctions(const struct ofputil_flow_mod *fm, | |
4645 | struct cls_conjunction **conjsp, size_t *n_conjsp) | |
18080541 BP |
4646 | { |
4647 | struct cls_conjunction *conjs = NULL; | |
4648 | int n_conjs = 0; | |
4649 | ||
f08e39dd BP |
4650 | const struct ofpact *ofpact; |
4651 | OFPACT_FOR_EACH (ofpact, fm->ofpacts, fm->ofpacts_len) { | |
4652 | if (ofpact->type == OFPACT_CONJUNCTION) { | |
18080541 | 4653 | n_conjs++; |
f08e39dd BP |
4654 | } else if (ofpact->type != OFPACT_NOTE) { |
4655 | /* "conjunction" may appear with "note" actions but not with any | |
4656 | * other type of actions. */ | |
4657 | ovs_assert(!n_conjs); | |
4658 | break; | |
18080541 | 4659 | } |
f08e39dd BP |
4660 | } |
4661 | if (n_conjs) { | |
4662 | int i = 0; | |
18080541 BP |
4663 | |
4664 | conjs = xzalloc(n_conjs * sizeof *conjs); | |
18080541 | 4665 | OFPACT_FOR_EACH (ofpact, fm->ofpacts, fm->ofpacts_len) { |
f08e39dd BP |
4666 | if (ofpact->type == OFPACT_CONJUNCTION) { |
4667 | struct ofpact_conjunction *oc = ofpact_get_CONJUNCTION(ofpact); | |
4668 | conjs[i].clause = oc->clause; | |
4669 | conjs[i].n_clauses = oc->n_clauses; | |
4670 | conjs[i].id = oc->id; | |
4671 | i++; | |
4672 | } | |
18080541 BP |
4673 | } |
4674 | } | |
4675 | ||
4676 | *conjsp = conjs; | |
4677 | *n_conjsp = n_conjs; | |
4678 | } | |
4679 | ||
5bacd5cd JR |
4680 | /* add_flow_init(), add_flow_start(), add_flow_revert(), and add_flow_finish() |
4681 | * implement OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT | |
1f42be1c JR |
4682 | * in which no matching flow already exists in the flow table. |
4683 | * | |
5bacd5cd JR |
4684 | * add_flow_init() creates a new flow according to 'fm' and stores it to 'ofm' |
4685 | * for later reference. If the flow replaces other flow, it will be updated to | |
4686 | * match modify semantics later by add_flow_start() (by calling | |
4687 | * replace_rule_start()). | |
1f42be1c | 4688 | * |
5bacd5cd | 4689 | * Returns 0 on success, or an OpenFlow error code on failure. |
1f42be1c | 4690 | * |
5bacd5cd JR |
4691 | * On successful return the caller must complete the operation by calling |
4692 | * add_flow_start(), and if that succeeds, then either add_flow_finish(), or | |
4693 | * add_flow_revert() if the operation needs to be reverted due to a later | |
4694 | * failure. | |
4695 | */ | |
90bf1e07 | 4696 | static enum ofperr |
5bacd5cd JR |
4697 | add_flow_init(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
4698 | const struct ofputil_flow_mod *fm) | |
4699 | OVS_EXCLUDED(ofproto_mutex) | |
064af421 | 4700 | { |
d0918789 | 4701 | struct oftable *table; |
8037acb4 | 4702 | struct cls_rule cr; |
5a361239 | 4703 | uint8_t table_id; |
39c94593 | 4704 | enum ofperr error; |
064af421 | 4705 | |
554764d1 | 4706 | if (!check_table_id(ofproto, fm->table_id)) { |
5bacd5cd | 4707 | return OFPERR_OFPBRC_BAD_TABLE_ID; |
48266274 BP |
4708 | } |
4709 | ||
7ee20df1 BP |
4710 | /* Pick table. */ |
4711 | if (fm->table_id == 0xff) { | |
13521ff5 | 4712 | if (ofproto->ofproto_class->rule_choose_table) { |
81a76618 BP |
4713 | error = ofproto->ofproto_class->rule_choose_table(ofproto, |
4714 | &fm->match, | |
7ee20df1 BP |
4715 | &table_id); |
4716 | if (error) { | |
4717 | return error; | |
4718 | } | |
cb22974d | 4719 | ovs_assert(table_id < ofproto->n_tables); |
7ee20df1 | 4720 | } else { |
5a361239 | 4721 | table_id = 0; |
7ee20df1 BP |
4722 | } |
4723 | } else if (fm->table_id < ofproto->n_tables) { | |
5a361239 | 4724 | table_id = fm->table_id; |
7ee20df1 | 4725 | } else { |
c22c56bd | 4726 | return OFPERR_OFPBRC_BAD_TABLE_ID; |
064af421 BP |
4727 | } |
4728 | ||
5a361239 | 4729 | table = &ofproto->tables[table_id]; |
834fe5cb BP |
4730 | if (table->flags & OFTABLE_READONLY |
4731 | && !(fm->flags & OFPUTIL_FF_NO_READONLY)) { | |
5c67e4af BP |
4732 | return OFPERR_OFPBRC_EPERM; |
4733 | } | |
4734 | ||
c84d8691 JR |
4735 | if (!(fm->flags & OFPUTIL_FF_HIDDEN_FIELDS) |
4736 | && !match_has_default_hidden_fields(&fm->match)) { | |
4737 | VLOG_WARN_RL(&rl, "%s: (add_flow) only internal flows can set " | |
4738 | "non-default values to hidden fields", ofproto->name); | |
4739 | return OFPERR_OFPBRC_EPERM; | |
adcf00ba AZ |
4740 | } |
4741 | ||
2c7ee524 JR |
4742 | if (!ofm->temp_rule) { |
4743 | cls_rule_init(&cr, &fm->match, fm->priority); | |
8037acb4 | 4744 | |
2c7ee524 JR |
4745 | /* Allocate new rule. Destroys 'cr'. */ |
4746 | error = ofproto_rule_create(ofproto, &cr, table - ofproto->tables, | |
4747 | fm->new_cookie, fm->idle_timeout, | |
4748 | fm->hard_timeout, fm->flags, | |
4749 | fm->importance, fm->ofpacts, | |
5c7c16d8 YHW |
4750 | fm->ofpacts_len, |
4751 | fm->match.flow.tunnel.metadata.present.map, | |
4752 | fm->ofpacts_tlv_bitmap, &ofm->temp_rule); | |
2c7ee524 JR |
4753 | if (error) { |
4754 | return error; | |
4755 | } | |
5bacd5cd | 4756 | |
2c7ee524 JR |
4757 | get_conjunctions(fm, &ofm->conjs, &ofm->n_conjs); |
4758 | } | |
5bacd5cd JR |
4759 | return 0; |
4760 | } | |
4761 | ||
2c7ee524 | 4762 | /* ofm->temp_rule is consumed only in the successful case. */ |
5bacd5cd JR |
4763 | static enum ofperr |
4764 | add_flow_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) | |
4765 | OVS_REQUIRES(ofproto_mutex) | |
4766 | { | |
4767 | struct rule *old_rule = NULL; | |
4768 | struct rule *new_rule = ofm->temp_rule; | |
4769 | const struct rule_actions *actions = rule_get_actions(new_rule); | |
4770 | struct oftable *table = &ofproto->tables[new_rule->table_id]; | |
4771 | enum ofperr error; | |
4772 | ||
4773 | /* Must check actions while holding ofproto_mutex to avoid a race. */ | |
4774 | error = ofproto_check_ofpacts(ofproto, actions->ofpacts, | |
4775 | actions->ofpacts_len); | |
4776 | if (error) { | |
4777 | return error; | |
4778 | } | |
4779 | ||
1f42be1c | 4780 | /* Check for the existence of an identical rule. |
2b7b1427 | 4781 | * This will not return rules earlier marked for removal. */ |
5bacd5cd JR |
4782 | old_rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, |
4783 | &new_rule->cr, | |
4784 | ofm->version)); | |
4785 | if (!old_rule) { | |
c84d8691 | 4786 | /* Check for overlap, if requested. */ |
5bacd5cd JR |
4787 | if (new_rule->flags & OFPUTIL_FF_CHECK_OVERLAP |
4788 | && classifier_rule_overlaps(&table->cls, &new_rule->cr, | |
4789 | ofm->version)) { | |
c84d8691 | 4790 | return OFPERR_OFPFMFC_OVERLAP; |
dd27be82 | 4791 | } |
b277c7cc | 4792 | |
c84d8691 | 4793 | /* If necessary, evict an existing rule to clear out space. */ |
6787a49f | 4794 | if (table->n_flows >= table->max_flows) { |
5bacd5cd JR |
4795 | if (!choose_rule_to_evict(table, &old_rule)) { |
4796 | return OFPERR_OFPFMFC_TABLE_FULL; | |
6787a49f | 4797 | } |
5bacd5cd JR |
4798 | eviction_group_remove_rule(old_rule); |
4799 | /* Marks 'old_rule' as an evicted rule rather than replaced rule. | |
6787a49f | 4800 | */ |
5bacd5cd | 4801 | old_rule->removed_reason = OFPRR_EVICTION; |
c09f755d | 4802 | } |
39c94593 | 4803 | } else { |
7338102b | 4804 | ofm->modify_cookie = true; |
39c94593 | 4805 | } |
81a76618 | 4806 | |
5bacd5cd JR |
4807 | if (old_rule) { |
4808 | rule_collection_add(&ofm->old_rules, old_rule); | |
064af421 | 4809 | } |
5bacd5cd | 4810 | /* Take ownership of the temp_rule. */ |
58026109 | 4811 | rule_collection_add(&ofm->new_rules, new_rule); |
5bacd5cd | 4812 | ofm->temp_rule = NULL; |
8037acb4 | 4813 | |
5bacd5cd | 4814 | replace_rule_start(ofproto, ofm, old_rule, new_rule); |
c84d8691 JR |
4815 | return 0; |
4816 | } | |
1ebeaaa7 | 4817 | |
6787a49f | 4818 | /* Revert the effects of add_flow_start(). */ |
1f42be1c | 4819 | static void |
8be00367 | 4820 | add_flow_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
1f42be1c JR |
4821 | OVS_REQUIRES(ofproto_mutex) |
4822 | { | |
5bacd5cd | 4823 | struct rule *old_rule = rule_collection_n(&ofm->old_rules) |
58026109 JR |
4824 | ? rule_collection_rules(&ofm->old_rules)[0] : NULL; |
4825 | struct rule *new_rule = rule_collection_rules(&ofm->new_rules)[0]; | |
8be00367 | 4826 | |
39c94593 | 4827 | replace_rule_revert(ofproto, old_rule, new_rule); |
1f42be1c JR |
4828 | } |
4829 | ||
39c94593 | 4830 | /* To be called after version bump. */ |
c84d8691 | 4831 | static void |
8be00367 | 4832 | add_flow_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 4833 | const struct openflow_mod_requester *req) |
c84d8691 JR |
4834 | OVS_REQUIRES(ofproto_mutex) |
4835 | { | |
5bacd5cd | 4836 | struct rule *old_rule = rule_collection_n(&ofm->old_rules) |
58026109 JR |
4837 | ? rule_collection_rules(&ofm->old_rules)[0] : NULL; |
4838 | struct rule *new_rule = rule_collection_rules(&ofm->new_rules)[0]; | |
39c94593 | 4839 | struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies); |
8037acb4 | 4840 | |
81dee635 | 4841 | replace_rule_finish(ofproto, ofm, req, old_rule, new_rule, &dead_cookies); |
39c94593 | 4842 | learned_cookies_flush(ofproto, &dead_cookies); |
802f84ff | 4843 | |
39c94593 JR |
4844 | if (old_rule) { |
4845 | ovsrcu_postpone(remove_rule_rcu, old_rule); | |
4846 | } else { | |
39c94593 | 4847 | ofmonitor_report(ofproto->connmgr, new_rule, NXFME_ADDED, 0, |
c84d8691 JR |
4848 | req ? req->ofconn : NULL, |
4849 | req ? req->request->xid : 0, NULL); | |
6c6eedc5 SJ |
4850 | |
4851 | /* Send Vacancy Events for OF1.4+. */ | |
4852 | send_table_status(ofproto, new_rule->table_id); | |
c84d8691 | 4853 | } |
c84d8691 | 4854 | } |
79eee1eb BP |
4855 | \f |
4856 | /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */ | |
064af421 | 4857 | |
5bacd5cd | 4858 | /* Create a new rule. Note that the rule is NOT inserted into a any data |
2c7ee524 JR |
4859 | * structures yet. Takes ownership of 'cr'. Only assigns '*new_rule' if |
4860 | * successful. */ | |
90bf1e07 | 4861 | static enum ofperr |
5bacd5cd JR |
4862 | ofproto_rule_create(struct ofproto *ofproto, struct cls_rule *cr, |
4863 | uint8_t table_id, ovs_be64 new_cookie, | |
4864 | uint16_t idle_timeout, uint16_t hard_timeout, | |
4865 | enum ofputil_flow_mod_flags flags, uint16_t importance, | |
4866 | const struct ofpact *ofpacts, size_t ofpacts_len, | |
5c7c16d8 | 4867 | uint64_t match_tlv_bitmap, uint64_t ofpacts_tlv_bitmap, |
5bacd5cd JR |
4868 | struct rule **new_rule) |
4869 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
79eee1eb | 4870 | { |
39c94593 | 4871 | struct rule *rule; |
dd27be82 | 4872 | enum ofperr error; |
79eee1eb | 4873 | |
39c94593 JR |
4874 | /* Allocate new rule. */ |
4875 | rule = ofproto->ofproto_class->rule_alloc(); | |
4876 | if (!rule) { | |
4877 | cls_rule_destroy(cr); | |
4878 | VLOG_WARN_RL(&rl, "%s: failed to allocate a rule.", ofproto->name); | |
4879 | return OFPERR_OFPFMFC_UNKNOWN; | |
af822017 BP |
4880 | } |
4881 | ||
39c94593 JR |
4882 | /* Initialize base state. */ |
4883 | *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto; | |
4884 | cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), cr); | |
4885 | ovs_refcount_init(&rule->ref_count); | |
834fe5cb | 4886 | |
39c94593 JR |
4887 | ovs_mutex_init(&rule->mutex); |
4888 | ovs_mutex_lock(&rule->mutex); | |
82a3160e | 4889 | *CONST_CAST(ovs_be64 *, &rule->flow_cookie) = new_cookie; |
5bacd5cd JR |
4890 | rule->created = rule->modified = time_msec(); |
4891 | rule->idle_timeout = idle_timeout; | |
4892 | rule->hard_timeout = hard_timeout; | |
4893 | *CONST_CAST(uint16_t *, &rule->importance) = importance; | |
f695ebfa | 4894 | rule->removed_reason = OVS_OFPRR_NONE; |
834fe5cb | 4895 | |
39c94593 | 4896 | *CONST_CAST(uint8_t *, &rule->table_id) = table_id; |
5bacd5cd JR |
4897 | rule->flags = flags & OFPUTIL_FF_STATE; |
4898 | ||
39c94593 | 4899 | *CONST_CAST(const struct rule_actions **, &rule->actions) |
5bacd5cd JR |
4900 | = rule_actions_create(ofpacts, ofpacts_len); |
4901 | ||
417e7e66 | 4902 | ovs_list_init(&rule->meter_list_node); |
39c94593 | 4903 | rule->eviction_group = NULL; |
39c94593 JR |
4904 | rule->monitor_flags = 0; |
4905 | rule->add_seqno = 0; | |
4906 | rule->modify_seqno = 0; | |
5bacd5cd | 4907 | ovs_list_init(&rule->expirable); |
dd27be82 JR |
4908 | ovs_mutex_unlock(&rule->mutex); |
4909 | ||
39c94593 JR |
4910 | /* Construct rule, initializing derived state. */ |
4911 | error = ofproto->ofproto_class->rule_construct(rule); | |
4912 | if (error) { | |
4913 | ofproto_rule_destroy__(rule); | |
4914 | return error; | |
dd27be82 | 4915 | } |
b277c7cc | 4916 | |
30d0452c | 4917 | rule->state = RULE_INITIALIZED; |
5c7c16d8 YHW |
4918 | rule->match_tlv_bitmap = match_tlv_bitmap; |
4919 | rule->ofpacts_tlv_bitmap = ofpacts_tlv_bitmap; | |
4920 | mf_vl_mff_ref(&rule->ofproto->vl_mff_map, match_tlv_bitmap); | |
4921 | mf_vl_mff_ref(&rule->ofproto->vl_mff_map, ofpacts_tlv_bitmap); | |
dd27be82 | 4922 | |
39c94593 JR |
4923 | *new_rule = rule; |
4924 | return 0; | |
4925 | } | |
884e1dc4 | 4926 | |
2c7ee524 JR |
4927 | /* Initialize 'ofm' for a learn action. If the rule already existed, reference |
4928 | * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the | |
4929 | * rule reference in both. This does not take the global 'ofproto_mutex'. */ | |
4930 | enum ofperr | |
4931 | ofproto_flow_mod_init_for_learn(struct ofproto *ofproto, | |
4932 | const struct ofputil_flow_mod *fm, | |
4933 | struct ofproto_flow_mod *ofm) | |
4934 | OVS_EXCLUDED(ofproto_mutex) | |
4935 | { | |
4936 | enum ofperr error; | |
4937 | ||
4938 | /* Reject flow mods that do not look like they were generated by a learn | |
4939 | * action. */ | |
4940 | if (fm->command != OFPFC_MODIFY_STRICT || fm->table_id == OFPTT_ALL | |
4941 | || fm->flags & OFPUTIL_FF_RESET_COUNTS | |
4942 | || fm->buffer_id != UINT32_MAX) { | |
4943 | return OFPERR_OFPFMFC_UNKNOWN; | |
4944 | } | |
4945 | ||
4946 | /* Check if the rule already exists, and we can get a reference to it. */ | |
4947 | struct oftable *table = &ofproto->tables[fm->table_id]; | |
4948 | struct rule *rule; | |
4949 | ||
4950 | rule = rule_from_cls_rule(classifier_find_match_exactly( | |
4951 | &table->cls, &fm->match, fm->priority, | |
4952 | OVS_VERSION_MAX)); | |
4953 | if (rule) { | |
4954 | /* Check if the rule's attributes match as well. */ | |
4955 | const struct rule_actions *actions; | |
4956 | ||
4957 | ovs_mutex_lock(&rule->mutex); | |
4958 | actions = rule_get_actions(rule); | |
4959 | if (rule->idle_timeout == fm->idle_timeout | |
4960 | && rule->hard_timeout == fm->hard_timeout | |
4961 | && rule->importance == fm->importance | |
4962 | && rule->flags == (fm->flags & OFPUTIL_FF_STATE) | |
4963 | && (!fm->modify_cookie || (fm->new_cookie == rule->flow_cookie)) | |
4964 | && ofpacts_equal(fm->ofpacts, fm->ofpacts_len, | |
4965 | actions->ofpacts, actions->ofpacts_len)) { | |
4966 | /* Rule already exists and need not change, except for the modified | |
4967 | * timestamp. Get a reference to the existing rule. */ | |
4968 | ovs_mutex_unlock(&rule->mutex); | |
4969 | if (!ofproto_rule_try_ref(rule)) { | |
4970 | rule = NULL; /* Pretend it did not exist. */ | |
4971 | } | |
4972 | } else { | |
4973 | ovs_mutex_unlock(&rule->mutex); | |
4974 | rule = NULL; | |
4975 | } | |
4976 | } | |
4977 | ||
4978 | /* Initialize ofproto_flow_mod for future use. */ | |
4979 | error = ofproto_flow_mod_init(ofproto, ofm, fm, rule); | |
4980 | if (error) { | |
4981 | ofproto_rule_unref(rule); | |
4982 | return error; | |
4983 | } | |
4984 | return 0; | |
4985 | } | |
4986 | ||
2c7ee524 | 4987 | enum ofperr |
1f4a8933 | 4988 | ofproto_flow_mod_learn_refresh(struct ofproto_flow_mod *ofm) |
2c7ee524 JR |
4989 | { |
4990 | enum ofperr error = 0; | |
4991 | ||
4992 | /* ofm->temp_rule is our reference to the learned rule. We have a | |
4993 | * reference to an existing rule, if it already was in the classifier, | |
4994 | * otherwise we may have a fresh rule that we need to insert. */ | |
4995 | struct rule *rule = ofm->temp_rule; | |
4996 | if (!rule) { | |
4997 | return OFPERR_OFPFMFC_UNKNOWN; | |
4998 | } | |
2c7ee524 JR |
4999 | |
5000 | /* Create a new rule if the current one has been removed from the | |
5001 | * classifier. We need to do this since RCU does not allow a current rule | |
5002 | * to be reinserted before all threads have quiesced. | |
5003 | * | |
5004 | * It is possible that the rule is removed asynchronously, e.g., right | |
1f4a8933 | 5005 | * after we have read the 'rule->state' below. In this case the next time |
2c7ee524 | 5006 | * this function is executed the rule will be reinstated. */ |
1f4a8933 | 5007 | if (rule->state == RULE_REMOVED) { |
2c7ee524 JR |
5008 | struct cls_rule cr; |
5009 | ||
5010 | cls_rule_clone(&cr, &rule->cr); | |
5011 | ovs_mutex_lock(&rule->mutex); | |
1f4a8933 | 5012 | error = ofproto_rule_create(rule->ofproto, &cr, rule->table_id, |
2c7ee524 JR |
5013 | rule->flow_cookie, |
5014 | rule->idle_timeout, | |
5015 | rule->hard_timeout, rule->flags, | |
5016 | rule->importance, | |
5017 | rule->actions->ofpacts, | |
5018 | rule->actions->ofpacts_len, | |
5c7c16d8 YHW |
5019 | rule->match_tlv_bitmap, |
5020 | rule->ofpacts_tlv_bitmap, | |
2c7ee524 JR |
5021 | &ofm->temp_rule); |
5022 | ovs_mutex_unlock(&rule->mutex); | |
2c7ee524 | 5023 | if (!error) { |
1f4a8933 | 5024 | ofproto_rule_unref(rule); /* Release old reference. */ |
2c7ee524 | 5025 | } |
2c7ee524 JR |
5026 | } else { |
5027 | /* Refresh the existing rule. */ | |
5028 | ovs_mutex_lock(&rule->mutex); | |
5029 | rule->modified = time_msec(); | |
5030 | ovs_mutex_unlock(&rule->mutex); | |
5031 | } | |
1f4a8933 JR |
5032 | return error; |
5033 | } | |
5034 | ||
5035 | enum ofperr | |
5036 | ofproto_flow_mod_learn_start(struct ofproto_flow_mod *ofm) | |
5037 | OVS_REQUIRES(ofproto_mutex) | |
5038 | { | |
5039 | struct rule *rule = ofm->temp_rule; | |
5040 | ||
5041 | /* ofproto_flow_mod_start() consumes the reference, so we | |
5042 | * take a new one. */ | |
5043 | ofproto_rule_ref(rule); | |
5044 | enum ofperr error = ofproto_flow_mod_start(rule->ofproto, ofm); | |
5045 | ofm->temp_rule = rule; | |
5046 | ||
5047 | return error; | |
5048 | } | |
5049 | ||
6dd3c787 JR |
5050 | void |
5051 | ofproto_flow_mod_learn_revert(struct ofproto_flow_mod *ofm) | |
5052 | OVS_REQUIRES(ofproto_mutex) | |
5053 | { | |
5054 | struct rule *rule = rule_collection_rules(&ofm->new_rules)[0]; | |
5055 | ofproto_flow_mod_revert(rule->ofproto, ofm); | |
5056 | } | |
5057 | ||
1f4a8933 JR |
5058 | void |
5059 | ofproto_flow_mod_learn_finish(struct ofproto_flow_mod *ofm, | |
5060 | struct ofproto *orig_ofproto) | |
5061 | OVS_REQUIRES(ofproto_mutex) | |
5062 | { | |
5063 | struct rule *rule = rule_collection_rules(&ofm->new_rules)[0]; | |
5064 | ||
5065 | /* If learning on a different bridge, must bump its version | |
5066 | * number and flush connmgr afterwards. */ | |
5067 | if (rule->ofproto != orig_ofproto) { | |
5068 | ofproto_bump_tables_version(rule->ofproto); | |
5069 | } | |
5070 | ofproto_flow_mod_finish(rule->ofproto, ofm, NULL); | |
5071 | if (rule->ofproto != orig_ofproto) { | |
5072 | ofmonitor_flush(rule->ofproto->connmgr); | |
5073 | } | |
5074 | } | |
5075 | ||
5076 | /* Refresh 'ofm->temp_rule', for which the caller holds a reference, if already | |
5077 | * in the classifier, insert it otherwise. If the rule has already been | |
5078 | * removed from the classifier, a new rule is created using 'ofm->temp_rule' as | |
5079 | * a template and the reference to the old 'ofm->temp_rule' is freed. If | |
5080 | * 'keep_ref' is true, then a reference to the current rule is held, otherwise | |
5081 | * it is released and 'ofm->temp_rule' is set to NULL. | |
5082 | * | |
5083 | * Caller needs to be the exclusive owner of 'ofm' as it is being manipulated | |
5084 | * during the call. */ | |
5085 | enum ofperr | |
5086 | ofproto_flow_mod_learn(struct ofproto_flow_mod *ofm, bool keep_ref) | |
5087 | OVS_EXCLUDED(ofproto_mutex) | |
5088 | { | |
5089 | enum ofperr error = ofproto_flow_mod_learn_refresh(ofm); | |
5090 | struct rule *rule = ofm->temp_rule; | |
5091 | ||
5092 | /* Do we need to insert the rule? */ | |
5093 | if (!error && rule->state == RULE_INITIALIZED) { | |
5094 | ovs_mutex_lock(&ofproto_mutex); | |
5095 | ofm->version = rule->ofproto->tables_version + 1; | |
5096 | error = ofproto_flow_mod_learn_start(ofm); | |
5097 | if (!error) { | |
5098 | ofproto_flow_mod_learn_finish(ofm, NULL); | |
5099 | } | |
5100 | ovs_mutex_unlock(&ofproto_mutex); | |
5101 | } | |
2c7ee524 | 5102 | |
2c7ee524 JR |
5103 | if (!keep_ref) { |
5104 | ofproto_rule_unref(rule); | |
5105 | ofm->temp_rule = NULL; | |
5106 | } | |
5107 | return error; | |
5108 | } | |
5109 | ||
39c94593 | 5110 | static void |
5bacd5cd JR |
5111 | replace_rule_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
5112 | struct rule *old_rule, struct rule *new_rule) | |
39c94593 JR |
5113 | { |
5114 | struct oftable *table = &ofproto->tables[new_rule->table_id]; | |
834fe5cb | 5115 | |
6787a49f | 5116 | /* 'old_rule' may be either an evicted rule or replaced rule. */ |
39c94593 | 5117 | if (old_rule) { |
5bacd5cd JR |
5118 | /* Copy values from old rule for modify semantics. */ |
5119 | if (old_rule->removed_reason != OFPRR_EVICTION) { | |
5120 | bool change_cookie = (ofm->modify_cookie | |
5121 | && new_rule->flow_cookie != OVS_BE64_MAX | |
5122 | && new_rule->flow_cookie != old_rule->flow_cookie); | |
5123 | ||
5124 | ovs_mutex_lock(&new_rule->mutex); | |
5125 | ovs_mutex_lock(&old_rule->mutex); | |
5126 | if (ofm->command != OFPFC_ADD) { | |
5127 | new_rule->idle_timeout = old_rule->idle_timeout; | |
5128 | new_rule->hard_timeout = old_rule->hard_timeout; | |
5129 | *CONST_CAST(uint16_t *, &new_rule->importance) = old_rule->importance; | |
5130 | new_rule->flags = old_rule->flags; | |
5131 | new_rule->created = old_rule->created; | |
5132 | } | |
5133 | if (!change_cookie) { | |
82a3160e BP |
5134 | *CONST_CAST(ovs_be64 *, &new_rule->flow_cookie) |
5135 | = old_rule->flow_cookie; | |
5bacd5cd JR |
5136 | } |
5137 | ovs_mutex_unlock(&old_rule->mutex); | |
5138 | ovs_mutex_unlock(&new_rule->mutex); | |
5139 | } | |
5140 | ||
39c94593 | 5141 | /* Mark the old rule for removal in the next version. */ |
5bacd5cd | 5142 | cls_rule_make_invisible_in_version(&old_rule->cr, ofm->version); |
9bab38ff JR |
5143 | |
5144 | /* Remove the old rule from data structures. */ | |
5145 | ofproto_rule_remove__(ofproto, old_rule); | |
d79e3d70 JR |
5146 | } else { |
5147 | table->n_flows++; | |
dd27be82 | 5148 | } |
cc099268 JR |
5149 | /* Insert flow to ofproto data structures, so that later flow_mods may |
5150 | * relate to it. This is reversible, in case later errors require this to | |
39c94593 JR |
5151 | * be reverted. */ |
5152 | ofproto_rule_insert__(ofproto, new_rule); | |
5153 | /* Make the new rule visible for classifier lookups only from the next | |
5154 | * version. */ | |
5bacd5cd JR |
5155 | classifier_insert(&table->cls, &new_rule->cr, ofm->version, ofm->conjs, |
5156 | ofm->n_conjs); | |
39c94593 JR |
5157 | } |
5158 | ||
cc099268 JR |
5159 | static void |
5160 | replace_rule_revert(struct ofproto *ofproto, | |
5161 | struct rule *old_rule, struct rule *new_rule) | |
39c94593 JR |
5162 | { |
5163 | struct oftable *table = &ofproto->tables[new_rule->table_id]; | |
35f48b8b | 5164 | |
39c94593 | 5165 | if (old_rule) { |
5bacd5cd JR |
5166 | if (old_rule->removed_reason == OFPRR_EVICTION) { |
5167 | /* Revert the eviction. */ | |
5168 | eviction_group_add_rule(old_rule); | |
5169 | } | |
5170 | ||
9bab38ff JR |
5171 | /* Restore the old rule to data structures. */ |
5172 | ofproto_rule_insert__(ofproto, old_rule); | |
5173 | ||
d79e3d70 | 5174 | /* Restore the original visibility of the old rule. */ |
39c94593 | 5175 | cls_rule_restore_visibility(&old_rule->cr); |
d79e3d70 JR |
5176 | } else { |
5177 | /* Restore table's rule count. */ | |
5178 | table->n_flows--; | |
834fe5cb BP |
5179 | } |
5180 | ||
39c94593 JR |
5181 | /* Remove the new rule immediately. It was never visible to lookups. */ |
5182 | if (!classifier_remove(&table->cls, &new_rule->cr)) { | |
5183 | OVS_NOT_REACHED(); | |
5ecc9d81 | 5184 | } |
39c94593 | 5185 | ofproto_rule_remove__(ofproto, new_rule); |
39c94593 | 5186 | ofproto_rule_unref(new_rule); |
dd27be82 | 5187 | } |
79eee1eb | 5188 | |
39c94593 | 5189 | /* Adds the 'new_rule', replacing the 'old_rule'. */ |
dd27be82 | 5190 | static void |
81dee635 | 5191 | replace_rule_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 5192 | const struct openflow_mod_requester *req, |
39c94593 JR |
5193 | struct rule *old_rule, struct rule *new_rule, |
5194 | struct ovs_list *dead_cookies) | |
dd27be82 JR |
5195 | OVS_REQUIRES(ofproto_mutex) |
5196 | { | |
6787a49f JR |
5197 | struct rule *replaced_rule; |
5198 | ||
5aacc3e2 JR |
5199 | replaced_rule = (old_rule && old_rule->removed_reason != OFPRR_EVICTION) |
5200 | ? old_rule : NULL; | |
dd27be82 | 5201 | |
6787a49f JR |
5202 | /* Insert the new flow to the ofproto provider. A non-NULL 'replaced_rule' |
5203 | * is a duplicate rule the 'new_rule' is replacing. The provider should | |
748eb2f5 | 5204 | * link the packet and byte counts from the old rule to the new one if |
8b6987d7 JR |
5205 | * 'modify_keep_counts' is 'true'. The 'replaced_rule' will be deleted |
5206 | * right after this call. */ | |
6787a49f | 5207 | ofproto->ofproto_class->rule_insert(new_rule, replaced_rule, |
8b6987d7 | 5208 | ofm->modify_keep_counts); |
39c94593 JR |
5209 | learned_cookies_inc(ofproto, rule_get_actions(new_rule)); |
5210 | ||
5211 | if (old_rule) { | |
5212 | const struct rule_actions *old_actions = rule_get_actions(old_rule); | |
81dee635 | 5213 | const struct rule_actions *new_actions = rule_get_actions(new_rule); |
39c94593 | 5214 | |
39c94593 JR |
5215 | learned_cookies_dec(ofproto, old_actions, dead_cookies); |
5216 | ||
6787a49f | 5217 | if (replaced_rule) { |
5bacd5cd JR |
5218 | enum nx_flow_update_event event = ofm->command == OFPFC_ADD |
5219 | ? NXFME_ADDED : NXFME_MODIFIED; | |
5220 | ||
81dee635 JR |
5221 | bool changed_cookie = (new_rule->flow_cookie |
5222 | != old_rule->flow_cookie); | |
6787a49f | 5223 | |
81dee635 JR |
5224 | bool changed_actions = !ofpacts_equal(new_actions->ofpacts, |
5225 | new_actions->ofpacts_len, | |
5226 | old_actions->ofpacts, | |
5227 | old_actions->ofpacts_len); | |
6787a49f | 5228 | |
5bacd5cd | 5229 | if (event != NXFME_MODIFIED || changed_actions |
81dee635 | 5230 | || changed_cookie) { |
5bacd5cd | 5231 | ofmonitor_report(ofproto->connmgr, new_rule, event, 0, |
6787a49f JR |
5232 | req ? req->ofconn : NULL, |
5233 | req ? req->request->xid : 0, | |
81dee635 | 5234 | changed_actions ? old_actions : NULL); |
6787a49f JR |
5235 | } |
5236 | } else { | |
5237 | /* XXX: This is slight duplication with delete_flows_finish__() */ | |
6787a49f JR |
5238 | ofmonitor_report(ofproto->connmgr, old_rule, NXFME_DELETED, |
5239 | OFPRR_EVICTION, | |
39c94593 | 5240 | req ? req->ofconn : NULL, |
6787a49f | 5241 | req ? req->request->xid : 0, NULL); |
39c94593 | 5242 | } |
dd27be82 | 5243 | } |
9ed18e46 BP |
5244 | } |
5245 | ||
2c7ee524 | 5246 | /* ofm->temp_rule is consumed only in the successful case. */ |
9387b970 | 5247 | static enum ofperr |
8be00367 | 5248 | modify_flows_start__(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
dd27be82 JR |
5249 | OVS_REQUIRES(ofproto_mutex) |
5250 | { | |
8be00367 JR |
5251 | struct rule_collection *old_rules = &ofm->old_rules; |
5252 | struct rule_collection *new_rules = &ofm->new_rules; | |
dd27be82 JR |
5253 | enum ofperr error; |
5254 | ||
fe59694b | 5255 | if (rule_collection_n(old_rules) > 0) { |
39c94593 | 5256 | /* Create a new 'modified' rule for each old rule. */ |
5bacd5cd JR |
5257 | struct rule *old_rule, *new_rule; |
5258 | const struct rule_actions *actions = rule_get_actions(ofm->temp_rule); | |
39c94593 | 5259 | |
5bacd5cd JR |
5260 | /* Must check actions while holding ofproto_mutex to avoid a race. */ |
5261 | error = ofproto_check_ofpacts(ofproto, actions->ofpacts, | |
5262 | actions->ofpacts_len); | |
5263 | if (error) { | |
5264 | return error; | |
5265 | } | |
5266 | ||
5267 | /* Use the temp rule as the first new rule, and as the template for | |
5268 | * the rest. */ | |
5269 | struct rule *temp = ofm->temp_rule; | |
5270 | ofm->temp_rule = NULL; /* We consume the template. */ | |
5271 | ||
5272 | bool first = true; | |
5273 | RULE_COLLECTION_FOR_EACH (old_rule, old_rules) { | |
5274 | if (first) { | |
5275 | /* The template rule's match is possibly a loose one, so it | |
5276 | * must be replaced with the old rule's match so that the new | |
5277 | * rule actually replaces the old one. */ | |
5278 | cls_rule_destroy(CONST_CAST(struct cls_rule *, &temp->cr)); | |
5279 | cls_rule_clone(CONST_CAST(struct cls_rule *, &temp->cr), | |
5280 | &old_rule->cr); | |
5c7c16d8 YHW |
5281 | if (temp->match_tlv_bitmap != old_rule->match_tlv_bitmap) { |
5282 | mf_vl_mff_unref(&temp->ofproto->vl_mff_map, | |
5283 | temp->match_tlv_bitmap); | |
5284 | temp->match_tlv_bitmap = old_rule->match_tlv_bitmap; | |
5285 | mf_vl_mff_ref(&temp->ofproto->vl_mff_map, | |
5286 | temp->match_tlv_bitmap); | |
5287 | } | |
5bacd5cd JR |
5288 | *CONST_CAST(uint8_t *, &temp->table_id) = old_rule->table_id; |
5289 | rule_collection_add(new_rules, temp); | |
5290 | first = false; | |
39c94593 | 5291 | } else { |
5bacd5cd JR |
5292 | struct cls_rule cr; |
5293 | cls_rule_clone(&cr, &old_rule->cr); | |
5294 | error = ofproto_rule_create(ofproto, &cr, old_rule->table_id, | |
5295 | temp->flow_cookie, | |
5296 | temp->idle_timeout, | |
5297 | temp->hard_timeout, temp->flags, | |
5298 | temp->importance, | |
5299 | temp->actions->ofpacts, | |
5300 | temp->actions->ofpacts_len, | |
5c7c16d8 YHW |
5301 | old_rule->match_tlv_bitmap, |
5302 | temp->ofpacts_tlv_bitmap, | |
5bacd5cd JR |
5303 | &new_rule); |
5304 | if (!error) { | |
5305 | rule_collection_add(new_rules, new_rule); | |
5306 | } else { | |
2c7ee524 JR |
5307 | /* Return the template rule in place in the error case. */ |
5308 | ofm->temp_rule = temp; | |
5309 | rule_collection_rules(new_rules)[0] = NULL; | |
5310 | ||
5bacd5cd JR |
5311 | rule_collection_unref(new_rules); |
5312 | rule_collection_destroy(new_rules); | |
5313 | return error; | |
5314 | } | |
39c94593 JR |
5315 | } |
5316 | } | |
fe59694b JR |
5317 | ovs_assert(rule_collection_n(new_rules) |
5318 | == rule_collection_n(old_rules)); | |
39c94593 | 5319 | |
fe59694b | 5320 | RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) { |
5bacd5cd | 5321 | replace_rule_start(ofproto, ofm, old_rule, new_rule); |
39c94593 | 5322 | } |
81dee635 | 5323 | } else if (ofm->modify_may_add_flow) { |
5bacd5cd | 5324 | /* No match, add a new flow, consumes 'temp'. */ |
8be00367 | 5325 | error = add_flow_start(ofproto, ofm); |
dd27be82 | 5326 | } else { |
2c7ee524 JR |
5327 | /* No flow to modify and may not add a flow. */ |
5328 | ofproto_rule_unref(ofm->temp_rule); | |
5329 | ofm->temp_rule = NULL; /* We consume the template. */ | |
d99f64d7 | 5330 | error = 0; |
dd27be82 | 5331 | } |
39c94593 | 5332 | |
dd27be82 JR |
5333 | return error; |
5334 | } | |
5335 | ||
5bacd5cd JR |
5336 | static enum ofperr |
5337 | modify_flows_init_loose(struct ofproto *ofproto, | |
5338 | struct ofproto_flow_mod *ofm, | |
5339 | const struct ofputil_flow_mod *fm) | |
5340 | OVS_EXCLUDED(ofproto_mutex) | |
5341 | { | |
5342 | rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, 0, | |
5343 | OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY, | |
5344 | OFPG_ANY); | |
5345 | rule_criteria_require_rw(&ofm->criteria, | |
5346 | (fm->flags & OFPUTIL_FF_NO_READONLY) != 0); | |
5347 | /* Must create a new flow in advance for the case that no matches are | |
5348 | * found. Also used for template for multiple modified flows. */ | |
5349 | add_flow_init(ofproto, ofm, fm); | |
5350 | ||
5351 | return 0; | |
5352 | } | |
5353 | ||
90bf1e07 | 5354 | /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on |
c184807c | 5355 | * failure. */ |
90bf1e07 | 5356 | static enum ofperr |
8be00367 | 5357 | modify_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
15aaf599 | 5358 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 5359 | { |
8be00367 | 5360 | struct rule_collection *old_rules = &ofm->old_rules; |
d51c8b71 | 5361 | enum ofperr error; |
9ed18e46 | 5362 | |
5bacd5cd | 5363 | error = collect_rules_loose(ofproto, &ofm->criteria, old_rules); |
a9b22b7f | 5364 | |
a8e547c1 | 5365 | if (!error) { |
8be00367 | 5366 | error = modify_flows_start__(ofproto, ofm); |
d99f64d7 JR |
5367 | } |
5368 | ||
5369 | if (error) { | |
39c94593 | 5370 | rule_collection_destroy(old_rules); |
d99f64d7 | 5371 | } |
5bacd5cd | 5372 | |
d99f64d7 JR |
5373 | return error; |
5374 | } | |
5375 | ||
1f42be1c | 5376 | static void |
8be00367 | 5377 | modify_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
1f42be1c JR |
5378 | OVS_REQUIRES(ofproto_mutex) |
5379 | { | |
8be00367 JR |
5380 | struct rule_collection *old_rules = &ofm->old_rules; |
5381 | struct rule_collection *new_rules = &ofm->new_rules; | |
5382 | ||
39c94593 | 5383 | /* Old rules were not changed yet, only need to revert new rules. */ |
5bacd5cd | 5384 | if (rule_collection_n(old_rules) > 0) { |
fe59694b JR |
5385 | struct rule *old_rule, *new_rule; |
5386 | RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) { | |
5387 | replace_rule_revert(ofproto, old_rule, new_rule); | |
39c94593 JR |
5388 | } |
5389 | rule_collection_destroy(new_rules); | |
5390 | rule_collection_destroy(old_rules); | |
1f42be1c | 5391 | } |
1f42be1c JR |
5392 | } |
5393 | ||
d99f64d7 | 5394 | static void |
8be00367 | 5395 | modify_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 5396 | const struct openflow_mod_requester *req) |
d99f64d7 JR |
5397 | OVS_REQUIRES(ofproto_mutex) |
5398 | { | |
8be00367 JR |
5399 | struct rule_collection *old_rules = &ofm->old_rules; |
5400 | struct rule_collection *new_rules = &ofm->new_rules; | |
5401 | ||
fe59694b JR |
5402 | if (rule_collection_n(old_rules) == 0 |
5403 | && rule_collection_n(new_rules) == 1) { | |
8be00367 | 5404 | add_flow_finish(ofproto, ofm, req); |
fe59694b | 5405 | } else if (rule_collection_n(old_rules) > 0) { |
39c94593 JR |
5406 | struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies); |
5407 | ||
fe59694b JR |
5408 | ovs_assert(rule_collection_n(new_rules) |
5409 | == rule_collection_n(old_rules)); | |
39c94593 | 5410 | |
fe59694b JR |
5411 | struct rule *old_rule, *new_rule; |
5412 | RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) { | |
81dee635 | 5413 | replace_rule_finish(ofproto, ofm, req, old_rule, new_rule, |
fe59694b | 5414 | &dead_cookies); |
39c94593 JR |
5415 | } |
5416 | learned_cookies_flush(ofproto, &dead_cookies); | |
cc099268 | 5417 | remove_rules_postponed(old_rules); |
d99f64d7 | 5418 | } |
d99f64d7 JR |
5419 | } |
5420 | ||
5bacd5cd JR |
5421 | static enum ofperr |
5422 | modify_flow_init_strict(struct ofproto *ofproto OVS_UNUSED, | |
5423 | struct ofproto_flow_mod *ofm, | |
5424 | const struct ofputil_flow_mod *fm) | |
5425 | OVS_EXCLUDED(ofproto_mutex) | |
5426 | { | |
5427 | rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, fm->priority, | |
5428 | OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY, | |
5429 | OFPG_ANY); | |
5430 | rule_criteria_require_rw(&ofm->criteria, | |
5431 | (fm->flags & OFPUTIL_FF_NO_READONLY) != 0); | |
5432 | /* Must create a new flow in advance for the case that no matches are | |
5433 | * found. Also used for template for multiple modified flows. */ | |
5434 | add_flow_init(ofproto, ofm, fm); | |
5435 | ||
5436 | return 0; | |
5437 | } | |
5438 | ||
79eee1eb | 5439 | /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error |
baae3d02 | 5440 | * code on failure. */ |
90bf1e07 | 5441 | static enum ofperr |
8be00367 | 5442 | modify_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
15aaf599 | 5443 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 5444 | { |
8be00367 | 5445 | struct rule_collection *old_rules = &ofm->old_rules; |
d51c8b71 | 5446 | enum ofperr error; |
6c1491fb | 5447 | |
5bacd5cd | 5448 | error = collect_rules_strict(ofproto, &ofm->criteria, old_rules); |
a9b22b7f | 5449 | |
a8e547c1 | 5450 | if (!error) { |
dd27be82 | 5451 | /* collect_rules_strict() can return max 1 rule. */ |
8be00367 | 5452 | error = modify_flows_start__(ofproto, ofm); |
d99f64d7 JR |
5453 | } |
5454 | ||
d99f64d7 JR |
5455 | return error; |
5456 | } | |
9ed18e46 BP |
5457 | \f |
5458 | /* OFPFC_DELETE implementation. */ | |
79eee1eb | 5459 | |
254750ce | 5460 | static void |
44e0c35d | 5461 | delete_flows_start__(struct ofproto *ofproto, ovs_version_t version, |
39c94593 JR |
5462 | const struct rule_collection *rules) |
5463 | OVS_REQUIRES(ofproto_mutex) | |
5464 | { | |
fe59694b JR |
5465 | struct rule *rule; |
5466 | ||
5467 | RULE_COLLECTION_FOR_EACH (rule, rules) { | |
d79e3d70 JR |
5468 | struct oftable *table = &ofproto->tables[rule->table_id]; |
5469 | ||
5470 | table->n_flows--; | |
8be00367 | 5471 | cls_rule_make_invisible_in_version(&rule->cr, version); |
9bab38ff JR |
5472 | |
5473 | /* Remove rule from ofproto data structures. */ | |
5474 | ofproto_rule_remove__(ofproto, rule); | |
39c94593 JR |
5475 | } |
5476 | } | |
5477 | ||
25070e04 JR |
5478 | static void |
5479 | delete_flows_revert__(struct ofproto *ofproto, | |
5480 | const struct rule_collection *rules) | |
5481 | OVS_REQUIRES(ofproto_mutex) | |
5482 | { | |
5483 | struct rule *rule; | |
5484 | ||
5485 | RULE_COLLECTION_FOR_EACH (rule, rules) { | |
5486 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
5487 | ||
5488 | /* Add rule back to ofproto data structures. */ | |
5489 | ofproto_rule_insert__(ofproto, rule); | |
5490 | ||
5491 | /* Restore table's rule count. */ | |
5492 | table->n_flows++; | |
5493 | ||
5494 | /* Restore the original visibility of the rule. */ | |
5495 | cls_rule_restore_visibility(&rule->cr); | |
5496 | } | |
5497 | } | |
5498 | ||
39c94593 JR |
5499 | static void |
5500 | delete_flows_finish__(struct ofproto *ofproto, | |
5501 | struct rule_collection *rules, | |
5502 | enum ofp_flow_removed_reason reason, | |
0a0d9385 | 5503 | const struct openflow_mod_requester *req) |
15aaf599 | 5504 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 5505 | { |
fe59694b | 5506 | if (rule_collection_n(rules)) { |
55951e15 | 5507 | struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies); |
fe59694b | 5508 | struct rule *rule; |
79eee1eb | 5509 | |
fe59694b | 5510 | RULE_COLLECTION_FOR_EACH (rule, rules) { |
f695ebfa JR |
5511 | /* This value will be used to send the flow removed message right |
5512 | * before the rule is actually destroyed. */ | |
5513 | rule->removed_reason = reason; | |
5514 | ||
9ca4a86f | 5515 | ofmonitor_report(ofproto->connmgr, rule, NXFME_DELETED, reason, |
c84d8691 JR |
5516 | req ? req->ofconn : NULL, |
5517 | req ? req->request->xid : 0, NULL); | |
6c6eedc5 SJ |
5518 | |
5519 | /* Send Vacancy Event for OF1.4+. */ | |
5520 | send_table_status(ofproto, rule->table_id); | |
5521 | ||
802f84ff JR |
5522 | learned_cookies_dec(ofproto, rule_get_actions(rule), |
5523 | &dead_cookies); | |
9ca4a86f | 5524 | } |
cc099268 | 5525 | remove_rules_postponed(rules); |
39c94593 | 5526 | |
35f48b8b | 5527 | learned_cookies_flush(ofproto, &dead_cookies); |
39c94593 JR |
5528 | } |
5529 | } | |
5530 | ||
5531 | /* Deletes the rules listed in 'rules'. | |
5532 | * The deleted rules will become invisible to the lookups in the next version. | |
5533 | * Destroys 'rules'. */ | |
5534 | static void | |
5535 | delete_flows__(struct rule_collection *rules, | |
5536 | enum ofp_flow_removed_reason reason, | |
0a0d9385 | 5537 | const struct openflow_mod_requester *req) |
39c94593 JR |
5538 | OVS_REQUIRES(ofproto_mutex) |
5539 | { | |
fe59694b JR |
5540 | if (rule_collection_n(rules)) { |
5541 | struct ofproto *ofproto = rule_collection_rules(rules)[0]->ofproto; | |
39c94593 | 5542 | |
8be00367 | 5543 | delete_flows_start__(ofproto, ofproto->tables_version + 1, rules); |
39c94593 JR |
5544 | ofproto_bump_tables_version(ofproto); |
5545 | delete_flows_finish__(ofproto, rules, reason, req); | |
9ca4a86f BP |
5546 | ofmonitor_flush(ofproto->connmgr); |
5547 | } | |
79eee1eb | 5548 | } |
79eee1eb | 5549 | |
5bacd5cd JR |
5550 | static enum ofperr |
5551 | delete_flows_init_loose(struct ofproto *ofproto OVS_UNUSED, | |
5552 | struct ofproto_flow_mod *ofm, | |
5553 | const struct ofputil_flow_mod *fm) | |
5554 | OVS_EXCLUDED(ofproto_mutex) | |
5555 | { | |
5556 | rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, 0, | |
5557 | OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, | |
5558 | fm->out_port, fm->out_group); | |
5559 | rule_criteria_require_rw(&ofm->criteria, | |
5560 | (fm->flags & OFPUTIL_FF_NO_READONLY) != 0); | |
5561 | return 0; | |
5562 | } | |
5563 | ||
79eee1eb | 5564 | /* Implements OFPFC_DELETE. */ |
90bf1e07 | 5565 | static enum ofperr |
8be00367 | 5566 | delete_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
15aaf599 | 5567 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 5568 | { |
8be00367 | 5569 | struct rule_collection *rules = &ofm->old_rules; |
90bf1e07 | 5570 | enum ofperr error; |
064af421 | 5571 | |
5bacd5cd | 5572 | error = collect_rules_loose(ofproto, &ofm->criteria, rules); |
a9b22b7f | 5573 | |
802f84ff | 5574 | if (!error) { |
8be00367 | 5575 | delete_flows_start__(ofproto, ofm->version, rules); |
a8e547c1 | 5576 | } |
a8e547c1 BP |
5577 | |
5578 | return error; | |
064af421 BP |
5579 | } |
5580 | ||
ce59413f | 5581 | static void |
8be00367 | 5582 | delete_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
ce59413f JR |
5583 | OVS_REQUIRES(ofproto_mutex) |
5584 | { | |
25070e04 | 5585 | delete_flows_revert__(ofproto, &ofm->old_rules); |
ce59413f JR |
5586 | } |
5587 | ||
1f42be1c | 5588 | static void |
2c7ee524 | 5589 | delete_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 5590 | const struct openflow_mod_requester *req) |
15aaf599 | 5591 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 5592 | { |
5aacc3e2 | 5593 | delete_flows_finish__(ofproto, &ofm->old_rules, OFPRR_DELETE, req); |
ce59413f JR |
5594 | } |
5595 | ||
5bacd5cd JR |
5596 | static enum ofperr |
5597 | delete_flows_init_strict(struct ofproto *ofproto OVS_UNUSED, | |
5598 | struct ofproto_flow_mod *ofm, | |
5599 | const struct ofputil_flow_mod *fm) | |
5600 | OVS_EXCLUDED(ofproto_mutex) | |
5601 | { | |
5602 | rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, fm->priority, | |
5603 | OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, | |
5604 | fm->out_port, fm->out_group); | |
5605 | rule_criteria_require_rw(&ofm->criteria, | |
5606 | (fm->flags & OFPUTIL_FF_NO_READONLY) != 0); | |
5607 | return 0; | |
5608 | } | |
5609 | ||
ce59413f JR |
5610 | /* Implements OFPFC_DELETE_STRICT. */ |
5611 | static enum ofperr | |
2c7ee524 | 5612 | delete_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
ce59413f JR |
5613 | OVS_REQUIRES(ofproto_mutex) |
5614 | { | |
8be00367 | 5615 | struct rule_collection *rules = &ofm->old_rules; |
ce59413f JR |
5616 | enum ofperr error; |
5617 | ||
5bacd5cd | 5618 | error = collect_rules_strict(ofproto, &ofm->criteria, rules); |
a9b22b7f | 5619 | |
802f84ff | 5620 | if (!error) { |
8be00367 | 5621 | delete_flows_start__(ofproto, ofm->version, rules); |
ce59413f JR |
5622 | } |
5623 | ||
5624 | return error; | |
5625 | } | |
5626 | ||
f695ebfa | 5627 | /* This may only be called by rule_destroy_cb()! */ |
abe529af | 5628 | static void |
f695ebfa JR |
5629 | ofproto_rule_send_removed(struct rule *rule) |
5630 | OVS_EXCLUDED(ofproto_mutex) | |
abe529af BP |
5631 | { |
5632 | struct ofputil_flow_removed fr; | |
dc437090 | 5633 | long long int used; |
abe529af | 5634 | |
5cb7a798 | 5635 | minimatch_expand(&rule->cr.match, &fr.match); |
81a76618 | 5636 | fr.priority = rule->cr.priority; |
f695ebfa | 5637 | |
25010c68 JR |
5638 | /* Synchronize with connmgr_destroy() calls to prevent connmgr disappearing |
5639 | * while we use it. */ | |
f695ebfa | 5640 | ovs_mutex_lock(&ofproto_mutex); |
25010c68 JR |
5641 | struct connmgr *connmgr = rule->ofproto->connmgr; |
5642 | if (!connmgr) { | |
5643 | ovs_mutex_unlock(&ofproto_mutex); | |
5644 | return; | |
5645 | } | |
5646 | ||
abe529af | 5647 | fr.cookie = rule->flow_cookie; |
f695ebfa | 5648 | fr.reason = rule->removed_reason; |
95216219 | 5649 | fr.table_id = rule->table_id; |
65e0be10 BP |
5650 | calc_duration(rule->created, time_msec(), |
5651 | &fr.duration_sec, &fr.duration_nsec); | |
d10976b7 | 5652 | ovs_mutex_lock(&rule->mutex); |
abe529af | 5653 | fr.idle_timeout = rule->idle_timeout; |
fa2bad0f | 5654 | fr.hard_timeout = rule->hard_timeout; |
d10976b7 | 5655 | ovs_mutex_unlock(&rule->mutex); |
abe529af | 5656 | rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count, |
dc437090 | 5657 | &fr.byte_count, &used); |
25010c68 | 5658 | connmgr_send_flow_removed(connmgr, &fr); |
f695ebfa | 5659 | ovs_mutex_unlock(&ofproto_mutex); |
abe529af BP |
5660 | } |
5661 | ||
5662 | /* Sends an OpenFlow "flow removed" message with the given 'reason' (either | |
5663 | * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its | |
5664 | * ofproto. | |
5665 | * | |
5666 | * ofproto implementation ->run() functions should use this function to expire | |
5667 | * OpenFlow flows. */ | |
5668 | void | |
5669 | ofproto_rule_expire(struct rule *rule, uint8_t reason) | |
15aaf599 | 5670 | OVS_REQUIRES(ofproto_mutex) |
abe529af | 5671 | { |
802f84ff JR |
5672 | struct rule_collection rules; |
5673 | ||
fe59694b JR |
5674 | rule_collection_init(&rules); |
5675 | rule_collection_add(&rules, rule); | |
802f84ff | 5676 | delete_flows__(&rules, reason, NULL); |
79eee1eb | 5677 | } |
994c9973 BP |
5678 | |
5679 | /* Reduces '*timeout' to no more than 'max'. A value of zero in either case | |
5680 | * means "infinite". */ | |
5681 | static void | |
5682 | reduce_timeout(uint16_t max, uint16_t *timeout) | |
5683 | { | |
5684 | if (max && (!*timeout || *timeout > max)) { | |
5685 | *timeout = max; | |
5686 | } | |
5687 | } | |
5688 | ||
5689 | /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle | |
5690 | * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to | |
5691 | * 'idle_timeout' seconds. Similarly for 'hard_timeout'. | |
5692 | * | |
5693 | * Suitable for implementing OFPACT_FIN_TIMEOUT. */ | |
1f4a8933 JR |
5694 | void |
5695 | ofproto_rule_reduce_timeouts__(struct rule *rule, | |
5696 | uint16_t idle_timeout, uint16_t hard_timeout) | |
5697 | OVS_REQUIRES(ofproto_mutex) | |
5698 | OVS_EXCLUDED(rule->mutex) | |
5699 | { | |
5700 | if (!idle_timeout && !hard_timeout) { | |
5701 | return; | |
5702 | } | |
5703 | ||
5704 | if (ovs_list_is_empty(&rule->expirable)) { | |
5705 | ovs_list_insert(&rule->ofproto->expirable, &rule->expirable); | |
5706 | } | |
5707 | ||
5708 | ovs_mutex_lock(&rule->mutex); | |
5709 | reduce_timeout(idle_timeout, &rule->idle_timeout); | |
5710 | reduce_timeout(hard_timeout, &rule->hard_timeout); | |
5711 | ovs_mutex_unlock(&rule->mutex); | |
5712 | } | |
5713 | ||
994c9973 BP |
5714 | void |
5715 | ofproto_rule_reduce_timeouts(struct rule *rule, | |
5716 | uint16_t idle_timeout, uint16_t hard_timeout) | |
d10976b7 | 5717 | OVS_EXCLUDED(ofproto_mutex, rule->mutex) |
994c9973 BP |
5718 | { |
5719 | if (!idle_timeout && !hard_timeout) { | |
5720 | return; | |
5721 | } | |
5722 | ||
abe7b10f | 5723 | ovs_mutex_lock(&ofproto_mutex); |
417e7e66 BW |
5724 | if (ovs_list_is_empty(&rule->expirable)) { |
5725 | ovs_list_insert(&rule->ofproto->expirable, &rule->expirable); | |
994c9973 | 5726 | } |
abe7b10f | 5727 | ovs_mutex_unlock(&ofproto_mutex); |
994c9973 | 5728 | |
d10976b7 | 5729 | ovs_mutex_lock(&rule->mutex); |
994c9973 BP |
5730 | reduce_timeout(idle_timeout, &rule->idle_timeout); |
5731 | reduce_timeout(hard_timeout, &rule->hard_timeout); | |
d10976b7 | 5732 | ovs_mutex_unlock(&rule->mutex); |
994c9973 | 5733 | } |
79eee1eb | 5734 | \f |
90bf1e07 | 5735 | static enum ofperr |
2e4f5fcf | 5736 | handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
15aaf599 | 5737 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 5738 | { |
a5b8d268 | 5739 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
7338102b | 5740 | struct ofputil_flow_mod fm; |
f25d0cf3 BP |
5741 | uint64_t ofpacts_stub[1024 / 8]; |
5742 | struct ofpbuf ofpacts; | |
90bf1e07 | 5743 | enum ofperr error; |
064af421 | 5744 | |
76589937 | 5745 | error = reject_slave_controller(ofconn); |
9deba63b | 5746 | if (error) { |
b0d38b2f | 5747 | return error; |
9deba63b | 5748 | } |
3052b0c5 | 5749 | |
f25d0cf3 | 5750 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
7338102b | 5751 | error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn), |
04f48a68 YHW |
5752 | ofproto_get_tun_tab(ofproto), |
5753 | &ofproto->vl_mff_map, &ofpacts, | |
7e9f8266 BP |
5754 | u16_to_ofp(ofproto->max_ports), |
5755 | ofproto->n_tables); | |
548de4dd | 5756 | if (!error) { |
0a0d9385 | 5757 | struct openflow_mod_requester req = { ofconn, oh }; |
7338102b | 5758 | error = handle_flow_mod__(ofproto, &fm, &req); |
49bdc010 | 5759 | } |
2e310801 | 5760 | |
f25d0cf3 | 5761 | ofpbuf_uninit(&ofpacts); |
f25d0cf3 | 5762 | return error; |
75a75043 BP |
5763 | } |
5764 | ||
90bf1e07 | 5765 | static enum ofperr |
7338102b | 5766 | handle_flow_mod__(struct ofproto *ofproto, const struct ofputil_flow_mod *fm, |
0a0d9385 | 5767 | const struct openflow_mod_requester *req) |
15aaf599 | 5768 | OVS_EXCLUDED(ofproto_mutex) |
75a75043 | 5769 | { |
7338102b | 5770 | struct ofproto_flow_mod ofm; |
35412852 | 5771 | enum ofperr error; |
75a75043 | 5772 | |
2c7ee524 | 5773 | error = ofproto_flow_mod_init(ofproto, &ofm, fm, NULL); |
5bacd5cd JR |
5774 | if (error) { |
5775 | return error; | |
5776 | } | |
7338102b | 5777 | |
15aaf599 | 5778 | ovs_mutex_lock(&ofproto_mutex); |
7338102b JR |
5779 | ofm.version = ofproto->tables_version + 1; |
5780 | error = ofproto_flow_mod_start(ofproto, &ofm); | |
1f42be1c | 5781 | if (!error) { |
39c94593 | 5782 | ofproto_bump_tables_version(ofproto); |
7338102b | 5783 | ofproto_flow_mod_finish(ofproto, &ofm, req); |
2c7ee524 | 5784 | ofmonitor_flush(ofproto->connmgr); |
3052b0c5 | 5785 | } |
15aaf599 | 5786 | ovs_mutex_unlock(&ofproto_mutex); |
35412852 | 5787 | |
35412852 | 5788 | return error; |
3052b0c5 BP |
5789 | } |
5790 | ||
90bf1e07 | 5791 | static enum ofperr |
d1e2cf21 | 5792 | handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh) |
9deba63b | 5793 | { |
f4f1ea7e BP |
5794 | struct ofputil_role_request request; |
5795 | struct ofputil_role_request reply; | |
9deba63b | 5796 | struct ofpbuf *buf; |
6ea4776b JR |
5797 | enum ofperr error; |
5798 | ||
f4f1ea7e | 5799 | error = ofputil_decode_role_message(oh, &request); |
6ea4776b JR |
5800 | if (error) { |
5801 | return error; | |
5802 | } | |
9deba63b | 5803 | |
f4f1ea7e | 5804 | if (request.role != OFPCR12_ROLE_NOCHANGE) { |
d26eda9f BP |
5805 | if (request.role != OFPCR12_ROLE_EQUAL |
5806 | && request.have_generation_id | |
f4f1ea7e BP |
5807 | && !ofconn_set_master_election_id(ofconn, request.generation_id)) { |
5808 | return OFPERR_OFPRRFC_STALE; | |
6ea4776b | 5809 | } |
6ea4776b | 5810 | |
f4f1ea7e BP |
5811 | ofconn_set_role(ofconn, request.role); |
5812 | } | |
9deba63b | 5813 | |
f4f1ea7e | 5814 | reply.role = ofconn_get_role(ofconn); |
147cc9d3 BP |
5815 | reply.have_generation_id = ofconn_get_master_election_id( |
5816 | ofconn, &reply.generation_id); | |
f4f1ea7e | 5817 | buf = ofputil_encode_role_reply(oh, &reply); |
b0421aa2 | 5818 | ofconn_send_reply(ofconn, buf); |
9deba63b BP |
5819 | |
5820 | return 0; | |
5821 | } | |
5822 | ||
90bf1e07 | 5823 | static enum ofperr |
6c1491fb BP |
5824 | handle_nxt_flow_mod_table_id(struct ofconn *ofconn, |
5825 | const struct ofp_header *oh) | |
5826 | { | |
982697a4 | 5827 | const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh); |
27527aa0 BP |
5828 | enum ofputil_protocol cur, next; |
5829 | ||
5830 | cur = ofconn_get_protocol(ofconn); | |
5831 | next = ofputil_protocol_set_tid(cur, msg->set != 0); | |
5832 | ofconn_set_protocol(ofconn, next); | |
6c1491fb | 5833 | |
6c1491fb BP |
5834 | return 0; |
5835 | } | |
5836 | ||
90bf1e07 | 5837 | static enum ofperr |
d1e2cf21 | 5838 | handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh) |
09246b99 | 5839 | { |
982697a4 | 5840 | const struct nx_set_flow_format *msg = ofpmsg_body(oh); |
27527aa0 BP |
5841 | enum ofputil_protocol cur, next; |
5842 | enum ofputil_protocol next_base; | |
09246b99 | 5843 | |
27527aa0 BP |
5844 | next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format)); |
5845 | if (!next_base) { | |
90bf1e07 | 5846 | return OFPERR_OFPBRC_EPERM; |
09246b99 | 5847 | } |
7ee20df1 | 5848 | |
27527aa0 BP |
5849 | cur = ofconn_get_protocol(ofconn); |
5850 | next = ofputil_protocol_set_base(cur, next_base); | |
27527aa0 | 5851 | ofconn_set_protocol(ofconn, next); |
b20f4073 | 5852 | |
7ee20df1 | 5853 | return 0; |
09246b99 BP |
5854 | } |
5855 | ||
90bf1e07 | 5856 | static enum ofperr |
54834960 EJ |
5857 | handle_nxt_set_packet_in_format(struct ofconn *ofconn, |
5858 | const struct ofp_header *oh) | |
5859 | { | |
982697a4 | 5860 | const struct nx_set_packet_in_format *msg = ofpmsg_body(oh); |
54834960 EJ |
5861 | uint32_t format; |
5862 | ||
54834960 | 5863 | format = ntohl(msg->format); |
6409e008 | 5864 | if (!ofputil_packet_in_format_is_valid(format)) { |
90bf1e07 | 5865 | return OFPERR_OFPBRC_EPERM; |
54834960 EJ |
5866 | } |
5867 | ||
54834960 EJ |
5868 | ofconn_set_packet_in_format(ofconn, format); |
5869 | return 0; | |
5870 | } | |
5871 | ||
80d5aefd BP |
5872 | static enum ofperr |
5873 | handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh) | |
5874 | { | |
5876b4db BP |
5875 | struct ofputil_async_cfg basis = ofconn_get_async_config(ofconn); |
5876 | struct ofputil_async_cfg ac; | |
d18cc1ee | 5877 | enum ofperr error; |
80d5aefd | 5878 | |
5876b4db | 5879 | error = ofputil_decode_set_async_config(oh, false, &basis, &ac); |
d18cc1ee AA |
5880 | if (error) { |
5881 | return error; | |
5882 | } | |
80d5aefd | 5883 | |
a930d4c5 | 5884 | ofconn_set_async_config(ofconn, &ac); |
4550b647 MM |
5885 | if (ofconn_get_type(ofconn) == OFCONN_SERVICE && |
5886 | !ofconn_get_miss_send_len(ofconn)) { | |
5887 | ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN); | |
5888 | } | |
80d5aefd BP |
5889 | |
5890 | return 0; | |
5891 | } | |
5892 | ||
c423b3b3 AC |
5893 | static enum ofperr |
5894 | handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh) | |
5895 | { | |
a930d4c5 | 5896 | struct ofputil_async_cfg ac = ofconn_get_async_config(ofconn); |
af7bc161 | 5897 | ofconn_send_reply(ofconn, ofputil_encode_get_async_reply(oh, &ac)); |
c423b3b3 AC |
5898 | |
5899 | return 0; | |
5900 | } | |
5901 | ||
a7349929 BP |
5902 | static enum ofperr |
5903 | handle_nxt_set_controller_id(struct ofconn *ofconn, | |
5904 | const struct ofp_header *oh) | |
5905 | { | |
982697a4 | 5906 | const struct nx_controller_id *nci = ofpmsg_body(oh); |
a7349929 | 5907 | |
a7349929 BP |
5908 | if (!is_all_zeros(nci->zero, sizeof nci->zero)) { |
5909 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
5910 | } | |
5911 | ||
5912 | ofconn_set_controller_id(ofconn, ntohs(nci->controller_id)); | |
5913 | return 0; | |
5914 | } | |
5915 | ||
90bf1e07 | 5916 | static enum ofperr |
d1e2cf21 | 5917 | handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh) |
246e61ea | 5918 | { |
246e61ea JP |
5919 | struct ofpbuf *buf; |
5920 | ||
982697a4 BP |
5921 | buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION |
5922 | ? OFPRAW_OFPT10_BARRIER_REPLY | |
5923 | : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0); | |
b0421aa2 | 5924 | ofconn_send_reply(ofconn, buf); |
246e61ea JP |
5925 | return 0; |
5926 | } | |
5927 | ||
2b07c8b1 BP |
5928 | static void |
5929 | ofproto_compose_flow_refresh_update(const struct rule *rule, | |
5930 | enum nx_flow_monitor_flags flags, | |
140f36ba DDP |
5931 | struct ovs_list *msgs, |
5932 | const struct tun_table *tun_table) | |
15aaf599 | 5933 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 | 5934 | { |
6f00e29b | 5935 | const struct rule_actions *actions; |
2b07c8b1 BP |
5936 | struct ofputil_flow_update fu; |
5937 | ||
2b07c8b1 BP |
5938 | fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD) |
5939 | ? NXFME_ADDED : NXFME_MODIFIED); | |
5940 | fu.reason = 0; | |
d10976b7 | 5941 | ovs_mutex_lock(&rule->mutex); |
2b07c8b1 BP |
5942 | fu.idle_timeout = rule->idle_timeout; |
5943 | fu.hard_timeout = rule->hard_timeout; | |
d10976b7 | 5944 | ovs_mutex_unlock(&rule->mutex); |
2b07c8b1 BP |
5945 | fu.table_id = rule->table_id; |
5946 | fu.cookie = rule->flow_cookie; | |
140f36ba | 5947 | minimatch_expand(&rule->cr.match, &fu.match); |
6b140a4e | 5948 | fu.priority = rule->cr.priority; |
6f00e29b | 5949 | |
b20f4073 | 5950 | actions = flags & NXFMF_ACTIONS ? rule_get_actions(rule) : NULL; |
6f00e29b BP |
5951 | fu.ofpacts = actions ? actions->ofpacts : NULL; |
5952 | fu.ofpacts_len = actions ? actions->ofpacts_len : 0; | |
2b07c8b1 | 5953 | |
417e7e66 | 5954 | if (ovs_list_is_empty(msgs)) { |
2b07c8b1 BP |
5955 | ofputil_start_flow_update(msgs); |
5956 | } | |
140f36ba | 5957 | ofputil_append_flow_update(&fu, msgs, tun_table); |
2b07c8b1 BP |
5958 | } |
5959 | ||
5960 | void | |
a8e547c1 | 5961 | ofmonitor_compose_refresh_updates(struct rule_collection *rules, |
ca6ba700 | 5962 | struct ovs_list *msgs) |
15aaf599 | 5963 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 | 5964 | { |
fe59694b | 5965 | struct rule *rule; |
2b07c8b1 | 5966 | |
fe59694b | 5967 | RULE_COLLECTION_FOR_EACH (rule, rules) { |
2b07c8b1 BP |
5968 | enum nx_flow_monitor_flags flags = rule->monitor_flags; |
5969 | rule->monitor_flags = 0; | |
5970 | ||
140f36ba DDP |
5971 | ofproto_compose_flow_refresh_update(rule, flags, msgs, |
5972 | ofproto_get_tun_tab(rule->ofproto)); | |
2b07c8b1 BP |
5973 | } |
5974 | } | |
5975 | ||
5976 | static void | |
5977 | ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m, | |
5978 | struct rule *rule, uint64_t seqno, | |
a8e547c1 | 5979 | struct rule_collection *rules) |
15aaf599 | 5980 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
5981 | { |
5982 | enum nx_flow_monitor_flags update; | |
5983 | ||
5e8b38b0 | 5984 | if (rule_is_hidden(rule)) { |
2b07c8b1 BP |
5985 | return; |
5986 | } | |
5987 | ||
b20f4073 | 5988 | if (!ofproto_rule_has_out_port(rule, m->out_port)) { |
2b07c8b1 BP |
5989 | return; |
5990 | } | |
5991 | ||
5992 | if (seqno) { | |
5993 | if (rule->add_seqno > seqno) { | |
5994 | update = NXFMF_ADD | NXFMF_MODIFY; | |
5995 | } else if (rule->modify_seqno > seqno) { | |
5996 | update = NXFMF_MODIFY; | |
5997 | } else { | |
5998 | return; | |
5999 | } | |
6000 | ||
6001 | if (!(m->flags & update)) { | |
6002 | return; | |
6003 | } | |
6004 | } else { | |
6005 | update = NXFMF_INITIAL; | |
6006 | } | |
6007 | ||
6008 | if (!rule->monitor_flags) { | |
a8e547c1 | 6009 | rule_collection_add(rules, rule); |
2b07c8b1 BP |
6010 | } |
6011 | rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS); | |
6012 | } | |
6013 | ||
6014 | static void | |
6015 | ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m, | |
6016 | uint64_t seqno, | |
a8e547c1 | 6017 | struct rule_collection *rules) |
15aaf599 | 6018 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
6019 | { |
6020 | const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn); | |
2b07c8b1 | 6021 | const struct oftable *table; |
81a76618 | 6022 | struct cls_rule target; |
2b07c8b1 | 6023 | |
bd53aa17 | 6024 | cls_rule_init_from_minimatch(&target, &m->match, 0); |
2b07c8b1 | 6025 | FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) { |
2b07c8b1 BP |
6026 | struct rule *rule; |
6027 | ||
44e0c35d | 6028 | CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &target, OVS_VERSION_MAX) { |
2b07c8b1 BP |
6029 | ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules); |
6030 | } | |
6031 | } | |
48d28ac1 | 6032 | cls_rule_destroy(&target); |
2b07c8b1 BP |
6033 | } |
6034 | ||
6035 | static void | |
6036 | ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m, | |
a8e547c1 | 6037 | struct rule_collection *rules) |
15aaf599 | 6038 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
6039 | { |
6040 | if (m->flags & NXFMF_INITIAL) { | |
6041 | ofproto_collect_ofmonitor_refresh_rules(m, 0, rules); | |
6042 | } | |
6043 | } | |
6044 | ||
6045 | void | |
6046 | ofmonitor_collect_resume_rules(struct ofmonitor *m, | |
a8e547c1 | 6047 | uint64_t seqno, struct rule_collection *rules) |
15aaf599 | 6048 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
6049 | { |
6050 | ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules); | |
6051 | } | |
6052 | ||
7b900689 SH |
6053 | static enum ofperr |
6054 | flow_monitor_delete(struct ofconn *ofconn, uint32_t id) | |
6055 | OVS_REQUIRES(ofproto_mutex) | |
6056 | { | |
6057 | struct ofmonitor *m; | |
6058 | enum ofperr error; | |
6059 | ||
6060 | m = ofmonitor_lookup(ofconn, id); | |
6061 | if (m) { | |
6062 | ofmonitor_destroy(m); | |
6063 | error = 0; | |
6064 | } else { | |
6065 | error = OFPERR_OFPMOFC_UNKNOWN_MONITOR; | |
6066 | } | |
6067 | ||
6068 | return error; | |
6069 | } | |
6070 | ||
2b07c8b1 | 6071 | static enum ofperr |
982697a4 | 6072 | handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh) |
15aaf599 | 6073 | OVS_EXCLUDED(ofproto_mutex) |
2b07c8b1 BP |
6074 | { |
6075 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
2b07c8b1 | 6076 | |
0a2869d5 BP |
6077 | struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length)); |
6078 | ||
6079 | struct ofmonitor **monitors = NULL; | |
6080 | size_t allocated_monitors = 0; | |
6081 | size_t n_monitors = 0; | |
6082 | ||
6083 | enum ofperr error; | |
15aaf599 BP |
6084 | |
6085 | ovs_mutex_lock(&ofproto_mutex); | |
2b07c8b1 BP |
6086 | for (;;) { |
6087 | struct ofputil_flow_monitor_request request; | |
6088 | struct ofmonitor *m; | |
6089 | int retval; | |
6090 | ||
6091 | retval = ofputil_decode_flow_monitor_request(&request, &b); | |
6092 | if (retval == EOF) { | |
6093 | break; | |
6094 | } else if (retval) { | |
6095 | error = retval; | |
6096 | goto error; | |
6097 | } | |
6098 | ||
6099 | if (request.table_id != 0xff | |
6100 | && request.table_id >= ofproto->n_tables) { | |
6101 | error = OFPERR_OFPBRC_BAD_TABLE_ID; | |
6102 | goto error; | |
6103 | } | |
6104 | ||
6105 | error = ofmonitor_create(&request, ofconn, &m); | |
6106 | if (error) { | |
6107 | goto error; | |
6108 | } | |
6109 | ||
6110 | if (n_monitors >= allocated_monitors) { | |
6111 | monitors = x2nrealloc(monitors, &allocated_monitors, | |
6112 | sizeof *monitors); | |
6113 | } | |
6114 | monitors[n_monitors++] = m; | |
6115 | } | |
6116 | ||
0a2869d5 | 6117 | struct rule_collection rules; |
a8e547c1 | 6118 | rule_collection_init(&rules); |
0a2869d5 | 6119 | for (size_t i = 0; i < n_monitors; i++) { |
2b07c8b1 BP |
6120 | ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules); |
6121 | } | |
6122 | ||
0a2869d5 | 6123 | struct ovs_list replies; |
982697a4 | 6124 | ofpmp_init(&replies, oh); |
2b07c8b1 | 6125 | ofmonitor_compose_refresh_updates(&rules, &replies); |
15aaf599 BP |
6126 | ovs_mutex_unlock(&ofproto_mutex); |
6127 | ||
a8e547c1 BP |
6128 | rule_collection_destroy(&rules); |
6129 | ||
2b07c8b1 | 6130 | ofconn_send_replies(ofconn, &replies); |
2b07c8b1 BP |
6131 | free(monitors); |
6132 | ||
6133 | return 0; | |
6134 | ||
6135 | error: | |
0a2869d5 | 6136 | for (size_t i = 0; i < n_monitors; i++) { |
2b07c8b1 BP |
6137 | ofmonitor_destroy(monitors[i]); |
6138 | } | |
6139 | free(monitors); | |
4cd1bc9d BP |
6140 | ovs_mutex_unlock(&ofproto_mutex); |
6141 | ||
2b07c8b1 BP |
6142 | return error; |
6143 | } | |
6144 | ||
6145 | static enum ofperr | |
6146 | handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh) | |
15aaf599 | 6147 | OVS_EXCLUDED(ofproto_mutex) |
2b07c8b1 | 6148 | { |
15aaf599 | 6149 | enum ofperr error; |
2b07c8b1 BP |
6150 | uint32_t id; |
6151 | ||
6152 | id = ofputil_decode_flow_monitor_cancel(oh); | |
15aaf599 BP |
6153 | |
6154 | ovs_mutex_lock(&ofproto_mutex); | |
7b900689 | 6155 | error = flow_monitor_delete(ofconn, id); |
15aaf599 | 6156 | ovs_mutex_unlock(&ofproto_mutex); |
2b07c8b1 | 6157 | |
15aaf599 | 6158 | return error; |
2b07c8b1 BP |
6159 | } |
6160 | ||
9cae45dc JR |
6161 | /* Meters implementation. |
6162 | * | |
6163 | * Meter table entry, indexed by the OpenFlow meter_id. | |
9cae45dc JR |
6164 | * 'created' is used to compute the duration for meter stats. |
6165 | * 'list rules' is needed so that we can delete the dependent rules when the | |
6166 | * meter table entry is deleted. | |
6167 | * 'provider_meter_id' is for the provider's private use. | |
6168 | */ | |
6169 | struct meter { | |
6170 | long long int created; /* Time created. */ | |
ca6ba700 | 6171 | struct ovs_list rules; /* List of "struct rule_dpif"s. */ |
9cae45dc JR |
6172 | ofproto_meter_id provider_meter_id; |
6173 | uint16_t flags; /* Meter flags. */ | |
6174 | uint16_t n_bands; /* Number of meter bands. */ | |
6175 | struct ofputil_meter_band *bands; | |
6176 | }; | |
6177 | ||
6178 | /* | |
076caa2f JR |
6179 | * This is used in instruction validation at flow set-up time, to map |
6180 | * the OpenFlow meter ID to the corresponding datapath provider meter | |
6181 | * ID. If either does not exist, returns false. Otherwise updates | |
6182 | * the meter action and returns true. | |
9cae45dc | 6183 | */ |
076caa2f JR |
6184 | static bool |
6185 | ofproto_fix_meter_action(const struct ofproto *ofproto, | |
6186 | struct ofpact_meter *ma) | |
6187 | { | |
6188 | if (ma->meter_id && ma->meter_id <= ofproto->meter_features.max_meters) { | |
6189 | const struct meter *meter = ofproto->meters[ma->meter_id]; | |
6190 | ||
6191 | if (meter && meter->provider_meter_id.uint32 != UINT32_MAX) { | |
6192 | /* Update the action with the provider's meter ID, so that we | |
6193 | * do not need any synchronization between ofproto_dpif_xlate | |
6194 | * and ofproto for meter table access. */ | |
6195 | ma->provider_meter_id = meter->provider_meter_id.uint32; | |
6196 | return true; | |
9cae45dc JR |
6197 | } |
6198 | } | |
076caa2f | 6199 | return false; |
9cae45dc JR |
6200 | } |
6201 | ||
062fea06 BP |
6202 | /* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's |
6203 | * list of rules. */ | |
6204 | static void | |
6205 | meter_insert_rule(struct rule *rule) | |
6206 | { | |
6207 | const struct rule_actions *a = rule_get_actions(rule); | |
6208 | uint32_t meter_id = ofpacts_get_meter(a->ofpacts, a->ofpacts_len); | |
6209 | struct meter *meter = rule->ofproto->meters[meter_id]; | |
6210 | ||
417e7e66 | 6211 | ovs_list_insert(&meter->rules, &rule->meter_list_node); |
062fea06 BP |
6212 | } |
6213 | ||
9cae45dc JR |
6214 | static void |
6215 | meter_update(struct meter *meter, const struct ofputil_meter_config *config) | |
6216 | { | |
6217 | free(meter->bands); | |
6218 | ||
6219 | meter->flags = config->flags; | |
6220 | meter->n_bands = config->n_bands; | |
6221 | meter->bands = xmemdup(config->bands, | |
6222 | config->n_bands * sizeof *meter->bands); | |
6223 | } | |
6224 | ||
6225 | static struct meter * | |
6226 | meter_create(const struct ofputil_meter_config *config, | |
6227 | ofproto_meter_id provider_meter_id) | |
6228 | { | |
6229 | struct meter *meter; | |
6230 | ||
6231 | meter = xzalloc(sizeof *meter); | |
6232 | meter->provider_meter_id = provider_meter_id; | |
6233 | meter->created = time_msec(); | |
417e7e66 | 6234 | ovs_list_init(&meter->rules); |
9cae45dc JR |
6235 | |
6236 | meter_update(meter, config); | |
6237 | ||
6238 | return meter; | |
6239 | } | |
6240 | ||
6b3f7f02 JR |
6241 | static void |
6242 | meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last) | |
15aaf599 | 6243 | OVS_REQUIRES(ofproto_mutex) |
6b3f7f02 | 6244 | { |
3059d7e5 | 6245 | for (uint32_t mid = first; mid <= last; ++mid) { |
6b3f7f02 JR |
6246 | struct meter *meter = ofproto->meters[mid]; |
6247 | if (meter) { | |
3059d7e5 JR |
6248 | /* First delete the rules that use this meter. */ |
6249 | if (!ovs_list_is_empty(&meter->rules)) { | |
6250 | struct rule_collection rules; | |
6251 | struct rule *rule; | |
6252 | ||
6253 | rule_collection_init(&rules); | |
6254 | ||
6255 | LIST_FOR_EACH (rule, meter_list_node, &meter->rules) { | |
6256 | rule_collection_add(&rules, rule); | |
6257 | } | |
6258 | delete_flows__(&rules, OFPRR_METER_DELETE, NULL); | |
6259 | } | |
6260 | ||
6b3f7f02 JR |
6261 | ofproto->meters[mid] = NULL; |
6262 | ofproto->ofproto_class->meter_del(ofproto, | |
6263 | meter->provider_meter_id); | |
6264 | free(meter->bands); | |
6265 | free(meter); | |
6266 | } | |
6267 | } | |
6268 | } | |
6269 | ||
9cae45dc JR |
6270 | static enum ofperr |
6271 | handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm) | |
6272 | { | |
6273 | ofproto_meter_id provider_meter_id = { UINT32_MAX }; | |
6274 | struct meter **meterp = &ofproto->meters[mm->meter.meter_id]; | |
6275 | enum ofperr error; | |
6276 | ||
6277 | if (*meterp) { | |
6278 | return OFPERR_OFPMMFC_METER_EXISTS; | |
6279 | } | |
6280 | ||
6281 | error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id, | |
6282 | &mm->meter); | |
6283 | if (!error) { | |
6284 | ovs_assert(provider_meter_id.uint32 != UINT32_MAX); | |
6285 | *meterp = meter_create(&mm->meter, provider_meter_id); | |
6286 | } | |
f0f8c6c2 | 6287 | return error; |
9cae45dc JR |
6288 | } |
6289 | ||
6290 | static enum ofperr | |
6291 | handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm) | |
6292 | { | |
6293 | struct meter *meter = ofproto->meters[mm->meter.meter_id]; | |
6294 | enum ofperr error; | |
e555eb7c | 6295 | uint32_t provider_meter_id; |
9cae45dc JR |
6296 | |
6297 | if (!meter) { | |
6298 | return OFPERR_OFPMMFC_UNKNOWN_METER; | |
6299 | } | |
6300 | ||
e555eb7c | 6301 | provider_meter_id = meter->provider_meter_id.uint32; |
9cae45dc JR |
6302 | error = ofproto->ofproto_class->meter_set(ofproto, |
6303 | &meter->provider_meter_id, | |
6304 | &mm->meter); | |
e555eb7c | 6305 | ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id); |
9cae45dc JR |
6306 | if (!error) { |
6307 | meter_update(meter, &mm->meter); | |
6308 | } | |
6309 | return error; | |
6310 | } | |
6311 | ||
6312 | static enum ofperr | |
baae3d02 | 6313 | handle_delete_meter(struct ofconn *ofconn, struct ofputil_meter_mod *mm) |
15aaf599 | 6314 | OVS_EXCLUDED(ofproto_mutex) |
9cae45dc JR |
6315 | { |
6316 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
6317 | uint32_t meter_id = mm->meter.meter_id; | |
a8e547c1 | 6318 | enum ofperr error = 0; |
9cae45dc | 6319 | uint32_t first, last; |
9cae45dc JR |
6320 | |
6321 | if (meter_id == OFPM13_ALL) { | |
6322 | first = 1; | |
6323 | last = ofproto->meter_features.max_meters; | |
6324 | } else { | |
6325 | if (!meter_id || meter_id > ofproto->meter_features.max_meters) { | |
6326 | return 0; | |
6327 | } | |
6328 | first = last = meter_id; | |
6329 | } | |
6330 | ||
9cae45dc | 6331 | /* Delete the meters. */ |
3059d7e5 | 6332 | ovs_mutex_lock(&ofproto_mutex); |
6b3f7f02 | 6333 | meter_delete(ofproto, first, last); |
15aaf599 | 6334 | ovs_mutex_unlock(&ofproto_mutex); |
a8e547c1 BP |
6335 | |
6336 | return error; | |
9cae45dc JR |
6337 | } |
6338 | ||
6339 | static enum ofperr | |
6340 | handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
6341 | { | |
6342 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
6343 | struct ofputil_meter_mod mm; | |
6344 | uint64_t bands_stub[256 / 8]; | |
6345 | struct ofpbuf bands; | |
6346 | uint32_t meter_id; | |
6347 | enum ofperr error; | |
6348 | ||
6349 | error = reject_slave_controller(ofconn); | |
6350 | if (error) { | |
6351 | return error; | |
6352 | } | |
6353 | ||
6354 | ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub); | |
6355 | ||
6356 | error = ofputil_decode_meter_mod(oh, &mm, &bands); | |
6357 | if (error) { | |
6358 | goto exit_free_bands; | |
6359 | } | |
6360 | ||
6361 | meter_id = mm.meter.meter_id; | |
6362 | ||
6363 | if (mm.command != OFPMC13_DELETE) { | |
6364 | /* Fails also when meters are not implemented by the provider. */ | |
b31e8700 | 6365 | if (meter_id == 0 || meter_id > OFPM13_MAX) { |
9cae45dc JR |
6366 | error = OFPERR_OFPMMFC_INVALID_METER; |
6367 | goto exit_free_bands; | |
b31e8700 JR |
6368 | } else if (meter_id > ofproto->meter_features.max_meters) { |
6369 | error = OFPERR_OFPMMFC_OUT_OF_METERS; | |
6370 | goto exit_free_bands; | |
9cae45dc JR |
6371 | } |
6372 | if (mm.meter.n_bands > ofproto->meter_features.max_bands) { | |
6373 | error = OFPERR_OFPMMFC_OUT_OF_BANDS; | |
6374 | goto exit_free_bands; | |
6375 | } | |
6376 | } | |
6377 | ||
6378 | switch (mm.command) { | |
6379 | case OFPMC13_ADD: | |
6380 | error = handle_add_meter(ofproto, &mm); | |
6381 | break; | |
6382 | ||
6383 | case OFPMC13_MODIFY: | |
6384 | error = handle_modify_meter(ofproto, &mm); | |
6385 | break; | |
6386 | ||
6387 | case OFPMC13_DELETE: | |
baae3d02 | 6388 | error = handle_delete_meter(ofconn, &mm); |
9cae45dc JR |
6389 | break; |
6390 | ||
6391 | default: | |
6392 | error = OFPERR_OFPMMFC_BAD_COMMAND; | |
6393 | break; | |
6394 | } | |
6395 | ||
3c35db62 NR |
6396 | if (!error) { |
6397 | struct ofputil_requestforward rf; | |
6398 | rf.xid = oh->xid; | |
6399 | rf.reason = OFPRFR_METER_MOD; | |
6400 | rf.meter_mod = &mm; | |
6401 | connmgr_send_requestforward(ofproto->connmgr, ofconn, &rf); | |
6402 | } | |
6403 | ||
9cae45dc JR |
6404 | exit_free_bands: |
6405 | ofpbuf_uninit(&bands); | |
6406 | return error; | |
6407 | } | |
6408 | ||
6409 | static enum ofperr | |
6410 | handle_meter_features_request(struct ofconn *ofconn, | |
6411 | const struct ofp_header *request) | |
6412 | { | |
6413 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
6414 | struct ofputil_meter_features features; | |
6415 | struct ofpbuf *b; | |
6416 | ||
6417 | if (ofproto->ofproto_class->meter_get_features) { | |
6418 | ofproto->ofproto_class->meter_get_features(ofproto, &features); | |
6419 | } else { | |
6420 | memset(&features, 0, sizeof features); | |
6421 | } | |
6422 | b = ofputil_encode_meter_features_reply(&features, request); | |
6423 | ||
6424 | ofconn_send_reply(ofconn, b); | |
6425 | return 0; | |
6426 | } | |
6427 | ||
6428 | static enum ofperr | |
6429 | handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request, | |
6430 | enum ofptype type) | |
6431 | { | |
6432 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
ca6ba700 | 6433 | struct ovs_list replies; |
9cae45dc JR |
6434 | uint64_t bands_stub[256 / 8]; |
6435 | struct ofpbuf bands; | |
6436 | uint32_t meter_id, first, last; | |
6437 | ||
6438 | ofputil_decode_meter_request(request, &meter_id); | |
6439 | ||
6440 | if (meter_id == OFPM13_ALL) { | |
6441 | first = 1; | |
6442 | last = ofproto->meter_features.max_meters; | |
6443 | } else { | |
6444 | if (!meter_id || meter_id > ofproto->meter_features.max_meters || | |
6445 | !ofproto->meters[meter_id]) { | |
6446 | return OFPERR_OFPMMFC_UNKNOWN_METER; | |
6447 | } | |
6448 | first = last = meter_id; | |
6449 | } | |
6450 | ||
6451 | ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub); | |
6452 | ofpmp_init(&replies, request); | |
6453 | ||
6454 | for (meter_id = first; meter_id <= last; ++meter_id) { | |
6455 | struct meter *meter = ofproto->meters[meter_id]; | |
6456 | if (!meter) { | |
6457 | continue; /* Skip non-existing meters. */ | |
6458 | } | |
261bd854 | 6459 | if (type == OFPTYPE_METER_STATS_REQUEST) { |
9cae45dc JR |
6460 | struct ofputil_meter_stats stats; |
6461 | ||
6462 | stats.meter_id = meter_id; | |
6463 | ||
6464 | /* Provider sets the packet and byte counts, we do the rest. */ | |
417e7e66 | 6465 | stats.flow_count = ovs_list_size(&meter->rules); |
9cae45dc JR |
6466 | calc_duration(meter->created, time_msec(), |
6467 | &stats.duration_sec, &stats.duration_nsec); | |
6468 | stats.n_bands = meter->n_bands; | |
6469 | ofpbuf_clear(&bands); | |
6470 | stats.bands | |
6471 | = ofpbuf_put_uninit(&bands, | |
6472 | meter->n_bands * sizeof *stats.bands); | |
6473 | ||
6474 | if (!ofproto->ofproto_class->meter_get(ofproto, | |
6475 | meter->provider_meter_id, | |
5dddf960 | 6476 | &stats, meter->n_bands)) { |
9cae45dc JR |
6477 | ofputil_append_meter_stats(&replies, &stats); |
6478 | } | |
6479 | } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */ | |
6480 | struct ofputil_meter_config config; | |
6481 | ||
6482 | config.meter_id = meter_id; | |
6483 | config.flags = meter->flags; | |
6484 | config.n_bands = meter->n_bands; | |
6485 | config.bands = meter->bands; | |
6486 | ofputil_append_meter_config(&replies, &config); | |
6487 | } | |
6488 | } | |
6489 | ||
6490 | ofconn_send_replies(ofconn, &replies); | |
6491 | ofpbuf_uninit(&bands); | |
6492 | return 0; | |
6493 | } | |
6494 | ||
db88b35c JR |
6495 | /* Returned group is RCU protected. */ |
6496 | static struct ofgroup * | |
5d08a275 JR |
6497 | ofproto_group_lookup__(const struct ofproto *ofproto, uint32_t group_id, |
6498 | ovs_version_t version) | |
db88b35c JR |
6499 | { |
6500 | struct ofgroup *group; | |
6501 | ||
6502 | CMAP_FOR_EACH_WITH_HASH (group, cmap_node, hash_int(group_id, 0), | |
6503 | &ofproto->groups) { | |
5d08a275 JR |
6504 | if (group->group_id == group_id |
6505 | && versions_visible_in_version(&group->versions, version)) { | |
db88b35c | 6506 | return group; |
7395c052 NZ |
6507 | } |
6508 | } | |
3fc3b679 | 6509 | |
db88b35c | 6510 | return NULL; |
7395c052 NZ |
6511 | } |
6512 | ||
3fc3b679 AZ |
6513 | /* If the group exists, this function increments the groups's reference count. |
6514 | * | |
6515 | * Make sure to call ofproto_group_unref() after no longer needing to maintain | |
6516 | * a reference to the group. */ | |
db88b35c | 6517 | struct ofgroup * |
76973237 | 6518 | ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id, |
5d08a275 | 6519 | ovs_version_t version, bool take_ref) |
7395c052 | 6520 | { |
db88b35c | 6521 | struct ofgroup *group; |
7395c052 | 6522 | |
5d08a275 | 6523 | group = ofproto_group_lookup__(ofproto, group_id, version); |
76973237 | 6524 | if (group && take_ref) { |
db88b35c JR |
6525 | /* Not holding a lock, so it is possible that another thread releases |
6526 | * the last reference just before we manage to get one. */ | |
6527 | return ofproto_group_try_ref(group) ? group : NULL; | |
7395c052 | 6528 | } |
76973237 | 6529 | return group; |
7395c052 NZ |
6530 | } |
6531 | ||
cc099268 | 6532 | /* Caller should hold 'ofproto_mutex' if it is important that the |
db88b35c | 6533 | * group is not removed by someone else. */ |
7e9f8266 BP |
6534 | static bool |
6535 | ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id) | |
7e9f8266 | 6536 | { |
5d08a275 | 6537 | return ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX) != NULL; |
7e9f8266 BP |
6538 | } |
6539 | ||
cc099268 JR |
6540 | static void |
6541 | group_add_rule(struct ofgroup *group, struct rule *rule) | |
732feb93 | 6542 | { |
cc099268 JR |
6543 | rule_collection_add(&group->rules, rule); |
6544 | } | |
732feb93 | 6545 | |
cc099268 JR |
6546 | static void |
6547 | group_remove_rule(struct ofgroup *group, struct rule *rule) | |
6548 | { | |
6549 | rule_collection_remove(&group->rules, rule); | |
732feb93 SH |
6550 | } |
6551 | ||
7395c052 | 6552 | static void |
ca6ba700 | 6553 | append_group_stats(struct ofgroup *group, struct ovs_list *replies) |
cc099268 | 6554 | OVS_REQUIRES(ofproto_mutex) |
7395c052 NZ |
6555 | { |
6556 | struct ofputil_group_stats ogs; | |
eaf004bd | 6557 | const struct ofproto *ofproto = group->ofproto; |
7395c052 NZ |
6558 | long long int now = time_msec(); |
6559 | int error; | |
6560 | ||
646b2a9c SH |
6561 | ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats); |
6562 | ||
732feb93 | 6563 | /* Provider sets the packet and byte counts, we do the rest. */ |
fe59694b | 6564 | ogs.ref_count = rule_collection_n(&group->rules); |
732feb93 SH |
6565 | ogs.n_buckets = group->n_buckets; |
6566 | ||
7395c052 NZ |
6567 | error = (ofproto->ofproto_class->group_get_stats |
6568 | ? ofproto->ofproto_class->group_get_stats(group, &ogs) | |
6569 | : EOPNOTSUPP); | |
6570 | if (error) { | |
7395c052 NZ |
6571 | ogs.packet_count = UINT64_MAX; |
6572 | ogs.byte_count = UINT64_MAX; | |
7395c052 NZ |
6573 | memset(ogs.bucket_stats, 0xff, |
6574 | ogs.n_buckets * sizeof *ogs.bucket_stats); | |
6575 | } | |
6576 | ||
6577 | ogs.group_id = group->group_id; | |
6578 | calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec); | |
6579 | ||
6580 | ofputil_append_group_stats(replies, &ogs); | |
646b2a9c SH |
6581 | |
6582 | free(ogs.bucket_stats); | |
7395c052 NZ |
6583 | } |
6584 | ||
19187a71 BP |
6585 | static void |
6586 | handle_group_request(struct ofconn *ofconn, | |
6587 | const struct ofp_header *request, uint32_t group_id, | |
ca6ba700 | 6588 | void (*cb)(struct ofgroup *, struct ovs_list *replies)) |
cc099268 | 6589 | OVS_EXCLUDED(ofproto_mutex) |
7395c052 NZ |
6590 | { |
6591 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
7395c052 | 6592 | struct ofgroup *group; |
ca6ba700 | 6593 | struct ovs_list replies; |
7395c052 NZ |
6594 | |
6595 | ofpmp_init(&replies, request); | |
cc099268 JR |
6596 | /* Must exclude modifications to guarantee iterating groups. */ |
6597 | ovs_mutex_lock(&ofproto_mutex); | |
7395c052 | 6598 | if (group_id == OFPG_ALL) { |
db88b35c | 6599 | CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) { |
19187a71 | 6600 | cb(group, &replies); |
7395c052 | 6601 | } |
7395c052 | 6602 | } else { |
5d08a275 | 6603 | group = ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX); |
db88b35c | 6604 | if (group) { |
19187a71 | 6605 | cb(group, &replies); |
7395c052 NZ |
6606 | } |
6607 | } | |
cc099268 | 6608 | ovs_mutex_unlock(&ofproto_mutex); |
7395c052 | 6609 | ofconn_send_replies(ofconn, &replies); |
7395c052 NZ |
6610 | } |
6611 | ||
6612 | static enum ofperr | |
19187a71 BP |
6613 | handle_group_stats_request(struct ofconn *ofconn, |
6614 | const struct ofp_header *request) | |
7395c052 | 6615 | { |
19187a71 BP |
6616 | uint32_t group_id; |
6617 | enum ofperr error; | |
7395c052 | 6618 | |
19187a71 BP |
6619 | error = ofputil_decode_group_stats_request(request, &group_id); |
6620 | if (error) { | |
6621 | return error; | |
7395c052 | 6622 | } |
7395c052 | 6623 | |
19187a71 BP |
6624 | handle_group_request(ofconn, request, group_id, append_group_stats); |
6625 | return 0; | |
6626 | } | |
6627 | ||
6628 | static void | |
ca6ba700 | 6629 | append_group_desc(struct ofgroup *group, struct ovs_list *replies) |
19187a71 BP |
6630 | { |
6631 | struct ofputil_group_desc gds; | |
7395c052 | 6632 | |
19187a71 BP |
6633 | gds.group_id = group->group_id; |
6634 | gds.type = group->type; | |
53eb84a5 SH |
6635 | gds.props = group->props; |
6636 | ||
19187a71 BP |
6637 | ofputil_append_group_desc_reply(&gds, &group->buckets, replies); |
6638 | } | |
6639 | ||
6640 | static enum ofperr | |
6641 | handle_group_desc_stats_request(struct ofconn *ofconn, | |
6642 | const struct ofp_header *request) | |
6643 | { | |
6644 | handle_group_request(ofconn, request, | |
6645 | ofputil_decode_group_desc_request(request), | |
6646 | append_group_desc); | |
7395c052 NZ |
6647 | return 0; |
6648 | } | |
6649 | ||
6650 | static enum ofperr | |
6651 | handle_group_features_stats_request(struct ofconn *ofconn, | |
6652 | const struct ofp_header *request) | |
6653 | { | |
6654 | struct ofproto *p = ofconn_get_ofproto(ofconn); | |
6655 | struct ofpbuf *msg; | |
6656 | ||
6657 | msg = ofputil_encode_group_features_reply(&p->ogf, request); | |
6658 | if (msg) { | |
6659 | ofconn_send_reply(ofconn, msg); | |
6660 | } | |
6661 | ||
6662 | return 0; | |
6663 | } | |
6664 | ||
56085be5 | 6665 | static void |
e016fb63 BP |
6666 | put_queue_get_config_reply(struct ofport *port, uint32_t queue, |
6667 | struct ovs_list *replies) | |
e8f9a7bb | 6668 | { |
e016fb63 | 6669 | struct ofputil_queue_config qc; |
e8f9a7bb | 6670 | |
e016fb63 BP |
6671 | /* None of the existing queues have compatible properties, so we hard-code |
6672 | * omitting min_rate and max_rate. */ | |
6673 | qc.port = port->ofp_port; | |
6674 | qc.queue = queue; | |
6675 | qc.min_rate = UINT16_MAX; | |
6676 | qc.max_rate = UINT16_MAX; | |
6677 | ofputil_append_queue_get_config_reply(&qc, replies); | |
6678 | } | |
e8f9a7bb | 6679 | |
e016fb63 BP |
6680 | static int |
6681 | handle_queue_get_config_request_for_port(struct ofport *port, uint32_t queue, | |
6682 | struct ovs_list *replies) | |
6683 | { | |
6684 | struct smap details = SMAP_INITIALIZER(&details); | |
6685 | if (queue != OFPQ_ALL) { | |
6686 | int error = netdev_get_queue(port->netdev, queue, &details); | |
6687 | switch (error) { | |
6688 | case 0: | |
6689 | put_queue_get_config_reply(port, queue, replies); | |
6690 | break; | |
6691 | case EOPNOTSUPP: | |
6692 | case EINVAL: | |
6693 | return OFPERR_OFPQOFC_BAD_QUEUE; | |
6694 | default: | |
6695 | return OFPERR_NXQOFC_QUEUE_ERROR; | |
6696 | } | |
6697 | } else { | |
6698 | struct netdev_queue_dump queue_dump; | |
6699 | uint32_t queue_id; | |
6700 | ||
6701 | NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, | |
6702 | port->netdev) { | |
6703 | put_queue_get_config_reply(port, queue_id, replies); | |
6704 | } | |
6705 | } | |
6706 | smap_destroy(&details); | |
6707 | return 0; | |
56085be5 BP |
6708 | } |
6709 | ||
6710 | static enum ofperr | |
6711 | handle_queue_get_config_request(struct ofconn *ofconn, | |
6712 | const struct ofp_header *oh) | |
6713 | { | |
e016fb63 BP |
6714 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
6715 | struct ovs_list replies; | |
6716 | struct ofport *port; | |
6717 | ofp_port_t req_port; | |
6718 | uint32_t req_queue; | |
6719 | enum ofperr error; | |
6720 | ||
6721 | error = ofputil_decode_queue_get_config_request(oh, &req_port, &req_queue); | |
6722 | if (error) { | |
6723 | return error; | |
6724 | } | |
6725 | ||
6726 | ofputil_start_queue_get_config_reply(oh, &replies); | |
6727 | if (req_port == OFPP_ANY) { | |
6728 | error = OFPERR_OFPQOFC_BAD_QUEUE; | |
6729 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { | |
6730 | if (!handle_queue_get_config_request_for_port(port, req_queue, | |
6731 | &replies)) { | |
6732 | error = 0; | |
6733 | } | |
6734 | } | |
6735 | } else { | |
6736 | port = ofproto_get_port(ofproto, req_port); | |
6737 | error = (port | |
6738 | ? handle_queue_get_config_request_for_port(port, req_queue, | |
6739 | &replies) | |
6740 | : OFPERR_OFPQOFC_BAD_PORT); | |
6741 | } | |
6742 | if (!error) { | |
6743 | ofconn_send_replies(ofconn, &replies); | |
6744 | } else { | |
6745 | ofpbuf_list_delete(&replies); | |
6746 | } | |
6747 | ||
6748 | return error; | |
e8f9a7bb VG |
6749 | } |
6750 | ||
809c7548 | 6751 | static enum ofperr |
63eded98 | 6752 | init_group(struct ofproto *ofproto, const struct ofputil_group_mod *gm, |
5d08a275 | 6753 | ovs_version_t version, struct ofgroup **ofgroup) |
809c7548 RW |
6754 | { |
6755 | enum ofperr error; | |
eaf004bd | 6756 | const long long int now = time_msec(); |
809c7548 RW |
6757 | |
6758 | if (gm->group_id > OFPG_MAX) { | |
6759 | return OFPERR_OFPGMFC_INVALID_GROUP; | |
6760 | } | |
6761 | if (gm->type > OFPGT11_FF) { | |
6762 | return OFPERR_OFPGMFC_BAD_TYPE; | |
6763 | } | |
6764 | ||
6765 | *ofgroup = ofproto->ofproto_class->group_alloc(); | |
6766 | if (!*ofgroup) { | |
6767 | VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name); | |
6768 | return OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
6769 | } | |
6770 | ||
5d08a275 | 6771 | *CONST_CAST(struct ofproto **, &(*ofgroup)->ofproto) = ofproto; |
eaf004bd AZ |
6772 | *CONST_CAST(uint32_t *, &((*ofgroup)->group_id)) = gm->group_id; |
6773 | *CONST_CAST(enum ofp11_group_type *, &(*ofgroup)->type) = gm->type; | |
6774 | *CONST_CAST(long long int *, &((*ofgroup)->created)) = now; | |
6775 | *CONST_CAST(long long int *, &((*ofgroup)->modified)) = now; | |
809c7548 | 6776 | ovs_refcount_init(&(*ofgroup)->ref_count); |
cc099268 | 6777 | (*ofgroup)->being_deleted = false; |
809c7548 | 6778 | |
db88b35c JR |
6779 | ovs_list_init(CONST_CAST(struct ovs_list *, &(*ofgroup)->buckets)); |
6780 | ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *, | |
6781 | &(*ofgroup)->buckets), | |
6782 | &gm->buckets, NULL); | |
63eded98 | 6783 | |
eaf004bd | 6784 | *CONST_CAST(uint32_t *, &(*ofgroup)->n_buckets) = |
417e7e66 | 6785 | ovs_list_size(&(*ofgroup)->buckets); |
809c7548 | 6786 | |
e8dba719 JR |
6787 | ofputil_group_properties_copy(CONST_CAST(struct ofputil_group_props *, |
6788 | &(*ofgroup)->props), | |
6789 | &gm->props); | |
cc099268 | 6790 | rule_collection_init(&(*ofgroup)->rules); |
bc65c25a | 6791 | |
5d08a275 JR |
6792 | /* Make group visible from 'version'. */ |
6793 | (*ofgroup)->versions = VERSIONS_INITIALIZER(version, | |
6794 | OVS_VERSION_NOT_REMOVED); | |
6795 | ||
809c7548 RW |
6796 | /* Construct called BEFORE any locks are held. */ |
6797 | error = ofproto->ofproto_class->group_construct(*ofgroup); | |
6798 | if (error) { | |
e8dba719 JR |
6799 | ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props *, |
6800 | &(*ofgroup)->props)); | |
db88b35c JR |
6801 | ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list *, |
6802 | &(*ofgroup)->buckets)); | |
809c7548 RW |
6803 | ofproto->ofproto_class->group_dealloc(*ofgroup); |
6804 | } | |
6805 | return error; | |
6806 | } | |
6807 | ||
6c5b90ce BP |
6808 | /* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to |
6809 | * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on | |
6810 | * failure. */ | |
7395c052 | 6811 | static enum ofperr |
2b0b0b80 | 6812 | add_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm) |
cc099268 | 6813 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 6814 | { |
7395c052 NZ |
6815 | enum ofperr error; |
6816 | ||
2b0b0b80 JR |
6817 | if (ofproto_group_exists(ofproto, ogm->gm.group_id)) { |
6818 | return OFPERR_OFPGMFC_GROUP_EXISTS; | |
7395c052 NZ |
6819 | } |
6820 | ||
2b0b0b80 JR |
6821 | if (ofproto->n_groups[ogm->gm.type] |
6822 | >= ofproto->ogf.max_groups[ogm->gm.type]) { | |
6823 | return OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
7395c052 NZ |
6824 | } |
6825 | ||
2b0b0b80 | 6826 | /* Allocate new group and initialize it. */ |
5d08a275 | 6827 | error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group); |
2b0b0b80 JR |
6828 | if (!error) { |
6829 | /* Insert new group. */ | |
6830 | cmap_insert(&ofproto->groups, &ogm->new_group->cmap_node, | |
6831 | hash_int(ogm->new_group->group_id, 0)); | |
6832 | ofproto->n_groups[ogm->new_group->type]++; | |
7395c052 | 6833 | } |
7395c052 NZ |
6834 | return error; |
6835 | } | |
6836 | ||
fce4730c SH |
6837 | /* Adds all of the buckets from 'ofgroup' to 'new_ofgroup'. The buckets |
6838 | * already in 'new_ofgroup' will be placed just after the (copy of the) bucket | |
6839 | * in 'ofgroup' with bucket ID 'command_bucket_id'. Special | |
6840 | * 'command_bucket_id' values OFPG15_BUCKET_FIRST and OFPG15_BUCKET_LAST are | |
6841 | * also honored. */ | |
6842 | static enum ofperr | |
6843 | copy_buckets_for_insert_bucket(const struct ofgroup *ofgroup, | |
6844 | struct ofgroup *new_ofgroup, | |
6845 | uint32_t command_bucket_id) | |
6846 | { | |
6847 | struct ofputil_bucket *last = NULL; | |
6848 | ||
6849 | if (command_bucket_id <= OFPG15_BUCKET_MAX) { | |
6850 | /* Check here to ensure that a bucket corresponding to | |
6851 | * command_bucket_id exists in the old bucket list. | |
6852 | * | |
6853 | * The subsequent search of below of new_ofgroup covers | |
6854 | * both buckets in the old bucket list and buckets added | |
6855 | * by the insert buckets group mod message this function processes. */ | |
6856 | if (!ofputil_bucket_find(&ofgroup->buckets, command_bucket_id)) { | |
6857 | return OFPERR_OFPGMFC_UNKNOWN_BUCKET; | |
6858 | } | |
6859 | ||
417e7e66 | 6860 | if (!ovs_list_is_empty(&new_ofgroup->buckets)) { |
fce4730c SH |
6861 | last = ofputil_bucket_list_back(&new_ofgroup->buckets); |
6862 | } | |
6863 | } | |
6864 | ||
db88b35c JR |
6865 | ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *, |
6866 | &new_ofgroup->buckets), | |
6867 | &ofgroup->buckets, NULL); | |
fce4730c | 6868 | |
23a31238 | 6869 | if (ofputil_bucket_check_duplicate_id(&new_ofgroup->buckets)) { |
d2873d53 | 6870 | VLOG_INFO_RL(&rl, "Duplicate bucket id"); |
fce4730c SH |
6871 | return OFPERR_OFPGMFC_BUCKET_EXISTS; |
6872 | } | |
6873 | ||
6874 | /* Rearrange list according to command_bucket_id */ | |
6875 | if (command_bucket_id == OFPG15_BUCKET_LAST) { | |
417e7e66 | 6876 | if (!ovs_list_is_empty(&ofgroup->buckets)) { |
8e19e655 BP |
6877 | struct ofputil_bucket *new_first; |
6878 | const struct ofputil_bucket *first; | |
fce4730c | 6879 | |
8e19e655 BP |
6880 | first = ofputil_bucket_list_front(&ofgroup->buckets); |
6881 | new_first = ofputil_bucket_find(&new_ofgroup->buckets, | |
6882 | first->bucket_id); | |
fce4730c | 6883 | |
417e7e66 | 6884 | ovs_list_splice(new_ofgroup->buckets.next, &new_first->list_node, |
db88b35c JR |
6885 | CONST_CAST(struct ovs_list *, |
6886 | &new_ofgroup->buckets)); | |
8e19e655 | 6887 | } |
fce4730c SH |
6888 | } else if (command_bucket_id <= OFPG15_BUCKET_MAX && last) { |
6889 | struct ofputil_bucket *after; | |
6890 | ||
6891 | /* Presence of bucket is checked above so after should never be NULL */ | |
6892 | after = ofputil_bucket_find(&new_ofgroup->buckets, command_bucket_id); | |
6893 | ||
417e7e66 | 6894 | ovs_list_splice(after->list_node.next, new_ofgroup->buckets.next, |
fce4730c SH |
6895 | last->list_node.next); |
6896 | } | |
6897 | ||
6898 | return 0; | |
6899 | } | |
6900 | ||
6901 | /* Appends all of the a copy of all the buckets from 'ofgroup' to 'new_ofgroup' | |
6902 | * with the exception of the bucket whose bucket id is 'command_bucket_id'. | |
6903 | * Special 'command_bucket_id' values OFPG15_BUCKET_FIRST, OFPG15_BUCKET_LAST | |
6904 | * and OFPG15_BUCKET_ALL are also honored. */ | |
6905 | static enum ofperr | |
6906 | copy_buckets_for_remove_bucket(const struct ofgroup *ofgroup, | |
6907 | struct ofgroup *new_ofgroup, | |
6908 | uint32_t command_bucket_id) | |
6909 | { | |
6910 | const struct ofputil_bucket *skip = NULL; | |
6911 | ||
6912 | if (command_bucket_id == OFPG15_BUCKET_ALL) { | |
6913 | return 0; | |
6914 | } | |
6915 | ||
6916 | if (command_bucket_id == OFPG15_BUCKET_FIRST) { | |
417e7e66 | 6917 | if (!ovs_list_is_empty(&ofgroup->buckets)) { |
fce4730c SH |
6918 | skip = ofputil_bucket_list_front(&ofgroup->buckets); |
6919 | } | |
6920 | } else if (command_bucket_id == OFPG15_BUCKET_LAST) { | |
417e7e66 | 6921 | if (!ovs_list_is_empty(&ofgroup->buckets)) { |
fce4730c SH |
6922 | skip = ofputil_bucket_list_back(&ofgroup->buckets); |
6923 | } | |
6924 | } else { | |
6925 | skip = ofputil_bucket_find(&ofgroup->buckets, command_bucket_id); | |
6926 | if (!skip) { | |
6927 | return OFPERR_OFPGMFC_UNKNOWN_BUCKET; | |
6928 | } | |
6929 | } | |
6930 | ||
db88b35c JR |
6931 | ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *, |
6932 | &new_ofgroup->buckets), | |
6933 | &ofgroup->buckets, skip); | |
fce4730c SH |
6934 | |
6935 | return 0; | |
6936 | } | |
6937 | ||
6938 | /* Implements OFPGC11_MODIFY, OFPGC15_INSERT_BUCKET and | |
6939 | * OFPGC15_REMOVE_BUCKET. Returns 0 on success or an OpenFlow error code | |
6c5b90ce | 6940 | * on failure. |
7395c052 | 6941 | * |
809c7548 RW |
6942 | * Note that the group is re-created and then replaces the old group in |
6943 | * ofproto's ofgroup hash map. Thus, the group is never altered while users of | |
6c5b90ce | 6944 | * the xlate module hold a pointer to the group. */ |
7395c052 | 6945 | static enum ofperr |
2b0b0b80 | 6946 | modify_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm) |
cc099268 | 6947 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 6948 | { |
2b0b0b80 JR |
6949 | struct ofgroup *old_group; /* Modified group. */ |
6950 | struct ofgroup *new_group; | |
7395c052 NZ |
6951 | enum ofperr error; |
6952 | ||
5d08a275 JR |
6953 | old_group = ofproto_group_lookup__(ofproto, ogm->gm.group_id, |
6954 | OVS_VERSION_MAX); | |
2b0b0b80 JR |
6955 | if (!old_group) { |
6956 | return OFPERR_OFPGMFC_UNKNOWN_GROUP; | |
7395c052 NZ |
6957 | } |
6958 | ||
2b0b0b80 JR |
6959 | if (old_group->type != ogm->gm.type |
6960 | && (ofproto->n_groups[ogm->gm.type] | |
6961 | >= ofproto->ogf.max_groups[ogm->gm.type])) { | |
6962 | return OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
7395c052 | 6963 | } |
809c7548 | 6964 | |
5d08a275 | 6965 | error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group); |
2b0b0b80 JR |
6966 | if (error) { |
6967 | return error; | |
7395c052 | 6968 | } |
2b0b0b80 | 6969 | new_group = ogm->new_group; |
7395c052 | 6970 | |
fce4730c | 6971 | /* Manipulate bucket list for bucket commands */ |
2b0b0b80 JR |
6972 | if (ogm->gm.command == OFPGC15_INSERT_BUCKET) { |
6973 | error = copy_buckets_for_insert_bucket(old_group, new_group, | |
6974 | ogm->gm.command_bucket_id); | |
6975 | } else if (ogm->gm.command == OFPGC15_REMOVE_BUCKET) { | |
6976 | error = copy_buckets_for_remove_bucket(old_group, new_group, | |
6977 | ogm->gm.command_bucket_id); | |
fce4730c SH |
6978 | } |
6979 | if (error) { | |
6980 | goto out; | |
6981 | } | |
6982 | ||
809c7548 | 6983 | /* The group creation time does not change during modification. */ |
2b0b0b80 JR |
6984 | *CONST_CAST(long long int *, &(new_group->created)) = old_group->created; |
6985 | *CONST_CAST(long long int *, &(new_group->modified)) = time_msec(); | |
7395c052 | 6986 | |
2b0b0b80 | 6987 | group_collection_add(&ogm->old_groups, old_group); |
db88b35c | 6988 | |
5d08a275 JR |
6989 | /* Mark the old group for deletion. */ |
6990 | versions_set_remove_version(&old_group->versions, ogm->version); | |
6991 | /* Insert replacement group. */ | |
6992 | cmap_insert(&ofproto->groups, &new_group->cmap_node, | |
6993 | hash_int(new_group->group_id, 0)); | |
cc099268 | 6994 | /* Transfer rules. */ |
2b0b0b80 | 6995 | rule_collection_move(&new_group->rules, &old_group->rules); |
db88b35c | 6996 | |
2b0b0b80 JR |
6997 | if (old_group->type != new_group->type) { |
6998 | ofproto->n_groups[old_group->type]--; | |
6999 | ofproto->n_groups[new_group->type]++; | |
7395c052 | 7000 | } |
2b0b0b80 | 7001 | return 0; |
7395c052 | 7002 | |
809c7548 | 7003 | out: |
2b0b0b80 | 7004 | ofproto_group_unref(new_group); |
7395c052 NZ |
7005 | return error; |
7006 | } | |
7007 | ||
88b87a36 JS |
7008 | /* Implements the OFPGC11_ADD_OR_MOD command which creates the group when it does not |
7009 | * exist yet and modifies it otherwise */ | |
7010 | static enum ofperr | |
2b0b0b80 JR |
7011 | add_or_modify_group_start(struct ofproto *ofproto, |
7012 | struct ofproto_group_mod *ogm) | |
cc099268 | 7013 | OVS_REQUIRES(ofproto_mutex) |
88b87a36 | 7014 | { |
88b87a36 | 7015 | enum ofperr error; |
88b87a36 | 7016 | |
2b0b0b80 JR |
7017 | if (!ofproto_group_exists(ofproto, ogm->gm.group_id)) { |
7018 | error = add_group_start(ofproto, ogm); | |
88b87a36 | 7019 | } else { |
2b0b0b80 | 7020 | error = modify_group_start(ofproto, ogm); |
88b87a36 | 7021 | } |
cc099268 | 7022 | |
88b87a36 JS |
7023 | return error; |
7024 | } | |
7025 | ||
7395c052 | 7026 | static void |
5d08a275 JR |
7027 | delete_group_start(struct ofproto *ofproto, ovs_version_t version, |
7028 | struct group_collection *groups, struct ofgroup *group) | |
cc099268 | 7029 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 7030 | { |
2b0b0b80 | 7031 | /* Makes flow deletion code leave the rule pointers in 'group->rules' |
cc099268 JR |
7032 | * intact, so that we can later refer to the rules deleted due to the group |
7033 | * deletion. Rule pointers will be removed from all other groups, if any, | |
7034 | * so we will never try to delete the same rule twice. */ | |
2b0b0b80 JR |
7035 | group->being_deleted = true; |
7036 | ||
7037 | /* Mark all the referring groups for deletion. */ | |
5d08a275 | 7038 | delete_flows_start__(ofproto, version, &group->rules); |
2b0b0b80 | 7039 | group_collection_add(groups, group); |
5d08a275 | 7040 | versions_set_remove_version(&group->versions, version); |
2b0b0b80 JR |
7041 | ofproto->n_groups[group->type]--; |
7042 | } | |
276f6518 | 7043 | |
2b0b0b80 JR |
7044 | static void |
7045 | delete_group_finish(struct ofproto *ofproto, struct ofgroup *group) | |
7046 | OVS_REQUIRES(ofproto_mutex) | |
7047 | { | |
7048 | /* Finish deletion of all flow entries containing this group in a group | |
7049 | * action. */ | |
7050 | delete_flows_finish__(ofproto, &group->rules, OFPRR_GROUP_DELETE, NULL); | |
276f6518 | 7051 | |
5d08a275 | 7052 | /* Group removal is postponed by the caller. */ |
7395c052 NZ |
7053 | } |
7054 | ||
6c5b90ce | 7055 | /* Implements OFPGC11_DELETE. */ |
7395c052 | 7056 | static void |
2b0b0b80 | 7057 | delete_groups_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm) |
cc099268 | 7058 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 7059 | { |
2b0b0b80 | 7060 | struct ofgroup *group; |
7395c052 | 7061 | |
2b0b0b80 JR |
7062 | if (ogm->gm.group_id == OFPG_ALL) { |
7063 | CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) { | |
5d08a275 JR |
7064 | if (versions_visible_in_version(&group->versions, ogm->version)) { |
7065 | delete_group_start(ofproto, ogm->version, &ogm->old_groups, | |
7066 | group); | |
7067 | } | |
7395c052 NZ |
7068 | } |
7069 | } else { | |
5d08a275 JR |
7070 | group = ofproto_group_lookup__(ofproto, ogm->gm.group_id, ogm->version); |
7071 | if (group) { | |
7072 | delete_group_start(ofproto, ogm->version, &ogm->old_groups, group); | |
7395c052 NZ |
7073 | } |
7074 | } | |
7395c052 NZ |
7075 | } |
7076 | ||
7077 | static enum ofperr | |
2b0b0b80 JR |
7078 | ofproto_group_mod_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm) |
7079 | OVS_REQUIRES(ofproto_mutex) | |
7395c052 | 7080 | { |
7395c052 NZ |
7081 | enum ofperr error; |
7082 | ||
2b0b0b80 JR |
7083 | ogm->new_group = NULL; |
7084 | group_collection_init(&ogm->old_groups); | |
7395c052 | 7085 | |
2b0b0b80 | 7086 | switch (ogm->gm.command) { |
7395c052 | 7087 | case OFPGC11_ADD: |
2b0b0b80 | 7088 | error = add_group_start(ofproto, ogm); |
3c35db62 | 7089 | break; |
7395c052 NZ |
7090 | |
7091 | case OFPGC11_MODIFY: | |
2b0b0b80 | 7092 | error = modify_group_start(ofproto, ogm); |
3c35db62 | 7093 | break; |
7395c052 | 7094 | |
88b87a36 | 7095 | case OFPGC11_ADD_OR_MOD: |
2b0b0b80 | 7096 | error = add_or_modify_group_start(ofproto, ogm); |
88b87a36 JS |
7097 | break; |
7098 | ||
7395c052 | 7099 | case OFPGC11_DELETE: |
2b0b0b80 | 7100 | delete_groups_start(ofproto, ogm); |
3c35db62 NR |
7101 | error = 0; |
7102 | break; | |
7395c052 | 7103 | |
fce4730c | 7104 | case OFPGC15_INSERT_BUCKET: |
2b0b0b80 | 7105 | error = modify_group_start(ofproto, ogm); |
3c35db62 | 7106 | break; |
fce4730c SH |
7107 | |
7108 | case OFPGC15_REMOVE_BUCKET: | |
2b0b0b80 | 7109 | error = modify_group_start(ofproto, ogm); |
3c35db62 | 7110 | break; |
fce4730c | 7111 | |
7395c052 | 7112 | default: |
2b0b0b80 | 7113 | if (ogm->gm.command > OFPGC11_DELETE) { |
d2873d53 | 7114 | VLOG_INFO_RL(&rl, "%s: Invalid group_mod command type %d", |
2b0b0b80 | 7115 | ofproto->name, ogm->gm.command); |
7395c052 | 7116 | } |
63eded98 | 7117 | error = OFPERR_OFPGMFC_BAD_COMMAND; |
2b0b0b80 | 7118 | break; |
7395c052 | 7119 | } |
2b0b0b80 JR |
7120 | return error; |
7121 | } | |
3c35db62 | 7122 | |
25070e04 JR |
7123 | static void |
7124 | ofproto_group_mod_revert(struct ofproto *ofproto, | |
7125 | struct ofproto_group_mod *ogm) | |
7126 | OVS_REQUIRES(ofproto_mutex) | |
7127 | { | |
7128 | struct ofgroup *new_group = ogm->new_group; | |
7129 | struct ofgroup *old_group; | |
7130 | ||
7131 | /* Restore replaced or deleted groups. */ | |
7132 | GROUP_COLLECTION_FOR_EACH (old_group, &ogm->old_groups) { | |
7133 | ofproto->n_groups[old_group->type]++; | |
7134 | if (new_group) { | |
7135 | ovs_assert(group_collection_n(&ogm->old_groups) == 1); | |
7136 | /* Transfer rules back. */ | |
7137 | rule_collection_move(&old_group->rules, &new_group->rules); | |
7138 | } else { | |
7139 | old_group->being_deleted = false; | |
7140 | /* Revert rule deletion. */ | |
7141 | delete_flows_revert__(ofproto, &old_group->rules); | |
7142 | } | |
7143 | /* Restore visibility. */ | |
7144 | versions_set_remove_version(&old_group->versions, | |
7145 | OVS_VERSION_NOT_REMOVED); | |
7146 | } | |
7147 | if (new_group) { | |
7148 | /* Remove the new group immediately. It was never visible to | |
7149 | * lookups. */ | |
7150 | cmap_remove(&ofproto->groups, &new_group->cmap_node, | |
7151 | hash_int(new_group->group_id, 0)); | |
7152 | ofproto->n_groups[new_group->type]--; | |
7153 | ofproto_group_unref(new_group); | |
7154 | } | |
7155 | } | |
7156 | ||
2b0b0b80 JR |
7157 | static void |
7158 | ofproto_group_mod_finish(struct ofproto *ofproto, | |
7159 | struct ofproto_group_mod *ogm, | |
7160 | const struct openflow_mod_requester *req) | |
7161 | OVS_REQUIRES(ofproto_mutex) | |
7162 | { | |
7163 | struct ofgroup *new_group = ogm->new_group; | |
7164 | struct ofgroup *old_group; | |
7165 | ||
ccb3bc08 JR |
7166 | if (new_group && group_collection_n(&ogm->old_groups) && |
7167 | ofproto->ofproto_class->group_modify) { | |
2b0b0b80 JR |
7168 | /* Modify a group. */ |
7169 | ovs_assert(group_collection_n(&ogm->old_groups) == 1); | |
2b0b0b80 JR |
7170 | |
7171 | /* XXX: OK to lose old group's stats? */ | |
7172 | ofproto->ofproto_class->group_modify(new_group); | |
5d08a275 | 7173 | } |
2b0b0b80 | 7174 | |
5d08a275 JR |
7175 | /* Delete old groups. */ |
7176 | GROUP_COLLECTION_FOR_EACH(old_group, &ogm->old_groups) { | |
7177 | delete_group_finish(ofproto, old_group); | |
2b0b0b80 | 7178 | } |
5d08a275 | 7179 | remove_groups_postponed(&ogm->old_groups); |
2b0b0b80 JR |
7180 | |
7181 | if (req) { | |
3c35db62 | 7182 | struct ofputil_requestforward rf; |
2b0b0b80 | 7183 | rf.xid = req->request->xid; |
3c35db62 | 7184 | rf.reason = OFPRFR_GROUP_MOD; |
2b0b0b80 JR |
7185 | rf.group_mod = &ogm->gm; |
7186 | connmgr_send_requestforward(ofproto->connmgr, req->ofconn, &rf); | |
7187 | } | |
7188 | } | |
7189 | ||
7190 | /* Delete all groups from 'ofproto'. | |
7191 | * | |
7192 | * This is intended for use within an ofproto provider's 'destruct' | |
7193 | * function. */ | |
7194 | void | |
7195 | ofproto_group_delete_all(struct ofproto *ofproto) | |
7196 | OVS_EXCLUDED(ofproto_mutex) | |
7197 | { | |
7198 | struct ofproto_group_mod ogm; | |
7199 | ||
7200 | ogm.gm.command = OFPGC11_DELETE; | |
7201 | ogm.gm.group_id = OFPG_ALL; | |
7202 | ||
7203 | ovs_mutex_lock(&ofproto_mutex); | |
5d08a275 | 7204 | ogm.version = ofproto->tables_version + 1; |
2b0b0b80 | 7205 | ofproto_group_mod_start(ofproto, &ogm); |
5d08a275 | 7206 | ofproto_bump_tables_version(ofproto); |
2b0b0b80 JR |
7207 | ofproto_group_mod_finish(ofproto, &ogm, NULL); |
7208 | ovs_mutex_unlock(&ofproto_mutex); | |
7209 | } | |
7210 | ||
7211 | static enum ofperr | |
7212 | handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
7213 | OVS_EXCLUDED(ofproto_mutex) | |
7214 | { | |
7215 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
7216 | struct ofproto_group_mod ogm; | |
7217 | enum ofperr error; | |
7218 | ||
7219 | error = reject_slave_controller(ofconn); | |
7220 | if (error) { | |
7221 | return error; | |
7222 | } | |
7223 | ||
7224 | error = ofputil_decode_group_mod(oh, &ogm.gm); | |
7225 | if (error) { | |
7226 | return error; | |
3c35db62 | 7227 | } |
2b0b0b80 JR |
7228 | |
7229 | ovs_mutex_lock(&ofproto_mutex); | |
5d08a275 | 7230 | ogm.version = ofproto->tables_version + 1; |
2b0b0b80 JR |
7231 | error = ofproto_group_mod_start(ofproto, &ogm); |
7232 | if (!error) { | |
7233 | struct openflow_mod_requester req = { ofconn, oh }; | |
5d08a275 JR |
7234 | |
7235 | ofproto_bump_tables_version(ofproto); | |
2b0b0b80 | 7236 | ofproto_group_mod_finish(ofproto, &ogm, &req); |
2c7ee524 | 7237 | ofmonitor_flush(ofproto->connmgr); |
2b0b0b80 | 7238 | } |
2b0b0b80 JR |
7239 | ovs_mutex_unlock(&ofproto_mutex); |
7240 | ||
75868d0e | 7241 | ofputil_uninit_group_mod(&ogm.gm); |
63eded98 | 7242 | |
3c35db62 | 7243 | return error; |
7395c052 NZ |
7244 | } |
7245 | ||
3c1bb396 BP |
7246 | enum ofputil_table_miss |
7247 | ofproto_table_get_miss_config(const struct ofproto *ofproto, uint8_t table_id) | |
6d328fa2 | 7248 | { |
82c22d34 BP |
7249 | enum ofputil_table_miss miss; |
7250 | ||
7251 | atomic_read_relaxed(&ofproto->tables[table_id].miss_config, &miss); | |
7252 | return miss; | |
7253 | } | |
7254 | ||
7255 | static void | |
7256 | table_mod__(struct oftable *oftable, | |
de7d3c07 | 7257 | const struct ofputil_table_mod *tm) |
82c22d34 | 7258 | { |
de7d3c07 | 7259 | if (tm->miss == OFPUTIL_TABLE_MISS_DEFAULT) { |
82c22d34 BP |
7260 | /* This is how an OFPT_TABLE_MOD decodes if it doesn't specify any |
7261 | * table-miss configuration (because the protocol used doesn't have | |
7262 | * such a concept), so there's nothing to do. */ | |
7263 | } else { | |
de7d3c07 | 7264 | atomic_store_relaxed(&oftable->miss_config, tm->miss); |
82c22d34 BP |
7265 | } |
7266 | ||
7267 | unsigned int new_eviction = oftable->eviction; | |
de7d3c07 | 7268 | if (tm->eviction == OFPUTIL_TABLE_EVICTION_ON) { |
82c22d34 | 7269 | new_eviction |= EVICTION_OPENFLOW; |
de7d3c07 | 7270 | } else if (tm->eviction == OFPUTIL_TABLE_EVICTION_OFF) { |
82c22d34 BP |
7271 | new_eviction &= ~EVICTION_OPENFLOW; |
7272 | } | |
afcea9ea | 7273 | |
82c22d34 BP |
7274 | if (new_eviction != oftable->eviction) { |
7275 | ovs_mutex_lock(&ofproto_mutex); | |
7276 | oftable_configure_eviction(oftable, new_eviction, | |
7277 | oftable->eviction_fields, | |
7278 | oftable->n_eviction_fields); | |
7279 | ovs_mutex_unlock(&ofproto_mutex); | |
7280 | } | |
de7d3c07 SJ |
7281 | |
7282 | if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) { | |
7283 | ovs_mutex_lock(&ofproto_mutex); | |
de7d3c07 SJ |
7284 | oftable->vacancy_down = tm->table_vacancy.vacancy_down; |
7285 | oftable->vacancy_up = tm->table_vacancy.vacancy_up; | |
6c6eedc5 SJ |
7286 | if (tm->vacancy == OFPUTIL_TABLE_VACANCY_OFF) { |
7287 | oftable->vacancy_event = 0; | |
7288 | } else if (!oftable->vacancy_event) { | |
7289 | uint8_t vacancy = oftable_vacancy(oftable); | |
7290 | oftable->vacancy_event = (vacancy < oftable->vacancy_up | |
7291 | ? OFPTR_VACANCY_UP | |
7292 | : OFPTR_VACANCY_DOWN); | |
7293 | } | |
de7d3c07 SJ |
7294 | ovs_mutex_unlock(&ofproto_mutex); |
7295 | } | |
6d328fa2 SH |
7296 | } |
7297 | ||
67761761 SH |
7298 | static enum ofperr |
7299 | table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm) | |
7300 | { | |
3c1bb396 | 7301 | if (!check_table_id(ofproto, tm->table_id)) { |
67761761 | 7302 | return OFPERR_OFPTMFC_BAD_TABLE; |
82c22d34 BP |
7303 | } |
7304 | ||
7305 | /* Don't allow the eviction flags to be changed (except to the only fixed | |
7306 | * value that OVS supports). OF1.4 says this is normal: "The | |
7307 | * OFPTMPT_EVICTION property usually cannot be modified using a | |
7308 | * OFP_TABLE_MOD request, because the eviction mechanism is switch | |
7309 | * defined". */ | |
7310 | if (tm->eviction_flags != UINT32_MAX | |
7311 | && tm->eviction_flags != OFPROTO_EVICTION_FLAGS) { | |
7312 | return OFPERR_OFPTMFC_BAD_CONFIG; | |
7313 | } | |
7314 | ||
7315 | if (tm->table_id == OFPTT_ALL) { | |
7316 | struct oftable *oftable; | |
7317 | OFPROTO_FOR_EACH_TABLE (oftable, ofproto) { | |
7318 | if (!(oftable->flags & (OFTABLE_HIDDEN | OFTABLE_READONLY))) { | |
de7d3c07 | 7319 | table_mod__(oftable, tm); |
3c1bb396 | 7320 | } |
3c1bb396 | 7321 | } |
82c22d34 BP |
7322 | } else { |
7323 | struct oftable *oftable = &ofproto->tables[tm->table_id]; | |
7324 | if (oftable->flags & OFTABLE_READONLY) { | |
7325 | return OFPERR_OFPTMFC_EPERM; | |
7326 | } | |
de7d3c07 | 7327 | table_mod__(oftable, tm); |
67761761 | 7328 | } |
82c22d34 | 7329 | |
67761761 SH |
7330 | return 0; |
7331 | } | |
7332 | ||
918f2b82 AZ |
7333 | static enum ofperr |
7334 | handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
7335 | { | |
67761761 | 7336 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
918f2b82 AZ |
7337 | struct ofputil_table_mod tm; |
7338 | enum ofperr error; | |
7339 | ||
7340 | error = reject_slave_controller(ofconn); | |
7341 | if (error) { | |
7342 | return error; | |
7343 | } | |
7344 | ||
7345 | error = ofputil_decode_table_mod(oh, &tm); | |
7346 | if (error) { | |
7347 | return error; | |
7348 | } | |
7349 | ||
67761761 | 7350 | return table_mod(ofproto, &tm); |
918f2b82 AZ |
7351 | } |
7352 | ||
5bacd5cd JR |
7353 | /* Free resources that may be allocated by ofproto_flow_mod_init(). */ |
7354 | void | |
7355 | ofproto_flow_mod_uninit(struct ofproto_flow_mod *ofm) | |
7356 | { | |
7357 | if (ofm->temp_rule) { | |
7358 | ofproto_rule_unref(ofm->temp_rule); | |
7359 | ofm->temp_rule = NULL; | |
7360 | } | |
7361 | if (ofm->criteria.version != OVS_VERSION_NOT_REMOVED) { | |
7362 | rule_criteria_destroy(&ofm->criteria); | |
7363 | } | |
7364 | if (ofm->conjs) { | |
7365 | free(ofm->conjs); | |
7366 | ofm->conjs = NULL; | |
7367 | ofm->n_conjs = 0; | |
7368 | } | |
7369 | } | |
7370 | ||
777af88d | 7371 | static enum ofperr |
5bacd5cd | 7372 | ofproto_flow_mod_init(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
2c7ee524 | 7373 | const struct ofputil_flow_mod *fm, struct rule *rule) |
5bacd5cd | 7374 | OVS_EXCLUDED(ofproto_mutex) |
777af88d | 7375 | { |
5bacd5cd JR |
7376 | enum ofperr error; |
7377 | ||
7378 | /* Forward flow mod fields we need later. */ | |
7379 | ofm->command = fm->command; | |
5bacd5cd JR |
7380 | ofm->modify_cookie = fm->modify_cookie; |
7381 | ||
7382 | ofm->modify_may_add_flow = (fm->new_cookie != OVS_BE64_MAX | |
7383 | && fm->cookie_mask == htonll(0)); | |
8b6987d7 JR |
7384 | /* Old flags must be kept when modifying a flow, but we still must |
7385 | * honor the reset counts flag if present in the flow mod. */ | |
7386 | ofm->modify_keep_counts = !(fm->flags & OFPUTIL_FF_RESET_COUNTS); | |
5bacd5cd JR |
7387 | |
7388 | /* Initialize state needed by ofproto_flow_mod_uninit(). */ | |
2c7ee524 | 7389 | ofm->temp_rule = rule; |
5bacd5cd JR |
7390 | ofm->criteria.version = OVS_VERSION_NOT_REMOVED; |
7391 | ofm->conjs = NULL; | |
7392 | ofm->n_conjs = 0; | |
7393 | ||
c184807c JR |
7394 | bool check_buffer_id = false; |
7395 | ||
5bacd5cd JR |
7396 | switch (ofm->command) { |
7397 | case OFPFC_ADD: | |
c184807c | 7398 | check_buffer_id = true; |
5bacd5cd JR |
7399 | error = add_flow_init(ofproto, ofm, fm); |
7400 | break; | |
7401 | case OFPFC_MODIFY: | |
c184807c | 7402 | check_buffer_id = true; |
5bacd5cd JR |
7403 | error = modify_flows_init_loose(ofproto, ofm, fm); |
7404 | break; | |
7405 | case OFPFC_MODIFY_STRICT: | |
c184807c | 7406 | check_buffer_id = true; |
5bacd5cd JR |
7407 | error = modify_flow_init_strict(ofproto, ofm, fm); |
7408 | break; | |
7409 | case OFPFC_DELETE: | |
7410 | error = delete_flows_init_loose(ofproto, ofm, fm); | |
7411 | break; | |
7412 | case OFPFC_DELETE_STRICT: | |
7413 | error = delete_flows_init_strict(ofproto, ofm, fm); | |
7414 | break; | |
7415 | default: | |
7416 | error = OFPERR_OFPFMFC_BAD_COMMAND; | |
7417 | break; | |
7418 | } | |
c184807c JR |
7419 | if (!error && check_buffer_id && fm->buffer_id != UINT32_MAX) { |
7420 | error = OFPERR_OFPBRC_BUFFER_UNKNOWN; | |
7421 | } | |
7422 | ||
cc099268 | 7423 | if (error) { |
5bacd5cd | 7424 | ofproto_flow_mod_uninit(ofm); |
cc099268 | 7425 | } |
5bacd5cd JR |
7426 | return error; |
7427 | } | |
cc099268 | 7428 | |
5bacd5cd JR |
7429 | static enum ofperr |
7430 | ofproto_flow_mod_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) | |
7431 | OVS_REQUIRES(ofproto_mutex) | |
7432 | { | |
7433 | enum ofperr error; | |
81dee635 | 7434 | |
5bacd5cd JR |
7435 | rule_collection_init(&ofm->old_rules); |
7436 | rule_collection_init(&ofm->new_rules); | |
81dee635 | 7437 | |
5a27991d | 7438 | switch (ofm->command) { |
1f42be1c | 7439 | case OFPFC_ADD: |
5bacd5cd JR |
7440 | error = add_flow_start(ofproto, ofm); |
7441 | break; | |
1f42be1c | 7442 | case OFPFC_MODIFY: |
5bacd5cd JR |
7443 | error = modify_flows_start_loose(ofproto, ofm); |
7444 | break; | |
1f42be1c | 7445 | case OFPFC_MODIFY_STRICT: |
5bacd5cd JR |
7446 | error = modify_flow_start_strict(ofproto, ofm); |
7447 | break; | |
1f42be1c | 7448 | case OFPFC_DELETE: |
5bacd5cd JR |
7449 | error = delete_flows_start_loose(ofproto, ofm); |
7450 | break; | |
1f42be1c | 7451 | case OFPFC_DELETE_STRICT: |
5bacd5cd JR |
7452 | error = delete_flow_start_strict(ofproto, ofm); |
7453 | break; | |
7454 | default: | |
7455 | OVS_NOT_REACHED(); | |
1f42be1c | 7456 | } |
5bacd5cd JR |
7457 | /* Release resources not needed after start. */ |
7458 | ofproto_flow_mod_uninit(ofm); | |
1f42be1c | 7459 | |
5bacd5cd JR |
7460 | if (error) { |
7461 | rule_collection_destroy(&ofm->old_rules); | |
7462 | rule_collection_destroy(&ofm->new_rules); | |
7463 | } | |
7464 | return error; | |
1f42be1c JR |
7465 | } |
7466 | ||
7467 | static void | |
8be00367 | 7468 | ofproto_flow_mod_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
1f42be1c JR |
7469 | OVS_REQUIRES(ofproto_mutex) |
7470 | { | |
5a27991d | 7471 | switch (ofm->command) { |
1f42be1c | 7472 | case OFPFC_ADD: |
8be00367 | 7473 | add_flow_revert(ofproto, ofm); |
1f42be1c JR |
7474 | break; |
7475 | ||
7476 | case OFPFC_MODIFY: | |
7477 | case OFPFC_MODIFY_STRICT: | |
8be00367 | 7478 | modify_flows_revert(ofproto, ofm); |
1f42be1c JR |
7479 | break; |
7480 | ||
7481 | case OFPFC_DELETE: | |
7482 | case OFPFC_DELETE_STRICT: | |
8be00367 | 7483 | delete_flows_revert(ofproto, ofm); |
1f42be1c JR |
7484 | break; |
7485 | ||
7486 | default: | |
7487 | break; | |
7488 | } | |
2c7ee524 | 7489 | |
5bacd5cd JR |
7490 | rule_collection_destroy(&ofm->old_rules); |
7491 | rule_collection_destroy(&ofm->new_rules); | |
1f42be1c JR |
7492 | } |
7493 | ||
7494 | static void | |
2c7ee524 | 7495 | ofproto_flow_mod_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 7496 | const struct openflow_mod_requester *req) |
1f42be1c JR |
7497 | OVS_REQUIRES(ofproto_mutex) |
7498 | { | |
5a27991d | 7499 | switch (ofm->command) { |
1f42be1c | 7500 | case OFPFC_ADD: |
8be00367 | 7501 | add_flow_finish(ofproto, ofm, req); |
1f42be1c JR |
7502 | break; |
7503 | ||
7504 | case OFPFC_MODIFY: | |
7505 | case OFPFC_MODIFY_STRICT: | |
8be00367 | 7506 | modify_flows_finish(ofproto, ofm, req); |
1f42be1c JR |
7507 | break; |
7508 | ||
7509 | case OFPFC_DELETE: | |
7510 | case OFPFC_DELETE_STRICT: | |
8be00367 | 7511 | delete_flows_finish(ofproto, ofm, req); |
1f42be1c JR |
7512 | break; |
7513 | ||
7514 | default: | |
7515 | break; | |
7516 | } | |
b0d38b2f | 7517 | |
5bacd5cd JR |
7518 | rule_collection_destroy(&ofm->old_rules); |
7519 | rule_collection_destroy(&ofm->new_rules); | |
7520 | ||
b0d38b2f | 7521 | if (req) { |
5a27991d | 7522 | ofconn_report_flow_mod(req->ofconn, ofm->command); |
b0d38b2f | 7523 | } |
1f42be1c JR |
7524 | } |
7525 | ||
39c94593 JR |
7526 | /* Commit phases (all while locking ofproto_mutex): |
7527 | * | |
7528 | * 1. Begin: Gather resources and make changes visible in the next version. | |
7529 | * - Mark affected rules for removal in the next version. | |
7530 | * - Create new replacement rules, make visible in the next | |
7531 | * version. | |
7532 | * - Do not send any events or notifications. | |
7533 | * | |
7534 | * 2. Revert: Fail if any errors are found. After this point no errors are | |
7535 | * possible. No visible changes were made, so rollback is minimal (remove | |
7536 | * added invisible rules, restore visibility of rules marked for removal). | |
7537 | * | |
1c38055d JR |
7538 | * 3. Finish: Make the changes visible for lookups. Insert replacement rules to |
7539 | * the ofproto provider. Remove replaced and deleted rules from ofproto data | |
7540 | * structures, and Schedule postponed removal of deleted rules from the | |
7541 | * classifier. Send notifications, buffered packets, etc. | |
39c94593 | 7542 | */ |
1f42be1c JR |
7543 | static enum ofperr |
7544 | do_bundle_commit(struct ofconn *ofconn, uint32_t id, uint16_t flags) | |
7545 | { | |
7546 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
44e0c35d | 7547 | ovs_version_t version = ofproto->tables_version + 1; |
1f42be1c JR |
7548 | struct ofp_bundle *bundle; |
7549 | struct ofp_bundle_entry *be; | |
777af88d | 7550 | enum ofperr error; |
1f42be1c JR |
7551 | |
7552 | bundle = ofconn_get_bundle(ofconn, id); | |
7553 | ||
7554 | if (!bundle) { | |
7555 | return OFPERR_OFPBFC_BAD_ID; | |
7556 | } | |
7557 | if (bundle->flags != flags) { | |
7558 | error = OFPERR_OFPBFC_BAD_FLAGS; | |
7559 | } else { | |
1c38055d JR |
7560 | bool prev_is_port_mod = false; |
7561 | ||
1f42be1c JR |
7562 | error = 0; |
7563 | ovs_mutex_lock(&ofproto_mutex); | |
39c94593 JR |
7564 | |
7565 | /* 1. Begin. */ | |
1f42be1c JR |
7566 | LIST_FOR_EACH (be, node, &bundle->msg_list) { |
7567 | if (be->type == OFPTYPE_PORT_MOD) { | |
1c38055d JR |
7568 | /* Our port mods are not atomic. */ |
7569 | if (flags & OFPBF_ATOMIC) { | |
7570 | error = OFPERR_OFPBFC_MSG_FAILED; | |
7571 | } else { | |
7572 | prev_is_port_mod = true; | |
8be00367 | 7573 | error = port_mod_start(ofconn, &be->opm.pm, &be->opm.port); |
1c38055d | 7574 | } |
25070e04 JR |
7575 | } else { |
7576 | /* Flow & group mods between port mods are applied as a single | |
7577 | * version, but the versions are published only after we know | |
7578 | * the commit is successful. */ | |
1c38055d | 7579 | if (prev_is_port_mod) { |
25070e04 | 7580 | prev_is_port_mod = false; |
8be00367 | 7581 | ++version; |
1c38055d | 7582 | } |
25070e04 JR |
7583 | if (be->type == OFPTYPE_FLOW_MOD) { |
7584 | /* Store the version in which the changes should take | |
7585 | * effect. */ | |
7586 | be->ofm.version = version; | |
7587 | error = ofproto_flow_mod_start(ofproto, &be->ofm); | |
7588 | } else if (be->type == OFPTYPE_GROUP_MOD) { | |
7589 | /* Store the version in which the changes should take | |
7590 | * effect. */ | |
7591 | be->ogm.version = version; | |
7592 | error = ofproto_group_mod_start(ofproto, &be->ogm); | |
6dd3c787 JR |
7593 | } else if (be->type == OFPTYPE_PACKET_OUT) { |
7594 | be->opo.version = version; | |
7595 | error = ofproto_packet_out_start(ofproto, &be->opo); | |
25070e04 JR |
7596 | } else { |
7597 | OVS_NOT_REACHED(); | |
7598 | } | |
1f42be1c JR |
7599 | } |
7600 | if (error) { | |
7601 | break; | |
7602 | } | |
7603 | } | |
1c38055d | 7604 | |
1f42be1c JR |
7605 | if (error) { |
7606 | /* Send error referring to the original message. */ | |
7607 | if (error) { | |
51bb26fa | 7608 | ofconn_send_error(ofconn, &be->ofp_msg, error); |
1f42be1c JR |
7609 | error = OFPERR_OFPBFC_MSG_FAILED; |
7610 | } | |
7611 | ||
39c94593 | 7612 | /* 2. Revert. Undo all the changes made above. */ |
1f42be1c JR |
7613 | LIST_FOR_EACH_REVERSE_CONTINUE(be, node, &bundle->msg_list) { |
7614 | if (be->type == OFPTYPE_FLOW_MOD) { | |
8be00367 | 7615 | ofproto_flow_mod_revert(ofproto, &be->ofm); |
25070e04 JR |
7616 | } else if (be->type == OFPTYPE_GROUP_MOD) { |
7617 | ofproto_group_mod_revert(ofproto, &be->ogm); | |
6dd3c787 JR |
7618 | } else if (be->type == OFPTYPE_PACKET_OUT) { |
7619 | ofproto_packet_out_revert(ofproto, &be->opo); | |
1f42be1c | 7620 | } |
1c38055d | 7621 | /* Nothing needs to be reverted for a port mod. */ |
1f42be1c JR |
7622 | } |
7623 | } else { | |
39c94593 | 7624 | /* 4. Finish. */ |
1f42be1c | 7625 | LIST_FOR_EACH (be, node, &bundle->msg_list) { |
25070e04 | 7626 | if (be->type == OFPTYPE_PORT_MOD) { |
1c38055d JR |
7627 | /* Perform the actual port mod. This is not atomic, i.e., |
7628 | * the effects will be immediately seen by upcall | |
7629 | * processing regardless of the lookup version. It should | |
7630 | * be noted that port configuration changes can originate | |
7631 | * also from OVSDB changes asynchronously to all upcall | |
7632 | * processing. */ | |
8be00367 | 7633 | port_mod_finish(ofconn, &be->opm.pm, be->opm.port); |
25070e04 | 7634 | } else { |
6dd3c787 JR |
7635 | version = |
7636 | (be->type == OFPTYPE_FLOW_MOD) ? be->ofm.version : | |
7637 | (be->type == OFPTYPE_GROUP_MOD) ? be->ogm.version : | |
7638 | (be->type == OFPTYPE_PACKET_OUT) ? be->opo.version : | |
7639 | version; | |
7640 | ||
7641 | /* Bump the lookup version to the one of the current | |
7642 | * message. This makes all the changes in the bundle at | |
7643 | * this version visible to lookups at once. */ | |
7644 | if (ofproto->tables_version < version) { | |
7645 | ofproto->tables_version = version; | |
7646 | ofproto->ofproto_class->set_tables_version( | |
7647 | ofproto, ofproto->tables_version); | |
7648 | } | |
7649 | ||
25070e04 | 7650 | struct openflow_mod_requester req = { ofconn, |
51bb26fa | 7651 | &be->ofp_msg }; |
25070e04 | 7652 | |
6dd3c787 | 7653 | if (be->type == OFPTYPE_FLOW_MOD) { |
25070e04 JR |
7654 | ofproto_flow_mod_finish(ofproto, &be->ofm, &req); |
7655 | } else if (be->type == OFPTYPE_GROUP_MOD) { | |
25070e04 | 7656 | ofproto_group_mod_finish(ofproto, &be->ogm, &req); |
6dd3c787 JR |
7657 | } else if (be->type == OFPTYPE_PACKET_OUT) { |
7658 | ofproto_packet_out_finish(ofproto, &be->opo); | |
25070e04 | 7659 | } |
1f42be1c JR |
7660 | } |
7661 | } | |
7662 | } | |
1c38055d | 7663 | |
1f42be1c JR |
7664 | ofmonitor_flush(ofproto->connmgr); |
7665 | ovs_mutex_unlock(&ofproto_mutex); | |
1f42be1c JR |
7666 | } |
7667 | ||
7668 | /* The bundle is discarded regardless the outcome. */ | |
0c78eebe | 7669 | ofp_bundle_remove__(ofconn, bundle); |
1f42be1c JR |
7670 | return error; |
7671 | } | |
7672 | ||
7673 | static enum ofperr | |
7674 | handle_bundle_control(struct ofconn *ofconn, const struct ofp_header *oh) | |
7675 | { | |
777af88d | 7676 | struct ofputil_bundle_ctrl_msg bctrl; |
777af88d | 7677 | struct ofputil_bundle_ctrl_msg reply; |
1f42be1c JR |
7678 | struct ofpbuf *buf; |
7679 | enum ofperr error; | |
777af88d | 7680 | |
ba9d374a JR |
7681 | error = reject_slave_controller(ofconn); |
7682 | if (error) { | |
7683 | return error; | |
7684 | } | |
7685 | ||
777af88d AC |
7686 | error = ofputil_decode_bundle_ctrl(oh, &bctrl); |
7687 | if (error) { | |
7688 | return error; | |
7689 | } | |
7690 | reply.flags = 0; | |
7691 | reply.bundle_id = bctrl.bundle_id; | |
7692 | ||
7693 | switch (bctrl.type) { | |
51bb26fa JR |
7694 | case OFPBCT_OPEN_REQUEST: |
7695 | error = ofp_bundle_open(ofconn, bctrl.bundle_id, bctrl.flags, oh); | |
777af88d AC |
7696 | reply.type = OFPBCT_OPEN_REPLY; |
7697 | break; | |
7698 | case OFPBCT_CLOSE_REQUEST: | |
7699 | error = ofp_bundle_close(ofconn, bctrl.bundle_id, bctrl.flags); | |
ea53e3a8 | 7700 | reply.type = OFPBCT_CLOSE_REPLY; |
777af88d AC |
7701 | break; |
7702 | case OFPBCT_COMMIT_REQUEST: | |
1f42be1c | 7703 | error = do_bundle_commit(ofconn, bctrl.bundle_id, bctrl.flags); |
777af88d AC |
7704 | reply.type = OFPBCT_COMMIT_REPLY; |
7705 | break; | |
7706 | case OFPBCT_DISCARD_REQUEST: | |
7707 | error = ofp_bundle_discard(ofconn, bctrl.bundle_id); | |
7708 | reply.type = OFPBCT_DISCARD_REPLY; | |
7709 | break; | |
7710 | ||
7711 | case OFPBCT_OPEN_REPLY: | |
7712 | case OFPBCT_CLOSE_REPLY: | |
7713 | case OFPBCT_COMMIT_REPLY: | |
7714 | case OFPBCT_DISCARD_REPLY: | |
7715 | return OFPERR_OFPBFC_BAD_TYPE; | |
7716 | break; | |
7717 | } | |
7718 | ||
7719 | if (!error) { | |
7720 | buf = ofputil_encode_bundle_ctrl_reply(oh, &reply); | |
7721 | ofconn_send_reply(ofconn, buf); | |
7722 | } | |
7723 | return error; | |
7724 | } | |
7725 | ||
777af88d AC |
7726 | static enum ofperr |
7727 | handle_bundle_add(struct ofconn *ofconn, const struct ofp_header *oh) | |
5bacd5cd | 7728 | OVS_EXCLUDED(ofproto_mutex) |
777af88d | 7729 | { |
7ac27a04 | 7730 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
777af88d AC |
7731 | enum ofperr error; |
7732 | struct ofputil_bundle_add_msg badd; | |
7ac27a04 JR |
7733 | struct ofp_bundle_entry *bmsg; |
7734 | enum ofptype type; | |
777af88d | 7735 | |
ba9d374a JR |
7736 | error = reject_slave_controller(ofconn); |
7737 | if (error) { | |
7738 | return error; | |
7739 | } | |
7740 | ||
7ac27a04 | 7741 | error = ofputil_decode_bundle_add(oh, &badd, &type); |
777af88d AC |
7742 | if (error) { |
7743 | return error; | |
7744 | } | |
7745 | ||
1f42be1c | 7746 | bmsg = ofp_bundle_entry_alloc(type, badd.msg); |
7ac27a04 | 7747 | |
6dd3c787 JR |
7748 | struct ofpbuf ofpacts; |
7749 | uint64_t ofpacts_stub[1024 / 8]; | |
7750 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); | |
7751 | ||
7ac27a04 | 7752 | if (type == OFPTYPE_PORT_MOD) { |
8be00367 | 7753 | error = ofputil_decode_port_mod(badd.msg, &bmsg->opm.pm, false); |
7ac27a04 | 7754 | } else if (type == OFPTYPE_FLOW_MOD) { |
5bacd5cd | 7755 | struct ofputil_flow_mod fm; |
7ac27a04 | 7756 | |
5bacd5cd | 7757 | error = ofputil_decode_flow_mod(&fm, badd.msg, |
7ac27a04 | 7758 | ofconn_get_protocol(ofconn), |
8d8ab6c2 | 7759 | ofproto_get_tun_tab(ofproto), |
04f48a68 | 7760 | &ofproto->vl_mff_map, &ofpacts, |
7ac27a04 JR |
7761 | u16_to_ofp(ofproto->max_ports), |
7762 | ofproto->n_tables); | |
5bacd5cd | 7763 | if (!error) { |
2c7ee524 | 7764 | error = ofproto_flow_mod_init(ofproto, &bmsg->ofm, &fm, NULL); |
5bacd5cd | 7765 | } |
25070e04 JR |
7766 | } else if (type == OFPTYPE_GROUP_MOD) { |
7767 | error = ofputil_decode_group_mod(badd.msg, &bmsg->ogm.gm); | |
6dd3c787 JR |
7768 | } else if (type == OFPTYPE_PACKET_OUT) { |
7769 | struct ofputil_packet_out po; | |
7770 | ||
7771 | COVERAGE_INC(ofproto_packet_out); | |
7772 | ||
7773 | /* Decode message. */ | |
7774 | error = ofputil_decode_packet_out(&po, badd.msg, &ofpacts); | |
7775 | if (!error) { | |
7776 | po.ofpacts = ofpbuf_steal_data(&ofpacts); /* Move to heap. */ | |
7777 | error = ofproto_packet_out_init(ofproto, ofconn, &bmsg->opo, &po); | |
7778 | } | |
7ac27a04 JR |
7779 | } else { |
7780 | OVS_NOT_REACHED(); | |
7781 | } | |
7782 | ||
6dd3c787 JR |
7783 | ofpbuf_uninit(&ofpacts); |
7784 | ||
7ac27a04 JR |
7785 | if (!error) { |
7786 | error = ofp_bundle_add_message(ofconn, badd.bundle_id, badd.flags, | |
51bb26fa | 7787 | bmsg, oh); |
7ac27a04 JR |
7788 | } |
7789 | ||
7790 | if (error) { | |
7791 | ofp_bundle_entry_free(bmsg); | |
7792 | } | |
7793 | ||
7794 | return error; | |
777af88d AC |
7795 | } |
7796 | ||
6159c531 | 7797 | static enum ofperr |
4e548ad9 | 7798 | handle_tlv_table_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
6159c531 | 7799 | { |
8d8ab6c2 JG |
7800 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
7801 | struct tun_table *old_tab, *new_tab; | |
4e548ad9 | 7802 | struct ofputil_tlv_table_mod ttm; |
6159c531 JG |
7803 | enum ofperr error; |
7804 | ||
7805 | error = reject_slave_controller(ofconn); | |
7806 | if (error) { | |
7807 | return error; | |
7808 | } | |
7809 | ||
4e548ad9 | 7810 | error = ofputil_decode_tlv_table_mod(oh, &ttm); |
6159c531 JG |
7811 | if (error) { |
7812 | return error; | |
7813 | } | |
7814 | ||
8d8ab6c2 JG |
7815 | old_tab = ovsrcu_get_protected(struct tun_table *, &ofproto->metadata_tab); |
7816 | error = tun_metadata_table_mod(&ttm, old_tab, &new_tab); | |
7817 | if (!error) { | |
04f48a68 YHW |
7818 | ovs_mutex_lock(&ofproto->vl_mff_map.mutex); |
7819 | error = mf_vl_mff_map_mod_from_tun_metadata(&ofproto->vl_mff_map, | |
7820 | &ttm); | |
7821 | ovs_mutex_unlock(&ofproto->vl_mff_map.mutex); | |
5c7c16d8 YHW |
7822 | if (!error) { |
7823 | ovsrcu_set(&ofproto->metadata_tab, new_tab); | |
7824 | tun_metadata_postpone_free(old_tab); | |
7825 | } | |
8d8ab6c2 | 7826 | } |
9558d2a5 | 7827 | |
4e548ad9 | 7828 | ofputil_uninit_tlv_table(&ttm.mappings); |
9558d2a5 | 7829 | return error; |
6159c531 JG |
7830 | } |
7831 | ||
7832 | static enum ofperr | |
4e548ad9 | 7833 | handle_tlv_table_request(struct ofconn *ofconn, const struct ofp_header *oh) |
6159c531 | 7834 | { |
8d8ab6c2 | 7835 | const struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
4e548ad9 | 7836 | struct ofputil_tlv_table_reply ttr; |
9558d2a5 JG |
7837 | struct ofpbuf *b; |
7838 | ||
8d8ab6c2 JG |
7839 | tun_metadata_table_request(ofproto_get_tun_tab(ofproto), &ttr); |
7840 | ||
4e548ad9 ML |
7841 | b = ofputil_encode_tlv_table_reply(oh, &ttr); |
7842 | ofputil_uninit_tlv_table(&ttr.mappings); | |
9558d2a5 JG |
7843 | |
7844 | ofconn_send_reply(ofconn, b); | |
7845 | return 0; | |
6159c531 JG |
7846 | } |
7847 | ||
90bf1e07 | 7848 | static enum ofperr |
d1e2cf21 | 7849 | handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg) |
15aaf599 | 7850 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 7851 | { |
6fd6ed71 | 7852 | const struct ofp_header *oh = msg->data; |
982697a4 | 7853 | enum ofptype type; |
90bf1e07 | 7854 | enum ofperr error; |
064af421 | 7855 | |
982697a4 | 7856 | error = ofptype_decode(&type, oh); |
d1e2cf21 BP |
7857 | if (error) { |
7858 | return error; | |
7859 | } | |
ff3c2c63 YT |
7860 | if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh) |
7861 | && ofpmp_more(oh)) { | |
7862 | /* We have no buffer implementation for multipart requests. | |
7863 | * Report overflow for requests which consists of multiple | |
7864 | * messages. */ | |
7865 | return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW; | |
7866 | } | |
064af421 | 7867 | |
982697a4 | 7868 | switch (type) { |
d1e2cf21 | 7869 | /* OpenFlow requests. */ |
982697a4 | 7870 | case OFPTYPE_ECHO_REQUEST: |
d1e2cf21 | 7871 | return handle_echo_request(ofconn, oh); |
064af421 | 7872 | |
982697a4 | 7873 | case OFPTYPE_FEATURES_REQUEST: |
d1e2cf21 | 7874 | return handle_features_request(ofconn, oh); |
064af421 | 7875 | |
982697a4 | 7876 | case OFPTYPE_GET_CONFIG_REQUEST: |
d1e2cf21 | 7877 | return handle_get_config_request(ofconn, oh); |
064af421 | 7878 | |
982697a4 BP |
7879 | case OFPTYPE_SET_CONFIG: |
7880 | return handle_set_config(ofconn, oh); | |
064af421 | 7881 | |
982697a4 BP |
7882 | case OFPTYPE_PACKET_OUT: |
7883 | return handle_packet_out(ofconn, oh); | |
064af421 | 7884 | |
982697a4 | 7885 | case OFPTYPE_PORT_MOD: |
d1e2cf21 | 7886 | return handle_port_mod(ofconn, oh); |
064af421 | 7887 | |
982697a4 | 7888 | case OFPTYPE_FLOW_MOD: |
2e4f5fcf | 7889 | return handle_flow_mod(ofconn, oh); |
064af421 | 7890 | |
7395c052 NZ |
7891 | case OFPTYPE_GROUP_MOD: |
7892 | return handle_group_mod(ofconn, oh); | |
7893 | ||
918f2b82 AZ |
7894 | case OFPTYPE_TABLE_MOD: |
7895 | return handle_table_mod(ofconn, oh); | |
7896 | ||
9cae45dc JR |
7897 | case OFPTYPE_METER_MOD: |
7898 | return handle_meter_mod(ofconn, oh); | |
7899 | ||
982697a4 | 7900 | case OFPTYPE_BARRIER_REQUEST: |
d1e2cf21 | 7901 | return handle_barrier_request(ofconn, oh); |
064af421 | 7902 | |
6ea4776b JR |
7903 | case OFPTYPE_ROLE_REQUEST: |
7904 | return handle_role_request(ofconn, oh); | |
7905 | ||
d1e2cf21 | 7906 | /* OpenFlow replies. */ |
982697a4 | 7907 | case OFPTYPE_ECHO_REPLY: |
d1e2cf21 | 7908 | return 0; |
246e61ea | 7909 | |
d1e2cf21 | 7910 | /* Nicira extension requests. */ |
982697a4 | 7911 | case OFPTYPE_FLOW_MOD_TABLE_ID: |
6c1491fb | 7912 | return handle_nxt_flow_mod_table_id(ofconn, oh); |
d1e2cf21 | 7913 | |
982697a4 | 7914 | case OFPTYPE_SET_FLOW_FORMAT: |
d1e2cf21 BP |
7915 | return handle_nxt_set_flow_format(ofconn, oh); |
7916 | ||
982697a4 | 7917 | case OFPTYPE_SET_PACKET_IN_FORMAT: |
54834960 EJ |
7918 | return handle_nxt_set_packet_in_format(ofconn, oh); |
7919 | ||
982697a4 | 7920 | case OFPTYPE_SET_CONTROLLER_ID: |
a7349929 BP |
7921 | return handle_nxt_set_controller_id(ofconn, oh); |
7922 | ||
982697a4 | 7923 | case OFPTYPE_FLOW_AGE: |
f27f2134 BP |
7924 | /* Nothing to do. */ |
7925 | return 0; | |
7926 | ||
982697a4 | 7927 | case OFPTYPE_FLOW_MONITOR_CANCEL: |
2b07c8b1 BP |
7928 | return handle_flow_monitor_cancel(ofconn, oh); |
7929 | ||
982697a4 | 7930 | case OFPTYPE_SET_ASYNC_CONFIG: |
80d5aefd BP |
7931 | return handle_nxt_set_async_config(ofconn, oh); |
7932 | ||
c423b3b3 AC |
7933 | case OFPTYPE_GET_ASYNC_REQUEST: |
7934 | return handle_nxt_get_async_request(ofconn, oh); | |
7935 | ||
77ab5fd2 BP |
7936 | case OFPTYPE_NXT_RESUME: |
7937 | return handle_nxt_resume(ofconn, oh); | |
7938 | ||
349adfb2 | 7939 | /* Statistics requests. */ |
982697a4 BP |
7940 | case OFPTYPE_DESC_STATS_REQUEST: |
7941 | return handle_desc_stats_request(ofconn, oh); | |
7942 | ||
7943 | case OFPTYPE_FLOW_STATS_REQUEST: | |
7944 | return handle_flow_stats_request(ofconn, oh); | |
7945 | ||
7946 | case OFPTYPE_AGGREGATE_STATS_REQUEST: | |
7947 | return handle_aggregate_stats_request(ofconn, oh); | |
7948 | ||
7949 | case OFPTYPE_TABLE_STATS_REQUEST: | |
7950 | return handle_table_stats_request(ofconn, oh); | |
7951 | ||
3c4e10fb BP |
7952 | case OFPTYPE_TABLE_FEATURES_STATS_REQUEST: |
7953 | return handle_table_features_request(ofconn, oh); | |
7954 | ||
03c72922 BP |
7955 | case OFPTYPE_TABLE_DESC_REQUEST: |
7956 | return handle_table_desc_request(ofconn, oh); | |
7957 | ||
982697a4 BP |
7958 | case OFPTYPE_PORT_STATS_REQUEST: |
7959 | return handle_port_stats_request(ofconn, oh); | |
7960 | ||
7961 | case OFPTYPE_QUEUE_STATS_REQUEST: | |
7962 | return handle_queue_stats_request(ofconn, oh); | |
7963 | ||
7964 | case OFPTYPE_PORT_DESC_STATS_REQUEST: | |
7965 | return handle_port_desc_stats_request(ofconn, oh); | |
7966 | ||
7967 | case OFPTYPE_FLOW_MONITOR_STATS_REQUEST: | |
7968 | return handle_flow_monitor_request(ofconn, oh); | |
7969 | ||
261bd854 BP |
7970 | case OFPTYPE_METER_STATS_REQUEST: |
7971 | case OFPTYPE_METER_CONFIG_STATS_REQUEST: | |
9cae45dc JR |
7972 | return handle_meter_request(ofconn, oh, type); |
7973 | ||
261bd854 | 7974 | case OFPTYPE_METER_FEATURES_STATS_REQUEST: |
9cae45dc JR |
7975 | return handle_meter_features_request(ofconn, oh); |
7976 | ||
261bd854 | 7977 | case OFPTYPE_GROUP_STATS_REQUEST: |
7395c052 NZ |
7978 | return handle_group_stats_request(ofconn, oh); |
7979 | ||
261bd854 | 7980 | case OFPTYPE_GROUP_DESC_STATS_REQUEST: |
7395c052 NZ |
7981 | return handle_group_desc_stats_request(ofconn, oh); |
7982 | ||
261bd854 | 7983 | case OFPTYPE_GROUP_FEATURES_STATS_REQUEST: |
7395c052 NZ |
7984 | return handle_group_features_stats_request(ofconn, oh); |
7985 | ||
7395c052 | 7986 | case OFPTYPE_QUEUE_GET_CONFIG_REQUEST: |
e8f9a7bb | 7987 | return handle_queue_get_config_request(ofconn, oh); |
2e1ae200 | 7988 | |
777af88d AC |
7989 | case OFPTYPE_BUNDLE_CONTROL: |
7990 | return handle_bundle_control(ofconn, oh); | |
7991 | ||
7992 | case OFPTYPE_BUNDLE_ADD_MESSAGE: | |
7993 | return handle_bundle_add(ofconn, oh); | |
7994 | ||
4e548ad9 ML |
7995 | case OFPTYPE_NXT_TLV_TABLE_MOD: |
7996 | return handle_tlv_table_mod(ofconn, oh); | |
6159c531 | 7997 | |
4e548ad9 ML |
7998 | case OFPTYPE_NXT_TLV_TABLE_REQUEST: |
7999 | return handle_tlv_table_request(ofconn, oh); | |
6159c531 | 8000 | |
fb8f22c1 BY |
8001 | case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST: |
8002 | return handle_ipfix_bridge_stats_request(ofconn, oh); | |
8003 | ||
8004 | case OFPTYPE_IPFIX_FLOW_STATS_REQUEST: | |
8005 | return handle_ipfix_flow_stats_request(ofconn, oh); | |
8006 | ||
2a7c4805 JP |
8007 | case OFPTYPE_CT_FLUSH_ZONE: |
8008 | return handle_nxt_ct_flush_zone(ofconn, oh); | |
8009 | ||
982697a4 BP |
8010 | case OFPTYPE_HELLO: |
8011 | case OFPTYPE_ERROR: | |
8012 | case OFPTYPE_FEATURES_REPLY: | |
8013 | case OFPTYPE_GET_CONFIG_REPLY: | |
8014 | case OFPTYPE_PACKET_IN: | |
8015 | case OFPTYPE_FLOW_REMOVED: | |
8016 | case OFPTYPE_PORT_STATUS: | |
8017 | case OFPTYPE_BARRIER_REPLY: | |
c545d38d | 8018 | case OFPTYPE_QUEUE_GET_CONFIG_REPLY: |
982697a4 BP |
8019 | case OFPTYPE_DESC_STATS_REPLY: |
8020 | case OFPTYPE_FLOW_STATS_REPLY: | |
8021 | case OFPTYPE_QUEUE_STATS_REPLY: | |
8022 | case OFPTYPE_PORT_STATS_REPLY: | |
8023 | case OFPTYPE_TABLE_STATS_REPLY: | |
8024 | case OFPTYPE_AGGREGATE_STATS_REPLY: | |
8025 | case OFPTYPE_PORT_DESC_STATS_REPLY: | |
8026 | case OFPTYPE_ROLE_REPLY: | |
8027 | case OFPTYPE_FLOW_MONITOR_PAUSED: | |
8028 | case OFPTYPE_FLOW_MONITOR_RESUMED: | |
8029 | case OFPTYPE_FLOW_MONITOR_STATS_REPLY: | |
2e1ae200 | 8030 | case OFPTYPE_GET_ASYNC_REPLY: |
261bd854 BP |
8031 | case OFPTYPE_GROUP_STATS_REPLY: |
8032 | case OFPTYPE_GROUP_DESC_STATS_REPLY: | |
8033 | case OFPTYPE_GROUP_FEATURES_STATS_REPLY: | |
8034 | case OFPTYPE_METER_STATS_REPLY: | |
8035 | case OFPTYPE_METER_CONFIG_STATS_REPLY: | |
8036 | case OFPTYPE_METER_FEATURES_STATS_REPLY: | |
8037 | case OFPTYPE_TABLE_FEATURES_STATS_REPLY: | |
03c72922 | 8038 | case OFPTYPE_TABLE_DESC_REPLY: |
252f3411 | 8039 | case OFPTYPE_ROLE_STATUS: |
3c35db62 | 8040 | case OFPTYPE_REQUESTFORWARD: |
6c6eedc5 | 8041 | case OFPTYPE_TABLE_STATUS: |
4e548ad9 | 8042 | case OFPTYPE_NXT_TLV_TABLE_REPLY: |
fb8f22c1 BY |
8043 | case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY: |
8044 | case OFPTYPE_IPFIX_FLOW_STATS_REPLY: | |
064af421 | 8045 | default: |
76ec08e0 YT |
8046 | if (ofpmsg_is_stat_request(oh)) { |
8047 | return OFPERR_OFPBRC_BAD_STAT; | |
8048 | } else { | |
8049 | return OFPERR_OFPBRC_BAD_TYPE; | |
8050 | } | |
064af421 | 8051 | } |
d1e2cf21 | 8052 | } |
064af421 | 8053 | |
b20f4073 | 8054 | static void |
e03248b7 | 8055 | handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg) |
15aaf599 | 8056 | OVS_EXCLUDED(ofproto_mutex) |
d1e2cf21 | 8057 | { |
d51c8b71 JR |
8058 | enum ofperr error = handle_openflow__(ofconn, ofp_msg); |
8059 | ||
b20f4073 | 8060 | if (error) { |
6fd6ed71 | 8061 | ofconn_send_error(ofconn, ofp_msg->data, error); |
064af421 | 8062 | } |
d1e2cf21 | 8063 | COVERAGE_INC(ofproto_recv_openflow); |
7ee20df1 BP |
8064 | } |
8065 | \f | |
064af421 | 8066 | static uint64_t |
fa60c019 | 8067 | pick_datapath_id(const struct ofproto *ofproto) |
064af421 | 8068 | { |
fa60c019 | 8069 | const struct ofport *port; |
064af421 | 8070 | |
abe529af | 8071 | port = ofproto_get_port(ofproto, OFPP_LOCAL); |
fa60c019 | 8072 | if (port) { |
74ff3298 | 8073 | struct eth_addr ea; |
fa60c019 BP |
8074 | int error; |
8075 | ||
74ff3298 | 8076 | error = netdev_get_etheraddr(port->netdev, &ea); |
064af421 BP |
8077 | if (!error) { |
8078 | return eth_addr_to_uint64(ea); | |
8079 | } | |
fbfa2911 BP |
8080 | VLOG_WARN("%s: could not get MAC address for %s (%s)", |
8081 | ofproto->name, netdev_get_name(port->netdev), | |
10a89ef0 | 8082 | ovs_strerror(error)); |
064af421 | 8083 | } |
fa60c019 | 8084 | return ofproto->fallback_dpid; |
064af421 BP |
8085 | } |
8086 | ||
8087 | static uint64_t | |
8088 | pick_fallback_dpid(void) | |
8089 | { | |
74ff3298 JR |
8090 | struct eth_addr ea; |
8091 | eth_addr_nicira_random(&ea); | |
064af421 BP |
8092 | return eth_addr_to_uint64(ea); |
8093 | } | |
8094 | \f | |
254750ce BP |
8095 | /* Table overflow policy. */ |
8096 | ||
ad3efdcb EJ |
8097 | /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep' |
8098 | * to NULL if the table is not configured to evict rules or if the table | |
8099 | * contains no evictable rules. (Rules with a readlock on their evict rwlock, | |
8100 | * or with no timeouts are not evictable.) */ | |
8101 | static bool | |
8102 | choose_rule_to_evict(struct oftable *table, struct rule **rulep) | |
15aaf599 | 8103 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8104 | { |
8105 | struct eviction_group *evg; | |
8106 | ||
ad3efdcb | 8107 | *rulep = NULL; |
82c22d34 | 8108 | if (!table->eviction) { |
ad3efdcb | 8109 | return false; |
254750ce BP |
8110 | } |
8111 | ||
8112 | /* In the common case, the outer and inner loops here will each be entered | |
8113 | * exactly once: | |
8114 | * | |
8115 | * - The inner loop normally "return"s in its first iteration. If the | |
8116 | * eviction group has any evictable rules, then it always returns in | |
8117 | * some iteration. | |
8118 | * | |
8119 | * - The outer loop only iterates more than once if the largest eviction | |
8120 | * group has no evictable rules. | |
8121 | * | |
8122 | * - The outer loop can exit only if table's 'max_flows' is all filled up | |
afe2143d | 8123 | * by unevictable rules. */ |
254750ce BP |
8124 | HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) { |
8125 | struct rule *rule; | |
8126 | ||
8127 | HEAP_FOR_EACH (rule, evg_node, &evg->rules) { | |
15aaf599 BP |
8128 | *rulep = rule; |
8129 | return true; | |
254750ce BP |
8130 | } |
8131 | } | |
8132 | ||
ad3efdcb | 8133 | return false; |
254750ce | 8134 | } |
254750ce BP |
8135 | \f |
8136 | /* Eviction groups. */ | |
8137 | ||
8138 | /* Returns the priority to use for an eviction_group that contains 'n_rules' | |
8139 | * rules. The priority contains low-order random bits to ensure that eviction | |
8140 | * groups with the same number of rules are prioritized randomly. */ | |
8141 | static uint32_t | |
8142 | eviction_group_priority(size_t n_rules) | |
8143 | { | |
8144 | uint16_t size = MIN(UINT16_MAX, n_rules); | |
8145 | return (size << 16) | random_uint16(); | |
8146 | } | |
8147 | ||
8148 | /* Updates 'evg', an eviction_group within 'table', following a change that | |
8149 | * adds or removes rules in 'evg'. */ | |
8150 | static void | |
8151 | eviction_group_resized(struct oftable *table, struct eviction_group *evg) | |
15aaf599 | 8152 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8153 | { |
8154 | heap_change(&table->eviction_groups_by_size, &evg->size_node, | |
8155 | eviction_group_priority(heap_count(&evg->rules))); | |
8156 | } | |
8157 | ||
8158 | /* Destroys 'evg', an eviction_group within 'table': | |
8159 | * | |
8160 | * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the | |
8161 | * rules themselves, just removes them from the eviction group.) | |
8162 | * | |
8163 | * - Removes 'evg' from 'table'. | |
8164 | * | |
8165 | * - Frees 'evg'. */ | |
8166 | static void | |
8167 | eviction_group_destroy(struct oftable *table, struct eviction_group *evg) | |
15aaf599 | 8168 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8169 | { |
8170 | while (!heap_is_empty(&evg->rules)) { | |
8171 | struct rule *rule; | |
8172 | ||
8173 | rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node); | |
8174 | rule->eviction_group = NULL; | |
8175 | } | |
8176 | hmap_remove(&table->eviction_groups_by_id, &evg->id_node); | |
8177 | heap_remove(&table->eviction_groups_by_size, &evg->size_node); | |
8178 | heap_destroy(&evg->rules); | |
8179 | free(evg); | |
8180 | } | |
8181 | ||
8182 | /* Removes 'rule' from its eviction group, if any. */ | |
8183 | static void | |
8184 | eviction_group_remove_rule(struct rule *rule) | |
15aaf599 | 8185 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8186 | { |
8187 | if (rule->eviction_group) { | |
8188 | struct oftable *table = &rule->ofproto->tables[rule->table_id]; | |
8189 | struct eviction_group *evg = rule->eviction_group; | |
8190 | ||
8191 | rule->eviction_group = NULL; | |
8192 | heap_remove(&evg->rules, &rule->evg_node); | |
8193 | if (heap_is_empty(&evg->rules)) { | |
8194 | eviction_group_destroy(table, evg); | |
8195 | } else { | |
8196 | eviction_group_resized(table, evg); | |
8197 | } | |
8198 | } | |
8199 | } | |
8200 | ||
8201 | /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and | |
8202 | * returns the hash value. */ | |
8203 | static uint32_t | |
8204 | eviction_group_hash_rule(struct rule *rule) | |
15aaf599 | 8205 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8206 | { |
8207 | struct oftable *table = &rule->ofproto->tables[rule->table_id]; | |
8208 | const struct mf_subfield *sf; | |
5cb7a798 | 8209 | struct flow flow; |
254750ce BP |
8210 | uint32_t hash; |
8211 | ||
8212 | hash = table->eviction_group_id_basis; | |
8fd47924 | 8213 | miniflow_expand(rule->cr.match.flow, &flow); |
254750ce BP |
8214 | for (sf = table->eviction_fields; |
8215 | sf < &table->eviction_fields[table->n_eviction_fields]; | |
8216 | sf++) | |
8217 | { | |
aff49b8c | 8218 | if (mf_are_prereqs_ok(sf->field, &flow, NULL)) { |
254750ce BP |
8219 | union mf_value value; |
8220 | ||
5cb7a798 | 8221 | mf_get_value(sf->field, &flow, &value); |
254750ce BP |
8222 | if (sf->ofs) { |
8223 | bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs); | |
8224 | } | |
8225 | if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) { | |
8226 | unsigned int start = sf->ofs + sf->n_bits; | |
8227 | bitwise_zero(&value, sf->field->n_bytes, start, | |
8228 | sf->field->n_bytes * 8 - start); | |
8229 | } | |
8230 | hash = hash_bytes(&value, sf->field->n_bytes, hash); | |
8231 | } else { | |
8232 | hash = hash_int(hash, 0); | |
8233 | } | |
8234 | } | |
8235 | ||
8236 | return hash; | |
8237 | } | |
8238 | ||
8239 | /* Returns an eviction group within 'table' with the given 'id', creating one | |
8240 | * if necessary. */ | |
8241 | static struct eviction_group * | |
8242 | eviction_group_find(struct oftable *table, uint32_t id) | |
15aaf599 | 8243 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8244 | { |
8245 | struct eviction_group *evg; | |
8246 | ||
8247 | HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) { | |
8248 | return evg; | |
8249 | } | |
8250 | ||
8251 | evg = xmalloc(sizeof *evg); | |
8252 | hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id); | |
8253 | heap_insert(&table->eviction_groups_by_size, &evg->size_node, | |
8254 | eviction_group_priority(0)); | |
8255 | heap_init(&evg->rules); | |
8256 | ||
8257 | return evg; | |
8258 | } | |
8259 | ||
8260 | /* Returns an eviction priority for 'rule'. The return value should be | |
f70b94de BP |
8261 | * interpreted so that higher priorities make a rule a more attractive |
8262 | * candidate for eviction. */ | |
8263 | static uint64_t | |
dc437090 | 8264 | rule_eviction_priority(struct ofproto *ofproto, struct rule *rule) |
15aaf599 | 8265 | OVS_REQUIRES(ofproto_mutex) |
254750ce | 8266 | { |
f70b94de BP |
8267 | /* Calculate absolute time when this flow will expire. If it will never |
8268 | * expire, then return 0 to make it unevictable. */ | |
dc437090 | 8269 | long long int expiration = LLONG_MAX; |
dc437090 | 8270 | if (rule->hard_timeout) { |
f70b94de BP |
8271 | /* 'modified' needs protection even when we hold 'ofproto_mutex'. */ |
8272 | ovs_mutex_lock(&rule->mutex); | |
8273 | long long int modified = rule->modified; | |
8274 | ovs_mutex_unlock(&rule->mutex); | |
8275 | ||
dc437090 JR |
8276 | expiration = modified + rule->hard_timeout * 1000; |
8277 | } | |
8278 | if (rule->idle_timeout) { | |
8279 | uint64_t packets, bytes; | |
8280 | long long int used; | |
8281 | long long int idle_expiration; | |
8282 | ||
8283 | ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used); | |
8284 | idle_expiration = used + rule->idle_timeout * 1000; | |
8285 | expiration = MIN(expiration, idle_expiration); | |
8286 | } | |
254750ce BP |
8287 | if (expiration == LLONG_MAX) { |
8288 | return 0; | |
8289 | } | |
8290 | ||
8291 | /* Calculate the time of expiration as a number of (approximate) seconds | |
8292 | * after program startup. | |
8293 | * | |
8294 | * This should work OK for program runs that last UINT32_MAX seconds or | |
8295 | * less. Therefore, please restart OVS at least once every 136 years. */ | |
f70b94de | 8296 | uint32_t expiration_ofs = (expiration >> 10) - (time_boot_msec() >> 10); |
254750ce | 8297 | |
f70b94de BP |
8298 | /* Combine expiration time with OpenFlow "importance" to form a single |
8299 | * priority value. We want flows with relatively low "importance" to be | |
8300 | * evicted before even considering expiration time, so put "importance" in | |
8301 | * the most significant bits and expiration time in the least significant | |
8302 | * bits. | |
8303 | * | |
8304 | * Small 'priority' should be evicted before those with large 'priority'. | |
8305 | * The caller expects the opposite convention (a large return value being | |
8306 | * more attractive for eviction) so we invert it before returning. */ | |
8307 | uint64_t priority = ((uint64_t) rule->importance << 32) + expiration_ofs; | |
8308 | return UINT64_MAX - priority; | |
254750ce BP |
8309 | } |
8310 | ||
8311 | /* Adds 'rule' to an appropriate eviction group for its oftable's | |
8312 | * configuration. Does nothing if 'rule''s oftable doesn't have eviction | |
8313 | * enabled, or if 'rule' is a permanent rule (one that will never expire on its | |
8314 | * own). | |
8315 | * | |
8316 | * The caller must ensure that 'rule' is not already in an eviction group. */ | |
8317 | static void | |
8318 | eviction_group_add_rule(struct rule *rule) | |
15aaf599 | 8319 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8320 | { |
8321 | struct ofproto *ofproto = rule->ofproto; | |
8322 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
a3779dbc | 8323 | bool has_timeout; |
254750ce | 8324 | |
dc437090 JR |
8325 | /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it |
8326 | * so no additional protection is needed. */ | |
a3779dbc | 8327 | has_timeout = rule->hard_timeout || rule->idle_timeout; |
a3779dbc | 8328 | |
82c22d34 | 8329 | if (table->eviction && has_timeout) { |
254750ce BP |
8330 | struct eviction_group *evg; |
8331 | ||
8332 | evg = eviction_group_find(table, eviction_group_hash_rule(rule)); | |
8333 | ||
8334 | rule->eviction_group = evg; | |
8335 | heap_insert(&evg->rules, &rule->evg_node, | |
dc437090 | 8336 | rule_eviction_priority(ofproto, rule)); |
254750ce BP |
8337 | eviction_group_resized(table, evg); |
8338 | } | |
8339 | } | |
8340 | \f | |
d0918789 BP |
8341 | /* oftables. */ |
8342 | ||
8343 | /* Initializes 'table'. */ | |
8344 | static void | |
8345 | oftable_init(struct oftable *table) | |
8346 | { | |
5c67e4af | 8347 | memset(table, 0, sizeof *table); |
d70e8c28 | 8348 | classifier_init(&table->cls, flow_segment_u64s); |
ec1c5c7e | 8349 | table->max_flows = UINT_MAX; |
d79e3d70 | 8350 | table->n_flows = 0; |
82c22d34 BP |
8351 | hmap_init(&table->eviction_groups_by_id); |
8352 | heap_init(&table->eviction_groups_by_size); | |
3c1bb396 | 8353 | atomic_init(&table->miss_config, OFPUTIL_TABLE_MISS_DEFAULT); |
f017d986 | 8354 | |
f017d986 JR |
8355 | classifier_set_prefix_fields(&table->cls, default_prefix_fields, |
8356 | ARRAY_SIZE(default_prefix_fields)); | |
d611866c SH |
8357 | |
8358 | atomic_init(&table->n_matched, 0); | |
8359 | atomic_init(&table->n_missed, 0); | |
d0918789 BP |
8360 | } |
8361 | ||
254750ce | 8362 | /* Destroys 'table', including its classifier and eviction groups. |
d0918789 BP |
8363 | * |
8364 | * The caller is responsible for freeing 'table' itself. */ | |
8365 | static void | |
8366 | oftable_destroy(struct oftable *table) | |
8367 | { | |
cb22974d | 8368 | ovs_assert(classifier_is_empty(&table->cls)); |
82c22d34 | 8369 | |
7394b8fb | 8370 | ovs_mutex_lock(&ofproto_mutex); |
82c22d34 | 8371 | oftable_configure_eviction(table, 0, NULL, 0); |
7394b8fb | 8372 | ovs_mutex_unlock(&ofproto_mutex); |
82c22d34 BP |
8373 | |
8374 | hmap_destroy(&table->eviction_groups_by_id); | |
8375 | heap_destroy(&table->eviction_groups_by_size); | |
d0918789 | 8376 | classifier_destroy(&table->cls); |
254750ce BP |
8377 | free(table->name); |
8378 | } | |
8379 | ||
8380 | /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty | |
8381 | * string, then 'table' will use its default name. | |
8382 | * | |
8383 | * This only affects the name exposed for a table exposed through the OpenFlow | |
8384 | * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */ | |
8385 | static void | |
8386 | oftable_set_name(struct oftable *table, const char *name) | |
8387 | { | |
8388 | if (name && name[0]) { | |
8389 | int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN); | |
8390 | if (!table->name || strncmp(name, table->name, len)) { | |
8391 | free(table->name); | |
8392 | table->name = xmemdup0(name, len); | |
8393 | } | |
8394 | } else { | |
8395 | free(table->name); | |
8396 | table->name = NULL; | |
8397 | } | |
8398 | } | |
8399 | ||
254750ce BP |
8400 | /* oftables support a choice of two policies when adding a rule would cause the |
8401 | * number of flows in the table to exceed the configured maximum number: either | |
8402 | * they can refuse to add the new flow or they can evict some existing flow. | |
8403 | * This function configures the latter policy on 'table', with fairness based | |
8404 | * on the values of the 'n_fields' fields specified in 'fields'. (Specifying | |
8405 | * 'n_fields' as 0 disables fairness.) */ | |
8406 | static void | |
82c22d34 BP |
8407 | oftable_configure_eviction(struct oftable *table, unsigned int eviction, |
8408 | const struct mf_subfield *fields, size_t n_fields) | |
15aaf599 | 8409 | OVS_REQUIRES(ofproto_mutex) |
254750ce | 8410 | { |
254750ce BP |
8411 | struct rule *rule; |
8412 | ||
82c22d34 | 8413 | if ((table->eviction != 0) == (eviction != 0) |
254750ce BP |
8414 | && n_fields == table->n_eviction_fields |
8415 | && (!n_fields | |
8416 | || !memcmp(fields, table->eviction_fields, | |
8417 | n_fields * sizeof *fields))) { | |
82c22d34 BP |
8418 | /* The set of eviction fields did not change. If 'eviction' changed, |
8419 | * it remains nonzero, so that we can just update table->eviction | |
8420 | * without fussing with the eviction groups. */ | |
8421 | table->eviction = eviction; | |
254750ce BP |
8422 | return; |
8423 | } | |
8424 | ||
82c22d34 BP |
8425 | /* Destroy existing eviction groups, then destroy and recreate data |
8426 | * structures to recover memory. */ | |
8427 | struct eviction_group *evg, *next; | |
8428 | HMAP_FOR_EACH_SAFE (evg, next, id_node, &table->eviction_groups_by_id) { | |
8429 | eviction_group_destroy(table, evg); | |
8430 | } | |
8431 | hmap_destroy(&table->eviction_groups_by_id); | |
254750ce | 8432 | hmap_init(&table->eviction_groups_by_id); |
82c22d34 | 8433 | heap_destroy(&table->eviction_groups_by_size); |
254750ce BP |
8434 | heap_init(&table->eviction_groups_by_size); |
8435 | ||
82c22d34 BP |
8436 | /* Replace eviction groups by the new ones, if there is a change. Free the |
8437 | * old fields only after allocating the new ones, because 'fields == | |
8438 | * table->eviction_fields' is possible. */ | |
8439 | struct mf_subfield *old_fields = table->eviction_fields; | |
8440 | table->n_eviction_fields = n_fields; | |
8441 | table->eviction_fields = (fields | |
8442 | ? xmemdup(fields, n_fields * sizeof *fields) | |
8443 | : NULL); | |
8444 | free(old_fields); | |
8445 | ||
8446 | /* Add the new eviction groups, if enabled. */ | |
8447 | table->eviction = eviction; | |
8448 | if (table->eviction) { | |
8449 | table->eviction_group_id_basis = random_uint32(); | |
8450 | CLS_FOR_EACH (rule, cr, &table->cls) { | |
8451 | eviction_group_add_rule(rule); | |
8452 | } | |
254750ce | 8453 | } |
d0918789 BP |
8454 | } |
8455 | ||
bc5e6a91 JR |
8456 | /* Inserts 'rule' from the ofproto data structures BEFORE caller has inserted |
8457 | * it to the classifier. */ | |
8458 | static void | |
8459 | ofproto_rule_insert__(struct ofproto *ofproto, struct rule *rule) | |
8460 | OVS_REQUIRES(ofproto_mutex) | |
8461 | { | |
8462 | const struct rule_actions *actions = rule_get_actions(rule); | |
8463 | ||
30d0452c JR |
8464 | /* A rule may not be reinserted. */ |
8465 | ovs_assert(rule->state == RULE_INITIALIZED); | |
39c94593 | 8466 | |
bc5e6a91 | 8467 | if (rule->hard_timeout || rule->idle_timeout) { |
417e7e66 | 8468 | ovs_list_insert(&ofproto->expirable, &rule->expirable); |
bc5e6a91 JR |
8469 | } |
8470 | cookies_insert(ofproto, rule); | |
8471 | eviction_group_add_rule(rule); | |
8472 | if (actions->has_meter) { | |
8473 | meter_insert_rule(rule); | |
8474 | } | |
cc099268 JR |
8475 | if (actions->has_groups) { |
8476 | const struct ofpact_group *a; | |
8477 | OFPACT_FOR_EACH_TYPE_FLATTENED (a, GROUP, actions->ofpacts, | |
8478 | actions->ofpacts_len) { | |
8479 | struct ofgroup *group; | |
8480 | ||
5d08a275 JR |
8481 | group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX, |
8482 | false); | |
cc099268 JR |
8483 | ovs_assert(group != NULL); |
8484 | group_add_rule(group, rule); | |
8485 | } | |
8486 | } | |
8487 | ||
30d0452c | 8488 | rule->state = RULE_INSERTED; |
bc5e6a91 JR |
8489 | } |
8490 | ||
6787a49f JR |
8491 | /* Removes 'rule' from the ofproto data structures. Caller may have deferred |
8492 | * the removal from the classifier. */ | |
d0918789 | 8493 | static void |
802f84ff | 8494 | ofproto_rule_remove__(struct ofproto *ofproto, struct rule *rule) |
15aaf599 | 8495 | OVS_REQUIRES(ofproto_mutex) |
d0918789 | 8496 | { |
30d0452c | 8497 | ovs_assert(rule->state == RULE_INSERTED); |
39c94593 | 8498 | |
98eaac36 | 8499 | cookies_remove(ofproto, rule); |
2c916028 | 8500 | |
254750ce | 8501 | eviction_group_remove_rule(rule); |
417e7e66 BW |
8502 | if (!ovs_list_is_empty(&rule->expirable)) { |
8503 | ovs_list_remove(&rule->expirable); | |
e503cc19 | 8504 | } |
417e7e66 BW |
8505 | if (!ovs_list_is_empty(&rule->meter_list_node)) { |
8506 | ovs_list_remove(&rule->meter_list_node); | |
8507 | ovs_list_init(&rule->meter_list_node); | |
9cae45dc | 8508 | } |
802f84ff | 8509 | |
cc099268 JR |
8510 | /* Remove the rule from any groups, except from the group that is being |
8511 | * deleted, if any. */ | |
8512 | const struct rule_actions *actions = rule_get_actions(rule); | |
8513 | ||
8514 | if (actions->has_groups) { | |
8515 | const struct ofpact_group *a; | |
8516 | ||
8483173b | 8517 | OFPACT_FOR_EACH_TYPE_FLATTENED(a, GROUP, actions->ofpacts, |
cc099268 JR |
8518 | actions->ofpacts_len) { |
8519 | struct ofgroup *group; | |
8520 | ||
5d08a275 JR |
8521 | group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX, |
8522 | false); | |
cc099268 JR |
8523 | ovs_assert(group); |
8524 | ||
8525 | /* Leave the rule for the group that is being deleted, if any, | |
8526 | * as we still need the list of rules for clean-up. */ | |
8527 | if (!group->being_deleted) { | |
8528 | group_remove_rule(group, rule); | |
8529 | } | |
8530 | } | |
8531 | } | |
8532 | ||
30d0452c | 8533 | rule->state = RULE_REMOVED; |
0b4f2078 | 8534 | } |
d0918789 | 8535 | \f |
abe529af | 8536 | /* unixctl commands. */ |
7aa697dd | 8537 | |
abe529af | 8538 | struct ofproto * |
2ac6bedd | 8539 | ofproto_lookup(const char *name) |
7aa697dd | 8540 | { |
2ac6bedd | 8541 | struct ofproto *ofproto; |
7aa697dd | 8542 | |
2ac6bedd BP |
8543 | HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0), |
8544 | &all_ofprotos) { | |
8545 | if (!strcmp(ofproto->name, name)) { | |
8546 | return ofproto; | |
8547 | } | |
7aa697dd | 8548 | } |
2ac6bedd | 8549 | return NULL; |
7aa697dd BP |
8550 | } |
8551 | ||
8552 | static void | |
0e15264f BP |
8553 | ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED, |
8554 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
7aa697dd | 8555 | { |
7aa697dd | 8556 | struct ofproto *ofproto; |
7aa697dd | 8557 | struct ds results; |
7aa697dd | 8558 | |
7aa697dd | 8559 | ds_init(&results); |
2ac6bedd BP |
8560 | HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) { |
8561 | ds_put_format(&results, "%s\n", ofproto->name); | |
7aa697dd | 8562 | } |
bde9f75d | 8563 | unixctl_command_reply(conn, ds_cstr(&results)); |
7aa697dd | 8564 | ds_destroy(&results); |
7aa697dd BP |
8565 | } |
8566 | ||
8567 | static void | |
8568 | ofproto_unixctl_init(void) | |
8569 | { | |
8570 | static bool registered; | |
8571 | if (registered) { | |
8572 | return; | |
8573 | } | |
8574 | registered = true; | |
8575 | ||
0e15264f BP |
8576 | unixctl_command_register("ofproto/list", "", 0, 0, |
8577 | ofproto_unixctl_list, NULL); | |
064af421 | 8578 | } |