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