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