]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
db5ce514 | 2 | * Copyright (c) 2009, 2010, 2011 Nicira Networks. |
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> | |
9d82ec47 | 22 | #include <sys/socket.h> |
064af421 BP |
23 | #include <net/if.h> |
24 | #include <netinet/in.h> | |
25 | #include <stdbool.h> | |
26 | #include <stdlib.h> | |
10a24935 | 27 | #include "byte-order.h" |
064af421 BP |
28 | #include "classifier.h" |
29 | #include "coverage.h" | |
30 | #include "discovery.h" | |
31 | #include "dpif.h" | |
4f2cad2c | 32 | #include "dynamic-string.h" |
064af421 | 33 | #include "fail-open.h" |
ca0f572c BP |
34 | #include "hash.h" |
35 | #include "hmap.h" | |
064af421 BP |
36 | #include "in-band.h" |
37 | #include "mac-learning.h" | |
53ddd40a | 38 | #include "multipath.h" |
064af421 BP |
39 | #include "netdev.h" |
40 | #include "netflow.h" | |
cdee00fd | 41 | #include "netlink.h" |
09246b99 | 42 | #include "nx-match.h" |
064af421 BP |
43 | #include "odp-util.h" |
44 | #include "ofp-print.h" | |
fa37b408 | 45 | #include "ofp-util.h" |
72b06300 | 46 | #include "ofproto-sflow.h" |
064af421 BP |
47 | #include "ofpbuf.h" |
48 | #include "openflow/nicira-ext.h" | |
49 | #include "openflow/openflow.h" | |
064af421 BP |
50 | #include "openvswitch/datapath-protocol.h" |
51 | #include "packets.h" | |
52 | #include "pinsched.h" | |
53 | #include "pktbuf.h" | |
54 | #include "poll-loop.h" | |
064af421 BP |
55 | #include "rconn.h" |
56 | #include "shash.h" | |
57 | #include "status.h" | |
fe55ad15 | 58 | #include "stream-ssl.h" |
064af421 BP |
59 | #include "svec.h" |
60 | #include "tag.h" | |
61 | #include "timeval.h" | |
c4617b3c | 62 | #include "unaligned.h" |
4f2cad2c | 63 | #include "unixctl.h" |
064af421 | 64 | #include "vconn.h" |
5136ce49 | 65 | #include "vlog.h" |
064af421 | 66 | |
d98e6007 | 67 | VLOG_DEFINE_THIS_MODULE(ofproto); |
064af421 | 68 | |
cc01d0bb BP |
69 | COVERAGE_DEFINE(facet_changed_rule); |
70 | COVERAGE_DEFINE(facet_revalidate); | |
d76f09ea | 71 | COVERAGE_DEFINE(odp_overflow); |
d76f09ea BP |
72 | COVERAGE_DEFINE(ofproto_agg_request); |
73 | COVERAGE_DEFINE(ofproto_costly_flags); | |
74 | COVERAGE_DEFINE(ofproto_ctlr_action); | |
cc01d0bb | 75 | COVERAGE_DEFINE(ofproto_del_rule); |
d76f09ea BP |
76 | COVERAGE_DEFINE(ofproto_error); |
77 | COVERAGE_DEFINE(ofproto_expiration); | |
78 | COVERAGE_DEFINE(ofproto_expired); | |
79 | COVERAGE_DEFINE(ofproto_flows_req); | |
80 | COVERAGE_DEFINE(ofproto_flush); | |
81 | COVERAGE_DEFINE(ofproto_invalidated); | |
d76f09ea | 82 | COVERAGE_DEFINE(ofproto_no_packet_in); |
d76f09ea BP |
83 | COVERAGE_DEFINE(ofproto_ofconn_stuck); |
84 | COVERAGE_DEFINE(ofproto_ofp2odp); | |
85 | COVERAGE_DEFINE(ofproto_packet_in); | |
86 | COVERAGE_DEFINE(ofproto_packet_out); | |
87 | COVERAGE_DEFINE(ofproto_queue_req); | |
88 | COVERAGE_DEFINE(ofproto_recv_openflow); | |
89 | COVERAGE_DEFINE(ofproto_reinit_ports); | |
d76f09ea BP |
90 | COVERAGE_DEFINE(ofproto_unexpected_rule); |
91 | COVERAGE_DEFINE(ofproto_uninstallable); | |
92 | COVERAGE_DEFINE(ofproto_update_port); | |
93 | ||
72b06300 | 94 | #include "sflow_api.h" |
064af421 | 95 | |
f29152ca BP |
96 | struct rule; |
97 | ||
064af421 | 98 | struct ofport { |
ca0f572c | 99 | struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */ |
064af421 BP |
100 | struct netdev *netdev; |
101 | struct ofp_phy_port opp; /* In host byte order. */ | |
ca0f572c | 102 | uint16_t odp_port; |
064af421 BP |
103 | }; |
104 | ||
105 | static void ofport_free(struct ofport *); | |
106 | static void hton_ofp_phy_port(struct ofp_phy_port *); | |
107 | ||
f29152ca BP |
108 | struct action_xlate_ctx { |
109 | /* action_xlate_ctx_init() initializes these members. */ | |
110 | ||
111 | /* The ofproto. */ | |
112 | struct ofproto *ofproto; | |
113 | ||
114 | /* Flow to which the OpenFlow actions apply. xlate_actions() will modify | |
115 | * this flow when actions change header fields. */ | |
116 | struct flow flow; | |
117 | ||
118 | /* The packet corresponding to 'flow', or a null pointer if we are | |
119 | * revalidating without a packet to refer to. */ | |
120 | const struct ofpbuf *packet; | |
121 | ||
7aa697dd BP |
122 | /* If nonnull, called just before executing a resubmit action. |
123 | * | |
124 | * This is normally null so the client has to set it manually after | |
125 | * calling action_xlate_ctx_init(). */ | |
126 | void (*resubmit_hook)(struct action_xlate_ctx *, const struct rule *); | |
127 | ||
ebe482fd EJ |
128 | /* If true, the speciality of 'flow' should be checked before executing |
129 | * its actions. If special_cb returns false on 'flow' rendered | |
130 | * uninstallable and no actions will be executed. */ | |
131 | bool check_special; | |
132 | ||
f29152ca BP |
133 | /* xlate_actions() initializes and uses these members. The client might want |
134 | * to look at them after it returns. */ | |
135 | ||
cdee00fd | 136 | struct ofpbuf *odp_actions; /* Datapath actions. */ |
f29152ca BP |
137 | tag_type tags; /* Tags associated with OFPP_NORMAL actions. */ |
138 | bool may_set_up_flow; /* True ordinarily; false if the actions must | |
139 | * be reassessed for every packet. */ | |
140 | uint16_t nf_output_iface; /* Output interface index for NetFlow. */ | |
141 | ||
142 | /* xlate_actions() initializes and uses these members, but the client has no | |
143 | * reason to look at them. */ | |
144 | ||
145 | int recurse; /* Recursion level, via xlate_table_action. */ | |
cdee00fd | 146 | int last_pop_priority; /* Offset in 'odp_actions' just past most |
7aec165d | 147 | * recent ODP_ACTION_ATTR_SET_PRIORITY. */ |
f29152ca BP |
148 | }; |
149 | ||
150 | static void action_xlate_ctx_init(struct action_xlate_ctx *, | |
151 | struct ofproto *, const struct flow *, | |
152 | const struct ofpbuf *); | |
cdee00fd BP |
153 | static struct ofpbuf *xlate_actions(struct action_xlate_ctx *, |
154 | const union ofp_action *in, size_t n_in); | |
064af421 | 155 | |
bcf84111 | 156 | /* An OpenFlow flow. */ |
064af421 | 157 | struct rule { |
0c43ad9a | 158 | long long int used; /* Time last used; time created if not used. */ |
064af421 | 159 | long long int created; /* Creation time. */ |
064af421 | 160 | |
bcf84111 | 161 | /* These statistics: |
064af421 | 162 | * |
bcf84111 BP |
163 | * - Do include packets and bytes from facets that have been deleted or |
164 | * whose own statistics have been folded into the rule. | |
79eee1eb | 165 | * |
bcf84111 BP |
166 | * - Do include packets and bytes sent "by hand" that were accounted to |
167 | * the rule without any facet being involved (this is a rare corner | |
168 | * case in rule_execute()). | |
064af421 | 169 | * |
bcf84111 BP |
170 | * - Do not include packet or bytes that can be obtained from any facet's |
171 | * packet_count or byte_count member or that can be obtained from the | |
172 | * datapath by, e.g., dpif_flow_get() for any facet. | |
173 | */ | |
174 | uint64_t packet_count; /* Number of packets received. */ | |
175 | uint64_t byte_count; /* Number of bytes received. */ | |
176 | ||
177 | ovs_be64 flow_cookie; /* Controller-issued identifier. */ | |
178 | ||
179 | struct cls_rule cr; /* In owning ofproto's classifier. */ | |
180 | uint16_t idle_timeout; /* In seconds from time of last use. */ | |
181 | uint16_t hard_timeout; /* In seconds from time of creation. */ | |
182 | bool send_flow_removed; /* Send a flow removed message? */ | |
183 | int n_actions; /* Number of elements in actions[]. */ | |
184 | union ofp_action *actions; /* OpenFlow actions. */ | |
185 | struct list facets; /* List of "struct facet"s. */ | |
064af421 BP |
186 | }; |
187 | ||
bcf84111 BP |
188 | static struct rule *rule_from_cls_rule(const struct cls_rule *); |
189 | static bool rule_is_hidden(const struct rule *); | |
064af421 | 190 | |
bcf84111 | 191 | static struct rule *rule_create(const struct cls_rule *, |
0193b2af | 192 | const union ofp_action *, size_t n_actions, |
ca069229 | 193 | uint16_t idle_timeout, uint16_t hard_timeout, |
8054fc48 | 194 | ovs_be64 flow_cookie, bool send_flow_removed); |
064af421 | 195 | static void rule_destroy(struct ofproto *, struct rule *); |
bcf84111 BP |
196 | static void rule_free(struct rule *); |
197 | ||
198 | static struct rule *rule_lookup(struct ofproto *, const struct flow *); | |
afe75089 | 199 | static void rule_insert(struct ofproto *, struct rule *); |
064af421 | 200 | static void rule_remove(struct ofproto *, struct rule *); |
bcf84111 BP |
201 | |
202 | static void rule_send_removed(struct ofproto *, struct rule *, uint8_t reason); | |
203 | ||
204 | /* An exact-match instantiation of an OpenFlow flow. */ | |
205 | struct facet { | |
206 | long long int used; /* Time last used; time created if not used. */ | |
207 | ||
208 | /* These statistics: | |
209 | * | |
210 | * - Do include packets and bytes sent "by hand", e.g. with | |
211 | * dpif_execute(). | |
212 | * | |
213 | * - Do include packets and bytes that were obtained from the datapath | |
214 | * when a flow was deleted (e.g. dpif_flow_del()) or when its | |
ba25b8f4 BP |
215 | * statistics were reset (e.g. dpif_flow_put() with |
216 | * DPIF_FP_ZERO_STATS). | |
bcf84111 BP |
217 | * |
218 | * - Do not include any packets or bytes that can currently be obtained | |
219 | * from the datapath by, e.g., dpif_flow_get(). | |
220 | */ | |
221 | uint64_t packet_count; /* Number of packets received. */ | |
222 | uint64_t byte_count; /* Number of bytes received. */ | |
223 | ||
224 | /* Number of bytes passed to account_cb. This may include bytes that can | |
225 | * currently obtained from the datapath (thus, it can be greater than | |
226 | * byte_count). */ | |
227 | uint64_t accounted_bytes; | |
228 | ||
229 | struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */ | |
230 | struct list list_node; /* In owning rule's 'facets' list. */ | |
231 | struct rule *rule; /* Owning rule. */ | |
232 | struct flow flow; /* Exact-match flow. */ | |
233 | bool installed; /* Installed in datapath? */ | |
234 | bool may_install; /* True ordinarily; false if actions must | |
235 | * be reassessed for every packet. */ | |
cf22f8cb | 236 | size_t actions_len; /* Number of bytes in actions[]. */ |
cdee00fd | 237 | struct nlattr *actions; /* Datapath actions. */ |
bcf84111 BP |
238 | tag_type tags; /* Tags (set only by hooks). */ |
239 | struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */ | |
240 | }; | |
241 | ||
242 | static struct facet *facet_create(struct ofproto *, struct rule *, | |
243 | const struct flow *, | |
244 | const struct ofpbuf *packet); | |
245 | static void facet_remove(struct ofproto *, struct facet *); | |
246 | static void facet_free(struct facet *); | |
247 | ||
248 | static struct facet *facet_lookup_valid(struct ofproto *, const struct flow *); | |
249 | static bool facet_revalidate(struct ofproto *, struct facet *); | |
250 | ||
251 | static void facet_install(struct ofproto *, struct facet *, bool zero_stats); | |
252 | static void facet_uninstall(struct ofproto *, struct facet *); | |
d530fcd2 | 253 | static void facet_flush_stats(struct ofproto *, struct facet *); |
bcf84111 | 254 | |
7f7ae89d BP |
255 | static void facet_make_actions(struct ofproto *, struct facet *, |
256 | const struct ofpbuf *packet); | |
bcf84111 | 257 | static void facet_update_stats(struct ofproto *, struct facet *, |
c97fb132 | 258 | const struct dpif_flow_stats *); |
064af421 | 259 | |
76ce9432 BP |
260 | /* ofproto supports two kinds of OpenFlow connections: |
261 | * | |
5899143f BP |
262 | * - "Primary" connections to ordinary OpenFlow controllers. ofproto |
263 | * maintains persistent connections to these controllers and by default | |
264 | * sends them asynchronous messages such as packet-ins. | |
76ce9432 | 265 | * |
5899143f | 266 | * - "Service" connections, e.g. from ovs-ofctl. When these connections |
76ce9432 BP |
267 | * drop, it is the other side's responsibility to reconnect them if |
268 | * necessary. ofproto does not send them asynchronous messages by default. | |
7d674866 BP |
269 | * |
270 | * Currently, active (tcp, ssl, unix) connections are always "primary" | |
271 | * connections and passive (ptcp, pssl, punix) connections are always "service" | |
272 | * connections. There is no inherent reason for this, but it reflects the | |
273 | * common case. | |
76ce9432 BP |
274 | */ |
275 | enum ofconn_type { | |
5899143f BP |
276 | OFCONN_PRIMARY, /* An ordinary OpenFlow controller. */ |
277 | OFCONN_SERVICE /* A service connection, e.g. "ovs-ofctl". */ | |
76ce9432 | 278 | }; |
064af421 | 279 | |
7d674866 BP |
280 | /* A listener for incoming OpenFlow "service" connections. */ |
281 | struct ofservice { | |
282 | struct hmap_node node; /* In struct ofproto's "services" hmap. */ | |
283 | struct pvconn *pvconn; /* OpenFlow connection listener. */ | |
284 | ||
285 | /* These are not used by ofservice directly. They are settings for | |
286 | * accepted "struct ofconn"s from the pvconn. */ | |
287 | int probe_interval; /* Max idle time before probing, in seconds. */ | |
288 | int rate_limit; /* Max packet-in rate in packets per second. */ | |
289 | int burst_limit; /* Limit on accumulating packet credits. */ | |
290 | }; | |
291 | ||
292 | static struct ofservice *ofservice_lookup(struct ofproto *, | |
293 | const char *target); | |
294 | static int ofservice_create(struct ofproto *, | |
295 | const struct ofproto_controller *); | |
296 | static void ofservice_reconfigure(struct ofservice *, | |
297 | const struct ofproto_controller *); | |
298 | static void ofservice_destroy(struct ofproto *, struct ofservice *); | |
299 | ||
76ce9432 BP |
300 | /* An OpenFlow connection. */ |
301 | struct ofconn { | |
302 | struct ofproto *ofproto; /* The ofproto that owns this connection. */ | |
303 | struct list node; /* In struct ofproto's "all_conns" list. */ | |
304 | struct rconn *rconn; /* OpenFlow connection. */ | |
305 | enum ofconn_type type; /* Type. */ | |
492f7572 | 306 | enum nx_flow_format flow_format; /* Currently selected flow format. */ |
76ce9432 BP |
307 | |
308 | /* OFPT_PACKET_IN related data. */ | |
309 | struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */ | |
82272ede BP |
310 | #define N_SCHEDULERS 2 |
311 | struct pinsched *schedulers[N_SCHEDULERS]; | |
76ce9432 BP |
312 | struct pktbuf *pktbuf; /* OpenFlow packet buffers. */ |
313 | int miss_send_len; /* Bytes to send of buffered packets. */ | |
314 | ||
315 | /* Number of OpenFlow messages queued on 'rconn' as replies to OpenFlow | |
316 | * requests, and the maximum number before we stop reading OpenFlow | |
317 | * requests. */ | |
064af421 BP |
318 | #define OFCONN_REPLY_MAX 100 |
319 | struct rconn_packet_counter *reply_counter; | |
76ce9432 | 320 | |
5899143f | 321 | /* type == OFCONN_PRIMARY only. */ |
9deba63b | 322 | enum nx_role role; /* Role. */ |
76ce9432 BP |
323 | struct hmap_node hmap_node; /* In struct ofproto's "controllers" map. */ |
324 | struct discovery *discovery; /* Controller discovery object, if enabled. */ | |
325 | struct status_category *ss; /* Switch status category. */ | |
d2ede7bc | 326 | enum ofproto_band band; /* In-band or out-of-band? */ |
064af421 BP |
327 | }; |
328 | ||
76ce9432 BP |
329 | |
330 | static struct ofconn *ofconn_create(struct ofproto *, struct rconn *, | |
331 | enum ofconn_type); | |
c475ae67 | 332 | static void ofconn_destroy(struct ofconn *); |
3269c562 | 333 | static void ofconn_run(struct ofconn *); |
064af421 | 334 | static void ofconn_wait(struct ofconn *); |
c91248b3 | 335 | static bool ofconn_receives_async_msgs(const struct ofconn *); |
eb15cdbb | 336 | static char *ofconn_make_name(const struct ofproto *, const char *target); |
7d674866 | 337 | static void ofconn_set_rate_limit(struct ofconn *, int rate, int burst); |
c91248b3 | 338 | |
064af421 BP |
339 | static void queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn, |
340 | struct rconn_packet_counter *counter); | |
341 | ||
856081f6 BP |
342 | static void send_packet_in(struct ofproto *, struct dpif_upcall *, |
343 | const struct flow *, bool clone); | |
344 | static void do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn); | |
76ce9432 | 345 | |
064af421 BP |
346 | struct ofproto { |
347 | /* Settings. */ | |
348 | uint64_t datapath_id; /* Datapath ID. */ | |
349 | uint64_t fallback_dpid; /* Datapath ID if no better choice found. */ | |
5a719c38 JP |
350 | char *mfr_desc; /* Manufacturer. */ |
351 | char *hw_desc; /* Hardware. */ | |
352 | char *sw_desc; /* Software version. */ | |
353 | char *serial_desc; /* Serial number. */ | |
8abc4ed7 | 354 | char *dp_desc; /* Datapath description. */ |
064af421 BP |
355 | |
356 | /* Datapath. */ | |
c228a364 | 357 | struct dpif *dpif; |
e9e28be3 | 358 | struct netdev_monitor *netdev_monitor; |
ca0f572c | 359 | struct hmap ports; /* Contains "struct ofport"s. */ |
064af421 BP |
360 | struct shash port_by_name; |
361 | uint32_t max_ports; | |
362 | ||
363 | /* Configuration. */ | |
364 | struct switch_status *switch_status; | |
064af421 | 365 | struct fail_open *fail_open; |
064af421 | 366 | struct netflow *netflow; |
72b06300 | 367 | struct ofproto_sflow *sflow; |
064af421 | 368 | |
d2ede7bc BP |
369 | /* In-band control. */ |
370 | struct in_band *in_band; | |
371 | long long int next_in_band_update; | |
917e50e1 BP |
372 | struct sockaddr_in *extra_in_band_remotes; |
373 | size_t n_extra_remotes; | |
b1da6250 | 374 | int in_band_queue; |
917e50e1 | 375 | |
064af421 BP |
376 | /* Flow table. */ |
377 | struct classifier cls; | |
064af421 | 378 | long long int next_expiration; |
bcf84111 BP |
379 | |
380 | /* Facets. */ | |
381 | struct hmap facets; | |
382 | bool need_revalidate; | |
064af421 BP |
383 | struct tag_set revalidate_set; |
384 | ||
385 | /* OpenFlow connections. */ | |
76ce9432 BP |
386 | struct hmap controllers; /* Controller "struct ofconn"s. */ |
387 | struct list all_conns; /* Contains "struct ofconn"s. */ | |
31681a5d | 388 | enum ofproto_fail_mode fail_mode; |
7d674866 BP |
389 | |
390 | /* OpenFlow listeners. */ | |
391 | struct hmap services; /* Contains "struct ofservice"s. */ | |
064af421 BP |
392 | struct pvconn **snoops; |
393 | size_t n_snoops; | |
394 | ||
395 | /* Hooks for ovs-vswitchd. */ | |
396 | const struct ofhooks *ofhooks; | |
397 | void *aux; | |
398 | ||
399 | /* Used by default ofhooks. */ | |
400 | struct mac_learning *ml; | |
401 | }; | |
402 | ||
7aa697dd BP |
403 | /* Map from dpif name to struct ofproto, for use by unixctl commands. */ |
404 | static struct shash all_ofprotos = SHASH_INITIALIZER(&all_ofprotos); | |
405 | ||
064af421 BP |
406 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
407 | ||
408 | static const struct ofhooks default_ofhooks; | |
409 | ||
fa60c019 | 410 | static uint64_t pick_datapath_id(const struct ofproto *); |
064af421 | 411 | static uint64_t pick_fallback_dpid(void); |
76ce9432 | 412 | |
0de7a4b4 | 413 | static int ofproto_expire(struct ofproto *); |
4a4cdb3b | 414 | |
856081f6 | 415 | static void handle_upcall(struct ofproto *, struct dpif_upcall *); |
064af421 | 416 | |
3269c562 | 417 | static void handle_openflow(struct ofconn *, struct ofpbuf *); |
064af421 | 418 | |
ca0f572c | 419 | static struct ofport *get_port(const struct ofproto *, uint16_t odp_port); |
064af421 BP |
420 | static void update_port(struct ofproto *, const char *devname); |
421 | static int init_ports(struct ofproto *); | |
422 | static void reinit_ports(struct ofproto *); | |
423 | ||
7aa697dd BP |
424 | static void ofproto_unixctl_init(void); |
425 | ||
064af421 | 426 | int |
1a6f1e2a JG |
427 | ofproto_create(const char *datapath, const char *datapath_type, |
428 | const struct ofhooks *ofhooks, void *aux, | |
064af421 BP |
429 | struct ofproto **ofprotop) |
430 | { | |
064af421 | 431 | struct ofproto *p; |
c228a364 | 432 | struct dpif *dpif; |
064af421 BP |
433 | int error; |
434 | ||
435 | *ofprotop = NULL; | |
436 | ||
7aa697dd BP |
437 | ofproto_unixctl_init(); |
438 | ||
064af421 | 439 | /* Connect to datapath and start listening for messages. */ |
1a6f1e2a | 440 | error = dpif_open(datapath, datapath_type, &dpif); |
064af421 BP |
441 | if (error) { |
442 | VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error)); | |
443 | return error; | |
444 | } | |
82272ede BP |
445 | error = dpif_recv_set_mask(dpif, |
446 | ((1u << DPIF_UC_MISS) | | |
447 | (1u << DPIF_UC_ACTION) | | |
448 | (1u << DPIF_UC_SAMPLE))); | |
064af421 BP |
449 | if (error) { |
450 | VLOG_ERR("failed to listen on datapath %s: %s", | |
451 | datapath, strerror(error)); | |
c228a364 | 452 | dpif_close(dpif); |
064af421 BP |
453 | return error; |
454 | } | |
c228a364 | 455 | dpif_flow_flush(dpif); |
8f24562a | 456 | dpif_recv_purge(dpif); |
064af421 BP |
457 | |
458 | /* Initialize settings. */ | |
ec6fde61 | 459 | p = xzalloc(sizeof *p); |
064af421 | 460 | p->fallback_dpid = pick_fallback_dpid(); |
fa60c019 | 461 | p->datapath_id = p->fallback_dpid; |
5a719c38 JP |
462 | p->mfr_desc = xstrdup(DEFAULT_MFR_DESC); |
463 | p->hw_desc = xstrdup(DEFAULT_HW_DESC); | |
464 | p->sw_desc = xstrdup(DEFAULT_SW_DESC); | |
465 | p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC); | |
23ff2821 | 466 | p->dp_desc = xstrdup(DEFAULT_DP_DESC); |
064af421 BP |
467 | |
468 | /* Initialize datapath. */ | |
469 | p->dpif = dpif; | |
8b61709d | 470 | p->netdev_monitor = netdev_monitor_create(); |
ca0f572c | 471 | hmap_init(&p->ports); |
064af421 | 472 | shash_init(&p->port_by_name); |
996c1b3d | 473 | p->max_ports = dpif_get_max_ports(dpif); |
064af421 BP |
474 | |
475 | /* Initialize submodules. */ | |
476 | p->switch_status = switch_status_create(p); | |
064af421 | 477 | p->fail_open = NULL; |
064af421 | 478 | p->netflow = NULL; |
72b06300 | 479 | p->sflow = NULL; |
064af421 | 480 | |
b1da6250 BP |
481 | /* Initialize in-band control. */ |
482 | p->in_band = NULL; | |
483 | p->in_band_queue = -1; | |
484 | ||
064af421 BP |
485 | /* Initialize flow table. */ |
486 | classifier_init(&p->cls); | |
064af421 | 487 | p->next_expiration = time_msec() + 1000; |
bcf84111 BP |
488 | |
489 | /* Initialize facet table. */ | |
490 | hmap_init(&p->facets); | |
491 | p->need_revalidate = false; | |
064af421 BP |
492 | tag_set_init(&p->revalidate_set); |
493 | ||
494 | /* Initialize OpenFlow connections. */ | |
495 | list_init(&p->all_conns); | |
76ce9432 | 496 | hmap_init(&p->controllers); |
7d674866 | 497 | hmap_init(&p->services); |
064af421 BP |
498 | p->snoops = NULL; |
499 | p->n_snoops = 0; | |
500 | ||
501 | /* Initialize hooks. */ | |
502 | if (ofhooks) { | |
503 | p->ofhooks = ofhooks; | |
504 | p->aux = aux; | |
505 | p->ml = NULL; | |
506 | } else { | |
507 | p->ofhooks = &default_ofhooks; | |
508 | p->aux = p; | |
509 | p->ml = mac_learning_create(); | |
510 | } | |
511 | ||
fa60c019 BP |
512 | /* Pick final datapath ID. */ |
513 | p->datapath_id = pick_datapath_id(p); | |
b123cc3c | 514 | VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id); |
fa60c019 | 515 | |
7aa697dd BP |
516 | shash_add_once(&all_ofprotos, dpif_name(p->dpif), p); |
517 | ||
064af421 BP |
518 | *ofprotop = p; |
519 | return 0; | |
520 | } | |
521 | ||
522 | void | |
523 | ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id) | |
524 | { | |
525 | uint64_t old_dpid = p->datapath_id; | |
fa60c019 | 526 | p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p); |
064af421 | 527 | if (p->datapath_id != old_dpid) { |
b123cc3c | 528 | VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id); |
76ce9432 BP |
529 | |
530 | /* Force all active connections to reconnect, since there is no way to | |
531 | * notify a controller that the datapath ID has changed. */ | |
fa05809b | 532 | ofproto_reconnect_controllers(p); |
064af421 BP |
533 | } |
534 | } | |
535 | ||
76ce9432 BP |
536 | static bool |
537 | is_discovery_controller(const struct ofproto_controller *c) | |
538 | { | |
539 | return !strcmp(c->target, "discover"); | |
540 | } | |
541 | ||
542 | static bool | |
543 | is_in_band_controller(const struct ofproto_controller *c) | |
544 | { | |
545 | return is_discovery_controller(c) || c->band == OFPROTO_IN_BAND; | |
546 | } | |
547 | ||
548 | /* Creates a new controller in 'ofproto'. Some of the settings are initially | |
549 | * drawn from 'c', but update_controller() needs to be called later to finish | |
550 | * the new ofconn's configuration. */ | |
551 | static void | |
552 | add_controller(struct ofproto *ofproto, const struct ofproto_controller *c) | |
553 | { | |
554 | struct discovery *discovery; | |
555 | struct ofconn *ofconn; | |
556 | ||
557 | if (is_discovery_controller(c)) { | |
558 | int error = discovery_create(c->accept_re, c->update_resolv_conf, | |
559 | ofproto->dpif, ofproto->switch_status, | |
560 | &discovery); | |
561 | if (error) { | |
562 | return; | |
563 | } | |
564 | } else { | |
565 | discovery = NULL; | |
566 | } | |
567 | ||
5899143f | 568 | ofconn = ofconn_create(ofproto, rconn_create(5, 8), OFCONN_PRIMARY); |
76ce9432 BP |
569 | ofconn->pktbuf = pktbuf_create(); |
570 | ofconn->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN; | |
571 | if (discovery) { | |
572 | ofconn->discovery = discovery; | |
573 | } else { | |
eb15cdbb BP |
574 | char *name = ofconn_make_name(ofproto, c->target); |
575 | rconn_connect(ofconn->rconn, c->target, name); | |
576 | free(name); | |
76ce9432 BP |
577 | } |
578 | hmap_insert(&ofproto->controllers, &ofconn->hmap_node, | |
579 | hash_string(c->target, 0)); | |
580 | } | |
581 | ||
582 | /* Reconfigures 'ofconn' to match 'c'. This function cannot update an ofconn's | |
583 | * target or turn discovery on or off (these are done by creating new ofconns | |
584 | * and deleting old ones), but it can update the rest of an ofconn's | |
585 | * settings. */ | |
586 | static void | |
587 | update_controller(struct ofconn *ofconn, const struct ofproto_controller *c) | |
064af421 | 588 | { |
76ce9432 | 589 | int probe_interval; |
79c9f2ee | 590 | |
d2ede7bc BP |
591 | ofconn->band = (is_in_band_controller(c) |
592 | ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND); | |
593 | ||
76ce9432 | 594 | rconn_set_max_backoff(ofconn->rconn, c->max_backoff); |
79c9f2ee | 595 | |
76ce9432 BP |
596 | probe_interval = c->probe_interval ? MAX(c->probe_interval, 5) : 0; |
597 | rconn_set_probe_interval(ofconn->rconn, probe_interval); | |
79c9f2ee | 598 | |
76ce9432 BP |
599 | if (ofconn->discovery) { |
600 | discovery_set_update_resolv_conf(ofconn->discovery, | |
601 | c->update_resolv_conf); | |
602 | discovery_set_accept_controller_re(ofconn->discovery, c->accept_re); | |
603 | } | |
79c9f2ee | 604 | |
7d674866 | 605 | ofconn_set_rate_limit(ofconn, c->rate_limit, c->burst_limit); |
76ce9432 | 606 | } |
79c9f2ee | 607 | |
76ce9432 BP |
608 | static const char * |
609 | ofconn_get_target(const struct ofconn *ofconn) | |
610 | { | |
eb15cdbb | 611 | return ofconn->discovery ? "discover" : rconn_get_target(ofconn->rconn); |
76ce9432 BP |
612 | } |
613 | ||
614 | static struct ofconn * | |
615 | find_controller_by_target(struct ofproto *ofproto, const char *target) | |
616 | { | |
617 | struct ofconn *ofconn; | |
618 | ||
4e8e4213 | 619 | HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node, |
76ce9432 BP |
620 | hash_string(target, 0), &ofproto->controllers) { |
621 | if (!strcmp(ofconn_get_target(ofconn), target)) { | |
622 | return ofconn; | |
79c9f2ee | 623 | } |
064af421 | 624 | } |
76ce9432 BP |
625 | return NULL; |
626 | } | |
064af421 | 627 | |
d2ede7bc BP |
628 | static void |
629 | update_in_band_remotes(struct ofproto *ofproto) | |
630 | { | |
631 | const struct ofconn *ofconn; | |
632 | struct sockaddr_in *addrs; | |
917e50e1 | 633 | size_t max_addrs, n_addrs; |
d2ede7bc | 634 | bool discovery; |
917e50e1 | 635 | size_t i; |
d2ede7bc | 636 | |
917e50e1 BP |
637 | /* Allocate enough memory for as many remotes as we could possibly have. */ |
638 | max_addrs = ofproto->n_extra_remotes + hmap_count(&ofproto->controllers); | |
639 | addrs = xmalloc(max_addrs * sizeof *addrs); | |
d2ede7bc BP |
640 | n_addrs = 0; |
641 | ||
642 | /* Add all the remotes. */ | |
643 | discovery = false; | |
4e8e4213 | 644 | HMAP_FOR_EACH (ofconn, hmap_node, &ofproto->controllers) { |
d2ede7bc BP |
645 | struct sockaddr_in *sin = &addrs[n_addrs]; |
646 | ||
487ec65f BP |
647 | if (ofconn->band == OFPROTO_OUT_OF_BAND) { |
648 | continue; | |
649 | } | |
650 | ||
d2ede7bc BP |
651 | sin->sin_addr.s_addr = rconn_get_remote_ip(ofconn->rconn); |
652 | if (sin->sin_addr.s_addr) { | |
653 | sin->sin_port = rconn_get_remote_port(ofconn->rconn); | |
654 | n_addrs++; | |
655 | } | |
656 | if (ofconn->discovery) { | |
657 | discovery = true; | |
658 | } | |
659 | } | |
917e50e1 BP |
660 | for (i = 0; i < ofproto->n_extra_remotes; i++) { |
661 | addrs[n_addrs++] = ofproto->extra_in_band_remotes[i]; | |
662 | } | |
d2ede7bc BP |
663 | |
664 | /* Create or update or destroy in-band. | |
665 | * | |
666 | * Ordinarily we only enable in-band if there's at least one remote | |
667 | * address, but discovery needs the in-band rules for DHCP to be installed | |
668 | * even before we know any remote addresses. */ | |
669 | if (n_addrs || discovery) { | |
670 | if (!ofproto->in_band) { | |
671 | in_band_create(ofproto, ofproto->dpif, ofproto->switch_status, | |
672 | &ofproto->in_band); | |
673 | } | |
40cae670 BP |
674 | if (ofproto->in_band) { |
675 | in_band_set_remotes(ofproto->in_band, addrs, n_addrs); | |
676 | } | |
b1da6250 | 677 | in_band_set_queue(ofproto->in_band, ofproto->in_band_queue); |
d2ede7bc BP |
678 | ofproto->next_in_band_update = time_msec() + 1000; |
679 | } else { | |
680 | in_band_destroy(ofproto->in_band); | |
681 | ofproto->in_band = NULL; | |
682 | } | |
683 | ||
684 | /* Clean up. */ | |
685 | free(addrs); | |
686 | } | |
687 | ||
31681a5d JP |
688 | static void |
689 | update_fail_open(struct ofproto *p) | |
690 | { | |
691 | struct ofconn *ofconn; | |
692 | ||
693 | if (!hmap_is_empty(&p->controllers) | |
694 | && p->fail_mode == OFPROTO_FAIL_STANDALONE) { | |
695 | struct rconn **rconns; | |
696 | size_t n; | |
697 | ||
698 | if (!p->fail_open) { | |
699 | p->fail_open = fail_open_create(p, p->switch_status); | |
700 | } | |
701 | ||
702 | n = 0; | |
703 | rconns = xmalloc(hmap_count(&p->controllers) * sizeof *rconns); | |
4e8e4213 | 704 | HMAP_FOR_EACH (ofconn, hmap_node, &p->controllers) { |
31681a5d JP |
705 | rconns[n++] = ofconn->rconn; |
706 | } | |
707 | ||
708 | fail_open_set_controllers(p->fail_open, rconns, n); | |
709 | /* p->fail_open takes ownership of 'rconns'. */ | |
710 | } else { | |
711 | fail_open_destroy(p->fail_open); | |
712 | p->fail_open = NULL; | |
713 | } | |
714 | } | |
715 | ||
76ce9432 BP |
716 | void |
717 | ofproto_set_controllers(struct ofproto *p, | |
718 | const struct ofproto_controller *controllers, | |
719 | size_t n_controllers) | |
720 | { | |
721 | struct shash new_controllers; | |
7d674866 BP |
722 | struct ofconn *ofconn, *next_ofconn; |
723 | struct ofservice *ofservice, *next_ofservice; | |
76ce9432 | 724 | bool ss_exists; |
76ce9432 | 725 | size_t i; |
79c9f2ee | 726 | |
7d674866 BP |
727 | /* Create newly configured controllers and services. |
728 | * Create a name to ofproto_controller mapping in 'new_controllers'. */ | |
76ce9432 BP |
729 | shash_init(&new_controllers); |
730 | for (i = 0; i < n_controllers; i++) { | |
731 | const struct ofproto_controller *c = &controllers[i]; | |
732 | ||
7d674866 BP |
733 | if (!vconn_verify_name(c->target) || !strcmp(c->target, "discover")) { |
734 | if (!find_controller_by_target(p, c->target)) { | |
735 | add_controller(p, c); | |
736 | } | |
737 | } else if (!pvconn_verify_name(c->target)) { | |
738 | if (!ofservice_lookup(p, c->target) && ofservice_create(p, c)) { | |
739 | continue; | |
740 | } | |
741 | } else { | |
742 | VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"", | |
743 | dpif_name(p->dpif), c->target); | |
744 | continue; | |
76ce9432 | 745 | } |
7d674866 BP |
746 | |
747 | shash_add_once(&new_controllers, c->target, &controllers[i]); | |
76ce9432 BP |
748 | } |
749 | ||
7d674866 BP |
750 | /* Delete controllers that are no longer configured. |
751 | * Update configuration of all now-existing controllers. */ | |
76ce9432 | 752 | ss_exists = false; |
4e8e4213 | 753 | HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &p->controllers) { |
76ce9432 BP |
754 | struct ofproto_controller *c; |
755 | ||
756 | c = shash_find_data(&new_controllers, ofconn_get_target(ofconn)); | |
757 | if (!c) { | |
758 | ofconn_destroy(ofconn); | |
79c9f2ee | 759 | } else { |
76ce9432 | 760 | update_controller(ofconn, c); |
76ce9432 BP |
761 | if (ofconn->ss) { |
762 | ss_exists = true; | |
763 | } | |
76ce9432 BP |
764 | } |
765 | } | |
7d674866 BP |
766 | |
767 | /* Delete services that are no longer configured. | |
768 | * Update configuration of all now-existing services. */ | |
4e8e4213 | 769 | HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &p->services) { |
7d674866 BP |
770 | struct ofproto_controller *c; |
771 | ||
772 | c = shash_find_data(&new_controllers, | |
773 | pvconn_get_name(ofservice->pvconn)); | |
774 | if (!c) { | |
775 | ofservice_destroy(p, ofservice); | |
776 | } else { | |
777 | ofservice_reconfigure(ofservice, c); | |
778 | } | |
779 | } | |
780 | ||
76ce9432 BP |
781 | shash_destroy(&new_controllers); |
782 | ||
d2ede7bc | 783 | update_in_band_remotes(p); |
31681a5d | 784 | update_fail_open(p); |
79c9f2ee | 785 | |
76ce9432 BP |
786 | if (!hmap_is_empty(&p->controllers) && !ss_exists) { |
787 | ofconn = CONTAINER_OF(hmap_first(&p->controllers), | |
788 | struct ofconn, hmap_node); | |
789 | ofconn->ss = switch_status_register(p->switch_status, "remote", | |
790 | rconn_status_cb, ofconn->rconn); | |
79c9f2ee | 791 | } |
064af421 BP |
792 | } |
793 | ||
31681a5d JP |
794 | void |
795 | ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode) | |
796 | { | |
797 | p->fail_mode = fail_mode; | |
798 | update_fail_open(p); | |
799 | } | |
800 | ||
fa05809b BP |
801 | /* Drops the connections between 'ofproto' and all of its controllers, forcing |
802 | * them to reconnect. */ | |
803 | void | |
804 | ofproto_reconnect_controllers(struct ofproto *ofproto) | |
805 | { | |
806 | struct ofconn *ofconn; | |
807 | ||
4e8e4213 | 808 | LIST_FOR_EACH (ofconn, node, &ofproto->all_conns) { |
fa05809b BP |
809 | rconn_reconnect(ofconn->rconn); |
810 | } | |
811 | } | |
812 | ||
917e50e1 BP |
813 | static bool |
814 | any_extras_changed(const struct ofproto *ofproto, | |
815 | const struct sockaddr_in *extras, size_t n) | |
816 | { | |
817 | size_t i; | |
818 | ||
819 | if (n != ofproto->n_extra_remotes) { | |
820 | return true; | |
821 | } | |
822 | ||
823 | for (i = 0; i < n; i++) { | |
824 | const struct sockaddr_in *old = &ofproto->extra_in_band_remotes[i]; | |
825 | const struct sockaddr_in *new = &extras[i]; | |
826 | ||
827 | if (old->sin_addr.s_addr != new->sin_addr.s_addr || | |
828 | old->sin_port != new->sin_port) { | |
829 | return true; | |
830 | } | |
831 | } | |
832 | ||
833 | return false; | |
834 | } | |
835 | ||
836 | /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s | |
837 | * in-band control should guarantee access, in the same way that in-band | |
838 | * control guarantees access to OpenFlow controllers. */ | |
839 | void | |
840 | ofproto_set_extra_in_band_remotes(struct ofproto *ofproto, | |
841 | const struct sockaddr_in *extras, size_t n) | |
842 | { | |
843 | if (!any_extras_changed(ofproto, extras, n)) { | |
844 | return; | |
845 | } | |
846 | ||
847 | free(ofproto->extra_in_band_remotes); | |
848 | ofproto->n_extra_remotes = n; | |
849 | ofproto->extra_in_band_remotes = xmemdup(extras, n * sizeof *extras); | |
850 | ||
851 | update_in_band_remotes(ofproto); | |
852 | } | |
853 | ||
b1da6250 BP |
854 | /* Sets the OpenFlow queue used by flows set up by in-band control on |
855 | * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control | |
856 | * flows will use the default queue. */ | |
857 | void | |
858 | ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id) | |
859 | { | |
860 | if (queue_id != ofproto->in_band_queue) { | |
861 | ofproto->in_band_queue = queue_id; | |
862 | update_in_band_remotes(ofproto); | |
863 | } | |
864 | } | |
865 | ||
064af421 BP |
866 | void |
867 | ofproto_set_desc(struct ofproto *p, | |
5a719c38 JP |
868 | const char *mfr_desc, const char *hw_desc, |
869 | const char *sw_desc, const char *serial_desc, | |
8abc4ed7 | 870 | const char *dp_desc) |
064af421 | 871 | { |
5a719c38 JP |
872 | struct ofp_desc_stats *ods; |
873 | ||
874 | if (mfr_desc) { | |
875 | if (strlen(mfr_desc) >= sizeof ods->mfr_desc) { | |
876 | VLOG_WARN("truncating mfr_desc, must be less than %zu characters", | |
877 | sizeof ods->mfr_desc); | |
878 | } | |
879 | free(p->mfr_desc); | |
880 | p->mfr_desc = xstrdup(mfr_desc); | |
064af421 | 881 | } |
5a719c38 JP |
882 | if (hw_desc) { |
883 | if (strlen(hw_desc) >= sizeof ods->hw_desc) { | |
884 | VLOG_WARN("truncating hw_desc, must be less than %zu characters", | |
885 | sizeof ods->hw_desc); | |
886 | } | |
887 | free(p->hw_desc); | |
888 | p->hw_desc = xstrdup(hw_desc); | |
064af421 | 889 | } |
5a719c38 JP |
890 | if (sw_desc) { |
891 | if (strlen(sw_desc) >= sizeof ods->sw_desc) { | |
892 | VLOG_WARN("truncating sw_desc, must be less than %zu characters", | |
893 | sizeof ods->sw_desc); | |
894 | } | |
895 | free(p->sw_desc); | |
896 | p->sw_desc = xstrdup(sw_desc); | |
897 | } | |
898 | if (serial_desc) { | |
899 | if (strlen(serial_desc) >= sizeof ods->serial_num) { | |
900 | VLOG_WARN("truncating serial_desc, must be less than %zu " | |
901 | "characters", | |
902 | sizeof ods->serial_num); | |
903 | } | |
904 | free(p->serial_desc); | |
905 | p->serial_desc = xstrdup(serial_desc); | |
064af421 | 906 | } |
8abc4ed7 | 907 | if (dp_desc) { |
5a719c38 JP |
908 | if (strlen(dp_desc) >= sizeof ods->dp_desc) { |
909 | VLOG_WARN("truncating dp_desc, must be less than %zu characters", | |
910 | sizeof ods->dp_desc); | |
911 | } | |
8abc4ed7 JP |
912 | free(p->dp_desc); |
913 | p->dp_desc = xstrdup(dp_desc); | |
914 | } | |
064af421 BP |
915 | } |
916 | ||
064af421 BP |
917 | static int |
918 | set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp, | |
919 | const struct svec *svec) | |
920 | { | |
921 | struct pvconn **pvconns = *pvconnsp; | |
922 | size_t n_pvconns = *n_pvconnsp; | |
923 | int retval = 0; | |
924 | size_t i; | |
925 | ||
926 | for (i = 0; i < n_pvconns; i++) { | |
927 | pvconn_close(pvconns[i]); | |
928 | } | |
929 | free(pvconns); | |
930 | ||
931 | pvconns = xmalloc(svec->n * sizeof *pvconns); | |
932 | n_pvconns = 0; | |
933 | for (i = 0; i < svec->n; i++) { | |
934 | const char *name = svec->names[i]; | |
935 | struct pvconn *pvconn; | |
936 | int error; | |
937 | ||
938 | error = pvconn_open(name, &pvconn); | |
939 | if (!error) { | |
940 | pvconns[n_pvconns++] = pvconn; | |
941 | } else { | |
942 | VLOG_ERR("failed to listen on %s: %s", name, strerror(error)); | |
943 | if (!retval) { | |
944 | retval = error; | |
945 | } | |
946 | } | |
947 | } | |
948 | ||
949 | *pvconnsp = pvconns; | |
950 | *n_pvconnsp = n_pvconns; | |
951 | ||
952 | return retval; | |
953 | } | |
954 | ||
064af421 BP |
955 | int |
956 | ofproto_set_snoops(struct ofproto *ofproto, const struct svec *snoops) | |
957 | { | |
958 | return set_pvconns(&ofproto->snoops, &ofproto->n_snoops, snoops); | |
959 | } | |
960 | ||
961 | int | |
0193b2af JG |
962 | ofproto_set_netflow(struct ofproto *ofproto, |
963 | const struct netflow_options *nf_options) | |
064af421 | 964 | { |
76343538 | 965 | if (nf_options && nf_options->collectors.n) { |
064af421 BP |
966 | if (!ofproto->netflow) { |
967 | ofproto->netflow = netflow_create(); | |
968 | } | |
0193b2af | 969 | return netflow_set_options(ofproto->netflow, nf_options); |
064af421 BP |
970 | } else { |
971 | netflow_destroy(ofproto->netflow); | |
972 | ofproto->netflow = NULL; | |
973 | return 0; | |
974 | } | |
975 | } | |
976 | ||
72b06300 BP |
977 | void |
978 | ofproto_set_sflow(struct ofproto *ofproto, | |
979 | const struct ofproto_sflow_options *oso) | |
980 | { | |
981 | struct ofproto_sflow *os = ofproto->sflow; | |
982 | if (oso) { | |
983 | if (!os) { | |
984 | struct ofport *ofport; | |
72b06300 BP |
985 | |
986 | os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif); | |
4e8e4213 | 987 | HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) { |
ca0f572c | 988 | ofproto_sflow_add_port(os, ofport->odp_port, |
72b06300 BP |
989 | netdev_get_name(ofport->netdev)); |
990 | } | |
991 | } | |
992 | ofproto_sflow_set_options(os, oso); | |
993 | } else { | |
994 | ofproto_sflow_destroy(os); | |
995 | ofproto->sflow = NULL; | |
996 | } | |
997 | } | |
998 | ||
064af421 BP |
999 | uint64_t |
1000 | ofproto_get_datapath_id(const struct ofproto *ofproto) | |
1001 | { | |
1002 | return ofproto->datapath_id; | |
1003 | } | |
1004 | ||
76ce9432 | 1005 | bool |
7d674866 | 1006 | ofproto_has_primary_controller(const struct ofproto *ofproto) |
064af421 | 1007 | { |
76ce9432 | 1008 | return !hmap_is_empty(&ofproto->controllers); |
064af421 BP |
1009 | } |
1010 | ||
abdfe474 JP |
1011 | enum ofproto_fail_mode |
1012 | ofproto_get_fail_mode(const struct ofproto *p) | |
1013 | { | |
1014 | return p->fail_mode; | |
1015 | } | |
1016 | ||
064af421 BP |
1017 | void |
1018 | ofproto_get_snoops(const struct ofproto *ofproto, struct svec *snoops) | |
1019 | { | |
1020 | size_t i; | |
1021 | ||
1022 | for (i = 0; i < ofproto->n_snoops; i++) { | |
1023 | svec_add(snoops, pvconn_get_name(ofproto->snoops[i])); | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | void | |
1028 | ofproto_destroy(struct ofproto *p) | |
1029 | { | |
7d674866 | 1030 | struct ofservice *ofservice, *next_ofservice; |
064af421 | 1031 | struct ofconn *ofconn, *next_ofconn; |
ca0f572c | 1032 | struct ofport *ofport, *next_ofport; |
064af421 BP |
1033 | size_t i; |
1034 | ||
1035 | if (!p) { | |
1036 | return; | |
1037 | } | |
1038 | ||
7aa697dd BP |
1039 | shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif)); |
1040 | ||
f7de2cdf | 1041 | /* Destroy fail-open and in-band early, since they touch the classifier. */ |
79c9f2ee BP |
1042 | fail_open_destroy(p->fail_open); |
1043 | p->fail_open = NULL; | |
1044 | ||
1045 | in_band_destroy(p->in_band); | |
1046 | p->in_band = NULL; | |
917e50e1 | 1047 | free(p->extra_in_band_remotes); |
2f6d3445 | 1048 | |
064af421 BP |
1049 | ofproto_flush_flows(p); |
1050 | classifier_destroy(&p->cls); | |
bcf84111 | 1051 | hmap_destroy(&p->facets); |
064af421 | 1052 | |
4e8e4213 | 1053 | LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &p->all_conns) { |
c475ae67 | 1054 | ofconn_destroy(ofconn); |
064af421 | 1055 | } |
76ce9432 | 1056 | hmap_destroy(&p->controllers); |
064af421 | 1057 | |
c228a364 | 1058 | dpif_close(p->dpif); |
e9e28be3 | 1059 | netdev_monitor_destroy(p->netdev_monitor); |
4e8e4213 | 1060 | HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) { |
ca0f572c | 1061 | hmap_remove(&p->ports, &ofport->hmap_node); |
064af421 BP |
1062 | ofport_free(ofport); |
1063 | } | |
1064 | shash_destroy(&p->port_by_name); | |
1065 | ||
1066 | switch_status_destroy(p->switch_status); | |
064af421 | 1067 | netflow_destroy(p->netflow); |
72b06300 | 1068 | ofproto_sflow_destroy(p->sflow); |
064af421 | 1069 | |
4e8e4213 | 1070 | HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &p->services) { |
7d674866 | 1071 | ofservice_destroy(p, ofservice); |
064af421 | 1072 | } |
7d674866 | 1073 | hmap_destroy(&p->services); |
064af421 BP |
1074 | |
1075 | for (i = 0; i < p->n_snoops; i++) { | |
1076 | pvconn_close(p->snoops[i]); | |
1077 | } | |
1078 | free(p->snoops); | |
1079 | ||
1080 | mac_learning_destroy(p->ml); | |
1081 | ||
5a719c38 JP |
1082 | free(p->mfr_desc); |
1083 | free(p->hw_desc); | |
1084 | free(p->sw_desc); | |
1085 | free(p->serial_desc); | |
cb871ae0 JP |
1086 | free(p->dp_desc); |
1087 | ||
ca0f572c | 1088 | hmap_destroy(&p->ports); |
3b917492 | 1089 | |
064af421 BP |
1090 | free(p); |
1091 | } | |
1092 | ||
1093 | int | |
1094 | ofproto_run(struct ofproto *p) | |
1095 | { | |
1096 | int error = ofproto_run1(p); | |
1097 | if (!error) { | |
1098 | error = ofproto_run2(p, false); | |
1099 | } | |
1100 | return error; | |
1101 | } | |
1102 | ||
e9e28be3 BP |
1103 | static void |
1104 | process_port_change(struct ofproto *ofproto, int error, char *devname) | |
1105 | { | |
1106 | if (error == ENOBUFS) { | |
1107 | reinit_ports(ofproto); | |
1108 | } else if (!error) { | |
1109 | update_port(ofproto, devname); | |
1110 | free(devname); | |
1111 | } | |
1112 | } | |
1113 | ||
e2bfacb6 BP |
1114 | /* Returns a "preference level" for snooping 'ofconn'. A higher return value |
1115 | * means that 'ofconn' is more interesting for monitoring than a lower return | |
1116 | * value. */ | |
1117 | static int | |
1118 | snoop_preference(const struct ofconn *ofconn) | |
1119 | { | |
1120 | switch (ofconn->role) { | |
1121 | case NX_ROLE_MASTER: | |
1122 | return 3; | |
1123 | case NX_ROLE_OTHER: | |
1124 | return 2; | |
1125 | case NX_ROLE_SLAVE: | |
1126 | return 1; | |
1127 | default: | |
1128 | /* Shouldn't happen. */ | |
1129 | return 0; | |
1130 | } | |
1131 | } | |
1132 | ||
76ce9432 BP |
1133 | /* One of ofproto's "snoop" pvconns has accepted a new connection on 'vconn'. |
1134 | * Connects this vconn to a controller. */ | |
1135 | static void | |
1136 | add_snooper(struct ofproto *ofproto, struct vconn *vconn) | |
1137 | { | |
e2bfacb6 | 1138 | struct ofconn *ofconn, *best; |
76ce9432 | 1139 | |
e2bfacb6 BP |
1140 | /* Pick a controller for monitoring. */ |
1141 | best = NULL; | |
4e8e4213 | 1142 | LIST_FOR_EACH (ofconn, node, &ofproto->all_conns) { |
5899143f | 1143 | if (ofconn->type == OFCONN_PRIMARY |
e2bfacb6 BP |
1144 | && (!best || snoop_preference(ofconn) > snoop_preference(best))) { |
1145 | best = ofconn; | |
76ce9432 | 1146 | } |
e2bfacb6 | 1147 | } |
76ce9432 | 1148 | |
e2bfacb6 BP |
1149 | if (best) { |
1150 | rconn_add_monitor(best->rconn, vconn); | |
1151 | } else { | |
1152 | VLOG_INFO_RL(&rl, "no controller connection to snoop"); | |
1153 | vconn_close(vconn); | |
76ce9432 | 1154 | } |
76ce9432 BP |
1155 | } |
1156 | ||
064af421 BP |
1157 | int |
1158 | ofproto_run1(struct ofproto *p) | |
1159 | { | |
1160 | struct ofconn *ofconn, *next_ofconn; | |
7d674866 | 1161 | struct ofservice *ofservice; |
064af421 BP |
1162 | char *devname; |
1163 | int error; | |
1164 | int i; | |
1165 | ||
149f577a JG |
1166 | if (shash_is_empty(&p->port_by_name)) { |
1167 | init_ports(p); | |
1168 | } | |
1169 | ||
064af421 | 1170 | for (i = 0; i < 50; i++) { |
856081f6 | 1171 | struct dpif_upcall packet; |
064af421 | 1172 | |
856081f6 | 1173 | error = dpif_recv(p->dpif, &packet); |
064af421 BP |
1174 | if (error) { |
1175 | if (error == ENODEV) { | |
1176 | /* Someone destroyed the datapath behind our back. The caller | |
1177 | * better destroy us and give up, because we're just going to | |
1178 | * spin from here on out. */ | |
39a559f2 BP |
1179 | static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5); |
1180 | VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally", | |
c228a364 | 1181 | dpif_name(p->dpif)); |
064af421 BP |
1182 | return ENODEV; |
1183 | } | |
1184 | break; | |
1185 | } | |
1186 | ||
856081f6 | 1187 | handle_upcall(p, &packet); |
064af421 BP |
1188 | } |
1189 | ||
e9e28be3 BP |
1190 | while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) { |
1191 | process_port_change(p, error, devname); | |
1192 | } | |
1193 | while ((error = netdev_monitor_poll(p->netdev_monitor, | |
1194 | &devname)) != EAGAIN) { | |
1195 | process_port_change(p, error, devname); | |
064af421 BP |
1196 | } |
1197 | ||
1198 | if (p->in_band) { | |
d2ede7bc BP |
1199 | if (time_msec() >= p->next_in_band_update) { |
1200 | update_in_band_remotes(p); | |
1201 | } | |
064af421 BP |
1202 | in_band_run(p->in_band); |
1203 | } | |
064af421 | 1204 | |
4e8e4213 | 1205 | LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &p->all_conns) { |
3269c562 | 1206 | ofconn_run(ofconn); |
064af421 BP |
1207 | } |
1208 | ||
7778bd15 BP |
1209 | /* Fail-open maintenance. Do this after processing the ofconns since |
1210 | * fail-open checks the status of the controller rconn. */ | |
1211 | if (p->fail_open) { | |
1212 | fail_open_run(p->fail_open); | |
1213 | } | |
1214 | ||
4e8e4213 | 1215 | HMAP_FOR_EACH (ofservice, node, &p->services) { |
064af421 BP |
1216 | struct vconn *vconn; |
1217 | int retval; | |
1218 | ||
7d674866 | 1219 | retval = pvconn_accept(ofservice->pvconn, OFP_VERSION, &vconn); |
064af421 | 1220 | if (!retval) { |
9794e806 | 1221 | struct rconn *rconn; |
eb15cdbb | 1222 | char *name; |
9794e806 | 1223 | |
7d674866 | 1224 | rconn = rconn_create(ofservice->probe_interval, 0); |
eb15cdbb BP |
1225 | name = ofconn_make_name(p, vconn_get_name(vconn)); |
1226 | rconn_connect_unreliably(rconn, vconn, name); | |
1227 | free(name); | |
1228 | ||
7d674866 BP |
1229 | ofconn = ofconn_create(p, rconn, OFCONN_SERVICE); |
1230 | ofconn_set_rate_limit(ofconn, ofservice->rate_limit, | |
1231 | ofservice->burst_limit); | |
064af421 BP |
1232 | } else if (retval != EAGAIN) { |
1233 | VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval)); | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | for (i = 0; i < p->n_snoops; i++) { | |
1238 | struct vconn *vconn; | |
1239 | int retval; | |
1240 | ||
1241 | retval = pvconn_accept(p->snoops[i], OFP_VERSION, &vconn); | |
1242 | if (!retval) { | |
76ce9432 | 1243 | add_snooper(p, vconn); |
064af421 BP |
1244 | } else if (retval != EAGAIN) { |
1245 | VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval)); | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | if (time_msec() >= p->next_expiration) { | |
0de7a4b4 BP |
1250 | int delay = ofproto_expire(p); |
1251 | p->next_expiration = time_msec() + delay; | |
064af421 | 1252 | COVERAGE_INC(ofproto_expiration); |
064af421 BP |
1253 | } |
1254 | ||
1255 | if (p->netflow) { | |
1256 | netflow_run(p->netflow); | |
1257 | } | |
72b06300 BP |
1258 | if (p->sflow) { |
1259 | ofproto_sflow_run(p->sflow); | |
1260 | } | |
064af421 BP |
1261 | |
1262 | return 0; | |
1263 | } | |
1264 | ||
064af421 BP |
1265 | int |
1266 | ofproto_run2(struct ofproto *p, bool revalidate_all) | |
1267 | { | |
bcf84111 BP |
1268 | /* Figure out what we need to revalidate now, if anything. */ |
1269 | struct tag_set revalidate_set = p->revalidate_set; | |
1270 | if (p->need_revalidate) { | |
1271 | revalidate_all = true; | |
1272 | } | |
1273 | ||
1274 | /* Clear the revalidation flags. */ | |
1275 | tag_set_init(&p->revalidate_set); | |
1276 | p->need_revalidate = false; | |
1277 | ||
1278 | /* Now revalidate if there's anything to do. */ | |
1279 | if (revalidate_all || !tag_set_is_empty(&revalidate_set)) { | |
1280 | struct facet *facet, *next; | |
1281 | ||
1282 | HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &p->facets) { | |
1283 | if (revalidate_all | |
1284 | || tag_set_intersects(&revalidate_set, facet->tags)) { | |
1285 | facet_revalidate(p, facet); | |
1286 | } | |
1287 | } | |
064af421 BP |
1288 | } |
1289 | ||
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | void | |
1294 | ofproto_wait(struct ofproto *p) | |
1295 | { | |
7d674866 | 1296 | struct ofservice *ofservice; |
064af421 BP |
1297 | struct ofconn *ofconn; |
1298 | size_t i; | |
1299 | ||
c228a364 | 1300 | dpif_recv_wait(p->dpif); |
e9e28be3 BP |
1301 | dpif_port_poll_wait(p->dpif); |
1302 | netdev_monitor_poll_wait(p->netdev_monitor); | |
4e8e4213 | 1303 | LIST_FOR_EACH (ofconn, node, &p->all_conns) { |
064af421 BP |
1304 | ofconn_wait(ofconn); |
1305 | } | |
1306 | if (p->in_band) { | |
7cf8b266 | 1307 | poll_timer_wait_until(p->next_in_band_update); |
064af421 BP |
1308 | in_band_wait(p->in_band); |
1309 | } | |
064af421 BP |
1310 | if (p->fail_open) { |
1311 | fail_open_wait(p->fail_open); | |
1312 | } | |
72b06300 BP |
1313 | if (p->sflow) { |
1314 | ofproto_sflow_wait(p->sflow); | |
1315 | } | |
064af421 BP |
1316 | if (!tag_set_is_empty(&p->revalidate_set)) { |
1317 | poll_immediate_wake(); | |
1318 | } | |
1319 | if (p->need_revalidate) { | |
1320 | /* Shouldn't happen, but if it does just go around again. */ | |
1321 | VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()"); | |
1322 | poll_immediate_wake(); | |
1323 | } else if (p->next_expiration != LLONG_MAX) { | |
7cf8b266 | 1324 | poll_timer_wait_until(p->next_expiration); |
064af421 | 1325 | } |
4e8e4213 | 1326 | HMAP_FOR_EACH (ofservice, node, &p->services) { |
7d674866 | 1327 | pvconn_wait(ofservice->pvconn); |
064af421 BP |
1328 | } |
1329 | for (i = 0; i < p->n_snoops; i++) { | |
1330 | pvconn_wait(p->snoops[i]); | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | void | |
1335 | ofproto_revalidate(struct ofproto *ofproto, tag_type tag) | |
1336 | { | |
1337 | tag_set_add(&ofproto->revalidate_set, tag); | |
1338 | } | |
1339 | ||
1340 | struct tag_set * | |
1341 | ofproto_get_revalidate_set(struct ofproto *ofproto) | |
1342 | { | |
1343 | return &ofproto->revalidate_set; | |
1344 | } | |
1345 | ||
1346 | bool | |
1347 | ofproto_is_alive(const struct ofproto *p) | |
1348 | { | |
76ce9432 | 1349 | return !hmap_is_empty(&p->controllers); |
064af421 BP |
1350 | } |
1351 | ||
bffc0589 AE |
1352 | void |
1353 | ofproto_get_ofproto_controller_info(const struct ofproto * ofproto, | |
1354 | struct shash *info) | |
1355 | { | |
1356 | const struct ofconn *ofconn; | |
1357 | ||
1358 | shash_init(info); | |
1359 | ||
1360 | HMAP_FOR_EACH (ofconn, hmap_node, &ofproto->controllers) { | |
1361 | const struct rconn *rconn = ofconn->rconn; | |
1362 | const int last_error = rconn_get_last_error(rconn); | |
1363 | struct ofproto_controller_info *cinfo = xmalloc(sizeof *cinfo); | |
1364 | ||
1365 | shash_add(info, rconn_get_target(rconn), cinfo); | |
1366 | ||
1367 | cinfo->is_connected = rconn_is_connected(rconn); | |
1368 | cinfo->role = ofconn->role; | |
1369 | ||
1370 | cinfo->pairs.n = 0; | |
1371 | ||
c18ea70d | 1372 | if (last_error) { |
bffc0589 AE |
1373 | cinfo->pairs.keys[cinfo->pairs.n] = "last_error"; |
1374 | cinfo->pairs.values[cinfo->pairs.n++] = | |
c18ea70d | 1375 | xstrdup(ovs_retval_to_string(last_error)); |
bffc0589 AE |
1376 | } |
1377 | ||
1378 | cinfo->pairs.keys[cinfo->pairs.n] = "state"; | |
1379 | cinfo->pairs.values[cinfo->pairs.n++] = | |
1380 | xstrdup(rconn_get_state(rconn)); | |
1381 | ||
1382 | cinfo->pairs.keys[cinfo->pairs.n] = "time_in_state"; | |
1383 | cinfo->pairs.values[cinfo->pairs.n++] = | |
1384 | xasprintf("%u", rconn_get_state_elapsed(rconn)); | |
1385 | } | |
1386 | } | |
1387 | ||
1388 | void | |
1389 | ofproto_free_ofproto_controller_info(struct shash *info) | |
1390 | { | |
1391 | struct shash_node *node; | |
1392 | ||
1393 | SHASH_FOR_EACH (node, info) { | |
1394 | struct ofproto_controller_info *cinfo = node->data; | |
1395 | while (cinfo->pairs.n) { | |
1396 | free((char *) cinfo->pairs.values[--cinfo->pairs.n]); | |
1397 | } | |
1398 | free(cinfo); | |
1399 | } | |
1400 | shash_destroy(info); | |
1401 | } | |
1402 | ||
3a6ccc8c BP |
1403 | /* Deletes port number 'odp_port' from the datapath for 'ofproto'. |
1404 | * | |
1405 | * This is almost the same as calling dpif_port_del() directly on the | |
1406 | * datapath, but it also makes 'ofproto' close its open netdev for the port | |
1407 | * (if any). This makes it possible to create a new netdev of a different | |
1408 | * type under the same name, which otherwise the netdev library would refuse | |
1409 | * to do because of the conflict. (The netdev would eventually get closed on | |
1410 | * the next trip through ofproto_run(), but this interface is more direct.) | |
1411 | * | |
3a6ccc8c BP |
1412 | * Returns 0 if successful, otherwise a positive errno. */ |
1413 | int | |
1414 | ofproto_port_del(struct ofproto *ofproto, uint16_t odp_port) | |
1415 | { | |
1416 | struct ofport *ofport = get_port(ofproto, odp_port); | |
0b61210e | 1417 | const char *name = ofport ? ofport->opp.name : "<unknown>"; |
3a6ccc8c BP |
1418 | int error; |
1419 | ||
1420 | error = dpif_port_del(ofproto->dpif, odp_port); | |
1421 | if (error) { | |
1422 | VLOG_ERR("%s: failed to remove port %"PRIu16" (%s) interface (%s)", | |
1423 | dpif_name(ofproto->dpif), odp_port, name, strerror(error)); | |
1424 | } else if (ofport) { | |
1425 | /* 'name' is ofport->opp.name and update_port() is going to destroy | |
1426 | * 'ofport'. Just in case update_port() refers to 'name' after it | |
1427 | * destroys 'ofport', make a copy of it around the update_port() | |
1428 | * call. */ | |
1429 | char *devname = xstrdup(name); | |
1430 | update_port(ofproto, devname); | |
1431 | free(devname); | |
1432 | } | |
1433 | return error; | |
1434 | } | |
1435 | ||
a4e2e1f2 EJ |
1436 | /* Checks if 'ofproto' thinks 'odp_port' should be included in floods. Returns |
1437 | * true if 'odp_port' exists and should be included, false otherwise. */ | |
1438 | bool | |
1439 | ofproto_port_is_floodable(struct ofproto *ofproto, uint16_t odp_port) | |
1440 | { | |
1441 | struct ofport *ofport = get_port(ofproto, odp_port); | |
1442 | return ofport && !(ofport->opp.config & OFPPC_NO_FLOOD); | |
1443 | } | |
1444 | ||
064af421 | 1445 | int |
ae412e7d | 1446 | ofproto_send_packet(struct ofproto *p, const struct flow *flow, |
064af421 BP |
1447 | const union ofp_action *actions, size_t n_actions, |
1448 | const struct ofpbuf *packet) | |
1449 | { | |
f29152ca | 1450 | struct action_xlate_ctx ctx; |
cdee00fd | 1451 | struct ofpbuf *odp_actions; |
064af421 | 1452 | |
f29152ca | 1453 | action_xlate_ctx_init(&ctx, p, flow, packet); |
ebe482fd EJ |
1454 | /* Always xlate packets originated in this function. */ |
1455 | ctx.check_special = false; | |
cdee00fd | 1456 | odp_actions = xlate_actions(&ctx, actions, n_actions); |
064af421 BP |
1457 | |
1458 | /* XXX Should we translate the dpif_execute() errno value into an OpenFlow | |
1459 | * error code? */ | |
cdee00fd BP |
1460 | dpif_execute(p->dpif, odp_actions->data, odp_actions->size, packet); |
1461 | ||
1462 | ofpbuf_delete(odp_actions); | |
1463 | ||
064af421 BP |
1464 | return 0; |
1465 | } | |
1466 | ||
fa8b054f BP |
1467 | /* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and |
1468 | * performs the 'n_actions' actions in 'actions'. The new flow will not | |
1469 | * timeout. | |
1470 | * | |
1471 | * If cls_rule->priority is in the range of priorities supported by OpenFlow | |
1472 | * (0...65535, inclusive) then the flow will be visible to OpenFlow | |
1473 | * controllers; otherwise, it will be hidden. | |
1474 | * | |
1475 | * The caller retains ownership of 'cls_rule' and 'actions'. */ | |
064af421 | 1476 | void |
cf3fad8a | 1477 | ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule, |
fa8b054f | 1478 | const union ofp_action *actions, size_t n_actions) |
064af421 BP |
1479 | { |
1480 | struct rule *rule; | |
bcf84111 | 1481 | rule = rule_create(cls_rule, actions, n_actions, 0, 0, 0, false); |
afe75089 | 1482 | rule_insert(p, rule); |
064af421 BP |
1483 | } |
1484 | ||
1485 | void | |
cf3fad8a | 1486 | ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target) |
064af421 BP |
1487 | { |
1488 | struct rule *rule; | |
1489 | ||
1490 | rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls, | |
cf3fad8a | 1491 | target)); |
064af421 BP |
1492 | if (rule) { |
1493 | rule_remove(ofproto, rule); | |
1494 | } | |
1495 | } | |
1496 | ||
064af421 BP |
1497 | void |
1498 | ofproto_flush_flows(struct ofproto *ofproto) | |
1499 | { | |
bcf84111 | 1500 | struct facet *facet, *next_facet; |
5ecc9d81 BP |
1501 | struct rule *rule, *next_rule; |
1502 | struct cls_cursor cursor; | |
bcf84111 | 1503 | |
064af421 | 1504 | COVERAGE_INC(ofproto_flush); |
bcf84111 BP |
1505 | |
1506 | HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) { | |
1507 | /* Mark the facet as not installed so that facet_remove() doesn't | |
1508 | * bother trying to uninstall it. There is no point in uninstalling it | |
1509 | * individually since we are about to blow away all the facets with | |
1510 | * dpif_flow_flush(). */ | |
1511 | facet->installed = false; | |
1512 | facet_remove(ofproto, facet); | |
1513 | } | |
5ecc9d81 BP |
1514 | |
1515 | cls_cursor_init(&cursor, &ofproto->cls, NULL); | |
1516 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) { | |
1517 | rule_remove(ofproto, rule); | |
1518 | } | |
1519 | ||
c228a364 | 1520 | dpif_flow_flush(ofproto->dpif); |
064af421 BP |
1521 | if (ofproto->in_band) { |
1522 | in_band_flushed(ofproto->in_band); | |
1523 | } | |
1524 | if (ofproto->fail_open) { | |
1525 | fail_open_flushed(ofproto->fail_open); | |
1526 | } | |
1527 | } | |
1528 | \f | |
1529 | static void | |
1530 | reinit_ports(struct ofproto *p) | |
1531 | { | |
b0ec0f27 | 1532 | struct dpif_port_dump dump; |
c77d9d13 BP |
1533 | struct shash_node *node; |
1534 | struct shash devnames; | |
064af421 | 1535 | struct ofport *ofport; |
4c738a8d | 1536 | struct dpif_port dpif_port; |
064af421 | 1537 | |
898bf89d JP |
1538 | COVERAGE_INC(ofproto_reinit_ports); |
1539 | ||
c77d9d13 | 1540 | shash_init(&devnames); |
4e8e4213 | 1541 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { |
c77d9d13 | 1542 | shash_add_once (&devnames, ofport->opp.name, NULL); |
064af421 | 1543 | } |
4c738a8d BP |
1544 | DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) { |
1545 | shash_add_once (&devnames, dpif_port.name, NULL); | |
064af421 | 1546 | } |
064af421 | 1547 | |
c77d9d13 BP |
1548 | SHASH_FOR_EACH (node, &devnames) { |
1549 | update_port(p, node->name); | |
064af421 | 1550 | } |
c77d9d13 | 1551 | shash_destroy(&devnames); |
064af421 BP |
1552 | } |
1553 | ||
064af421 | 1554 | static struct ofport * |
4c738a8d | 1555 | make_ofport(const struct dpif_port *dpif_port) |
064af421 | 1556 | { |
149f577a | 1557 | struct netdev_options netdev_options; |
064af421 BP |
1558 | enum netdev_flags flags; |
1559 | struct ofport *ofport; | |
1560 | struct netdev *netdev; | |
064af421 BP |
1561 | int error; |
1562 | ||
149f577a | 1563 | memset(&netdev_options, 0, sizeof netdev_options); |
4c738a8d BP |
1564 | netdev_options.name = dpif_port->name; |
1565 | netdev_options.type = dpif_port->type; | |
149f577a | 1566 | netdev_options.ethertype = NETDEV_ETH_TYPE_NONE; |
149f577a JG |
1567 | |
1568 | error = netdev_open(&netdev_options, &netdev); | |
064af421 BP |
1569 | if (error) { |
1570 | VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s " | |
1571 | "cannot be opened (%s)", | |
4c738a8d BP |
1572 | dpif_port->name, dpif_port->port_no, |
1573 | dpif_port->name, strerror(error)); | |
064af421 BP |
1574 | return NULL; |
1575 | } | |
1576 | ||
0a6f5542 | 1577 | ofport = xzalloc(sizeof *ofport); |
064af421 | 1578 | ofport->netdev = netdev; |
4c738a8d BP |
1579 | ofport->odp_port = dpif_port->port_no; |
1580 | ofport->opp.port_no = odp_port_to_ofp_port(dpif_port->port_no); | |
80992a35 | 1581 | netdev_get_etheraddr(netdev, ofport->opp.hw_addr); |
4c738a8d | 1582 | ovs_strlcpy(ofport->opp.name, dpif_port->name, sizeof ofport->opp.name); |
064af421 BP |
1583 | |
1584 | netdev_get_flags(netdev, &flags); | |
1585 | ofport->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN; | |
1586 | ||
85da620e | 1587 | ofport->opp.state = netdev_get_carrier(netdev) ? 0 : OFPPS_LINK_DOWN; |
064af421 BP |
1588 | |
1589 | netdev_get_features(netdev, | |
1590 | &ofport->opp.curr, &ofport->opp.advertised, | |
1591 | &ofport->opp.supported, &ofport->opp.peer); | |
1592 | return ofport; | |
1593 | } | |
1594 | ||
1595 | static bool | |
4c738a8d | 1596 | ofport_conflicts(const struct ofproto *p, const struct dpif_port *dpif_port) |
064af421 | 1597 | { |
4c738a8d | 1598 | if (get_port(p, dpif_port->port_no)) { |
064af421 | 1599 | VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath", |
4c738a8d | 1600 | dpif_port->port_no); |
064af421 | 1601 | return true; |
4c738a8d | 1602 | } else if (shash_find(&p->port_by_name, dpif_port->name)) { |
064af421 | 1603 | VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath", |
4c738a8d | 1604 | dpif_port->name); |
064af421 BP |
1605 | return true; |
1606 | } else { | |
1607 | return false; | |
1608 | } | |
1609 | } | |
1610 | ||
1611 | static int | |
1612 | ofport_equal(const struct ofport *a_, const struct ofport *b_) | |
1613 | { | |
1614 | const struct ofp_phy_port *a = &a_->opp; | |
1615 | const struct ofp_phy_port *b = &b_->opp; | |
1616 | ||
1617 | BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */ | |
1618 | return (a->port_no == b->port_no | |
1619 | && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr) | |
0b61210e | 1620 | && !strcmp(a->name, b->name) |
064af421 BP |
1621 | && a->state == b->state |
1622 | && a->config == b->config | |
1623 | && a->curr == b->curr | |
1624 | && a->advertised == b->advertised | |
1625 | && a->supported == b->supported | |
1626 | && a->peer == b->peer); | |
1627 | } | |
1628 | ||
1629 | static void | |
1630 | send_port_status(struct ofproto *p, const struct ofport *ofport, | |
1631 | uint8_t reason) | |
1632 | { | |
1633 | /* XXX Should limit the number of queued port status change messages. */ | |
1634 | struct ofconn *ofconn; | |
4e8e4213 | 1635 | LIST_FOR_EACH (ofconn, node, &p->all_conns) { |
064af421 BP |
1636 | struct ofp_port_status *ops; |
1637 | struct ofpbuf *b; | |
1638 | ||
197a992f BP |
1639 | /* Primary controllers, even slaves, should always get port status |
1640 | updates. Otherwise obey ofconn_receives_async_msgs(). */ | |
1641 | if (ofconn->type != OFCONN_PRIMARY | |
1642 | && !ofconn_receives_async_msgs(ofconn)) { | |
9deba63b BP |
1643 | continue; |
1644 | } | |
1645 | ||
064af421 BP |
1646 | ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b); |
1647 | ops->reason = reason; | |
1648 | ops->desc = ofport->opp; | |
1649 | hton_ofp_phy_port(&ops->desc); | |
1650 | queue_tx(b, ofconn, NULL); | |
1651 | } | |
064af421 BP |
1652 | } |
1653 | ||
1654 | static void | |
1655 | ofport_install(struct ofproto *p, struct ofport *ofport) | |
1656 | { | |
0b61210e | 1657 | const char *netdev_name = ofport->opp.name; |
72b06300 | 1658 | |
e9e28be3 | 1659 | netdev_monitor_add(p->netdev_monitor, ofport->netdev); |
ca0f572c | 1660 | hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0)); |
72b06300 BP |
1661 | shash_add(&p->port_by_name, netdev_name, ofport); |
1662 | if (p->sflow) { | |
ca0f572c | 1663 | ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name); |
72b06300 | 1664 | } |
064af421 BP |
1665 | } |
1666 | ||
1667 | static void | |
1668 | ofport_remove(struct ofproto *p, struct ofport *ofport) | |
1669 | { | |
e9e28be3 | 1670 | netdev_monitor_remove(p->netdev_monitor, ofport->netdev); |
ca0f572c | 1671 | hmap_remove(&p->ports, &ofport->hmap_node); |
064af421 | 1672 | shash_delete(&p->port_by_name, |
0b61210e | 1673 | shash_find(&p->port_by_name, ofport->opp.name)); |
72b06300 | 1674 | if (p->sflow) { |
ca0f572c | 1675 | ofproto_sflow_del_port(p->sflow, ofport->odp_port); |
72b06300 | 1676 | } |
064af421 BP |
1677 | } |
1678 | ||
1679 | static void | |
1680 | ofport_free(struct ofport *ofport) | |
1681 | { | |
1682 | if (ofport) { | |
1683 | netdev_close(ofport->netdev); | |
1684 | free(ofport); | |
1685 | } | |
1686 | } | |
1687 | ||
ca0f572c BP |
1688 | static struct ofport * |
1689 | get_port(const struct ofproto *ofproto, uint16_t odp_port) | |
1690 | { | |
1691 | struct ofport *port; | |
1692 | ||
4e8e4213 | 1693 | HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, |
ca0f572c BP |
1694 | hash_int(odp_port, 0), &ofproto->ports) { |
1695 | if (port->odp_port == odp_port) { | |
1696 | return port; | |
1697 | } | |
1698 | } | |
1699 | return NULL; | |
1700 | } | |
1701 | ||
064af421 BP |
1702 | static void |
1703 | update_port(struct ofproto *p, const char *devname) | |
1704 | { | |
4c738a8d | 1705 | struct dpif_port dpif_port; |
c874dc6d BP |
1706 | struct ofport *old_ofport; |
1707 | struct ofport *new_ofport; | |
064af421 BP |
1708 | int error; |
1709 | ||
1710 | COVERAGE_INC(ofproto_update_port); | |
c874dc6d BP |
1711 | |
1712 | /* Query the datapath for port information. */ | |
4c738a8d | 1713 | error = dpif_port_query_by_name(p->dpif, devname, &dpif_port); |
064af421 | 1714 | |
c874dc6d BP |
1715 | /* Find the old ofport. */ |
1716 | old_ofport = shash_find_data(&p->port_by_name, devname); | |
1717 | if (!error) { | |
1718 | if (!old_ofport) { | |
1719 | /* There's no port named 'devname' but there might be a port with | |
1720 | * the same port number. This could happen if a port is deleted | |
1721 | * and then a new one added in its place very quickly, or if a port | |
1722 | * is renamed. In the former case we want to send an OFPPR_DELETE | |
1723 | * and an OFPPR_ADD, and in the latter case we want to send a | |
1724 | * single OFPPR_MODIFY. We can distinguish the cases by comparing | |
1725 | * the old port's ifindex against the new port, or perhaps less | |
1726 | * reliably but more portably by comparing the old port's MAC | |
1727 | * against the new port's MAC. However, this code isn't that smart | |
1728 | * and always sends an OFPPR_MODIFY (XXX). */ | |
4c738a8d | 1729 | old_ofport = get_port(p, dpif_port.port_no); |
064af421 | 1730 | } |
c874dc6d | 1731 | } else if (error != ENOENT && error != ENODEV) { |
064af421 BP |
1732 | VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error " |
1733 | "%s", strerror(error)); | |
4c738a8d | 1734 | goto exit; |
064af421 | 1735 | } |
c874dc6d BP |
1736 | |
1737 | /* Create a new ofport. */ | |
4c738a8d | 1738 | new_ofport = !error ? make_ofport(&dpif_port) : NULL; |
c874dc6d BP |
1739 | |
1740 | /* Eliminate a few pathological cases. */ | |
1741 | if (!old_ofport && !new_ofport) { | |
4c738a8d | 1742 | goto exit; |
c874dc6d BP |
1743 | } else if (old_ofport && new_ofport) { |
1744 | /* Most of the 'config' bits are OpenFlow soft state, but | |
bc4a55cd BP |
1745 | * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the |
1746 | * OpenFlow bits from old_ofport. (make_ofport() only sets | |
1747 | * OFPPC_PORT_DOWN and leaves the other bits 0.) */ | |
c874dc6d BP |
1748 | new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN; |
1749 | ||
1750 | if (ofport_equal(old_ofport, new_ofport)) { | |
1751 | /* False alarm--no change. */ | |
1752 | ofport_free(new_ofport); | |
4c738a8d | 1753 | goto exit; |
c874dc6d BP |
1754 | } |
1755 | } | |
1756 | ||
1757 | /* Now deal with the normal cases. */ | |
1758 | if (old_ofport) { | |
1759 | ofport_remove(p, old_ofport); | |
1760 | } | |
1761 | if (new_ofport) { | |
1762 | ofport_install(p, new_ofport); | |
1763 | } | |
1764 | send_port_status(p, new_ofport ? new_ofport : old_ofport, | |
1765 | (!old_ofport ? OFPPR_ADD | |
1766 | : !new_ofport ? OFPPR_DELETE | |
1767 | : OFPPR_MODIFY)); | |
1768 | ofport_free(old_ofport); | |
4c738a8d BP |
1769 | |
1770 | exit: | |
1771 | dpif_port_destroy(&dpif_port); | |
064af421 BP |
1772 | } |
1773 | ||
1774 | static int | |
1775 | init_ports(struct ofproto *p) | |
1776 | { | |
b0ec0f27 | 1777 | struct dpif_port_dump dump; |
4c738a8d | 1778 | struct dpif_port dpif_port; |
064af421 | 1779 | |
4c738a8d BP |
1780 | DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) { |
1781 | if (!ofport_conflicts(p, &dpif_port)) { | |
1782 | struct ofport *ofport = make_ofport(&dpif_port); | |
064af421 BP |
1783 | if (ofport) { |
1784 | ofport_install(p, ofport); | |
1785 | } | |
1786 | } | |
1787 | } | |
b0ec0f27 | 1788 | |
064af421 BP |
1789 | return 0; |
1790 | } | |
1791 | \f | |
1792 | static struct ofconn * | |
76ce9432 | 1793 | ofconn_create(struct ofproto *p, struct rconn *rconn, enum ofconn_type type) |
064af421 | 1794 | { |
76ce9432 BP |
1795 | struct ofconn *ofconn = xzalloc(sizeof *ofconn); |
1796 | ofconn->ofproto = p; | |
064af421 BP |
1797 | list_push_back(&p->all_conns, &ofconn->node); |
1798 | ofconn->rconn = rconn; | |
76ce9432 | 1799 | ofconn->type = type; |
b70eac89 | 1800 | ofconn->flow_format = NXFF_OPENFLOW10; |
9deba63b | 1801 | ofconn->role = NX_ROLE_OTHER; |
76ce9432 | 1802 | ofconn->packet_in_counter = rconn_packet_counter_create (); |
064af421 | 1803 | ofconn->pktbuf = NULL; |
064af421 | 1804 | ofconn->miss_send_len = 0; |
064af421 BP |
1805 | ofconn->reply_counter = rconn_packet_counter_create (); |
1806 | return ofconn; | |
1807 | } | |
1808 | ||
1809 | static void | |
c475ae67 | 1810 | ofconn_destroy(struct ofconn *ofconn) |
064af421 | 1811 | { |
5899143f | 1812 | if (ofconn->type == OFCONN_PRIMARY) { |
76ce9432 BP |
1813 | hmap_remove(&ofconn->ofproto->controllers, &ofconn->hmap_node); |
1814 | } | |
1815 | discovery_destroy(ofconn->discovery); | |
1816 | ||
064af421 | 1817 | list_remove(&ofconn->node); |
76ce9432 | 1818 | switch_status_unregister(ofconn->ss); |
064af421 BP |
1819 | rconn_destroy(ofconn->rconn); |
1820 | rconn_packet_counter_destroy(ofconn->packet_in_counter); | |
1821 | rconn_packet_counter_destroy(ofconn->reply_counter); | |
1822 | pktbuf_destroy(ofconn->pktbuf); | |
1823 | free(ofconn); | |
1824 | } | |
1825 | ||
1826 | static void | |
3269c562 | 1827 | ofconn_run(struct ofconn *ofconn) |
064af421 | 1828 | { |
3269c562 | 1829 | struct ofproto *p = ofconn->ofproto; |
064af421 | 1830 | int iteration; |
76ce9432 BP |
1831 | size_t i; |
1832 | ||
1833 | if (ofconn->discovery) { | |
1834 | char *controller_name; | |
1835 | if (rconn_is_connectivity_questionable(ofconn->rconn)) { | |
1836 | discovery_question_connectivity(ofconn->discovery); | |
1837 | } | |
1838 | if (discovery_run(ofconn->discovery, &controller_name)) { | |
1839 | if (controller_name) { | |
eb15cdbb BP |
1840 | char *ofconn_name = ofconn_make_name(p, controller_name); |
1841 | rconn_connect(ofconn->rconn, controller_name, ofconn_name); | |
1842 | free(ofconn_name); | |
76ce9432 BP |
1843 | } else { |
1844 | rconn_disconnect(ofconn->rconn); | |
1845 | } | |
1846 | } | |
1847 | } | |
1848 | ||
1849 | for (i = 0; i < N_SCHEDULERS; i++) { | |
1850 | pinsched_run(ofconn->schedulers[i], do_send_packet_in, ofconn); | |
1851 | } | |
064af421 BP |
1852 | |
1853 | rconn_run(ofconn->rconn); | |
1854 | ||
1855 | if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) { | |
1856 | /* Limit the number of iterations to prevent other tasks from | |
1857 | * starving. */ | |
1858 | for (iteration = 0; iteration < 50; iteration++) { | |
1859 | struct ofpbuf *of_msg = rconn_recv(ofconn->rconn); | |
1860 | if (!of_msg) { | |
1861 | break; | |
1862 | } | |
7778bd15 BP |
1863 | if (p->fail_open) { |
1864 | fail_open_maybe_recover(p->fail_open); | |
1865 | } | |
3269c562 | 1866 | handle_openflow(ofconn, of_msg); |
064af421 BP |
1867 | ofpbuf_delete(of_msg); |
1868 | } | |
1869 | } | |
1870 | ||
76ce9432 | 1871 | if (!ofconn->discovery && !rconn_is_alive(ofconn->rconn)) { |
c475ae67 | 1872 | ofconn_destroy(ofconn); |
064af421 BP |
1873 | } |
1874 | } | |
1875 | ||
1876 | static void | |
1877 | ofconn_wait(struct ofconn *ofconn) | |
1878 | { | |
76ce9432 BP |
1879 | int i; |
1880 | ||
1881 | if (ofconn->discovery) { | |
1882 | discovery_wait(ofconn->discovery); | |
1883 | } | |
1884 | for (i = 0; i < N_SCHEDULERS; i++) { | |
1885 | pinsched_wait(ofconn->schedulers[i]); | |
1886 | } | |
064af421 BP |
1887 | rconn_run_wait(ofconn->rconn); |
1888 | if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) { | |
1889 | rconn_recv_wait(ofconn->rconn); | |
1890 | } else { | |
1891 | COVERAGE_INC(ofproto_ofconn_stuck); | |
1892 | } | |
1893 | } | |
c91248b3 BP |
1894 | |
1895 | /* Returns true if 'ofconn' should receive asynchronous messages. */ | |
1896 | static bool | |
1897 | ofconn_receives_async_msgs(const struct ofconn *ofconn) | |
1898 | { | |
5899143f BP |
1899 | if (ofconn->type == OFCONN_PRIMARY) { |
1900 | /* Primary controllers always get asynchronous messages unless they | |
c91248b3 BP |
1901 | * have configured themselves as "slaves". */ |
1902 | return ofconn->role != NX_ROLE_SLAVE; | |
1903 | } else { | |
5899143f BP |
1904 | /* Service connections don't get asynchronous messages unless they have |
1905 | * explicitly asked for them by setting a nonzero miss send length. */ | |
c91248b3 BP |
1906 | return ofconn->miss_send_len > 0; |
1907 | } | |
1908 | } | |
eb15cdbb BP |
1909 | |
1910 | /* Returns a human-readable name for an OpenFlow connection between 'ofproto' | |
1911 | * and 'target', suitable for use in log messages for identifying the | |
1912 | * connection. | |
1913 | * | |
1914 | * The name is dynamically allocated. The caller should free it (with free()) | |
1915 | * when it is no longer needed. */ | |
1916 | static char * | |
1917 | ofconn_make_name(const struct ofproto *ofproto, const char *target) | |
1918 | { | |
1919 | return xasprintf("%s<->%s", dpif_base_name(ofproto->dpif), target); | |
1920 | } | |
7d674866 BP |
1921 | |
1922 | static void | |
1923 | ofconn_set_rate_limit(struct ofconn *ofconn, int rate, int burst) | |
1924 | { | |
1925 | int i; | |
1926 | ||
1927 | for (i = 0; i < N_SCHEDULERS; i++) { | |
1928 | struct pinsched **s = &ofconn->schedulers[i]; | |
1929 | ||
1930 | if (rate > 0) { | |
1931 | if (!*s) { | |
1932 | *s = pinsched_create(rate, burst, | |
1933 | ofconn->ofproto->switch_status); | |
1934 | } else { | |
1935 | pinsched_set_limits(*s, rate, burst); | |
1936 | } | |
1937 | } else { | |
1938 | pinsched_destroy(*s); | |
1939 | *s = NULL; | |
1940 | } | |
1941 | } | |
1942 | } | |
1943 | \f | |
1944 | static void | |
1945 | ofservice_reconfigure(struct ofservice *ofservice, | |
1946 | const struct ofproto_controller *c) | |
1947 | { | |
1948 | ofservice->probe_interval = c->probe_interval; | |
1949 | ofservice->rate_limit = c->rate_limit; | |
1950 | ofservice->burst_limit = c->burst_limit; | |
1951 | } | |
1952 | ||
1953 | /* Creates a new ofservice in 'ofproto'. Returns 0 if successful, otherwise a | |
1954 | * positive errno value. */ | |
1955 | static int | |
1956 | ofservice_create(struct ofproto *ofproto, const struct ofproto_controller *c) | |
1957 | { | |
1958 | struct ofservice *ofservice; | |
1959 | struct pvconn *pvconn; | |
1960 | int error; | |
1961 | ||
1962 | error = pvconn_open(c->target, &pvconn); | |
1963 | if (error) { | |
1964 | return error; | |
1965 | } | |
1966 | ||
1967 | ofservice = xzalloc(sizeof *ofservice); | |
1968 | hmap_insert(&ofproto->services, &ofservice->node, | |
1969 | hash_string(c->target, 0)); | |
1970 | ofservice->pvconn = pvconn; | |
1971 | ||
1972 | ofservice_reconfigure(ofservice, c); | |
1973 | ||
1974 | return 0; | |
1975 | } | |
1976 | ||
1977 | static void | |
1978 | ofservice_destroy(struct ofproto *ofproto, struct ofservice *ofservice) | |
1979 | { | |
1980 | hmap_remove(&ofproto->services, &ofservice->node); | |
1981 | pvconn_close(ofservice->pvconn); | |
1982 | free(ofservice); | |
1983 | } | |
1984 | ||
1985 | /* Finds and returns the ofservice within 'ofproto' that has the given | |
1986 | * 'target', or a null pointer if none exists. */ | |
1987 | static struct ofservice * | |
1988 | ofservice_lookup(struct ofproto *ofproto, const char *target) | |
1989 | { | |
1990 | struct ofservice *ofservice; | |
1991 | ||
4e8e4213 BP |
1992 | HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0), |
1993 | &ofproto->services) { | |
7d674866 BP |
1994 | if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) { |
1995 | return ofservice; | |
1996 | } | |
1997 | } | |
1998 | return NULL; | |
1999 | } | |
064af421 | 2000 | \f |
bcf84111 BP |
2001 | /* Returns true if 'rule' should be hidden from the controller. |
2002 | * | |
2003 | * Rules with priority higher than UINT16_MAX are set up by ofproto itself | |
2004 | * (e.g. by in-band control) and are intentionally hidden from the | |
2005 | * controller. */ | |
2006 | static bool | |
2007 | rule_is_hidden(const struct rule *rule) | |
2008 | { | |
2009 | return rule->cr.priority > UINT16_MAX; | |
2010 | } | |
2011 | ||
2012 | /* Creates and returns a new rule initialized as specified. | |
2013 | * | |
2014 | * The caller is responsible for inserting the rule into the classifier (with | |
2015 | * rule_insert()). */ | |
064af421 | 2016 | static struct rule * |
bcf84111 | 2017 | rule_create(const struct cls_rule *cls_rule, |
064af421 | 2018 | const union ofp_action *actions, size_t n_actions, |
ca069229 | 2019 | uint16_t idle_timeout, uint16_t hard_timeout, |
8054fc48 | 2020 | ovs_be64 flow_cookie, bool send_flow_removed) |
064af421 | 2021 | { |
ec6fde61 | 2022 | struct rule *rule = xzalloc(sizeof *rule); |
bcf84111 | 2023 | rule->cr = *cls_rule; |
064af421 BP |
2024 | rule->idle_timeout = idle_timeout; |
2025 | rule->hard_timeout = hard_timeout; | |
39997502 | 2026 | rule->flow_cookie = flow_cookie; |
064af421 | 2027 | rule->used = rule->created = time_msec(); |
ca069229 | 2028 | rule->send_flow_removed = send_flow_removed; |
bcf84111 | 2029 | list_init(&rule->facets); |
3dffcf07 BP |
2030 | if (n_actions > 0) { |
2031 | rule->n_actions = n_actions; | |
2032 | rule->actions = xmemdup(actions, n_actions * sizeof *actions); | |
2033 | } | |
0193b2af | 2034 | |
064af421 BP |
2035 | return rule; |
2036 | } | |
2037 | ||
2038 | static struct rule * | |
2039 | rule_from_cls_rule(const struct cls_rule *cls_rule) | |
2040 | { | |
2041 | return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL; | |
2042 | } | |
2043 | ||
2044 | static void | |
2045 | rule_free(struct rule *rule) | |
2046 | { | |
2047 | free(rule->actions); | |
064af421 BP |
2048 | free(rule); |
2049 | } | |
2050 | ||
bcf84111 BP |
2051 | /* Destroys 'rule' and iterates through all of its facets and revalidates them, |
2052 | * destroying any that no longer has a rule (which is probably all of them). | |
064af421 | 2053 | * |
bcf84111 | 2054 | * The caller must have already removed 'rule' from the classifier. */ |
064af421 BP |
2055 | static void |
2056 | rule_destroy(struct ofproto *ofproto, struct rule *rule) | |
2057 | { | |
bcf84111 BP |
2058 | struct facet *facet, *next_facet; |
2059 | LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) { | |
2060 | facet_revalidate(ofproto, facet); | |
064af421 BP |
2061 | } |
2062 | rule_free(rule); | |
2063 | } | |
2064 | ||
bcf84111 BP |
2065 | /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action |
2066 | * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't | |
2067 | * count). */ | |
064af421 | 2068 | static bool |
8054fc48 | 2069 | rule_has_out_port(const struct rule *rule, ovs_be16 out_port) |
064af421 BP |
2070 | { |
2071 | const union ofp_action *oa; | |
2072 | struct actions_iterator i; | |
2073 | ||
2074 | if (out_port == htons(OFPP_NONE)) { | |
2075 | return true; | |
2076 | } | |
2077 | for (oa = actions_first(&i, rule->actions, rule->n_actions); oa; | |
2078 | oa = actions_next(&i)) { | |
c1c9c9c4 | 2079 | if (action_outputs_to_port(oa, out_port)) { |
064af421 BP |
2080 | return true; |
2081 | } | |
2082 | } | |
2083 | return false; | |
2084 | } | |
2085 | ||
750638bb BP |
2086 | /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on |
2087 | * 'packet', which arrived on 'in_port'. | |
2088 | * | |
2089 | * Takes ownership of 'packet'. */ | |
9dbb9d5e | 2090 | static bool |
856081f6 | 2091 | execute_odp_actions(struct ofproto *ofproto, const struct flow *flow, |
cf22f8cb | 2092 | const struct nlattr *odp_actions, size_t actions_len, |
750638bb | 2093 | struct ofpbuf *packet) |
9dbb9d5e | 2094 | { |
b9298d3f | 2095 | if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t)) |
7aec165d | 2096 | && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) { |
9dbb9d5e BP |
2097 | /* As an optimization, avoid a round-trip from userspace to kernel to |
2098 | * userspace. This also avoids possibly filling up kernel packet | |
2099 | * buffers along the way. */ | |
856081f6 | 2100 | struct dpif_upcall upcall; |
9dbb9d5e | 2101 | |
82272ede | 2102 | upcall.type = DPIF_UC_ACTION; |
856081f6 BP |
2103 | upcall.packet = packet; |
2104 | upcall.key = NULL; | |
2105 | upcall.key_len = 0; | |
2106 | upcall.userdata = nl_attr_get_u64(odp_actions); | |
2107 | upcall.sample_pool = 0; | |
2108 | upcall.actions = NULL; | |
2109 | upcall.actions_len = 0; | |
9dbb9d5e | 2110 | |
856081f6 | 2111 | send_packet_in(ofproto, &upcall, flow, false); |
9dbb9d5e | 2112 | |
750638bb BP |
2113 | return true; |
2114 | } else { | |
2115 | int error; | |
9dbb9d5e | 2116 | |
cdee00fd | 2117 | error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet); |
750638bb BP |
2118 | ofpbuf_delete(packet); |
2119 | return !error; | |
2120 | } | |
9dbb9d5e BP |
2121 | } |
2122 | ||
bcf84111 BP |
2123 | /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s |
2124 | * statistics appropriately. 'packet' must have at least sizeof(struct | |
2125 | * ofp_packet_in) bytes of headroom. | |
064af421 | 2126 | * |
bcf84111 BP |
2127 | * For correct results, 'packet' must actually be in 'facet''s flow; that is, |
2128 | * applying flow_extract() to 'packet' would yield the same flow as | |
2129 | * 'facet->flow'. | |
064af421 | 2130 | * |
bcf84111 BP |
2131 | * 'facet' must have accurately composed ODP actions; that is, it must not be |
2132 | * in need of revalidation. | |
750638bb BP |
2133 | * |
2134 | * Takes ownership of 'packet'. */ | |
064af421 | 2135 | static void |
bcf84111 BP |
2136 | facet_execute(struct ofproto *ofproto, struct facet *facet, |
2137 | struct ofpbuf *packet) | |
064af421 | 2138 | { |
c97fb132 | 2139 | struct dpif_flow_stats stats; |
bcf84111 BP |
2140 | |
2141 | assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in)); | |
2142 | ||
2143 | flow_extract_stats(&facet->flow, packet, &stats); | |
856081f6 | 2144 | if (execute_odp_actions(ofproto, &facet->flow, |
cdee00fd | 2145 | facet->actions, facet->actions_len, packet)) { |
bcf84111 BP |
2146 | facet_update_stats(ofproto, facet, &stats); |
2147 | facet->used = time_msec(); | |
2148 | netflow_flow_update_time(ofproto->netflow, | |
2149 | &facet->nf_flow, facet->used); | |
2150 | } | |
2151 | } | |
2152 | ||
2153 | /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s | |
2154 | * statistics (or the statistics for one of its facets) appropriately. | |
2155 | * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom. | |
2156 | * | |
2157 | * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited | |
2158 | * with statistics for 'packet' either way. | |
2159 | * | |
2160 | * Takes ownership of 'packet'. */ | |
2161 | static void | |
2162 | rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port, | |
2163 | struct ofpbuf *packet) | |
2164 | { | |
f29152ca | 2165 | struct action_xlate_ctx ctx; |
cdee00fd | 2166 | struct ofpbuf *odp_actions; |
bcf84111 | 2167 | struct facet *facet; |
bcf84111 BP |
2168 | struct flow flow; |
2169 | size_t size; | |
064af421 | 2170 | |
750638bb BP |
2171 | assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in)); |
2172 | ||
bcf84111 BP |
2173 | flow_extract(packet, 0, in_port, &flow); |
2174 | ||
2175 | /* First look for a related facet. If we find one, account it to that. */ | |
2176 | facet = facet_lookup_valid(ofproto, &flow); | |
2177 | if (facet && facet->rule == rule) { | |
2178 | facet_execute(ofproto, facet, packet); | |
2179 | return; | |
064af421 BP |
2180 | } |
2181 | ||
bcf84111 BP |
2182 | /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then |
2183 | * create a new facet for it and use that. */ | |
2184 | if (rule_lookup(ofproto, &flow) == rule) { | |
2185 | facet = facet_create(ofproto, rule, &flow, packet); | |
2186 | facet_execute(ofproto, facet, packet); | |
2187 | facet_install(ofproto, facet, true); | |
2188 | return; | |
2189 | } | |
2190 | ||
2191 | /* We can't account anything to a facet. If we were to try, then that | |
2192 | * facet would have a non-matching rule, busting our invariants. */ | |
f29152ca | 2193 | action_xlate_ctx_init(&ctx, ofproto, &flow, packet); |
cdee00fd | 2194 | odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions); |
bcf84111 | 2195 | size = packet->size; |
856081f6 | 2196 | if (execute_odp_actions(ofproto, &flow, odp_actions->data, |
cdee00fd | 2197 | odp_actions->size, packet)) { |
064af421 | 2198 | rule->used = time_msec(); |
bcf84111 BP |
2199 | rule->packet_count++; |
2200 | rule->byte_count += size; | |
064af421 | 2201 | } |
cdee00fd | 2202 | ofpbuf_delete(odp_actions); |
064af421 BP |
2203 | } |
2204 | ||
afe75089 | 2205 | /* Inserts 'rule' into 'p''s flow table. */ |
064af421 | 2206 | static void |
afe75089 | 2207 | rule_insert(struct ofproto *p, struct rule *rule) |
064af421 BP |
2208 | { |
2209 | struct rule *displaced_rule; | |
2210 | ||
064af421 | 2211 | displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr)); |
bcf84111 BP |
2212 | if (displaced_rule) { |
2213 | rule_destroy(p, displaced_rule); | |
064af421 | 2214 | } |
bcf84111 | 2215 | p->need_revalidate = true; |
064af421 BP |
2216 | } |
2217 | ||
bcf84111 BP |
2218 | /* Creates and returns a new facet within 'ofproto' owned by 'rule', given a |
2219 | * 'flow' and an example 'packet' within that flow. | |
2220 | * | |
2221 | * The caller must already have determined that no facet with an identical | |
2222 | * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in | |
2223 | * 'ofproto''s classifier table. */ | |
2224 | static struct facet * | |
2225 | facet_create(struct ofproto *ofproto, struct rule *rule, | |
2226 | const struct flow *flow, const struct ofpbuf *packet) | |
064af421 | 2227 | { |
bcf84111 | 2228 | struct facet *facet; |
fbb2ea0b | 2229 | |
bcf84111 BP |
2230 | facet = xzalloc(sizeof *facet); |
2231 | facet->used = time_msec(); | |
2232 | hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0)); | |
2233 | list_push_back(&rule->facets, &facet->list_node); | |
2234 | facet->rule = rule; | |
2235 | facet->flow = *flow; | |
2236 | netflow_flow_init(&facet->nf_flow); | |
2237 | netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used); | |
2238 | ||
2239 | facet_make_actions(ofproto, facet, packet); | |
064af421 | 2240 | |
bcf84111 BP |
2241 | return facet; |
2242 | } | |
2243 | ||
2244 | static void | |
2245 | facet_free(struct facet *facet) | |
2246 | { | |
2247 | free(facet->actions); | |
2248 | free(facet); | |
064af421 BP |
2249 | } |
2250 | ||
431d4707 | 2251 | /* Remove 'rule' from 'ofproto' and free up the associated memory: |
431d4707 BP |
2252 | * |
2253 | * - Removes 'rule' from the classifier. | |
2254 | * | |
bcf84111 BP |
2255 | * - If 'rule' has facets, revalidates them (and possibly uninstalls and |
2256 | * destroys them), via rule_destroy(). | |
431d4707 | 2257 | */ |
064af421 BP |
2258 | static void |
2259 | rule_remove(struct ofproto *ofproto, struct rule *rule) | |
2260 | { | |
bcf84111 BP |
2261 | COVERAGE_INC(ofproto_del_rule); |
2262 | ofproto->need_revalidate = true; | |
064af421 BP |
2263 | classifier_remove(&ofproto->cls, &rule->cr); |
2264 | rule_destroy(ofproto, rule); | |
2265 | } | |
2266 | ||
bcf84111 BP |
2267 | /* Remove 'facet' from 'ofproto' and free up the associated memory: |
2268 | * | |
2269 | * - If 'facet' was installed in the datapath, uninstalls it and updates its | |
2270 | * rule's statistics, via facet_uninstall(). | |
2271 | * | |
2272 | * - Removes 'facet' from its rule and from ofproto->facets. | |
2273 | */ | |
2274 | static void | |
2275 | facet_remove(struct ofproto *ofproto, struct facet *facet) | |
2276 | { | |
2277 | facet_uninstall(ofproto, facet); | |
d530fcd2 | 2278 | facet_flush_stats(ofproto, facet); |
bcf84111 BP |
2279 | hmap_remove(&ofproto->facets, &facet->hmap_node); |
2280 | list_remove(&facet->list_node); | |
2281 | facet_free(facet); | |
2282 | } | |
2283 | ||
7f7ae89d BP |
2284 | /* Composes the ODP actions for 'facet' based on its rule's actions. */ |
2285 | static void | |
bcf84111 BP |
2286 | facet_make_actions(struct ofproto *p, struct facet *facet, |
2287 | const struct ofpbuf *packet) | |
064af421 | 2288 | { |
bcf84111 | 2289 | const struct rule *rule = facet->rule; |
cdee00fd | 2290 | struct ofpbuf *odp_actions; |
f29152ca | 2291 | struct action_xlate_ctx ctx; |
064af421 | 2292 | |
f29152ca | 2293 | action_xlate_ctx_init(&ctx, p, &facet->flow, packet); |
cdee00fd | 2294 | odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions); |
19739aee JP |
2295 | facet->tags = ctx.tags; |
2296 | facet->may_install = ctx.may_set_up_flow; | |
2297 | facet->nf_flow.output_iface = ctx.nf_output_iface; | |
064af421 | 2298 | |
cdee00fd BP |
2299 | if (facet->actions_len != odp_actions->size |
2300 | || memcmp(facet->actions, odp_actions->data, odp_actions->size)) { | |
7f7ae89d | 2301 | free(facet->actions); |
cdee00fd BP |
2302 | facet->actions_len = odp_actions->size; |
2303 | facet->actions = xmemdup(odp_actions->data, odp_actions->size); | |
064af421 | 2304 | } |
cdee00fd BP |
2305 | |
2306 | ofpbuf_delete(odp_actions); | |
064af421 BP |
2307 | } |
2308 | ||
2309 | static int | |
ba25b8f4 | 2310 | facet_put__(struct ofproto *ofproto, struct facet *facet, |
c62b0064 BP |
2311 | const struct nlattr *actions, size_t actions_len, |
2312 | struct dpif_flow_stats *stats) | |
064af421 | 2313 | { |
36956a7d | 2314 | uint32_t keybuf[ODPUTIL_FLOW_KEY_U32S]; |
c62b0064 | 2315 | enum dpif_flow_put_flags flags; |
36956a7d BP |
2316 | struct ofpbuf key; |
2317 | ||
c62b0064 BP |
2318 | flags = DPIF_FP_CREATE | DPIF_FP_MODIFY; |
2319 | if (stats) { | |
2320 | flags |= DPIF_FP_ZERO_STATS; | |
2321 | } | |
2322 | ||
36956a7d BP |
2323 | ofpbuf_use_stack(&key, keybuf, sizeof keybuf); |
2324 | odp_flow_key_from_flow(&key, &facet->flow); | |
2325 | assert(key.base == keybuf); | |
2326 | ||
feebdea2 | 2327 | return dpif_flow_put(ofproto->dpif, flags, key.data, key.size, |
c62b0064 | 2328 | actions, actions_len, stats); |
064af421 BP |
2329 | } |
2330 | ||
bcf84111 BP |
2331 | /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If |
2332 | * 'zero_stats' is true, clears any existing statistics from the datapath for | |
2333 | * 'facet'. */ | |
064af421 | 2334 | static void |
bcf84111 | 2335 | facet_install(struct ofproto *p, struct facet *facet, bool zero_stats) |
064af421 | 2336 | { |
c62b0064 BP |
2337 | struct dpif_flow_stats stats; |
2338 | ||
2339 | if (facet->may_install | |
2340 | && !facet_put__(p, facet, facet->actions, facet->actions_len, | |
2341 | zero_stats ? &stats : NULL)) { | |
2342 | facet->installed = true; | |
064af421 BP |
2343 | } |
2344 | } | |
2345 | ||
bcf84111 BP |
2346 | /* Ensures that the bytes in 'facet', plus 'extra_bytes', have been passed up |
2347 | * to the accounting hook function in the ofhooks structure. */ | |
064af421 | 2348 | static void |
bcf84111 BP |
2349 | facet_account(struct ofproto *ofproto, |
2350 | struct facet *facet, uint64_t extra_bytes) | |
064af421 | 2351 | { |
bcf84111 | 2352 | uint64_t total_bytes = facet->byte_count + extra_bytes; |
064af421 BP |
2353 | |
2354 | if (ofproto->ofhooks->account_flow_cb | |
bcf84111 | 2355 | && total_bytes > facet->accounted_bytes) |
064af421 BP |
2356 | { |
2357 | ofproto->ofhooks->account_flow_cb( | |
cdee00fd | 2358 | &facet->flow, facet->tags, facet->actions, facet->actions_len, |
bcf84111 BP |
2359 | total_bytes - facet->accounted_bytes, ofproto->aux); |
2360 | facet->accounted_bytes = total_bytes; | |
064af421 BP |
2361 | } |
2362 | } | |
2363 | ||
d530fcd2 | 2364 | /* If 'rule' is installed in the datapath, uninstalls it. */ |
064af421 | 2365 | static void |
bcf84111 | 2366 | facet_uninstall(struct ofproto *p, struct facet *facet) |
064af421 | 2367 | { |
bcf84111 | 2368 | if (facet->installed) { |
36956a7d | 2369 | uint32_t keybuf[ODPUTIL_FLOW_KEY_U32S]; |
c97fb132 | 2370 | struct dpif_flow_stats stats; |
36956a7d | 2371 | struct ofpbuf key; |
064af421 | 2372 | |
36956a7d BP |
2373 | ofpbuf_use_stack(&key, keybuf, sizeof keybuf); |
2374 | odp_flow_key_from_flow(&key, &facet->flow); | |
2375 | assert(key.base == keybuf); | |
2376 | ||
feebdea2 BP |
2377 | if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) { |
2378 | facet_update_stats(p, facet, &stats); | |
064af421 | 2379 | } |
bcf84111 | 2380 | facet->installed = false; |
064af421 BP |
2381 | } |
2382 | } | |
2383 | ||
bcf84111 BP |
2384 | /* Returns true if the only action for 'facet' is to send to the controller. |
2385 | * (We don't report NetFlow expiration messages for such facets because they | |
2386 | * are just part of the control logic for the network, not real traffic). */ | |
0193b2af | 2387 | static bool |
bcf84111 | 2388 | facet_is_controller_flow(struct facet *facet) |
0193b2af | 2389 | { |
bcf84111 BP |
2390 | return (facet |
2391 | && facet->rule->n_actions == 1 | |
2392 | && action_outputs_to_port(&facet->rule->actions[0], | |
c1c9c9c4 | 2393 | htons(OFPP_CONTROLLER))); |
0193b2af JG |
2394 | } |
2395 | ||
bcf84111 BP |
2396 | /* Folds all of 'facet''s statistics into its rule. Also updates the |
2397 | * accounting ofhook and emits a NetFlow expiration if appropriate. */ | |
064af421 | 2398 | static void |
d530fcd2 | 2399 | facet_flush_stats(struct ofproto *ofproto, struct facet *facet) |
064af421 | 2400 | { |
bcf84111 | 2401 | facet_account(ofproto, facet, 0); |
064af421 | 2402 | |
bcf84111 | 2403 | if (ofproto->netflow && !facet_is_controller_flow(facet)) { |
064af421 | 2404 | struct ofexpired expired; |
bcf84111 BP |
2405 | expired.flow = facet->flow; |
2406 | expired.packet_count = facet->packet_count; | |
2407 | expired.byte_count = facet->byte_count; | |
2408 | expired.used = facet->used; | |
2409 | netflow_expire(ofproto->netflow, &facet->nf_flow, &expired); | |
2410 | } | |
2411 | ||
2412 | facet->rule->packet_count += facet->packet_count; | |
2413 | facet->rule->byte_count += facet->byte_count; | |
2414 | ||
2415 | /* Reset counters to prevent double counting if 'facet' ever gets | |
2416 | * reinstalled. */ | |
2417 | facet->packet_count = 0; | |
2418 | facet->byte_count = 0; | |
2419 | facet->accounted_bytes = 0; | |
2420 | ||
2421 | netflow_flow_clear(&facet->nf_flow); | |
2422 | } | |
2423 | ||
2424 | /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'. | |
2425 | * Returns it if found, otherwise a null pointer. | |
2426 | * | |
2427 | * The returned facet might need revalidation; use facet_lookup_valid() | |
2428 | * instead if that is important. */ | |
2429 | static struct facet * | |
2430 | facet_find(struct ofproto *ofproto, const struct flow *flow) | |
2431 | { | |
2432 | struct facet *facet; | |
2433 | ||
2434 | HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0), | |
2435 | &ofproto->facets) { | |
2436 | if (flow_equal(flow, &facet->flow)) { | |
2437 | return facet; | |
2438 | } | |
2439 | } | |
2440 | ||
2441 | return NULL; | |
2442 | } | |
2443 | ||
2444 | /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'. | |
2445 | * Returns it if found, otherwise a null pointer. | |
2446 | * | |
2447 | * The returned facet is guaranteed to be valid. */ | |
2448 | static struct facet * | |
2449 | facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow) | |
2450 | { | |
2451 | struct facet *facet = facet_find(ofproto, flow); | |
2452 | ||
2453 | /* The facet we found might not be valid, since we could be in need of | |
2454 | * revalidation. If it is not valid, don't return it. */ | |
2455 | if (facet | |
2456 | && ofproto->need_revalidate | |
2457 | && !facet_revalidate(ofproto, facet)) { | |
2458 | COVERAGE_INC(ofproto_invalidated); | |
2459 | return NULL; | |
064af421 | 2460 | } |
064af421 | 2461 | |
bcf84111 BP |
2462 | return facet; |
2463 | } | |
0193b2af | 2464 | |
bcf84111 BP |
2465 | /* Re-searches 'ofproto''s classifier for a rule matching 'facet': |
2466 | * | |
2467 | * - If the rule found is different from 'facet''s current rule, moves | |
2468 | * 'facet' to the new rule and recompiles its actions. | |
2469 | * | |
2470 | * - If the rule found is the same as 'facet''s current rule, leaves 'facet' | |
2471 | * where it is and recompiles its actions anyway. | |
2472 | * | |
2473 | * - If there is none, destroys 'facet'. | |
2474 | * | |
d530fcd2 | 2475 | * Returns true if 'facet' still exists, false if it has been destroyed. */ |
bcf84111 BP |
2476 | static bool |
2477 | facet_revalidate(struct ofproto *ofproto, struct facet *facet) | |
2478 | { | |
f29152ca | 2479 | struct action_xlate_ctx ctx; |
cdee00fd | 2480 | struct ofpbuf *odp_actions; |
d530fcd2 | 2481 | struct rule *new_rule; |
d530fcd2 | 2482 | bool actions_changed; |
bcf84111 BP |
2483 | |
2484 | COVERAGE_INC(facet_revalidate); | |
d530fcd2 BP |
2485 | |
2486 | /* Determine the new rule. */ | |
2487 | new_rule = rule_lookup(ofproto, &facet->flow); | |
2488 | if (!new_rule) { | |
2489 | /* No new rule, so delete the facet. */ | |
bcf84111 BP |
2490 | facet_remove(ofproto, facet); |
2491 | return false; | |
2492 | } | |
2493 | ||
d530fcd2 BP |
2494 | /* Calculate new ODP actions. |
2495 | * | |
cdee00fd BP |
2496 | * We do not modify any 'facet' state yet, because we might need to, e.g., |
2497 | * emit a NetFlow expiration and, if so, we need to have the old state | |
2498 | * around to properly compose it. */ | |
f29152ca | 2499 | action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL); |
cdee00fd BP |
2500 | odp_actions = xlate_actions(&ctx, new_rule->actions, new_rule->n_actions); |
2501 | actions_changed = (facet->actions_len != odp_actions->size | |
2502 | || memcmp(facet->actions, odp_actions->data, | |
2503 | facet->actions_len)); | |
d530fcd2 BP |
2504 | |
2505 | /* If the ODP actions changed or the installability changed, then we need | |
2506 | * to talk to the datapath. */ | |
e4add896 BP |
2507 | if (actions_changed || ctx.may_set_up_flow != facet->installed) { |
2508 | if (ctx.may_set_up_flow) { | |
c97fb132 | 2509 | struct dpif_flow_stats stats; |
d530fcd2 | 2510 | |
c62b0064 BP |
2511 | facet_put__(ofproto, facet, |
2512 | odp_actions->data, odp_actions->size, &stats); | |
feebdea2 | 2513 | facet_update_stats(ofproto, facet, &stats); |
d530fcd2 BP |
2514 | } else { |
2515 | facet_uninstall(ofproto, facet); | |
2516 | } | |
2517 | ||
2518 | /* The datapath flow is gone or has zeroed stats, so push stats out of | |
2519 | * 'facet' into 'rule'. */ | |
2520 | facet_flush_stats(ofproto, facet); | |
2521 | } | |
2522 | ||
2523 | /* Update 'facet' now that we've taken care of all the old state. */ | |
f29152ca BP |
2524 | facet->tags = ctx.tags; |
2525 | facet->nf_flow.output_iface = ctx.nf_output_iface; | |
2526 | facet->may_install = ctx.may_set_up_flow; | |
d530fcd2 BP |
2527 | if (actions_changed) { |
2528 | free(facet->actions); | |
cdee00fd BP |
2529 | facet->actions_len = odp_actions->size; |
2530 | facet->actions = xmemdup(odp_actions->data, odp_actions->size); | |
d530fcd2 BP |
2531 | } |
2532 | if (facet->rule != new_rule) { | |
bcf84111 BP |
2533 | COVERAGE_INC(facet_changed_rule); |
2534 | list_remove(&facet->list_node); | |
d530fcd2 BP |
2535 | list_push_back(&new_rule->facets, &facet->list_node); |
2536 | facet->rule = new_rule; | |
2537 | facet->used = new_rule->created; | |
0c0afbec | 2538 | } |
bcf84111 | 2539 | |
cdfcd496 BP |
2540 | ofpbuf_delete(odp_actions); |
2541 | ||
bcf84111 | 2542 | return true; |
064af421 BP |
2543 | } |
2544 | \f | |
2545 | static void | |
2546 | queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn, | |
2547 | struct rconn_packet_counter *counter) | |
2548 | { | |
2549 | update_openflow_length(msg); | |
2550 | if (rconn_send(ofconn->rconn, msg, counter)) { | |
2551 | ofpbuf_delete(msg); | |
2552 | } | |
2553 | } | |
2554 | ||
064af421 BP |
2555 | static void |
2556 | send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh, | |
2557 | int error) | |
2558 | { | |
dc4762ed | 2559 | struct ofpbuf *buf = ofputil_encode_error_msg(error, oh); |
26c112c2 BP |
2560 | if (buf) { |
2561 | COVERAGE_INC(ofproto_error); | |
2562 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
2563 | } | |
064af421 BP |
2564 | } |
2565 | ||
2566 | static void | |
2567 | hton_ofp_phy_port(struct ofp_phy_port *opp) | |
2568 | { | |
2569 | opp->port_no = htons(opp->port_no); | |
2570 | opp->config = htonl(opp->config); | |
2571 | opp->state = htonl(opp->state); | |
2572 | opp->curr = htonl(opp->curr); | |
2573 | opp->advertised = htonl(opp->advertised); | |
2574 | opp->supported = htonl(opp->supported); | |
2575 | opp->peer = htonl(opp->peer); | |
2576 | } | |
2577 | ||
2578 | static int | |
d1e2cf21 | 2579 | handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2580 | { |
d1e2cf21 | 2581 | queue_tx(make_echo_reply(oh), ofconn, ofconn->reply_counter); |
064af421 BP |
2582 | return 0; |
2583 | } | |
2584 | ||
2585 | static int | |
d1e2cf21 | 2586 | handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 BP |
2587 | { |
2588 | struct ofp_switch_features *osf; | |
2589 | struct ofpbuf *buf; | |
064af421 BP |
2590 | struct ofport *port; |
2591 | ||
2592 | osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf); | |
3269c562 | 2593 | osf->datapath_id = htonll(ofconn->ofproto->datapath_id); |
064af421 BP |
2594 | osf->n_buffers = htonl(pktbuf_capacity()); |
2595 | osf->n_tables = 2; | |
2596 | osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS | | |
0254ae23 | 2597 | OFPC_PORT_STATS | OFPC_ARP_MATCH_IP); |
064af421 BP |
2598 | osf->actions = htonl((1u << OFPAT_OUTPUT) | |
2599 | (1u << OFPAT_SET_VLAN_VID) | | |
2600 | (1u << OFPAT_SET_VLAN_PCP) | | |
2601 | (1u << OFPAT_STRIP_VLAN) | | |
2602 | (1u << OFPAT_SET_DL_SRC) | | |
2603 | (1u << OFPAT_SET_DL_DST) | | |
2604 | (1u << OFPAT_SET_NW_SRC) | | |
2605 | (1u << OFPAT_SET_NW_DST) | | |
959a2ecd | 2606 | (1u << OFPAT_SET_NW_TOS) | |
064af421 | 2607 | (1u << OFPAT_SET_TP_SRC) | |
c1c9c9c4 BP |
2608 | (1u << OFPAT_SET_TP_DST) | |
2609 | (1u << OFPAT_ENQUEUE)); | |
064af421 | 2610 | |
3269c562 | 2611 | HMAP_FOR_EACH (port, hmap_node, &ofconn->ofproto->ports) { |
064af421 BP |
2612 | hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp)); |
2613 | } | |
2614 | ||
2615 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
2616 | return 0; | |
2617 | } | |
2618 | ||
2619 | static int | |
d1e2cf21 | 2620 | handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 BP |
2621 | { |
2622 | struct ofpbuf *buf; | |
2623 | struct ofp_switch_config *osc; | |
2624 | uint16_t flags; | |
2625 | bool drop_frags; | |
2626 | ||
2627 | /* Figure out flags. */ | |
3269c562 | 2628 | dpif_get_drop_frags(ofconn->ofproto->dpif, &drop_frags); |
064af421 | 2629 | flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL; |
064af421 BP |
2630 | |
2631 | /* Send reply. */ | |
2632 | osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf); | |
2633 | osc->flags = htons(flags); | |
2634 | osc->miss_send_len = htons(ofconn->miss_send_len); | |
2635 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
2636 | ||
2637 | return 0; | |
2638 | } | |
2639 | ||
2640 | static int | |
d1e2cf21 | 2641 | handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc) |
064af421 | 2642 | { |
d1e2cf21 | 2643 | uint16_t flags = ntohs(osc->flags); |
064af421 | 2644 | |
5899143f | 2645 | if (ofconn->type == OFCONN_PRIMARY && ofconn->role != NX_ROLE_SLAVE) { |
064af421 BP |
2646 | switch (flags & OFPC_FRAG_MASK) { |
2647 | case OFPC_FRAG_NORMAL: | |
3269c562 | 2648 | dpif_set_drop_frags(ofconn->ofproto->dpif, false); |
064af421 BP |
2649 | break; |
2650 | case OFPC_FRAG_DROP: | |
3269c562 | 2651 | dpif_set_drop_frags(ofconn->ofproto->dpif, true); |
064af421 BP |
2652 | break; |
2653 | default: | |
2654 | VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")", | |
2655 | osc->flags); | |
2656 | break; | |
2657 | } | |
2658 | } | |
2659 | ||
064af421 BP |
2660 | ofconn->miss_send_len = ntohs(osc->miss_send_len); |
2661 | ||
2662 | return 0; | |
2663 | } | |
2664 | ||
5f8bfd69 BP |
2665 | /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a |
2666 | * flow translation. */ | |
33b5304b | 2667 | #define MAX_RESUBMIT_RECURSION 16 |
5f8bfd69 | 2668 | |
064af421 BP |
2669 | static void do_xlate_actions(const union ofp_action *in, size_t n_in, |
2670 | struct action_xlate_ctx *ctx); | |
2671 | ||
2672 | static void | |
2673 | add_output_action(struct action_xlate_ctx *ctx, uint16_t port) | |
2674 | { | |
ca0f572c | 2675 | const struct ofport *ofport = get_port(ctx->ofproto, port); |
6cfaf517 BP |
2676 | |
2677 | if (ofport) { | |
2678 | if (ofport->opp.config & OFPPC_NO_FWD) { | |
2679 | /* Forwarding disabled on port. */ | |
2680 | return; | |
2681 | } | |
2682 | } else { | |
2683 | /* | |
2684 | * We don't have an ofport record for this port, but it doesn't hurt to | |
2685 | * allow forwarding to it anyhow. Maybe such a port will appear later | |
2686 | * and we're pre-populating the flow table. | |
2687 | */ | |
064af421 | 2688 | } |
6cfaf517 | 2689 | |
7aec165d | 2690 | nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, port); |
6a07af36 | 2691 | ctx->nf_output_iface = port; |
064af421 BP |
2692 | } |
2693 | ||
2694 | static struct rule * | |
bcf84111 | 2695 | rule_lookup(struct ofproto *ofproto, const struct flow *flow) |
064af421 | 2696 | { |
3c4486a5 | 2697 | return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow)); |
064af421 BP |
2698 | } |
2699 | ||
2700 | static void | |
2701 | xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port) | |
2702 | { | |
5f8bfd69 | 2703 | if (ctx->recurse < MAX_RESUBMIT_RECURSION) { |
2c5d1389 | 2704 | uint16_t old_in_port; |
064af421 | 2705 | struct rule *rule; |
064af421 | 2706 | |
2c5d1389 BP |
2707 | /* Look up a flow with 'in_port' as the input port. Then restore the |
2708 | * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will | |
2709 | * have surprising behavior). */ | |
2710 | old_in_port = ctx->flow.in_port; | |
e18fe8a2 | 2711 | ctx->flow.in_port = in_port; |
bcf84111 | 2712 | rule = rule_lookup(ctx->ofproto, &ctx->flow); |
2c5d1389 BP |
2713 | ctx->flow.in_port = old_in_port; |
2714 | ||
7aa697dd BP |
2715 | if (ctx->resubmit_hook) { |
2716 | ctx->resubmit_hook(ctx, rule); | |
2717 | } | |
2718 | ||
064af421 | 2719 | if (rule) { |
064af421 BP |
2720 | ctx->recurse++; |
2721 | do_xlate_actions(rule->actions, rule->n_actions, ctx); | |
2722 | ctx->recurse--; | |
2723 | } | |
5f8bfd69 | 2724 | } else { |
db5ce514 | 2725 | static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1); |
5f8bfd69 BP |
2726 | |
2727 | VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times", | |
2728 | MAX_RESUBMIT_RECURSION); | |
064af421 BP |
2729 | } |
2730 | } | |
2731 | ||
f1588b1f BP |
2732 | static void |
2733 | flood_packets(struct ofproto *ofproto, uint16_t odp_in_port, uint32_t mask, | |
cdee00fd | 2734 | uint16_t *nf_output_iface, struct ofpbuf *odp_actions) |
f1588b1f BP |
2735 | { |
2736 | struct ofport *ofport; | |
2737 | ||
2738 | HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) { | |
2739 | uint16_t odp_port = ofport->odp_port; | |
2740 | if (odp_port != odp_in_port && !(ofport->opp.config & mask)) { | |
7aec165d | 2741 | nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port); |
f1588b1f BP |
2742 | } |
2743 | } | |
2744 | *nf_output_iface = NF_OUT_FLOOD; | |
2745 | } | |
2746 | ||
064af421 | 2747 | static void |
aae51f53 BP |
2748 | xlate_output_action__(struct action_xlate_ctx *ctx, |
2749 | uint16_t port, uint16_t max_len) | |
064af421 BP |
2750 | { |
2751 | uint16_t odp_port; | |
6a07af36 JG |
2752 | uint16_t prev_nf_output_iface = ctx->nf_output_iface; |
2753 | ||
2754 | ctx->nf_output_iface = NF_OUT_DROP; | |
064af421 | 2755 | |
aae51f53 | 2756 | switch (port) { |
064af421 | 2757 | case OFPP_IN_PORT: |
e18fe8a2 | 2758 | add_output_action(ctx, ctx->flow.in_port); |
064af421 BP |
2759 | break; |
2760 | case OFPP_TABLE: | |
e18fe8a2 | 2761 | xlate_table_action(ctx, ctx->flow.in_port); |
064af421 BP |
2762 | break; |
2763 | case OFPP_NORMAL: | |
e18fe8a2 | 2764 | if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet, |
cdee00fd | 2765 | ctx->odp_actions, &ctx->tags, |
6a07af36 | 2766 | &ctx->nf_output_iface, |
064af421 BP |
2767 | ctx->ofproto->aux)) { |
2768 | COVERAGE_INC(ofproto_uninstallable); | |
d6fbec6d | 2769 | ctx->may_set_up_flow = false; |
064af421 BP |
2770 | } |
2771 | break; | |
2772 | case OFPP_FLOOD: | |
f1588b1f | 2773 | flood_packets(ctx->ofproto, ctx->flow.in_port, OFPPC_NO_FLOOD, |
cdee00fd | 2774 | &ctx->nf_output_iface, ctx->odp_actions); |
9628cd42 | 2775 | break; |
064af421 | 2776 | case OFPP_ALL: |
f1588b1f | 2777 | flood_packets(ctx->ofproto, ctx->flow.in_port, 0, |
cdee00fd | 2778 | &ctx->nf_output_iface, ctx->odp_actions); |
064af421 BP |
2779 | break; |
2780 | case OFPP_CONTROLLER: | |
7aec165d | 2781 | nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len); |
064af421 BP |
2782 | break; |
2783 | case OFPP_LOCAL: | |
2784 | add_output_action(ctx, ODPP_LOCAL); | |
2785 | break; | |
2786 | default: | |
aae51f53 | 2787 | odp_port = ofp_port_to_odp_port(port); |
e18fe8a2 | 2788 | if (odp_port != ctx->flow.in_port) { |
064af421 BP |
2789 | add_output_action(ctx, odp_port); |
2790 | } | |
2791 | break; | |
2792 | } | |
6a07af36 JG |
2793 | |
2794 | if (prev_nf_output_iface == NF_OUT_FLOOD) { | |
2795 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
2796 | } else if (ctx->nf_output_iface == NF_OUT_DROP) { | |
2797 | ctx->nf_output_iface = prev_nf_output_iface; | |
2798 | } else if (prev_nf_output_iface != NF_OUT_DROP && | |
2799 | ctx->nf_output_iface != NF_OUT_FLOOD) { | |
2800 | ctx->nf_output_iface = NF_OUT_MULTI; | |
2801 | } | |
064af421 BP |
2802 | } |
2803 | ||
aae51f53 BP |
2804 | static void |
2805 | xlate_output_action(struct action_xlate_ctx *ctx, | |
2806 | const struct ofp_action_output *oao) | |
2807 | { | |
2808 | xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len)); | |
2809 | } | |
2810 | ||
c1c9c9c4 BP |
2811 | /* If the final ODP action in 'ctx' is "pop priority", drop it, as an |
2812 | * optimization, because we're going to add another action that sets the | |
2813 | * priority immediately after, or because there are no actions following the | |
2814 | * pop. */ | |
2815 | static void | |
2816 | remove_pop_action(struct action_xlate_ctx *ctx) | |
2817 | { | |
cdee00fd BP |
2818 | if (ctx->odp_actions->size == ctx->last_pop_priority) { |
2819 | ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN); | |
2820 | ctx->last_pop_priority = -1; | |
2821 | } | |
2822 | } | |
2823 | ||
2824 | static void | |
2825 | add_pop_action(struct action_xlate_ctx *ctx) | |
2826 | { | |
2827 | if (ctx->odp_actions->size != ctx->last_pop_priority) { | |
7aec165d | 2828 | nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY); |
cdee00fd | 2829 | ctx->last_pop_priority = ctx->odp_actions->size; |
c1c9c9c4 BP |
2830 | } |
2831 | } | |
2832 | ||
2833 | static void | |
2834 | xlate_enqueue_action(struct action_xlate_ctx *ctx, | |
2835 | const struct ofp_action_enqueue *oae) | |
2836 | { | |
2837 | uint16_t ofp_port, odp_port; | |
aae51f53 BP |
2838 | uint32_t priority; |
2839 | int error; | |
2840 | ||
2841 | error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id), | |
2842 | &priority); | |
2843 | if (error) { | |
2844 | /* Fall back to ordinary output action. */ | |
2845 | xlate_output_action__(ctx, ntohs(oae->port), 0); | |
2846 | return; | |
2847 | } | |
c1c9c9c4 BP |
2848 | |
2849 | /* Figure out ODP output port. */ | |
2850 | ofp_port = ntohs(oae->port); | |
2851 | if (ofp_port != OFPP_IN_PORT) { | |
2852 | odp_port = ofp_port_to_odp_port(ofp_port); | |
2853 | } else { | |
2854 | odp_port = ctx->flow.in_port; | |
2855 | } | |
2856 | ||
2857 | /* Add ODP actions. */ | |
2858 | remove_pop_action(ctx); | |
7aec165d | 2859 | nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority); |
c1c9c9c4 | 2860 | add_output_action(ctx, odp_port); |
cdee00fd | 2861 | add_pop_action(ctx); |
c1c9c9c4 BP |
2862 | |
2863 | /* Update NetFlow output port. */ | |
2864 | if (ctx->nf_output_iface == NF_OUT_DROP) { | |
2865 | ctx->nf_output_iface = odp_port; | |
2866 | } else if (ctx->nf_output_iface != NF_OUT_FLOOD) { | |
2867 | ctx->nf_output_iface = NF_OUT_MULTI; | |
2868 | } | |
2869 | } | |
2870 | ||
eedc0097 JP |
2871 | static void |
2872 | xlate_set_queue_action(struct action_xlate_ctx *ctx, | |
2873 | const struct nx_action_set_queue *nasq) | |
2874 | { | |
2875 | uint32_t priority; | |
2876 | int error; | |
2877 | ||
2878 | error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id), | |
2879 | &priority); | |
2880 | if (error) { | |
2881 | /* Couldn't translate queue to a priority, so ignore. A warning | |
2882 | * has already been logged. */ | |
2883 | return; | |
2884 | } | |
2885 | ||
2886 | remove_pop_action(ctx); | |
7aec165d | 2887 | nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority); |
eedc0097 JP |
2888 | } |
2889 | ||
350a665f BP |
2890 | static void |
2891 | xlate_set_dl_tci(struct action_xlate_ctx *ctx) | |
2892 | { | |
66642cb4 BP |
2893 | ovs_be16 tci = ctx->flow.vlan_tci; |
2894 | if (!(tci & htons(VLAN_CFI))) { | |
7aec165d | 2895 | nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN); |
350a665f | 2896 | } else { |
7aec165d | 2897 | nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI, |
cdee00fd | 2898 | tci & ~htons(VLAN_CFI)); |
350a665f BP |
2899 | } |
2900 | } | |
2901 | ||
7b064a79 BP |
2902 | struct xlate_reg_state { |
2903 | ovs_be16 vlan_tci; | |
2904 | ovs_be64 tun_id; | |
2905 | }; | |
2906 | ||
b6c9e612 | 2907 | static void |
7b064a79 BP |
2908 | save_reg_state(const struct action_xlate_ctx *ctx, |
2909 | struct xlate_reg_state *state) | |
b6c9e612 | 2910 | { |
7b064a79 BP |
2911 | state->vlan_tci = ctx->flow.vlan_tci; |
2912 | state->tun_id = ctx->flow.tun_id; | |
2913 | } | |
b6c9e612 | 2914 | |
7b064a79 BP |
2915 | static void |
2916 | update_reg_state(struct action_xlate_ctx *ctx, | |
2917 | const struct xlate_reg_state *state) | |
2918 | { | |
2919 | if (ctx->flow.vlan_tci != state->vlan_tci) { | |
b6c9e612 BP |
2920 | xlate_set_dl_tci(ctx); |
2921 | } | |
7b064a79 | 2922 | if (ctx->flow.tun_id != state->tun_id) { |
7aec165d BP |
2923 | nl_msg_put_be64(ctx->odp_actions, |
2924 | ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id); | |
926947e6 | 2925 | } |
b6c9e612 BP |
2926 | } |
2927 | ||
064af421 BP |
2928 | static void |
2929 | xlate_nicira_action(struct action_xlate_ctx *ctx, | |
2930 | const struct nx_action_header *nah) | |
2931 | { | |
2932 | const struct nx_action_resubmit *nar; | |
659586ef | 2933 | const struct nx_action_set_tunnel *nast; |
eedc0097 | 2934 | const struct nx_action_set_queue *nasq; |
53ddd40a | 2935 | const struct nx_action_multipath *nam; |
e41a9130 | 2936 | enum nx_action_subtype subtype = ntohs(nah->subtype); |
7b064a79 | 2937 | struct xlate_reg_state state; |
b9298d3f | 2938 | ovs_be64 tun_id; |
064af421 BP |
2939 | |
2940 | assert(nah->vendor == htonl(NX_VENDOR_ID)); | |
2941 | switch (subtype) { | |
2942 | case NXAST_RESUBMIT: | |
2943 | nar = (const struct nx_action_resubmit *) nah; | |
2944 | xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port))); | |
2945 | break; | |
2946 | ||
659586ef JG |
2947 | case NXAST_SET_TUNNEL: |
2948 | nast = (const struct nx_action_set_tunnel *) nah; | |
b9298d3f | 2949 | tun_id = htonll(ntohl(nast->tun_id)); |
7aec165d | 2950 | nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id); |
b9298d3f | 2951 | ctx->flow.tun_id = tun_id; |
659586ef JG |
2952 | break; |
2953 | ||
401eeb92 BP |
2954 | case NXAST_DROP_SPOOFED_ARP: |
2955 | if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) { | |
7aec165d BP |
2956 | nl_msg_put_flag(ctx->odp_actions, |
2957 | ODP_ACTION_ATTR_DROP_SPOOFED_ARP); | |
401eeb92 BP |
2958 | } |
2959 | break; | |
2960 | ||
eedc0097 JP |
2961 | case NXAST_SET_QUEUE: |
2962 | nasq = (const struct nx_action_set_queue *) nah; | |
2963 | xlate_set_queue_action(ctx, nasq); | |
2964 | break; | |
2965 | ||
2966 | case NXAST_POP_QUEUE: | |
cdee00fd | 2967 | add_pop_action(ctx); |
eedc0097 JP |
2968 | break; |
2969 | ||
b6c9e612 | 2970 | case NXAST_REG_MOVE: |
7b064a79 BP |
2971 | save_reg_state(ctx, &state); |
2972 | nxm_execute_reg_move((const struct nx_action_reg_move *) nah, | |
2973 | &ctx->flow); | |
2974 | update_reg_state(ctx, &state); | |
b6c9e612 BP |
2975 | break; |
2976 | ||
2977 | case NXAST_REG_LOAD: | |
7b064a79 | 2978 | save_reg_state(ctx, &state); |
b6c9e612 BP |
2979 | nxm_execute_reg_load((const struct nx_action_reg_load *) nah, |
2980 | &ctx->flow); | |
7b064a79 BP |
2981 | update_reg_state(ctx, &state); |
2982 | break; | |
96fc46e8 BP |
2983 | |
2984 | case NXAST_NOTE: | |
2985 | /* Nothing to do. */ | |
b6c9e612 BP |
2986 | break; |
2987 | ||
b9298d3f BP |
2988 | case NXAST_SET_TUNNEL64: |
2989 | tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id; | |
7aec165d | 2990 | nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id); |
b9298d3f BP |
2991 | ctx->flow.tun_id = tun_id; |
2992 | break; | |
2993 | ||
53ddd40a BP |
2994 | case NXAST_MULTIPATH: |
2995 | nam = (const struct nx_action_multipath *) nah; | |
2996 | multipath_execute(nam, &ctx->flow); | |
2997 | break; | |
2998 | ||
999f0d45 | 2999 | /* If you add a new action here that modifies flow data, don't forget to |
c1c9c9c4 | 3000 | * update the flow key in ctx->flow at the same time. */ |
999f0d45 | 3001 | |
e41a9130 | 3002 | case NXAST_SNAT__OBSOLETE: |
064af421 | 3003 | default: |
e41a9130 | 3004 | VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype); |
064af421 BP |
3005 | break; |
3006 | } | |
3007 | } | |
3008 | ||
3009 | static void | |
3010 | do_xlate_actions(const union ofp_action *in, size_t n_in, | |
3011 | struct action_xlate_ctx *ctx) | |
3012 | { | |
3013 | struct actions_iterator iter; | |
3014 | const union ofp_action *ia; | |
3015 | const struct ofport *port; | |
3016 | ||
ca0f572c | 3017 | port = get_port(ctx->ofproto, ctx->flow.in_port); |
064af421 | 3018 | if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) && |
ba186119 | 3019 | port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp) |
064af421 BP |
3020 | ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) { |
3021 | /* Drop this flow. */ | |
3022 | return; | |
3023 | } | |
3024 | ||
3025 | for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) { | |
e41a9130 | 3026 | enum ofp_action_type type = ntohs(ia->type); |
cdee00fd | 3027 | const struct ofp_action_dl_addr *oada; |
064af421 BP |
3028 | |
3029 | switch (type) { | |
3030 | case OFPAT_OUTPUT: | |
3031 | xlate_output_action(ctx, &ia->output); | |
3032 | break; | |
3033 | ||
3034 | case OFPAT_SET_VLAN_VID: | |
66642cb4 BP |
3035 | ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK); |
3036 | ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI); | |
350a665f | 3037 | xlate_set_dl_tci(ctx); |
064af421 BP |
3038 | break; |
3039 | ||
3040 | case OFPAT_SET_VLAN_PCP: | |
66642cb4 BP |
3041 | ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK); |
3042 | ctx->flow.vlan_tci |= htons( | |
3043 | (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI); | |
350a665f | 3044 | xlate_set_dl_tci(ctx); |
064af421 BP |
3045 | break; |
3046 | ||
3047 | case OFPAT_STRIP_VLAN: | |
66642cb4 | 3048 | ctx->flow.vlan_tci = htons(0); |
350a665f | 3049 | xlate_set_dl_tci(ctx); |
064af421 BP |
3050 | break; |
3051 | ||
3052 | case OFPAT_SET_DL_SRC: | |
cdee00fd | 3053 | oada = ((struct ofp_action_dl_addr *) ia); |
7aec165d | 3054 | nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC, |
cdee00fd BP |
3055 | oada->dl_addr, ETH_ADDR_LEN); |
3056 | memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN); | |
064af421 BP |
3057 | break; |
3058 | ||
3059 | case OFPAT_SET_DL_DST: | |
cdee00fd | 3060 | oada = ((struct ofp_action_dl_addr *) ia); |
7aec165d | 3061 | nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST, |
cdee00fd BP |
3062 | oada->dl_addr, ETH_ADDR_LEN); |
3063 | memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN); | |
064af421 BP |
3064 | break; |
3065 | ||
3066 | case OFPAT_SET_NW_SRC: | |
7aec165d | 3067 | nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC, |
cdee00fd BP |
3068 | ia->nw_addr.nw_addr); |
3069 | ctx->flow.nw_src = ia->nw_addr.nw_addr; | |
064af421 BP |
3070 | break; |
3071 | ||
2d70a31a | 3072 | case OFPAT_SET_NW_DST: |
7aec165d | 3073 | nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST, |
cdee00fd BP |
3074 | ia->nw_addr.nw_addr); |
3075 | ctx->flow.nw_dst = ia->nw_addr.nw_addr; | |
2d38e234 | 3076 | break; |
959a2ecd JP |
3077 | |
3078 | case OFPAT_SET_NW_TOS: | |
7aec165d | 3079 | nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS, |
cdee00fd BP |
3080 | ia->nw_tos.nw_tos); |
3081 | ctx->flow.nw_tos = ia->nw_tos.nw_tos; | |
2d70a31a JP |
3082 | break; |
3083 | ||
064af421 | 3084 | case OFPAT_SET_TP_SRC: |
7aec165d | 3085 | nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC, |
cdee00fd BP |
3086 | ia->tp_port.tp_port); |
3087 | ctx->flow.tp_src = ia->tp_port.tp_port; | |
064af421 BP |
3088 | break; |
3089 | ||
2d70a31a | 3090 | case OFPAT_SET_TP_DST: |
7aec165d | 3091 | nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST, |
cdee00fd BP |
3092 | ia->tp_port.tp_port); |
3093 | ctx->flow.tp_dst = ia->tp_port.tp_port; | |
2d70a31a JP |
3094 | break; |
3095 | ||
064af421 BP |
3096 | case OFPAT_VENDOR: |
3097 | xlate_nicira_action(ctx, (const struct nx_action_header *) ia); | |
3098 | break; | |
3099 | ||
c1c9c9c4 BP |
3100 | case OFPAT_ENQUEUE: |
3101 | xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia); | |
3102 | break; | |
3103 | ||
064af421 | 3104 | default: |
e41a9130 | 3105 | VLOG_DBG_RL(&rl, "unknown action type %d", (int) type); |
064af421 BP |
3106 | break; |
3107 | } | |
3108 | } | |
3109 | } | |
3110 | ||
f29152ca BP |
3111 | static void |
3112 | action_xlate_ctx_init(struct action_xlate_ctx *ctx, | |
3113 | struct ofproto *ofproto, const struct flow *flow, | |
3114 | const struct ofpbuf *packet) | |
064af421 | 3115 | { |
f29152ca BP |
3116 | ctx->ofproto = ofproto; |
3117 | ctx->flow = *flow; | |
3118 | ctx->packet = packet; | |
7aa697dd | 3119 | ctx->resubmit_hook = NULL; |
ebe482fd | 3120 | ctx->check_special = true; |
f29152ca | 3121 | } |
1eb0942d | 3122 | |
cdee00fd | 3123 | static struct ofpbuf * |
f29152ca BP |
3124 | xlate_actions(struct action_xlate_ctx *ctx, |
3125 | const union ofp_action *in, size_t n_in) | |
3126 | { | |
064af421 | 3127 | COVERAGE_INC(ofproto_ofp2odp); |
cdee00fd BP |
3128 | |
3129 | ctx->odp_actions = ofpbuf_new(512); | |
f29152ca BP |
3130 | ctx->tags = 0; |
3131 | ctx->may_set_up_flow = true; | |
3132 | ctx->nf_output_iface = NF_OUT_DROP; | |
3133 | ctx->recurse = 0; | |
cdee00fd | 3134 | ctx->last_pop_priority = -1; |
ebe482fd EJ |
3135 | |
3136 | if (!ctx->check_special | |
7a7ae75a BP |
3137 | || !ctx->ofproto->ofhooks->special_cb |
3138 | || ctx->ofproto->ofhooks->special_cb(&ctx->flow, ctx->packet, | |
3139 | ctx->ofproto->aux)) { | |
ebe482fd EJ |
3140 | do_xlate_actions(in, n_in, ctx); |
3141 | } else { | |
3142 | ctx->may_set_up_flow = false; | |
3143 | } | |
3144 | ||
f29152ca | 3145 | remove_pop_action(ctx); |
0ad9b732 | 3146 | |
d6fbec6d | 3147 | /* Check with in-band control to see if we're allowed to set up this |
0ad9b732 | 3148 | * flow. */ |
cdee00fd BP |
3149 | if (!in_band_rule_check(ctx->ofproto->in_band, &ctx->flow, |
3150 | ctx->odp_actions->data, ctx->odp_actions->size)) { | |
f29152ca | 3151 | ctx->may_set_up_flow = false; |
0ad9b732 JP |
3152 | } |
3153 | ||
cdee00fd | 3154 | return ctx->odp_actions; |
064af421 BP |
3155 | } |
3156 | ||
9deba63b BP |
3157 | /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow |
3158 | * error message code (composed with ofp_mkerr()) for the caller to propagate | |
3159 | * upward. Otherwise, returns 0. | |
3160 | * | |
2228b50d | 3161 | * The log message mentions 'msg_type'. */ |
9deba63b | 3162 | static int |
2228b50d | 3163 | reject_slave_controller(struct ofconn *ofconn, const const char *msg_type) |
9deba63b | 3164 | { |
5899143f | 3165 | if (ofconn->type == OFCONN_PRIMARY && ofconn->role == NX_ROLE_SLAVE) { |
9deba63b | 3166 | static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5); |
9deba63b | 3167 | VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller", |
2228b50d | 3168 | msg_type); |
9deba63b BP |
3169 | |
3170 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM); | |
3171 | } else { | |
3172 | return 0; | |
3173 | } | |
3174 | } | |
3175 | ||
064af421 | 3176 | static int |
d1e2cf21 | 3177 | handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3178 | { |
3269c562 | 3179 | struct ofproto *p = ofconn->ofproto; |
064af421 BP |
3180 | struct ofp_packet_out *opo; |
3181 | struct ofpbuf payload, *buffer; | |
ac51afaf | 3182 | union ofp_action *ofp_actions; |
f29152ca | 3183 | struct action_xlate_ctx ctx; |
cdee00fd | 3184 | struct ofpbuf *odp_actions; |
ac51afaf | 3185 | struct ofpbuf request; |
ae412e7d | 3186 | struct flow flow; |
ac51afaf | 3187 | size_t n_ofp_actions; |
064af421 | 3188 | uint16_t in_port; |
064af421 BP |
3189 | int error; |
3190 | ||
ac51afaf BP |
3191 | COVERAGE_INC(ofproto_packet_out); |
3192 | ||
2228b50d | 3193 | error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT"); |
9deba63b BP |
3194 | if (error) { |
3195 | return error; | |
3196 | } | |
3197 | ||
ac51afaf | 3198 | /* Get ofp_packet_out. */ |
0bc9407d | 3199 | ofpbuf_use_const(&request, oh, ntohs(oh->length)); |
bbc32a88 | 3200 | opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions)); |
ac51afaf BP |
3201 | |
3202 | /* Get actions. */ | |
3203 | error = ofputil_pull_actions(&request, ntohs(opo->actions_len), | |
3204 | &ofp_actions, &n_ofp_actions); | |
064af421 BP |
3205 | if (error) { |
3206 | return error; | |
3207 | } | |
064af421 | 3208 | |
ac51afaf | 3209 | /* Get payload. */ |
064af421 BP |
3210 | if (opo->buffer_id != htonl(UINT32_MAX)) { |
3211 | error = pktbuf_retrieve(ofconn->pktbuf, ntohl(opo->buffer_id), | |
3212 | &buffer, &in_port); | |
7778bd15 | 3213 | if (error || !buffer) { |
064af421 BP |
3214 | return error; |
3215 | } | |
3216 | payload = *buffer; | |
3217 | } else { | |
ac51afaf | 3218 | payload = request; |
064af421 BP |
3219 | buffer = NULL; |
3220 | } | |
3221 | ||
ac51afaf BP |
3222 | /* Extract flow, check actions. */ |
3223 | flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)), | |
3224 | &flow); | |
f1defbf9 | 3225 | error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports); |
ac51afaf BP |
3226 | if (error) { |
3227 | goto exit; | |
3228 | } | |
3229 | ||
3230 | /* Send. */ | |
f29152ca | 3231 | action_xlate_ctx_init(&ctx, p, &flow, &payload); |
cdee00fd BP |
3232 | odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions); |
3233 | dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload); | |
3234 | ofpbuf_delete(odp_actions); | |
064af421 | 3235 | |
ac51afaf BP |
3236 | exit: |
3237 | ofpbuf_delete(buffer); | |
3238 | return 0; | |
064af421 BP |
3239 | } |
3240 | ||
3241 | static void | |
3242 | update_port_config(struct ofproto *p, struct ofport *port, | |
3243 | uint32_t config, uint32_t mask) | |
3244 | { | |
3245 | mask &= config ^ port->opp.config; | |
3246 | if (mask & OFPPC_PORT_DOWN) { | |
3247 | if (config & OFPPC_PORT_DOWN) { | |
3248 | netdev_turn_flags_off(port->netdev, NETDEV_UP, true); | |
3249 | } else { | |
3250 | netdev_turn_flags_on(port->netdev, NETDEV_UP, true); | |
3251 | } | |
3252 | } | |
f1588b1f BP |
3253 | #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \ |
3254 | OFPPC_NO_FWD | OFPPC_NO_FLOOD) | |
064af421 BP |
3255 | if (mask & REVALIDATE_BITS) { |
3256 | COVERAGE_INC(ofproto_costly_flags); | |
3257 | port->opp.config ^= mask & REVALIDATE_BITS; | |
3258 | p->need_revalidate = true; | |
3259 | } | |
3260 | #undef REVALIDATE_BITS | |
064af421 BP |
3261 | if (mask & OFPPC_NO_PACKET_IN) { |
3262 | port->opp.config ^= OFPPC_NO_PACKET_IN; | |
3263 | } | |
3264 | } | |
3265 | ||
3266 | static int | |
d1e2cf21 | 3267 | handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3268 | { |
3269c562 | 3269 | struct ofproto *p = ofconn->ofproto; |
d1e2cf21 | 3270 | const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh; |
064af421 BP |
3271 | struct ofport *port; |
3272 | int error; | |
3273 | ||
2228b50d | 3274 | error = reject_slave_controller(ofconn, "OFPT_PORT_MOD"); |
9deba63b BP |
3275 | if (error) { |
3276 | return error; | |
3277 | } | |
064af421 | 3278 | |
ca0f572c | 3279 | port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no))); |
064af421 BP |
3280 | if (!port) { |
3281 | return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT); | |
3282 | } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) { | |
3283 | return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR); | |
3284 | } else { | |
3285 | update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask)); | |
3286 | if (opm->advertise) { | |
3287 | netdev_set_advertisements(port->netdev, ntohl(opm->advertise)); | |
3288 | } | |
3289 | } | |
3290 | return 0; | |
3291 | } | |
3292 | ||
3293 | static struct ofpbuf * | |
06a5e131 | 3294 | make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len) |
064af421 BP |
3295 | { |
3296 | struct ofp_stats_reply *osr; | |
3297 | struct ofpbuf *msg; | |
3298 | ||
3299 | msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX)); | |
3300 | osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg); | |
3301 | osr->type = type; | |
3302 | osr->flags = htons(0); | |
3303 | return msg; | |
3304 | } | |
3305 | ||
3306 | static struct ofpbuf * | |
d1e2cf21 | 3307 | start_ofp_stats_reply(const struct ofp_header *request, size_t body_len) |
064af421 | 3308 | { |
d1e2cf21 BP |
3309 | const struct ofp_stats_request *osr |
3310 | = (const struct ofp_stats_request *) request; | |
3311 | return make_ofp_stats_reply(osr->header.xid, osr->type, body_len); | |
064af421 BP |
3312 | } |
3313 | ||
3314 | static void * | |
06a5e131 BP |
3315 | append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn, |
3316 | struct ofpbuf **msgp) | |
064af421 BP |
3317 | { |
3318 | struct ofpbuf *msg = *msgp; | |
3319 | assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply)); | |
3320 | if (nbytes + msg->size > UINT16_MAX) { | |
3321 | struct ofp_stats_reply *reply = msg->data; | |
3322 | reply->flags = htons(OFPSF_REPLY_MORE); | |
06a5e131 | 3323 | *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes); |
064af421 BP |
3324 | queue_tx(msg, ofconn, ofconn->reply_counter); |
3325 | } | |
3326 | return ofpbuf_put_uninit(*msgp, nbytes); | |
3327 | } | |
3328 | ||
09246b99 BP |
3329 | static struct ofpbuf * |
3330 | make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len) | |
3331 | { | |
3332 | struct nicira_stats_msg *nsm; | |
3333 | struct ofpbuf *msg; | |
3334 | ||
3335 | msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX)); | |
3336 | nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg); | |
3337 | nsm->type = htons(OFPST_VENDOR); | |
3338 | nsm->flags = htons(0); | |
3339 | nsm->vendor = htonl(NX_VENDOR_ID); | |
d5f2379b | 3340 | nsm->subtype = subtype; |
09246b99 BP |
3341 | return msg; |
3342 | } | |
3343 | ||
3344 | static struct ofpbuf * | |
3345 | start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len) | |
3346 | { | |
3347 | return make_nxstats_reply(request->header.xid, request->subtype, body_len); | |
3348 | } | |
3349 | ||
3350 | static void | |
3351 | append_nxstats_reply(size_t nbytes, struct ofconn *ofconn, | |
3352 | struct ofpbuf **msgp) | |
3353 | { | |
3354 | struct ofpbuf *msg = *msgp; | |
3355 | assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg)); | |
3356 | if (nbytes + msg->size > UINT16_MAX) { | |
3357 | struct nicira_stats_msg *reply = msg->data; | |
3358 | reply->flags = htons(OFPSF_REPLY_MORE); | |
3359 | *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes); | |
3360 | queue_tx(msg, ofconn, ofconn->reply_counter); | |
3361 | } | |
3362 | ofpbuf_prealloc_tailroom(*msgp, nbytes); | |
3363 | } | |
3364 | ||
064af421 | 3365 | static int |
3269c562 | 3366 | handle_desc_stats_request(struct ofconn *ofconn, |
d1e2cf21 | 3367 | const struct ofp_header *request) |
064af421 | 3368 | { |
3269c562 | 3369 | struct ofproto *p = ofconn->ofproto; |
064af421 BP |
3370 | struct ofp_desc_stats *ods; |
3371 | struct ofpbuf *msg; | |
3372 | ||
06a5e131 BP |
3373 | msg = start_ofp_stats_reply(request, sizeof *ods); |
3374 | ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg); | |
5a719c38 JP |
3375 | memset(ods, 0, sizeof *ods); |
3376 | ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc); | |
3377 | ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc); | |
3378 | ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc); | |
3379 | ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num); | |
3380 | ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc); | |
064af421 BP |
3381 | queue_tx(msg, ofconn, ofconn->reply_counter); |
3382 | ||
3383 | return 0; | |
3384 | } | |
3385 | ||
064af421 | 3386 | static int |
3269c562 | 3387 | handle_table_stats_request(struct ofconn *ofconn, |
d1e2cf21 | 3388 | const struct ofp_header *request) |
064af421 | 3389 | { |
3269c562 | 3390 | struct ofproto *p = ofconn->ofproto; |
064af421 BP |
3391 | struct ofp_table_stats *ots; |
3392 | struct ofpbuf *msg; | |
064af421 | 3393 | |
06a5e131 | 3394 | msg = start_ofp_stats_reply(request, sizeof *ots * 2); |
064af421 | 3395 | |
064af421 | 3396 | /* Classifier table. */ |
06a5e131 | 3397 | ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg); |
064af421 | 3398 | memset(ots, 0, sizeof *ots); |
064af421 | 3399 | strcpy(ots->name, "classifier"); |
b70eac89 | 3400 | ots->wildcards = (ofconn->flow_format == NXFF_OPENFLOW10 |
f9bfea14 | 3401 | ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL)); |
ad828225 | 3402 | ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */ |
bcf84111 | 3403 | ots->active_count = htonl(classifier_count(&p->cls)); |
c4617b3c BP |
3404 | put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */ |
3405 | put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */ | |
064af421 BP |
3406 | |
3407 | queue_tx(msg, ofconn, ofconn->reply_counter); | |
3408 | return 0; | |
3409 | } | |
3410 | ||
abaad8cf | 3411 | static void |
ca0f572c | 3412 | append_port_stat(struct ofport *port, struct ofconn *ofconn, |
a4948b95 | 3413 | struct ofpbuf **msgp) |
abaad8cf JP |
3414 | { |
3415 | struct netdev_stats stats; | |
3416 | struct ofp_port_stats *ops; | |
3417 | ||
d295e8e9 JP |
3418 | /* Intentionally ignore return value, since errors will set |
3419 | * 'stats' to all-1s, which is correct for OpenFlow, and | |
abaad8cf JP |
3420 | * netdev_get_stats() will log errors. */ |
3421 | netdev_get_stats(port->netdev, &stats); | |
3422 | ||
06a5e131 | 3423 | ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp); |
ca0f572c | 3424 | ops->port_no = htons(port->opp.port_no); |
abaad8cf | 3425 | memset(ops->pad, 0, sizeof ops->pad); |
c4617b3c BP |
3426 | put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets)); |
3427 | put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets)); | |
3428 | put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes)); | |
3429 | put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes)); | |
3430 | put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped)); | |
3431 | put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped)); | |
3432 | put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors)); | |
3433 | put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors)); | |
3434 | put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors)); | |
3435 | put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors)); | |
3436 | put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors)); | |
3437 | put_32aligned_be64(&ops->collisions, htonll(stats.collisions)); | |
abaad8cf JP |
3438 | } |
3439 | ||
064af421 | 3440 | static int |
d1e2cf21 | 3441 | handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3442 | { |
3269c562 | 3443 | struct ofproto *p = ofconn->ofproto; |
d1e2cf21 | 3444 | const struct ofp_port_stats_request *psr = ofputil_stats_body(oh); |
064af421 BP |
3445 | struct ofp_port_stats *ops; |
3446 | struct ofpbuf *msg; | |
3447 | struct ofport *port; | |
064af421 | 3448 | |
d1e2cf21 | 3449 | msg = start_ofp_stats_reply(oh, sizeof *ops * 16); |
abaad8cf | 3450 | if (psr->port_no != htons(OFPP_NONE)) { |
ca0f572c | 3451 | port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no))); |
abaad8cf | 3452 | if (port) { |
ca0f572c | 3453 | append_port_stat(port, ofconn, &msg); |
abaad8cf JP |
3454 | } |
3455 | } else { | |
4e8e4213 | 3456 | HMAP_FOR_EACH (port, hmap_node, &p->ports) { |
ca0f572c | 3457 | append_port_stat(port, ofconn, &msg); |
abaad8cf | 3458 | } |
064af421 BP |
3459 | } |
3460 | ||
3461 | queue_tx(msg, ofconn, ofconn->reply_counter); | |
3462 | return 0; | |
3463 | } | |
3464 | ||
01149cfd | 3465 | /* Obtains statistic counters for 'rule' within 'p' and stores them into |
bcf84111 BP |
3466 | * '*packet_countp' and '*byte_countp'. The returned statistics include |
3467 | * statistics for all of 'rule''s facets. */ | |
064af421 BP |
3468 | static void |
3469 | query_stats(struct ofproto *p, struct rule *rule, | |
3470 | uint64_t *packet_countp, uint64_t *byte_countp) | |
3471 | { | |
36956a7d | 3472 | uint32_t keybuf[ODPUTIL_FLOW_KEY_U32S]; |
064af421 | 3473 | uint64_t packet_count, byte_count; |
bcf84111 | 3474 | struct facet *facet; |
36956a7d | 3475 | struct ofpbuf key; |
064af421 | 3476 | |
01149cfd | 3477 | /* Start from historical data for 'rule' itself that are no longer tracked |
bcf84111 | 3478 | * by the datapath. This counts, for example, facets that have expired. */ |
b3137fe8 JG |
3479 | packet_count = rule->packet_count; |
3480 | byte_count = rule->byte_count; | |
3481 | ||
bc4a05c6 | 3482 | /* Ask the datapath for statistics on all of the rule's facets. |
01149cfd BP |
3483 | * |
3484 | * Also, add any statistics that are not tracked by the datapath for each | |
bcf84111 | 3485 | * facet. This includes, for example, statistics for packets that were |
01149cfd | 3486 | * executed "by hand" by ofproto via dpif_execute() but must be accounted |
bcf84111 | 3487 | * to a rule. */ |
36956a7d | 3488 | ofpbuf_use_stack(&key, keybuf, sizeof keybuf); |
bcf84111 | 3489 | LIST_FOR_EACH (facet, list_node, &rule->facets) { |
c97fb132 | 3490 | struct dpif_flow_stats stats; |
064af421 | 3491 | |
36956a7d BP |
3492 | ofpbuf_clear(&key); |
3493 | odp_flow_key_from_flow(&key, &facet->flow); | |
693c4a01 | 3494 | dpif_flow_get(p->dpif, key.data, key.size, NULL, &stats); |
bcf84111 | 3495 | |
feebdea2 BP |
3496 | packet_count += stats.n_packets + facet->packet_count; |
3497 | byte_count += stats.n_bytes + facet->byte_count; | |
064af421 | 3498 | } |
064af421 | 3499 | |
01149cfd | 3500 | /* Return the stats to the caller. */ |
064af421 BP |
3501 | *packet_countp = packet_count; |
3502 | *byte_countp = byte_count; | |
3503 | } | |
3504 | ||
c6ebb8fb BP |
3505 | static void |
3506 | calc_flow_duration(long long int start, ovs_be32 *sec, ovs_be32 *nsec) | |
3507 | { | |
3508 | long long int msecs = time_msec() - start; | |
3509 | *sec = htonl(msecs / 1000); | |
3510 | *nsec = htonl((msecs % 1000) * (1000 * 1000)); | |
3511 | } | |
3512 | ||
064af421 | 3513 | static void |
5ecc9d81 BP |
3514 | put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule, |
3515 | ovs_be16 out_port, struct ofpbuf **replyp) | |
064af421 | 3516 | { |
064af421 BP |
3517 | struct ofp_flow_stats *ofs; |
3518 | uint64_t packet_count, byte_count; | |
c4617b3c | 3519 | ovs_be64 cookie; |
064af421 BP |
3520 | size_t act_len, len; |
3521 | ||
5ecc9d81 | 3522 | if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) { |
064af421 BP |
3523 | return; |
3524 | } | |
3525 | ||
3526 | act_len = sizeof *rule->actions * rule->n_actions; | |
3527 | len = offsetof(struct ofp_flow_stats, actions) + act_len; | |
3528 | ||
5ecc9d81 | 3529 | query_stats(ofconn->ofproto, rule, &packet_count, &byte_count); |
064af421 | 3530 | |
5ecc9d81 | 3531 | ofs = append_ofp_stats_reply(len, ofconn, replyp); |
064af421 | 3532 | ofs->length = htons(len); |
ad828225 | 3533 | ofs->table_id = 0; |
064af421 | 3534 | ofs->pad = 0; |
ff9d3826 | 3535 | ofputil_cls_rule_to_match(&rule->cr, ofconn->flow_format, &ofs->match, |
c4617b3c BP |
3536 | rule->flow_cookie, &cookie); |
3537 | put_32aligned_be64(&ofs->cookie, cookie); | |
c6ebb8fb | 3538 | calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec); |
064af421 BP |
3539 | ofs->priority = htons(rule->cr.priority); |
3540 | ofs->idle_timeout = htons(rule->idle_timeout); | |
3541 | ofs->hard_timeout = htons(rule->hard_timeout); | |
39997502 | 3542 | memset(ofs->pad2, 0, sizeof ofs->pad2); |
c4617b3c BP |
3543 | put_32aligned_be64(&ofs->packet_count, htonll(packet_count)); |
3544 | put_32aligned_be64(&ofs->byte_count, htonll(byte_count)); | |
3dffcf07 BP |
3545 | if (rule->n_actions > 0) { |
3546 | memcpy(ofs->actions, rule->actions, act_len); | |
3547 | } | |
064af421 BP |
3548 | } |
3549 | ||
3c4486a5 BP |
3550 | static bool |
3551 | is_valid_table(uint8_t table_id) | |
064af421 | 3552 | { |
3c4486a5 | 3553 | return table_id == 0 || table_id == 0xff; |
064af421 BP |
3554 | } |
3555 | ||
3556 | static int | |
d1e2cf21 | 3557 | handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3558 | { |
d1e2cf21 | 3559 | const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh); |
5ecc9d81 | 3560 | struct ofpbuf *reply; |
064af421 | 3561 | |
064af421 | 3562 | COVERAGE_INC(ofproto_flows_req); |
d1e2cf21 | 3563 | reply = start_ofp_stats_reply(oh, 1024); |
3c4486a5 | 3564 | if (is_valid_table(fsr->table_id)) { |
5ecc9d81 | 3565 | struct cls_cursor cursor; |
3c4486a5 | 3566 | struct cls_rule target; |
5ecc9d81 | 3567 | struct rule *rule; |
3c4486a5 | 3568 | |
d8ae4d67 BP |
3569 | ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0, |
3570 | &target); | |
5ecc9d81 BP |
3571 | cls_cursor_init(&cursor, &ofconn->ofproto->cls, &target); |
3572 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
3573 | put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply); | |
3574 | } | |
3c4486a5 | 3575 | } |
5ecc9d81 | 3576 | queue_tx(reply, ofconn, ofconn->reply_counter); |
3c4486a5 | 3577 | |
064af421 BP |
3578 | return 0; |
3579 | } | |
3580 | ||
09246b99 | 3581 | static void |
5ecc9d81 BP |
3582 | put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule, |
3583 | ovs_be16 out_port, struct ofpbuf **replyp) | |
09246b99 | 3584 | { |
09246b99 BP |
3585 | struct nx_flow_stats *nfs; |
3586 | uint64_t packet_count, byte_count; | |
3587 | size_t act_len, start_len; | |
5ecc9d81 | 3588 | struct ofpbuf *reply; |
09246b99 | 3589 | |
5ecc9d81 | 3590 | if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) { |
09246b99 BP |
3591 | return; |
3592 | } | |
3593 | ||
5ecc9d81 | 3594 | query_stats(ofconn->ofproto, rule, &packet_count, &byte_count); |
09246b99 BP |
3595 | |
3596 | act_len = sizeof *rule->actions * rule->n_actions; | |
3597 | ||
5ecc9d81 | 3598 | append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp); |
1dfee98d | 3599 | start_len = (*replyp)->size; |
5ecc9d81 BP |
3600 | reply = *replyp; |
3601 | ||
3602 | nfs = ofpbuf_put_uninit(reply, sizeof *nfs); | |
09246b99 BP |
3603 | nfs->table_id = 0; |
3604 | nfs->pad = 0; | |
3605 | calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec); | |
3606 | nfs->cookie = rule->flow_cookie; | |
3607 | nfs->priority = htons(rule->cr.priority); | |
3608 | nfs->idle_timeout = htons(rule->idle_timeout); | |
3609 | nfs->hard_timeout = htons(rule->hard_timeout); | |
5ecc9d81 | 3610 | nfs->match_len = htons(nx_put_match(reply, &rule->cr)); |
09246b99 BP |
3611 | memset(nfs->pad2, 0, sizeof nfs->pad2); |
3612 | nfs->packet_count = htonll(packet_count); | |
3613 | nfs->byte_count = htonll(byte_count); | |
3614 | if (rule->n_actions > 0) { | |
5ecc9d81 | 3615 | ofpbuf_put(reply, rule->actions, act_len); |
09246b99 | 3616 | } |
5ecc9d81 | 3617 | nfs->length = htons(reply->size - start_len); |
09246b99 BP |
3618 | } |
3619 | ||
3620 | static int | |
d1e2cf21 | 3621 | handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh) |
09246b99 BP |
3622 | { |
3623 | struct nx_flow_stats_request *nfsr; | |
09246b99 | 3624 | struct cls_rule target; |
5ecc9d81 | 3625 | struct ofpbuf *reply; |
d1e2cf21 | 3626 | struct ofpbuf b; |
09246b99 BP |
3627 | int error; |
3628 | ||
0bc9407d | 3629 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
d1e2cf21 | 3630 | |
09246b99 | 3631 | /* Dissect the message. */ |
bbc32a88 | 3632 | nfsr = ofpbuf_pull(&b, sizeof *nfsr); |
d1e2cf21 | 3633 | error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target); |
09246b99 BP |
3634 | if (error) { |
3635 | return error; | |
3636 | } | |
d1e2cf21 BP |
3637 | if (b.size) { |
3638 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); | |
3639 | } | |
09246b99 BP |
3640 | |
3641 | COVERAGE_INC(ofproto_flows_req); | |
5ecc9d81 | 3642 | reply = start_nxstats_reply(&nfsr->nsm, 1024); |
3c4486a5 | 3643 | if (is_valid_table(nfsr->table_id)) { |
5ecc9d81 BP |
3644 | struct cls_cursor cursor; |
3645 | struct rule *rule; | |
3646 | ||
3647 | cls_cursor_init(&cursor, &ofconn->ofproto->cls, &target); | |
3648 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
3649 | put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply); | |
3650 | } | |
3c4486a5 | 3651 | } |
5ecc9d81 BP |
3652 | queue_tx(reply, ofconn, ofconn->reply_counter); |
3653 | ||
09246b99 BP |
3654 | return 0; |
3655 | } | |
3656 | ||
4f2cad2c | 3657 | static void |
5ecc9d81 | 3658 | flow_stats_ds(struct ofproto *ofproto, struct rule *rule, struct ds *results) |
4f2cad2c | 3659 | { |
4f2cad2c JP |
3660 | uint64_t packet_count, byte_count; |
3661 | size_t act_len = sizeof *rule->actions * rule->n_actions; | |
3662 | ||
5ecc9d81 | 3663 | query_stats(ofproto, rule, &packet_count, &byte_count); |
4f2cad2c JP |
3664 | |
3665 | ds_put_format(results, "duration=%llds, ", | |
3666 | (time_msec() - rule->created) / 1000); | |
3693a563 | 3667 | ds_put_format(results, "idle=%.3fs, ", (time_msec() - rule->used) / 1000.0); |
52ae00b3 | 3668 | ds_put_format(results, "priority=%u, ", rule->cr.priority); |
4f2cad2c JP |
3669 | ds_put_format(results, "n_packets=%"PRIu64", ", packet_count); |
3670 | ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count); | |
cb833cf6 | 3671 | cls_rule_format(&rule->cr, results); |
a5df0e72 | 3672 | ds_put_char(results, ','); |
3dffcf07 BP |
3673 | if (act_len > 0) { |
3674 | ofp_print_actions(results, &rule->actions->header, act_len); | |
3c8552c1 JP |
3675 | } else { |
3676 | ds_put_cstr(results, "drop"); | |
3dffcf07 | 3677 | } |
4f2cad2c JP |
3678 | ds_put_cstr(results, "\n"); |
3679 | } | |
3680 | ||
d295e8e9 | 3681 | /* Adds a pretty-printed description of all flows to 'results', including |
4f2cad2c JP |
3682 | * those marked hidden by secchan (e.g., by in-band control). */ |
3683 | void | |
3684 | ofproto_get_all_flows(struct ofproto *p, struct ds *results) | |
3685 | { | |
5ecc9d81 BP |
3686 | struct cls_cursor cursor; |
3687 | struct rule *rule; | |
064af421 | 3688 | |
5ecc9d81 BP |
3689 | cls_cursor_init(&cursor, &p->cls, NULL); |
3690 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
3691 | flow_stats_ds(p, rule, results); | |
064af421 | 3692 | } |
064af421 BP |
3693 | } |
3694 | ||
27d34fce BP |
3695 | static void |
3696 | query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target, | |
734bbeb4 | 3697 | ovs_be16 out_port, uint8_t table_id, |
27d34fce BP |
3698 | struct ofp_aggregate_stats_reply *oasr) |
3699 | { | |
5ecc9d81 BP |
3700 | uint64_t total_packets = 0; |
3701 | uint64_t total_bytes = 0; | |
3702 | int n_flows = 0; | |
27d34fce BP |
3703 | |
3704 | COVERAGE_INC(ofproto_agg_request); | |
5ecc9d81 | 3705 | |
3c4486a5 | 3706 | if (is_valid_table(table_id)) { |
5ecc9d81 BP |
3707 | struct cls_cursor cursor; |
3708 | struct rule *rule; | |
3c4486a5 | 3709 | |
5ecc9d81 BP |
3710 | cls_cursor_init(&cursor, &ofproto->cls, target); |
3711 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
3712 | if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) { | |
3713 | uint64_t packet_count; | |
3714 | uint64_t byte_count; | |
3715 | ||
3716 | query_stats(ofproto, rule, &packet_count, &byte_count); | |
3717 | ||
3718 | total_packets += packet_count; | |
3719 | total_bytes += byte_count; | |
3720 | n_flows++; | |
3721 | } | |
3722 | } | |
3c4486a5 | 3723 | } |
27d34fce | 3724 | |
5ecc9d81 | 3725 | oasr->flow_count = htonl(n_flows); |
c4617b3c BP |
3726 | put_32aligned_be64(&oasr->packet_count, htonll(total_packets)); |
3727 | put_32aligned_be64(&oasr->byte_count, htonll(total_bytes)); | |
27d34fce BP |
3728 | memset(oasr->pad, 0, sizeof oasr->pad); |
3729 | } | |
3730 | ||
064af421 | 3731 | static int |
3269c562 | 3732 | handle_aggregate_stats_request(struct ofconn *ofconn, |
d1e2cf21 | 3733 | const struct ofp_header *oh) |
064af421 | 3734 | { |
d1e2cf21 | 3735 | const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh); |
064af421 | 3736 | struct ofp_aggregate_stats_reply *reply; |
064af421 BP |
3737 | struct cls_rule target; |
3738 | struct ofpbuf *msg; | |
3739 | ||
d8ae4d67 BP |
3740 | ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0, |
3741 | &target); | |
064af421 | 3742 | |
d1e2cf21 | 3743 | msg = start_ofp_stats_reply(oh, sizeof *reply); |
06a5e131 | 3744 | reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg); |
27d34fce BP |
3745 | query_aggregate_stats(ofconn->ofproto, &target, request->out_port, |
3746 | request->table_id, reply); | |
064af421 BP |
3747 | queue_tx(msg, ofconn, ofconn->reply_counter); |
3748 | return 0; | |
3749 | } | |
3750 | ||
09246b99 | 3751 | static int |
d1e2cf21 | 3752 | handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh) |
09246b99 BP |
3753 | { |
3754 | struct nx_aggregate_stats_request *request; | |
3755 | struct ofp_aggregate_stats_reply *reply; | |
3756 | struct cls_rule target; | |
d1e2cf21 | 3757 | struct ofpbuf b; |
09246b99 BP |
3758 | struct ofpbuf *buf; |
3759 | int error; | |
3760 | ||
0bc9407d | 3761 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
d1e2cf21 | 3762 | |
09246b99 | 3763 | /* Dissect the message. */ |
bbc32a88 | 3764 | request = ofpbuf_pull(&b, sizeof *request); |
d1e2cf21 | 3765 | error = nx_pull_match(&b, ntohs(request->match_len), 0, &target); |
09246b99 BP |
3766 | if (error) { |
3767 | return error; | |
3768 | } | |
d1e2cf21 BP |
3769 | if (b.size) { |
3770 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); | |
3771 | } | |
09246b99 BP |
3772 | |
3773 | /* Reply. */ | |
3774 | COVERAGE_INC(ofproto_flows_req); | |
3775 | buf = start_nxstats_reply(&request->nsm, sizeof *reply); | |
3776 | reply = ofpbuf_put_uninit(buf, sizeof *reply); | |
3777 | query_aggregate_stats(ofconn->ofproto, &target, request->out_port, | |
3778 | request->table_id, reply); | |
3779 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
3780 | ||
3781 | return 0; | |
3782 | } | |
3783 | ||
c1c9c9c4 BP |
3784 | struct queue_stats_cbdata { |
3785 | struct ofconn *ofconn; | |
ca0f572c | 3786 | struct ofport *ofport; |
c1c9c9c4 | 3787 | struct ofpbuf *msg; |
c1c9c9c4 BP |
3788 | }; |
3789 | ||
3790 | static void | |
db9220c3 | 3791 | put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id, |
c1c9c9c4 BP |
3792 | const struct netdev_queue_stats *stats) |
3793 | { | |
3794 | struct ofp_queue_stats *reply; | |
3795 | ||
06a5e131 | 3796 | reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg); |
ca0f572c | 3797 | reply->port_no = htons(cbdata->ofport->opp.port_no); |
c1c9c9c4 BP |
3798 | memset(reply->pad, 0, sizeof reply->pad); |
3799 | reply->queue_id = htonl(queue_id); | |
c4617b3c BP |
3800 | put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes)); |
3801 | put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets)); | |
3802 | put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors)); | |
c1c9c9c4 BP |
3803 | } |
3804 | ||
3805 | static void | |
db9220c3 | 3806 | handle_queue_stats_dump_cb(uint32_t queue_id, |
c1c9c9c4 BP |
3807 | struct netdev_queue_stats *stats, |
3808 | void *cbdata_) | |
3809 | { | |
3810 | struct queue_stats_cbdata *cbdata = cbdata_; | |
3811 | ||
3812 | put_queue_stats(cbdata, queue_id, stats); | |
3813 | } | |
3814 | ||
3815 | static void | |
ca0f572c | 3816 | handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id, |
c1c9c9c4 BP |
3817 | struct queue_stats_cbdata *cbdata) |
3818 | { | |
ca0f572c | 3819 | cbdata->ofport = port; |
c1c9c9c4 BP |
3820 | if (queue_id == OFPQ_ALL) { |
3821 | netdev_dump_queue_stats(port->netdev, | |
3822 | handle_queue_stats_dump_cb, cbdata); | |
3823 | } else { | |
3824 | struct netdev_queue_stats stats; | |
3825 | ||
1ac788f6 BP |
3826 | if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) { |
3827 | put_queue_stats(cbdata, queue_id, &stats); | |
3828 | } | |
c1c9c9c4 BP |
3829 | } |
3830 | } | |
3831 | ||
3832 | static int | |
d1e2cf21 | 3833 | handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh) |
c1c9c9c4 | 3834 | { |
3269c562 | 3835 | struct ofproto *ofproto = ofconn->ofproto; |
d1e2cf21 | 3836 | const struct ofp_queue_stats_request *qsr; |
c1c9c9c4 BP |
3837 | struct queue_stats_cbdata cbdata; |
3838 | struct ofport *port; | |
3839 | unsigned int port_no; | |
3840 | uint32_t queue_id; | |
3841 | ||
d1e2cf21 BP |
3842 | qsr = ofputil_stats_body(oh); |
3843 | if (!qsr) { | |
c1c9c9c4 BP |
3844 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN); |
3845 | } | |
c1c9c9c4 BP |
3846 | |
3847 | COVERAGE_INC(ofproto_queue_req); | |
3848 | ||
3849 | cbdata.ofconn = ofconn; | |
d1e2cf21 | 3850 | cbdata.msg = start_ofp_stats_reply(oh, 128); |
c1c9c9c4 BP |
3851 | |
3852 | port_no = ntohs(qsr->port_no); | |
3853 | queue_id = ntohl(qsr->queue_id); | |
3854 | if (port_no == OFPP_ALL) { | |
4e8e4213 | 3855 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
ca0f572c | 3856 | handle_queue_stats_for_port(port, queue_id, &cbdata); |
c1c9c9c4 BP |
3857 | } |
3858 | } else if (port_no < ofproto->max_ports) { | |
ca0f572c | 3859 | port = get_port(ofproto, ofp_port_to_odp_port(port_no)); |
c1c9c9c4 | 3860 | if (port) { |
ca0f572c | 3861 | handle_queue_stats_for_port(port, queue_id, &cbdata); |
c1c9c9c4 BP |
3862 | } |
3863 | } else { | |
3864 | ofpbuf_delete(cbdata.msg); | |
3865 | return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT); | |
3866 | } | |
3867 | queue_tx(cbdata.msg, ofconn, ofconn->reply_counter); | |
3868 | ||
3869 | return 0; | |
3870 | } | |
3871 | ||
064af421 | 3872 | static void |
bcf84111 | 3873 | facet_update_time(struct ofproto *ofproto, struct facet *facet, |
c97fb132 | 3874 | const struct dpif_flow_stats *stats) |
064af421 | 3875 | { |
c97fb132 | 3876 | long long int used = stats->used; |
bcf84111 BP |
3877 | if (used > facet->used) { |
3878 | facet->used = used; | |
3879 | if (used > facet->rule->used) { | |
3880 | facet->rule->used = used; | |
4836f9f2 | 3881 | } |
bcf84111 | 3882 | netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used); |
064af421 BP |
3883 | } |
3884 | } | |
3885 | ||
bcf84111 BP |
3886 | /* Folds the statistics from 'stats' into the counters in 'facet'. |
3887 | * | |
3888 | * Because of the meaning of a facet's counters, it only makes sense to do this | |
3889 | * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a | |
3890 | * packet that was sent by hand or if it represents statistics that have been | |
3891 | * cleared out of the datapath. */ | |
064af421 | 3892 | static void |
bcf84111 | 3893 | facet_update_stats(struct ofproto *ofproto, struct facet *facet, |
c97fb132 | 3894 | const struct dpif_flow_stats *stats) |
064af421 | 3895 | { |
064af421 | 3896 | if (stats->n_packets) { |
bcf84111 BP |
3897 | facet_update_time(ofproto, facet, stats); |
3898 | facet->packet_count += stats->n_packets; | |
3899 | facet->byte_count += stats->n_bytes; | |
3900 | netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags); | |
064af421 BP |
3901 | } |
3902 | } | |
3903 | ||
79eee1eb BP |
3904 | /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT |
3905 | * in which no matching flow already exists in the flow table. | |
3906 | * | |
3907 | * Adds the flow specified by 'ofm', which is followed by 'n_actions' | |
3269c562 BP |
3908 | * ofp_actions, to ofconn->ofproto's flow table. Returns 0 on success or an |
3909 | * OpenFlow error code as encoded by ofp_mkerr() on failure. | |
79eee1eb BP |
3910 | * |
3911 | * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id, | |
3912 | * if any. */ | |
064af421 | 3913 | static int |
3052b0c5 | 3914 | add_flow(struct ofconn *ofconn, struct flow_mod *fm) |
064af421 | 3915 | { |
3269c562 | 3916 | struct ofproto *p = ofconn->ofproto; |
064af421 BP |
3917 | struct ofpbuf *packet; |
3918 | struct rule *rule; | |
3919 | uint16_t in_port; | |
3920 | int error; | |
3921 | ||
3052b0c5 BP |
3922 | if (fm->flags & OFPFF_CHECK_OVERLAP |
3923 | && classifier_rule_overlaps(&p->cls, &fm->cr)) { | |
3924 | return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP); | |
49bdc010 JP |
3925 | } |
3926 | ||
064af421 | 3927 | error = 0; |
3052b0c5 BP |
3928 | if (fm->buffer_id != UINT32_MAX) { |
3929 | error = pktbuf_retrieve(ofconn->pktbuf, fm->buffer_id, | |
064af421 | 3930 | &packet, &in_port); |
212fe71c BP |
3931 | } else { |
3932 | packet = NULL; | |
165cd8a3 | 3933 | in_port = UINT16_MAX; |
064af421 BP |
3934 | } |
3935 | ||
bcf84111 BP |
3936 | rule = rule_create(&fm->cr, fm->actions, fm->n_actions, |
3937 | fm->idle_timeout, fm->hard_timeout, fm->cookie, | |
3938 | fm->flags & OFPFF_SEND_FLOW_REM); | |
afe75089 BP |
3939 | rule_insert(p, rule); |
3940 | if (packet) { | |
3941 | rule_execute(p, rule, in_port, packet); | |
3942 | } | |
064af421 BP |
3943 | return error; |
3944 | } | |
3945 | ||
79eee1eb | 3946 | static struct rule * |
3052b0c5 | 3947 | find_flow_strict(struct ofproto *p, const struct flow_mod *fm) |
064af421 | 3948 | { |
3052b0c5 | 3949 | return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr)); |
79eee1eb | 3950 | } |
064af421 | 3951 | |
79eee1eb | 3952 | static int |
3269c562 | 3953 | send_buffered_packet(struct ofconn *ofconn, |
3052b0c5 | 3954 | struct rule *rule, uint32_t buffer_id) |
79eee1eb BP |
3955 | { |
3956 | struct ofpbuf *packet; | |
3957 | uint16_t in_port; | |
79eee1eb | 3958 | int error; |
064af421 | 3959 | |
3052b0c5 | 3960 | if (buffer_id == UINT32_MAX) { |
79eee1eb | 3961 | return 0; |
064af421 | 3962 | } |
79eee1eb | 3963 | |
3052b0c5 | 3964 | error = pktbuf_retrieve(ofconn->pktbuf, buffer_id, &packet, &in_port); |
79eee1eb BP |
3965 | if (error) { |
3966 | return error; | |
3967 | } | |
3968 | ||
bcf84111 | 3969 | rule_execute(ofconn->ofproto, rule, in_port, packet); |
79eee1eb | 3970 | |
064af421 BP |
3971 | return 0; |
3972 | } | |
79eee1eb BP |
3973 | \f |
3974 | /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */ | |
064af421 BP |
3975 | |
3976 | struct modify_flows_cbdata { | |
3977 | struct ofproto *ofproto; | |
3052b0c5 | 3978 | const struct flow_mod *fm; |
79eee1eb | 3979 | struct rule *match; |
064af421 BP |
3980 | }; |
3981 | ||
3052b0c5 BP |
3982 | static int modify_flow(struct ofproto *, const struct flow_mod *, |
3983 | struct rule *); | |
79eee1eb BP |
3984 | |
3985 | /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as | |
3986 | * encoded by ofp_mkerr() on failure. | |
3987 | * | |
3988 | * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id, | |
3989 | * if any. */ | |
3990 | static int | |
3052b0c5 | 3991 | modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm) |
79eee1eb | 3992 | { |
5ecc9d81 BP |
3993 | struct ofproto *p = ofconn->ofproto; |
3994 | struct rule *match = NULL; | |
3995 | struct cls_cursor cursor; | |
3996 | struct rule *rule; | |
79eee1eb | 3997 | |
5ecc9d81 BP |
3998 | cls_cursor_init(&cursor, &p->cls, &fm->cr); |
3999 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
4000 | if (!rule_is_hidden(rule)) { | |
4001 | match = rule; | |
4002 | modify_flow(p, fm, rule); | |
4003 | } | |
4004 | } | |
79eee1eb | 4005 | |
5ecc9d81 | 4006 | if (match) { |
d6302b0f BP |
4007 | /* This credits the packet to whichever flow happened to match last. |
4008 | * That's weird. Maybe we should do a lookup for the flow that | |
4009 | * actually matches the packet? Who knows. */ | |
5ecc9d81 | 4010 | send_buffered_packet(ofconn, match, fm->buffer_id); |
79eee1eb BP |
4011 | return 0; |
4012 | } else { | |
3052b0c5 | 4013 | return add_flow(ofconn, fm); |
79eee1eb BP |
4014 | } |
4015 | } | |
4016 | ||
4017 | /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error | |
4018 | * code as encoded by ofp_mkerr() on failure. | |
4019 | * | |
4020 | * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id, | |
4021 | * if any. */ | |
4022 | static int | |
3052b0c5 | 4023 | modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm) |
79eee1eb | 4024 | { |
3052b0c5 BP |
4025 | struct ofproto *p = ofconn->ofproto; |
4026 | struct rule *rule = find_flow_strict(p, fm); | |
79eee1eb | 4027 | if (rule && !rule_is_hidden(rule)) { |
3052b0c5 BP |
4028 | modify_flow(p, fm, rule); |
4029 | return send_buffered_packet(ofconn, rule, fm->buffer_id); | |
79eee1eb | 4030 | } else { |
3052b0c5 | 4031 | return add_flow(ofconn, fm); |
79eee1eb BP |
4032 | } |
4033 | } | |
4034 | ||
79eee1eb BP |
4035 | /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has |
4036 | * been identified as a flow in 'p''s flow table to be modified, by changing | |
4037 | * the rule's actions to match those in 'ofm' (which is followed by 'n_actions' | |
4038 | * ofp_action[] structures). */ | |
064af421 | 4039 | static int |
3052b0c5 | 4040 | modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule) |
064af421 | 4041 | { |
3052b0c5 | 4042 | size_t actions_len = fm->n_actions * sizeof *rule->actions; |
79eee1eb | 4043 | |
3052b0c5 | 4044 | rule->flow_cookie = fm->cookie; |
79eee1eb BP |
4045 | |
4046 | /* If the actions are the same, do nothing. */ | |
3052b0c5 BP |
4047 | if (fm->n_actions == rule->n_actions |
4048 | && (!fm->n_actions | |
4049 | || !memcmp(fm->actions, rule->actions, actions_len))) { | |
79eee1eb BP |
4050 | return 0; |
4051 | } | |
4052 | ||
4053 | /* Replace actions. */ | |
4054 | free(rule->actions); | |
3052b0c5 BP |
4055 | rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL; |
4056 | rule->n_actions = fm->n_actions; | |
79eee1eb | 4057 | |
bcf84111 | 4058 | p->need_revalidate = true; |
79eee1eb BP |
4059 | |
4060 | return 0; | |
4061 | } | |
4062 | \f | |
4063 | /* OFPFC_DELETE implementation. */ | |
4064 | ||
8054fc48 | 4065 | static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port); |
79eee1eb BP |
4066 | |
4067 | /* Implements OFPFC_DELETE. */ | |
4068 | static void | |
3052b0c5 | 4069 | delete_flows_loose(struct ofproto *p, const struct flow_mod *fm) |
79eee1eb | 4070 | { |
5ecc9d81 BP |
4071 | struct rule *rule, *next_rule; |
4072 | struct cls_cursor cursor; | |
064af421 | 4073 | |
5ecc9d81 BP |
4074 | cls_cursor_init(&cursor, &p->cls, &fm->cr); |
4075 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) { | |
4076 | delete_flow(p, rule, htons(fm->out_port)); | |
4077 | } | |
064af421 BP |
4078 | } |
4079 | ||
79eee1eb BP |
4080 | /* Implements OFPFC_DELETE_STRICT. */ |
4081 | static void | |
3052b0c5 | 4082 | delete_flow_strict(struct ofproto *p, struct flow_mod *fm) |
79eee1eb | 4083 | { |
3052b0c5 | 4084 | struct rule *rule = find_flow_strict(p, fm); |
79eee1eb | 4085 | if (rule) { |
3052b0c5 | 4086 | delete_flow(p, rule, htons(fm->out_port)); |
79eee1eb BP |
4087 | } |
4088 | } | |
4089 | ||
79eee1eb BP |
4090 | /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has |
4091 | * been identified as a flow to delete from 'p''s flow table, by deleting the | |
4092 | * flow and sending out a OFPT_FLOW_REMOVED message to any interested | |
4093 | * controller. | |
4094 | * | |
4095 | * Will not delete 'rule' if it is hidden. Will delete 'rule' only if | |
4096 | * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the | |
4097 | * specified 'out_port'. */ | |
4098 | static void | |
8054fc48 | 4099 | delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port) |
79eee1eb BP |
4100 | { |
4101 | if (rule_is_hidden(rule)) { | |
4102 | return; | |
4103 | } | |
4104 | ||
4105 | if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) { | |
4106 | return; | |
4107 | } | |
4108 | ||
bcf84111 | 4109 | rule_send_removed(p, rule, OFPRR_DELETE); |
79eee1eb BP |
4110 | rule_remove(p, rule); |
4111 | } | |
4112 | \f | |
064af421 | 4113 | static int |
2e4f5fcf | 4114 | handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 4115 | { |
3052b0c5 | 4116 | struct ofproto *p = ofconn->ofproto; |
2e4f5fcf | 4117 | struct flow_mod fm; |
064af421 BP |
4118 | int error; |
4119 | ||
3052b0c5 | 4120 | error = reject_slave_controller(ofconn, "flow_mod"); |
9deba63b BP |
4121 | if (error) { |
4122 | return error; | |
4123 | } | |
3052b0c5 | 4124 | |
2e4f5fcf | 4125 | error = ofputil_decode_flow_mod(&fm, oh, ofconn->flow_format); |
064af421 BP |
4126 | if (error) { |
4127 | return error; | |
4128 | } | |
4129 | ||
2e4f5fcf BP |
4130 | /* We do not support the emergency flow cache. It will hopefully get |
4131 | * dropped from OpenFlow in the near future. */ | |
4132 | if (fm.flags & OFPFF_EMERG) { | |
49bdc010 JP |
4133 | /* There isn't a good fit for an error code, so just state that the |
4134 | * flow table is full. */ | |
4135 | return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL); | |
4136 | } | |
4137 | ||
2e4f5fcf BP |
4138 | error = validate_actions(fm.actions, fm.n_actions, |
4139 | &fm.cr.flow, p->max_ports); | |
4140 | if (error) { | |
4141 | return error; | |
4142 | } | |
4143 | ||
4144 | switch (fm.command) { | |
3052b0c5 | 4145 | case OFPFC_ADD: |
2e4f5fcf | 4146 | return add_flow(ofconn, &fm); |
3052b0c5 BP |
4147 | |
4148 | case OFPFC_MODIFY: | |
2e4f5fcf | 4149 | return modify_flows_loose(ofconn, &fm); |
3052b0c5 BP |
4150 | |
4151 | case OFPFC_MODIFY_STRICT: | |
2e4f5fcf | 4152 | return modify_flow_strict(ofconn, &fm); |
3052b0c5 BP |
4153 | |
4154 | case OFPFC_DELETE: | |
2e4f5fcf | 4155 | delete_flows_loose(p, &fm); |
3052b0c5 BP |
4156 | return 0; |
4157 | ||
4158 | case OFPFC_DELETE_STRICT: | |
2e4f5fcf | 4159 | delete_flow_strict(p, &fm); |
3052b0c5 BP |
4160 | return 0; |
4161 | ||
4162 | default: | |
4163 | return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND); | |
4164 | } | |
4165 | } | |
4166 | ||
659586ef | 4167 | static int |
d1e2cf21 | 4168 | handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh) |
659586ef | 4169 | { |
d1e2cf21 BP |
4170 | const struct nxt_tun_id_cookie *msg |
4171 | = (const struct nxt_tun_id_cookie *) oh; | |
659586ef | 4172 | |
b70eac89 | 4173 | ofconn->flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10; |
659586ef JG |
4174 | return 0; |
4175 | } | |
4176 | ||
9deba63b | 4177 | static int |
d1e2cf21 | 4178 | handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh) |
9deba63b | 4179 | { |
d1e2cf21 | 4180 | struct nx_role_request *nrr = (struct nx_role_request *) oh; |
9deba63b BP |
4181 | struct nx_role_request *reply; |
4182 | struct ofpbuf *buf; | |
4183 | uint32_t role; | |
4184 | ||
5899143f | 4185 | if (ofconn->type != OFCONN_PRIMARY) { |
9deba63b BP |
4186 | VLOG_WARN_RL(&rl, "ignoring role request on non-controller " |
4187 | "connection"); | |
4188 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM); | |
4189 | } | |
4190 | ||
4191 | role = ntohl(nrr->role); | |
4192 | if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER | |
4193 | && role != NX_ROLE_SLAVE) { | |
4194 | VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role); | |
4195 | ||
4196 | /* There's no good error code for this. */ | |
4197 | return ofp_mkerr(OFPET_BAD_REQUEST, -1); | |
4198 | } | |
4199 | ||
4200 | if (role == NX_ROLE_MASTER) { | |
4201 | struct ofconn *other; | |
4202 | ||
3269c562 | 4203 | HMAP_FOR_EACH (other, hmap_node, &ofconn->ofproto->controllers) { |
9deba63b BP |
4204 | if (other->role == NX_ROLE_MASTER) { |
4205 | other->role = NX_ROLE_SLAVE; | |
4206 | } | |
4207 | } | |
4208 | } | |
4209 | ofconn->role = role; | |
4210 | ||
d1e2cf21 | 4211 | reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf); |
9deba63b BP |
4212 | reply->role = htonl(role); |
4213 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
4214 | ||
4215 | return 0; | |
4216 | } | |
4217 | ||
09246b99 | 4218 | static int |
d1e2cf21 | 4219 | handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh) |
09246b99 | 4220 | { |
d1e2cf21 BP |
4221 | const struct nxt_set_flow_format *msg |
4222 | = (const struct nxt_set_flow_format *) oh; | |
09246b99 | 4223 | uint32_t format; |
09246b99 BP |
4224 | |
4225 | format = ntohl(msg->format); | |
4226 | if (format == NXFF_OPENFLOW10 | |
4227 | || format == NXFF_TUN_ID_FROM_COOKIE | |
4228 | || format == NXFF_NXM) { | |
4229 | ofconn->flow_format = format; | |
4230 | return 0; | |
4231 | } else { | |
4232 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM); | |
4233 | } | |
4234 | } | |
4235 | ||
064af421 | 4236 | static int |
d1e2cf21 | 4237 | handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh) |
246e61ea JP |
4238 | { |
4239 | struct ofp_header *ob; | |
4240 | struct ofpbuf *buf; | |
4241 | ||
4242 | /* Currently, everything executes synchronously, so we can just | |
4243 | * immediately send the barrier reply. */ | |
4244 | ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf); | |
4245 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
4246 | return 0; | |
4247 | } | |
4248 | ||
d1e2cf21 BP |
4249 | static int |
4250 | handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg) | |
064af421 | 4251 | { |
d1e2cf21 BP |
4252 | const struct ofp_header *oh = msg->data; |
4253 | const struct ofputil_msg_type *type; | |
064af421 BP |
4254 | int error; |
4255 | ||
d1e2cf21 BP |
4256 | error = ofputil_decode_msg_type(oh, &type); |
4257 | if (error) { | |
4258 | return error; | |
4259 | } | |
064af421 | 4260 | |
d1e2cf21 BP |
4261 | switch (ofputil_msg_type_code(type)) { |
4262 | /* OpenFlow requests. */ | |
4263 | case OFPUTIL_OFPT_ECHO_REQUEST: | |
4264 | return handle_echo_request(ofconn, oh); | |
064af421 | 4265 | |
d1e2cf21 BP |
4266 | case OFPUTIL_OFPT_FEATURES_REQUEST: |
4267 | return handle_features_request(ofconn, oh); | |
064af421 | 4268 | |
d1e2cf21 BP |
4269 | case OFPUTIL_OFPT_GET_CONFIG_REQUEST: |
4270 | return handle_get_config_request(ofconn, oh); | |
064af421 | 4271 | |
d1e2cf21 BP |
4272 | case OFPUTIL_OFPT_SET_CONFIG: |
4273 | return handle_set_config(ofconn, msg->data); | |
064af421 | 4274 | |
d1e2cf21 BP |
4275 | case OFPUTIL_OFPT_PACKET_OUT: |
4276 | return handle_packet_out(ofconn, oh); | |
064af421 | 4277 | |
d1e2cf21 BP |
4278 | case OFPUTIL_OFPT_PORT_MOD: |
4279 | return handle_port_mod(ofconn, oh); | |
064af421 | 4280 | |
d1e2cf21 | 4281 | case OFPUTIL_OFPT_FLOW_MOD: |
2e4f5fcf | 4282 | return handle_flow_mod(ofconn, oh); |
064af421 | 4283 | |
d1e2cf21 BP |
4284 | case OFPUTIL_OFPT_BARRIER_REQUEST: |
4285 | return handle_barrier_request(ofconn, oh); | |
064af421 | 4286 | |
d1e2cf21 BP |
4287 | /* OpenFlow replies. */ |
4288 | case OFPUTIL_OFPT_ECHO_REPLY: | |
4289 | return 0; | |
246e61ea | 4290 | |
d1e2cf21 BP |
4291 | /* Nicira extension requests. */ |
4292 | case OFPUTIL_NXT_STATUS_REQUEST: | |
4293 | return switch_status_handle_request( | |
4294 | ofconn->ofproto->switch_status, ofconn->rconn, oh); | |
4295 | ||
4296 | case OFPUTIL_NXT_TUN_ID_FROM_COOKIE: | |
4297 | return handle_tun_id_from_cookie(ofconn, oh); | |
4298 | ||
4299 | case OFPUTIL_NXT_ROLE_REQUEST: | |
4300 | return handle_role_request(ofconn, oh); | |
4301 | ||
4302 | case OFPUTIL_NXT_SET_FLOW_FORMAT: | |
4303 | return handle_nxt_set_flow_format(ofconn, oh); | |
4304 | ||
4305 | case OFPUTIL_NXT_FLOW_MOD: | |
2e4f5fcf | 4306 | return handle_flow_mod(ofconn, oh); |
d1e2cf21 BP |
4307 | |
4308 | /* OpenFlow statistics requests. */ | |
4309 | case OFPUTIL_OFPST_DESC_REQUEST: | |
4310 | return handle_desc_stats_request(ofconn, oh); | |
4311 | ||
4312 | case OFPUTIL_OFPST_FLOW_REQUEST: | |
4313 | return handle_flow_stats_request(ofconn, oh); | |
4314 | ||
4315 | case OFPUTIL_OFPST_AGGREGATE_REQUEST: | |
4316 | return handle_aggregate_stats_request(ofconn, oh); | |
4317 | ||
4318 | case OFPUTIL_OFPST_TABLE_REQUEST: | |
4319 | return handle_table_stats_request(ofconn, oh); | |
4320 | ||
4321 | case OFPUTIL_OFPST_PORT_REQUEST: | |
4322 | return handle_port_stats_request(ofconn, oh); | |
4323 | ||
4324 | case OFPUTIL_OFPST_QUEUE_REQUEST: | |
4325 | return handle_queue_stats_request(ofconn, oh); | |
4326 | ||
4327 | /* Nicira extension statistics requests. */ | |
4328 | case OFPUTIL_NXST_FLOW_REQUEST: | |
4329 | return handle_nxst_flow(ofconn, oh); | |
4330 | ||
4331 | case OFPUTIL_NXST_AGGREGATE_REQUEST: | |
4332 | return handle_nxst_aggregate(ofconn, oh); | |
4333 | ||
4334 | case OFPUTIL_INVALID: | |
4335 | case OFPUTIL_OFPT_HELLO: | |
4336 | case OFPUTIL_OFPT_ERROR: | |
4337 | case OFPUTIL_OFPT_FEATURES_REPLY: | |
4338 | case OFPUTIL_OFPT_GET_CONFIG_REPLY: | |
4339 | case OFPUTIL_OFPT_PACKET_IN: | |
4340 | case OFPUTIL_OFPT_FLOW_REMOVED: | |
4341 | case OFPUTIL_OFPT_PORT_STATUS: | |
4342 | case OFPUTIL_OFPT_BARRIER_REPLY: | |
4343 | case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST: | |
4344 | case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY: | |
4345 | case OFPUTIL_OFPST_DESC_REPLY: | |
4346 | case OFPUTIL_OFPST_FLOW_REPLY: | |
4347 | case OFPUTIL_OFPST_QUEUE_REPLY: | |
4348 | case OFPUTIL_OFPST_PORT_REPLY: | |
4349 | case OFPUTIL_OFPST_TABLE_REPLY: | |
4350 | case OFPUTIL_OFPST_AGGREGATE_REPLY: | |
4351 | case OFPUTIL_NXT_STATUS_REPLY: | |
4352 | case OFPUTIL_NXT_ROLE_REPLY: | |
4353 | case OFPUTIL_NXT_FLOW_REMOVED: | |
4354 | case OFPUTIL_NXST_FLOW_REPLY: | |
4355 | case OFPUTIL_NXST_AGGREGATE_REPLY: | |
064af421 BP |
4356 | default: |
4357 | if (VLOG_IS_WARN_ENABLED()) { | |
4358 | char *s = ofp_to_string(oh, ntohs(oh->length), 2); | |
4359 | VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s); | |
4360 | free(s); | |
4361 | } | |
d1e2cf21 BP |
4362 | if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) { |
4363 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT); | |
4364 | } else { | |
4365 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE); | |
4366 | } | |
064af421 | 4367 | } |
d1e2cf21 | 4368 | } |
064af421 | 4369 | |
d1e2cf21 BP |
4370 | static void |
4371 | handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg) | |
4372 | { | |
4373 | int error = handle_openflow__(ofconn, ofp_msg); | |
064af421 BP |
4374 | if (error) { |
4375 | send_error_oh(ofconn, ofp_msg->data, error); | |
4376 | } | |
d1e2cf21 | 4377 | COVERAGE_INC(ofproto_recv_openflow); |
064af421 BP |
4378 | } |
4379 | \f | |
4380 | static void | |
856081f6 | 4381 | handle_miss_upcall(struct ofproto *p, struct dpif_upcall *upcall) |
064af421 | 4382 | { |
bcf84111 | 4383 | struct facet *facet; |
ae412e7d | 4384 | struct flow flow; |
064af421 | 4385 | |
856081f6 BP |
4386 | /* Obtain in_port and tun_id, at least. */ |
4387 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
064af421 | 4388 | |
856081f6 BP |
4389 | /* Set header pointers in 'flow'. */ |
4390 | flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow); | |
4617e2c1 | 4391 | |
ebe482fd EJ |
4392 | if (p->ofhooks->special_cb |
4393 | && !p->ofhooks->special_cb(&flow, upcall->packet, p->aux)) { | |
4394 | ofpbuf_delete(upcall->packet); | |
4395 | return; | |
4396 | } | |
4397 | ||
0ad9b732 JP |
4398 | /* Check with in-band control to see if this packet should be sent |
4399 | * to the local port regardless of the flow table. */ | |
856081f6 | 4400 | if (in_band_msg_in_hook(p->in_band, &flow, upcall->packet)) { |
cdee00fd | 4401 | struct ofpbuf odp_actions; |
0ad9b732 | 4402 | |
cdee00fd | 4403 | ofpbuf_init(&odp_actions, 32); |
7aec165d | 4404 | nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, ODPP_LOCAL); |
856081f6 BP |
4405 | dpif_execute(p->dpif, odp_actions.data, odp_actions.size, |
4406 | upcall->packet); | |
cdee00fd | 4407 | ofpbuf_uninit(&odp_actions); |
0ad9b732 JP |
4408 | } |
4409 | ||
bcf84111 BP |
4410 | facet = facet_lookup_valid(p, &flow); |
4411 | if (!facet) { | |
4412 | struct rule *rule = rule_lookup(p, &flow); | |
4413 | if (!rule) { | |
4414 | /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */ | |
856081f6 | 4415 | struct ofport *port = get_port(p, flow.in_port); |
bcf84111 BP |
4416 | if (port) { |
4417 | if (port->opp.config & OFPPC_NO_PACKET_IN) { | |
4418 | COVERAGE_INC(ofproto_no_packet_in); | |
4419 | /* XXX install 'drop' flow entry */ | |
856081f6 | 4420 | ofpbuf_delete(upcall->packet); |
bcf84111 BP |
4421 | return; |
4422 | } | |
4423 | } else { | |
4424 | VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, | |
856081f6 | 4425 | flow.in_port); |
064af421 | 4426 | } |
064af421 | 4427 | |
bcf84111 | 4428 | COVERAGE_INC(ofproto_packet_in); |
856081f6 | 4429 | send_packet_in(p, upcall, &flow, false); |
bcf84111 | 4430 | return; |
064af421 | 4431 | } |
bcf84111 | 4432 | |
856081f6 | 4433 | facet = facet_create(p, rule, &flow, upcall->packet); |
bcf84111 BP |
4434 | } else if (!facet->may_install) { |
4435 | /* The facet is not installable, that is, we need to process every | |
4436 | * packet, so process the current packet's actions into 'facet'. */ | |
856081f6 | 4437 | facet_make_actions(p, facet, upcall->packet); |
064af421 BP |
4438 | } |
4439 | ||
bcf84111 | 4440 | if (facet->rule->cr.priority == FAIL_OPEN_PRIORITY) { |
7778bd15 BP |
4441 | /* |
4442 | * Extra-special case for fail-open mode. | |
4443 | * | |
4444 | * We are in fail-open mode and the packet matched the fail-open rule, | |
4445 | * but we are connected to a controller too. We should send the packet | |
4446 | * up to the controller in the hope that it will try to set up a flow | |
4447 | * and thereby allow us to exit fail-open. | |
4448 | * | |
4449 | * See the top-level comment in fail-open.c for more information. | |
4450 | */ | |
856081f6 | 4451 | send_packet_in(p, upcall, &flow, true); |
7778bd15 | 4452 | } |
750638bb | 4453 | |
856081f6 | 4454 | facet_execute(p, facet, upcall->packet); |
bcf84111 | 4455 | facet_install(p, facet, false); |
064af421 | 4456 | } |
72b06300 BP |
4457 | |
4458 | static void | |
856081f6 | 4459 | handle_upcall(struct ofproto *p, struct dpif_upcall *upcall) |
72b06300 | 4460 | { |
856081f6 | 4461 | struct flow flow; |
72b06300 | 4462 | |
856081f6 | 4463 | switch (upcall->type) { |
82272ede | 4464 | case DPIF_UC_ACTION: |
72b06300 | 4465 | COVERAGE_INC(ofproto_ctlr_action); |
856081f6 BP |
4466 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); |
4467 | send_packet_in(p, upcall, &flow, false); | |
72b06300 BP |
4468 | break; |
4469 | ||
82272ede | 4470 | case DPIF_UC_SAMPLE: |
72b06300 | 4471 | if (p->sflow) { |
856081f6 BP |
4472 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); |
4473 | ofproto_sflow_received(p->sflow, upcall, &flow); | |
72b06300 | 4474 | } |
856081f6 | 4475 | ofpbuf_delete(upcall->packet); |
72b06300 BP |
4476 | break; |
4477 | ||
82272ede | 4478 | case DPIF_UC_MISS: |
856081f6 | 4479 | handle_miss_upcall(p, upcall); |
72b06300 BP |
4480 | break; |
4481 | ||
982b8810 | 4482 | case DPIF_N_UC_TYPES: |
72b06300 | 4483 | default: |
856081f6 | 4484 | VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type); |
72b06300 BP |
4485 | break; |
4486 | } | |
4487 | } | |
064af421 | 4488 | \f |
4a4cdb3b BP |
4489 | /* Flow expiration. */ |
4490 | ||
0de7a4b4 | 4491 | static int ofproto_dp_max_idle(const struct ofproto *); |
4a4cdb3b | 4492 | static void ofproto_update_used(struct ofproto *); |
5ecc9d81 | 4493 | static void rule_expire(struct ofproto *, struct rule *); |
bcf84111 | 4494 | static void ofproto_expire_facets(struct ofproto *, int dp_max_idle); |
4a4cdb3b BP |
4495 | |
4496 | /* This function is called periodically by ofproto_run(). Its job is to | |
4497 | * collect updates for the flows that have been installed into the datapath, | |
4498 | * most importantly when they last were used, and then use that information to | |
0de7a4b4 BP |
4499 | * expire flows that have not been used recently. |
4500 | * | |
4501 | * Returns the number of milliseconds after which it should be called again. */ | |
4502 | static int | |
4a4cdb3b BP |
4503 | ofproto_expire(struct ofproto *ofproto) |
4504 | { | |
5ecc9d81 BP |
4505 | struct rule *rule, *next_rule; |
4506 | struct cls_cursor cursor; | |
4507 | int dp_max_idle; | |
4a4cdb3b BP |
4508 | |
4509 | /* Update 'used' for each flow in the datapath. */ | |
4510 | ofproto_update_used(ofproto); | |
4511 | ||
bcf84111 | 4512 | /* Expire facets that have been idle too long. */ |
5ecc9d81 BP |
4513 | dp_max_idle = ofproto_dp_max_idle(ofproto); |
4514 | ofproto_expire_facets(ofproto, dp_max_idle); | |
bcf84111 BP |
4515 | |
4516 | /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */ | |
5ecc9d81 BP |
4517 | cls_cursor_init(&cursor, &ofproto->cls, NULL); |
4518 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) { | |
4519 | rule_expire(ofproto, rule); | |
4520 | } | |
4a4cdb3b BP |
4521 | |
4522 | /* Let the hook know that we're at a stable point: all outstanding data | |
4523 | * in existing flows has been accounted to the account_cb. Thus, the | |
4524 | * hook can now reasonably do operations that depend on having accurate | |
4525 | * flow volume accounting (currently, that's just bond rebalancing). */ | |
4526 | if (ofproto->ofhooks->account_checkpoint_cb) { | |
4527 | ofproto->ofhooks->account_checkpoint_cb(ofproto->aux); | |
4528 | } | |
0de7a4b4 | 4529 | |
5ecc9d81 | 4530 | return MIN(dp_max_idle, 1000); |
4a4cdb3b BP |
4531 | } |
4532 | ||
bcf84111 | 4533 | /* Update 'used' member of installed facets. */ |
4a4cdb3b BP |
4534 | static void |
4535 | ofproto_update_used(struct ofproto *p) | |
4536 | { | |
c97fb132 | 4537 | const struct dpif_flow_stats *stats; |
704a1e09 | 4538 | struct dpif_flow_dump dump; |
feebdea2 BP |
4539 | const struct nlattr *key; |
4540 | size_t key_len; | |
4a4cdb3b | 4541 | |
704a1e09 | 4542 | dpif_flow_dump_start(&dump, p->dpif); |
feebdea2 | 4543 | while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) { |
bcf84111 | 4544 | struct facet *facet; |
ae412e7d | 4545 | struct flow flow; |
14608a15 | 4546 | |
feebdea2 | 4547 | if (odp_flow_key_to_flow(key, key_len, &flow)) { |
36956a7d BP |
4548 | struct ds s; |
4549 | ||
4550 | ds_init(&s); | |
feebdea2 | 4551 | odp_flow_key_format(key, key_len, &s); |
36956a7d BP |
4552 | VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s", |
4553 | ds_cstr(&s)); | |
4554 | ds_destroy(&s); | |
4555 | ||
4556 | continue; | |
4557 | } | |
bcf84111 | 4558 | facet = facet_find(p, &flow); |
4a4cdb3b | 4559 | |
bcf84111 | 4560 | if (facet && facet->installed) { |
feebdea2 BP |
4561 | facet_update_time(p, facet, stats); |
4562 | facet_account(p, facet, stats->n_bytes); | |
4a4cdb3b BP |
4563 | } else { |
4564 | /* There's a flow in the datapath that we know nothing about. | |
4565 | * Delete it. */ | |
4566 | COVERAGE_INC(ofproto_unexpected_rule); | |
feebdea2 | 4567 | dpif_flow_del(p->dpif, key, key_len, NULL); |
4a4cdb3b | 4568 | } |
4a4cdb3b | 4569 | } |
704a1e09 | 4570 | dpif_flow_dump_done(&dump); |
4a4cdb3b BP |
4571 | } |
4572 | ||
0de7a4b4 | 4573 | /* Calculates and returns the number of milliseconds of idle time after which |
bcf84111 | 4574 | * facets should expire from the datapath and we should fold their statistics |
0de7a4b4 BP |
4575 | * into their parent rules in userspace. */ |
4576 | static int | |
4577 | ofproto_dp_max_idle(const struct ofproto *ofproto) | |
4578 | { | |
4579 | /* | |
4580 | * Idle time histogram. | |
4581 | * | |
bcf84111 | 4582 | * Most of the time a switch has a relatively small number of facets. When |
0de7a4b4 BP |
4583 | * this is the case we might as well keep statistics for all of them in |
4584 | * userspace and to cache them in the kernel datapath for performance as | |
4585 | * well. | |
4586 | * | |
bcf84111 | 4587 | * As the number of facets increases, the memory required to maintain |
0de7a4b4 | 4588 | * statistics about them in userspace and in the kernel becomes |
bcf84111 | 4589 | * significant. However, with a large number of facets it is likely that |
0de7a4b4 BP |
4590 | * only a few of them are "heavy hitters" that consume a large amount of |
4591 | * bandwidth. At this point, only heavy hitters are worth caching in the | |
bcf84111 | 4592 | * kernel and maintaining in userspaces; other facets we can discard. |
0de7a4b4 BP |
4593 | * |
4594 | * The technique used to compute the idle time is to build a histogram with | |
bcf84111 BP |
4595 | * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet |
4596 | * that is installed in the kernel gets dropped in the appropriate bucket. | |
0de7a4b4 | 4597 | * After the histogram has been built, we compute the cutoff so that only |
bcf84111 BP |
4598 | * the most-recently-used 1% of facets (but at least 1000 flows) are kept |
4599 | * cached. At least the most-recently-used bucket of facets is kept, so | |
4600 | * actually an arbitrary number of facets can be kept in any given | |
0de7a4b4 BP |
4601 | * expiration run (though the next run will delete most of those unless |
4602 | * they receive additional data). | |
4603 | * | |
bcf84111 BP |
4604 | * This requires a second pass through the facets, in addition to the pass |
4605 | * made by ofproto_update_used(), because the former function never looks | |
4606 | * at uninstallable facets. | |
0de7a4b4 BP |
4607 | */ |
4608 | enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) }; | |
4609 | enum { N_BUCKETS = 5000 / BUCKET_WIDTH }; | |
4610 | int buckets[N_BUCKETS] = { 0 }; | |
bcf84111 | 4611 | struct facet *facet; |
0de7a4b4 | 4612 | int total, bucket; |
0de7a4b4 BP |
4613 | long long int now; |
4614 | int i; | |
4615 | ||
bcf84111 | 4616 | total = hmap_count(&ofproto->facets); |
0de7a4b4 BP |
4617 | if (total <= 1000) { |
4618 | return N_BUCKETS * BUCKET_WIDTH; | |
4619 | } | |
4620 | ||
4621 | /* Build histogram. */ | |
4622 | now = time_msec(); | |
bcf84111 BP |
4623 | HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) { |
4624 | long long int idle = now - facet->used; | |
0de7a4b4 BP |
4625 | int bucket = (idle <= 0 ? 0 |
4626 | : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1 | |
4627 | : (unsigned int) idle / BUCKET_WIDTH); | |
4628 | buckets[bucket]++; | |
4629 | } | |
4630 | ||
4631 | /* Find the first bucket whose flows should be expired. */ | |
4632 | for (bucket = 0; bucket < N_BUCKETS; bucket++) { | |
4633 | if (buckets[bucket]) { | |
4634 | int subtotal = 0; | |
4635 | do { | |
4636 | subtotal += buckets[bucket++]; | |
4637 | } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100)); | |
4638 | break; | |
4639 | } | |
4640 | } | |
4641 | ||
4642 | if (VLOG_IS_DBG_ENABLED()) { | |
4643 | struct ds s; | |
4644 | ||
4645 | ds_init(&s); | |
4646 | ds_put_cstr(&s, "keep"); | |
4647 | for (i = 0; i < N_BUCKETS; i++) { | |
4648 | if (i == bucket) { | |
4649 | ds_put_cstr(&s, ", drop"); | |
4650 | } | |
4651 | if (buckets[i]) { | |
4652 | ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]); | |
4653 | } | |
4654 | } | |
4655 | VLOG_INFO("%s: %s (msec:count)", | |
4656 | dpif_name(ofproto->dpif), ds_cstr(&s)); | |
4657 | ds_destroy(&s); | |
4658 | } | |
4659 | ||
4660 | return bucket * BUCKET_WIDTH; | |
4661 | } | |
4662 | ||
4a4cdb3b | 4663 | static void |
bcf84111 | 4664 | facet_active_timeout(struct ofproto *ofproto, struct facet *facet) |
4a4cdb3b | 4665 | { |
bcf84111 BP |
4666 | if (ofproto->netflow && !facet_is_controller_flow(facet) && |
4667 | netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) { | |
4a4cdb3b | 4668 | struct ofexpired expired; |
feebdea2 | 4669 | |
bcf84111 | 4670 | if (facet->installed) { |
c97fb132 | 4671 | struct dpif_flow_stats stats; |
c33087b8 BP |
4672 | |
4673 | facet_put__(ofproto, facet, facet->actions, facet->actions_len, | |
4674 | &stats); | |
4675 | facet_update_stats(ofproto, facet, &stats); | |
4a4cdb3b BP |
4676 | } |
4677 | ||
c33087b8 BP |
4678 | expired.flow = facet->flow; |
4679 | expired.packet_count = facet->packet_count; | |
4680 | expired.byte_count = facet->byte_count; | |
4681 | expired.used = facet->used; | |
bcf84111 BP |
4682 | netflow_expire(ofproto->netflow, &facet->nf_flow, &expired); |
4683 | } | |
4684 | } | |
4a4cdb3b | 4685 | |
bcf84111 BP |
4686 | static void |
4687 | ofproto_expire_facets(struct ofproto *ofproto, int dp_max_idle) | |
4688 | { | |
4689 | long long int cutoff = time_msec() - dp_max_idle; | |
4690 | struct facet *facet, *next_facet; | |
4691 | ||
4692 | HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) { | |
4693 | facet_active_timeout(ofproto, facet); | |
4694 | if (facet->used < cutoff) { | |
4695 | facet_remove(ofproto, facet); | |
4696 | } | |
4a4cdb3b BP |
4697 | } |
4698 | } | |
4699 | ||
5ecc9d81 BP |
4700 | /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules, |
4701 | * then delete it entirely. */ | |
4a4cdb3b | 4702 | static void |
5ecc9d81 | 4703 | rule_expire(struct ofproto *ofproto, struct rule *rule) |
4a4cdb3b | 4704 | { |
bcf84111 BP |
4705 | struct facet *facet, *next_facet; |
4706 | long long int now; | |
4707 | uint8_t reason; | |
4a4cdb3b | 4708 | |
bcf84111 | 4709 | /* Has 'rule' expired? */ |
4a4cdb3b | 4710 | now = time_msec(); |
bcf84111 BP |
4711 | if (rule->hard_timeout |
4712 | && now > rule->created + rule->hard_timeout * 1000) { | |
4713 | reason = OFPRR_HARD_TIMEOUT; | |
4714 | } else if (rule->idle_timeout && list_is_empty(&rule->facets) | |
4715 | && now >rule->used + rule->idle_timeout * 1000) { | |
4716 | reason = OFPRR_IDLE_TIMEOUT; | |
4a4cdb3b | 4717 | } else { |
bcf84111 | 4718 | return; |
4a4cdb3b | 4719 | } |
064af421 | 4720 | |
bcf84111 | 4721 | COVERAGE_INC(ofproto_expired); |
064af421 | 4722 | |
bcf84111 BP |
4723 | /* Update stats. (This is a no-op if the rule expired due to an idle |
4724 | * timeout, because that only happens when the rule has no facets left.) */ | |
4725 | LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) { | |
5ecc9d81 | 4726 | facet_remove(ofproto, facet); |
064af421 BP |
4727 | } |
4728 | ||
bcf84111 BP |
4729 | /* Get rid of the rule. */ |
4730 | if (!rule_is_hidden(rule)) { | |
5ecc9d81 | 4731 | rule_send_removed(ofproto, rule, reason); |
bcf84111 | 4732 | } |
5ecc9d81 | 4733 | rule_remove(ofproto, rule); |
064af421 | 4734 | } |
bcf84111 | 4735 | \f |
064af421 | 4736 | static struct ofpbuf * |
09246b99 BP |
4737 | compose_ofp_flow_removed(struct ofconn *ofconn, const struct rule *rule, |
4738 | uint8_t reason) | |
064af421 | 4739 | { |
ca069229 | 4740 | struct ofp_flow_removed *ofr; |
064af421 BP |
4741 | struct ofpbuf *buf; |
4742 | ||
0224fbde | 4743 | ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0), &buf); |
ff9d3826 BP |
4744 | ofputil_cls_rule_to_match(&rule->cr, ofconn->flow_format, &ofr->match, |
4745 | rule->flow_cookie, &ofr->cookie); | |
ca069229 JP |
4746 | ofr->priority = htons(rule->cr.priority); |
4747 | ofr->reason = reason; | |
c6ebb8fb | 4748 | calc_flow_duration(rule->created, &ofr->duration_sec, &ofr->duration_nsec); |
ca069229 JP |
4749 | ofr->idle_timeout = htons(rule->idle_timeout); |
4750 | ofr->packet_count = htonll(rule->packet_count); | |
4751 | ofr->byte_count = htonll(rule->byte_count); | |
064af421 BP |
4752 | |
4753 | return buf; | |
4754 | } | |
4755 | ||
09246b99 BP |
4756 | static struct ofpbuf * |
4757 | compose_nx_flow_removed(const struct rule *rule, uint8_t reason) | |
4758 | { | |
4759 | struct nx_flow_removed *nfr; | |
4760 | struct ofpbuf *buf; | |
4761 | int match_len; | |
4762 | ||
0224fbde | 4763 | make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &buf); |
09246b99 | 4764 | match_len = nx_put_match(buf, &rule->cr); |
0224fbde | 4765 | |
4d0ed519 | 4766 | nfr = buf->data; |
09246b99 BP |
4767 | nfr->cookie = rule->flow_cookie; |
4768 | nfr->priority = htons(rule->cr.priority); | |
4769 | nfr->reason = reason; | |
4770 | calc_flow_duration(rule->created, &nfr->duration_sec, &nfr->duration_nsec); | |
4771 | nfr->idle_timeout = htons(rule->idle_timeout); | |
4772 | nfr->match_len = htons(match_len); | |
4773 | nfr->packet_count = htonll(rule->packet_count); | |
4774 | nfr->byte_count = htonll(rule->byte_count); | |
4775 | ||
4776 | return buf; | |
4777 | } | |
4778 | ||
ca069229 | 4779 | static void |
bcf84111 | 4780 | rule_send_removed(struct ofproto *p, struct rule *rule, uint8_t reason) |
064af421 BP |
4781 | { |
4782 | struct ofconn *ofconn; | |
064af421 | 4783 | |
3b587616 BP |
4784 | if (!rule->send_flow_removed) { |
4785 | return; | |
4786 | } | |
4787 | ||
4e8e4213 | 4788 | LIST_FOR_EACH (ofconn, node, &p->all_conns) { |
7a0efeb5 BP |
4789 | struct ofpbuf *msg; |
4790 | ||
4791 | if (!rconn_is_connected(ofconn->rconn) | |
4792 | || !ofconn_receives_async_msgs(ofconn)) { | |
4793 | continue; | |
064af421 | 4794 | } |
7a0efeb5 | 4795 | |
09246b99 BP |
4796 | msg = (ofconn->flow_format == NXFF_NXM |
4797 | ? compose_nx_flow_removed(rule, reason) | |
4798 | : compose_ofp_flow_removed(ofconn, rule, reason)); | |
6d6c7259 BP |
4799 | |
4800 | /* Account flow expirations under ofconn->reply_counter, the counter | |
4801 | * for replies to OpenFlow requests. That works because preventing | |
4802 | * OpenFlow requests from being processed also prevents new flows from | |
4803 | * being added (and expiring). (It also prevents processing OpenFlow | |
4804 | * requests that would not add new flows, so it is imperfect.) */ | |
7a0efeb5 | 4805 | queue_tx(msg, ofconn, ofconn->reply_counter); |
064af421 BP |
4806 | } |
4807 | } | |
4808 | ||
856081f6 | 4809 | /* pinsched callback for sending 'ofp_packet_in' on 'ofconn'. */ |
064af421 | 4810 | static void |
856081f6 | 4811 | do_send_packet_in(struct ofpbuf *ofp_packet_in, void *ofconn_) |
064af421 | 4812 | { |
76ce9432 | 4813 | struct ofconn *ofconn = ofconn_; |
43253595 | 4814 | |
856081f6 | 4815 | rconn_send_with_limit(ofconn->rconn, ofp_packet_in, |
43253595 BP |
4816 | ofconn->packet_in_counter, 100); |
4817 | } | |
4818 | ||
856081f6 BP |
4819 | /* Takes 'upcall', whose packet has the flow specified by 'flow', composes an |
4820 | * OpenFlow packet-in message from it, and passes it to 'ofconn''s packet | |
4821 | * scheduler for sending. | |
43253595 | 4822 | * |
856081f6 BP |
4823 | * If 'clone' is true, the caller retains ownership of 'upcall->packet'. |
4824 | * Otherwise, ownership is transferred to this function. */ | |
43253595 | 4825 | static void |
856081f6 BP |
4826 | schedule_packet_in(struct ofconn *ofconn, struct dpif_upcall *upcall, |
4827 | const struct flow *flow, bool clone) | |
43253595 | 4828 | { |
856081f6 | 4829 | enum { OPI_SIZE = offsetof(struct ofp_packet_in, data) }; |
76ce9432 | 4830 | struct ofproto *ofproto = ofconn->ofproto; |
856081f6 BP |
4831 | struct ofp_packet_in *opi; |
4832 | int total_len, send_len; | |
4833 | struct ofpbuf *packet; | |
76ce9432 | 4834 | uint32_t buffer_id; |
82272ede | 4835 | int idx; |
064af421 | 4836 | |
856081f6 | 4837 | /* Get OpenFlow buffer_id. */ |
82272ede | 4838 | if (upcall->type == DPIF_UC_ACTION) { |
76ce9432 | 4839 | buffer_id = UINT32_MAX; |
43253595 BP |
4840 | } else if (ofproto->fail_open && fail_open_is_active(ofproto->fail_open)) { |
4841 | buffer_id = pktbuf_get_null(); | |
89b9612d BP |
4842 | } else if (!ofconn->pktbuf) { |
4843 | buffer_id = UINT32_MAX; | |
76ce9432 | 4844 | } else { |
856081f6 | 4845 | buffer_id = pktbuf_save(ofconn->pktbuf, upcall->packet, flow->in_port); |
76ce9432 | 4846 | } |
372179d4 | 4847 | |
43253595 | 4848 | /* Figure out how much of the packet to send. */ |
856081f6 | 4849 | total_len = send_len = upcall->packet->size; |
43253595 BP |
4850 | if (buffer_id != UINT32_MAX) { |
4851 | send_len = MIN(send_len, ofconn->miss_send_len); | |
4852 | } | |
82272ede | 4853 | if (upcall->type == DPIF_UC_ACTION) { |
856081f6 BP |
4854 | send_len = MIN(send_len, upcall->userdata); |
4855 | } | |
064af421 | 4856 | |
856081f6 | 4857 | /* Copy or steal buffer for OFPT_PACKET_IN. */ |
43253595 | 4858 | if (clone) { |
856081f6 BP |
4859 | packet = ofpbuf_clone_data_with_headroom(upcall->packet->data, |
4860 | send_len, OPI_SIZE); | |
43253595 | 4861 | } else { |
856081f6 BP |
4862 | packet = upcall->packet; |
4863 | packet->size = send_len; | |
43253595 BP |
4864 | } |
4865 | ||
856081f6 BP |
4866 | /* Add OFPT_PACKET_IN. */ |
4867 | opi = ofpbuf_push_zeros(packet, OPI_SIZE); | |
4868 | opi->header.version = OFP_VERSION; | |
4869 | opi->header.type = OFPT_PACKET_IN; | |
4870 | opi->total_len = htons(total_len); | |
4871 | opi->in_port = htons(odp_port_to_ofp_port(flow->in_port)); | |
82272ede | 4872 | opi->reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION; |
43253595 BP |
4873 | opi->buffer_id = htonl(buffer_id); |
4874 | update_openflow_length(packet); | |
4875 | ||
4876 | /* Hand over to packet scheduler. It might immediately call into | |
4877 | * do_send_packet_in() or it might buffer it for a while (until a later | |
4878 | * call to pinsched_run()). */ | |
82272ede BP |
4879 | idx = upcall->type == DPIF_UC_MISS ? 0 : 1; |
4880 | pinsched_send(ofconn->schedulers[idx], flow->in_port, | |
43253595 | 4881 | packet, do_send_packet_in, ofconn); |
064af421 BP |
4882 | } |
4883 | ||
82272ede | 4884 | /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an |
856081f6 BP |
4885 | * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to |
4886 | * their individual configurations. | |
43253595 BP |
4887 | * |
4888 | * Takes ownership of 'packet'. */ | |
4889 | static void | |
856081f6 BP |
4890 | send_packet_in(struct ofproto *ofproto, struct dpif_upcall *upcall, |
4891 | const struct flow *flow, bool clone) | |
43253595 | 4892 | { |
76ce9432 | 4893 | struct ofconn *ofconn, *prev; |
76ce9432 BP |
4894 | |
4895 | prev = NULL; | |
4e8e4213 | 4896 | LIST_FOR_EACH (ofconn, node, &ofproto->all_conns) { |
c91248b3 | 4897 | if (ofconn_receives_async_msgs(ofconn)) { |
9deba63b | 4898 | if (prev) { |
856081f6 | 4899 | schedule_packet_in(prev, upcall, flow, true); |
9deba63b BP |
4900 | } |
4901 | prev = ofconn; | |
064af421 | 4902 | } |
76ce9432 BP |
4903 | } |
4904 | if (prev) { | |
856081f6 BP |
4905 | schedule_packet_in(prev, upcall, flow, clone); |
4906 | } else if (!clone) { | |
4907 | ofpbuf_delete(upcall->packet); | |
064af421 | 4908 | } |
064af421 BP |
4909 | } |
4910 | ||
4911 | static uint64_t | |
fa60c019 | 4912 | pick_datapath_id(const struct ofproto *ofproto) |
064af421 | 4913 | { |
fa60c019 | 4914 | const struct ofport *port; |
064af421 | 4915 | |
ca0f572c | 4916 | port = get_port(ofproto, ODPP_LOCAL); |
fa60c019 BP |
4917 | if (port) { |
4918 | uint8_t ea[ETH_ADDR_LEN]; | |
4919 | int error; | |
4920 | ||
4921 | error = netdev_get_etheraddr(port->netdev, ea); | |
064af421 BP |
4922 | if (!error) { |
4923 | return eth_addr_to_uint64(ea); | |
4924 | } | |
4925 | VLOG_WARN("could not get MAC address for %s (%s)", | |
fa60c019 | 4926 | netdev_get_name(port->netdev), strerror(error)); |
064af421 | 4927 | } |
fa60c019 | 4928 | return ofproto->fallback_dpid; |
064af421 BP |
4929 | } |
4930 | ||
4931 | static uint64_t | |
4932 | pick_fallback_dpid(void) | |
4933 | { | |
4934 | uint8_t ea[ETH_ADDR_LEN]; | |
70150daf | 4935 | eth_addr_nicira_random(ea); |
064af421 BP |
4936 | return eth_addr_to_uint64(ea); |
4937 | } | |
4938 | \f | |
7aa697dd BP |
4939 | static void |
4940 | ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED, | |
4941 | void *aux OVS_UNUSED) | |
4942 | { | |
4943 | const struct shash_node *node; | |
4944 | struct ds results; | |
4945 | ||
4946 | ds_init(&results); | |
4947 | SHASH_FOR_EACH (node, &all_ofprotos) { | |
4948 | ds_put_format(&results, "%s\n", node->name); | |
4949 | } | |
4950 | unixctl_command_reply(conn, 200, ds_cstr(&results)); | |
4951 | ds_destroy(&results); | |
4952 | } | |
4953 | ||
4954 | struct ofproto_trace { | |
4955 | struct action_xlate_ctx ctx; | |
4956 | struct flow flow; | |
4957 | struct ds *result; | |
4958 | }; | |
4959 | ||
4960 | static void | |
4961 | trace_format_rule(struct ds *result, int level, const struct rule *rule) | |
4962 | { | |
4963 | ds_put_char_multiple(result, '\t', level); | |
4964 | if (!rule) { | |
4965 | ds_put_cstr(result, "No match\n"); | |
4966 | return; | |
4967 | } | |
4968 | ||
4969 | ds_put_format(result, "Rule: cookie=%#"PRIx64" ", | |
4970 | ntohll(rule->flow_cookie)); | |
4971 | cls_rule_format(&rule->cr, result); | |
4972 | ds_put_char(result, '\n'); | |
4973 | ||
4974 | ds_put_char_multiple(result, '\t', level); | |
4975 | ds_put_cstr(result, "OpenFlow "); | |
4976 | ofp_print_actions(result, (const struct ofp_action_header *) rule->actions, | |
4977 | rule->n_actions * sizeof *rule->actions); | |
4978 | ds_put_char(result, '\n'); | |
4979 | } | |
4980 | ||
4981 | static void | |
4982 | trace_format_flow(struct ds *result, int level, const char *title, | |
4983 | struct ofproto_trace *trace) | |
4984 | { | |
4985 | ds_put_char_multiple(result, '\t', level); | |
4986 | ds_put_format(result, "%s: ", title); | |
4987 | if (flow_equal(&trace->ctx.flow, &trace->flow)) { | |
4988 | ds_put_cstr(result, "unchanged"); | |
4989 | } else { | |
4990 | flow_format(result, &trace->ctx.flow); | |
4991 | trace->flow = trace->ctx.flow; | |
4992 | } | |
4993 | ds_put_char(result, '\n'); | |
4994 | } | |
4995 | ||
4996 | static void | |
4997 | trace_resubmit(struct action_xlate_ctx *ctx, const struct rule *rule) | |
4998 | { | |
4999 | struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx); | |
5000 | struct ds *result = trace->result; | |
5001 | ||
5002 | ds_put_char(result, '\n'); | |
5003 | trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace); | |
5004 | trace_format_rule(result, ctx->recurse + 1, rule); | |
5005 | } | |
5006 | ||
5007 | static void | |
5008 | ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_, | |
5009 | void *aux OVS_UNUSED) | |
5010 | { | |
5011 | char *dpname, *in_port_s, *tun_id_s, *packet_s; | |
5012 | char *args = xstrdup(args_); | |
5013 | char *save_ptr = NULL; | |
5014 | struct ofproto *ofproto; | |
5015 | struct ofpbuf packet; | |
5016 | struct rule *rule; | |
5017 | struct ds result; | |
5018 | struct flow flow; | |
5019 | uint16_t in_port; | |
11e6a15b | 5020 | ovs_be64 tun_id; |
7aa697dd BP |
5021 | char *s; |
5022 | ||
5023 | ofpbuf_init(&packet, strlen(args) / 2); | |
5024 | ds_init(&result); | |
5025 | ||
5026 | dpname = strtok_r(args, " ", &save_ptr); | |
5027 | tun_id_s = strtok_r(NULL, " ", &save_ptr); | |
5028 | in_port_s = strtok_r(NULL, " ", &save_ptr); | |
5029 | packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */ | |
5030 | if (!dpname || !in_port_s || !packet_s) { | |
5031 | unixctl_command_reply(conn, 501, "Bad command syntax"); | |
5032 | goto exit; | |
5033 | } | |
5034 | ||
5035 | ofproto = shash_find_data(&all_ofprotos, dpname); | |
5036 | if (!ofproto) { | |
5037 | unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list " | |
5038 | "for help)"); | |
5039 | goto exit; | |
5040 | } | |
5041 | ||
11e6a15b | 5042 | tun_id = htonll(strtoull(tun_id_s, NULL, 10)); |
7aa697dd BP |
5043 | in_port = ofp_port_to_odp_port(atoi(in_port_s)); |
5044 | ||
5045 | packet_s = ofpbuf_put_hex(&packet, packet_s, NULL); | |
5046 | packet_s += strspn(packet_s, " "); | |
5047 | if (*packet_s != '\0') { | |
5048 | unixctl_command_reply(conn, 501, "Trailing garbage in command"); | |
5049 | goto exit; | |
5050 | } | |
5051 | if (packet.size < ETH_HEADER_LEN) { | |
5052 | unixctl_command_reply(conn, 501, "Packet data too short for Ethernet"); | |
5053 | goto exit; | |
5054 | } | |
5055 | ||
5056 | ds_put_cstr(&result, "Packet: "); | |
5057 | s = ofp_packet_to_string(packet.data, packet.size, packet.size); | |
5058 | ds_put_cstr(&result, s); | |
5059 | free(s); | |
5060 | ||
5061 | flow_extract(&packet, tun_id, in_port, &flow); | |
5062 | ds_put_cstr(&result, "Flow: "); | |
5063 | flow_format(&result, &flow); | |
5064 | ds_put_char(&result, '\n'); | |
5065 | ||
5066 | rule = rule_lookup(ofproto, &flow); | |
5067 | trace_format_rule(&result, 0, rule); | |
5068 | if (rule) { | |
5069 | struct ofproto_trace trace; | |
cdee00fd | 5070 | struct ofpbuf *odp_actions; |
7aa697dd BP |
5071 | |
5072 | trace.result = &result; | |
5073 | trace.flow = flow; | |
5074 | action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet); | |
5075 | trace.ctx.resubmit_hook = trace_resubmit; | |
cdee00fd BP |
5076 | odp_actions = xlate_actions(&trace.ctx, |
5077 | rule->actions, rule->n_actions); | |
7aa697dd BP |
5078 | |
5079 | ds_put_char(&result, '\n'); | |
5080 | trace_format_flow(&result, 0, "Final flow", &trace); | |
5081 | ds_put_cstr(&result, "Datapath actions: "); | |
cdee00fd BP |
5082 | format_odp_actions(&result, odp_actions->data, odp_actions->size); |
5083 | ofpbuf_delete(odp_actions); | |
7aa697dd BP |
5084 | } |
5085 | ||
5086 | unixctl_command_reply(conn, 200, ds_cstr(&result)); | |
5087 | ||
5088 | exit: | |
5089 | ds_destroy(&result); | |
5090 | ofpbuf_uninit(&packet); | |
5091 | free(args); | |
5092 | } | |
5093 | ||
5094 | static void | |
5095 | ofproto_unixctl_init(void) | |
5096 | { | |
5097 | static bool registered; | |
5098 | if (registered) { | |
5099 | return; | |
5100 | } | |
5101 | registered = true; | |
5102 | ||
5103 | unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL); | |
5104 | unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL); | |
5105 | } | |
5106 | \f | |
064af421 | 5107 | static bool |
ae412e7d | 5108 | default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet, |
cdee00fd | 5109 | struct ofpbuf *odp_actions, tag_type *tags, |
6a07af36 | 5110 | uint16_t *nf_output_iface, void *ofproto_) |
064af421 BP |
5111 | { |
5112 | struct ofproto *ofproto = ofproto_; | |
5113 | int out_port; | |
5114 | ||
5115 | /* Drop frames for reserved multicast addresses. */ | |
5116 | if (eth_addr_is_reserved(flow->dl_dst)) { | |
5117 | return true; | |
5118 | } | |
5119 | ||
5120 | /* Learn source MAC (but don't try to learn from revalidation). */ | |
5121 | if (packet != NULL) { | |
5122 | tag_type rev_tag = mac_learning_learn(ofproto->ml, flow->dl_src, | |
7febb910 JG |
5123 | 0, flow->in_port, |
5124 | GRAT_ARP_LOCK_NONE); | |
064af421 BP |
5125 | if (rev_tag) { |
5126 | /* The log messages here could actually be useful in debugging, | |
5127 | * so keep the rate limit relatively high. */ | |
5128 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300); | |
5129 | VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16, | |
5130 | ETH_ADDR_ARGS(flow->dl_src), flow->in_port); | |
5131 | ofproto_revalidate(ofproto, rev_tag); | |
5132 | } | |
5133 | } | |
5134 | ||
5135 | /* Determine output port. */ | |
7febb910 JG |
5136 | out_port = mac_learning_lookup_tag(ofproto->ml, flow->dl_dst, 0, tags, |
5137 | NULL); | |
064af421 | 5138 | if (out_port < 0) { |
f1588b1f | 5139 | flood_packets(ofproto, flow->in_port, OFPPC_NO_FLOOD, |
cdee00fd | 5140 | nf_output_iface, odp_actions); |
064af421 | 5141 | } else if (out_port != flow->in_port) { |
7aec165d | 5142 | nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, out_port); |
6a07af36 | 5143 | *nf_output_iface = out_port; |
064af421 BP |
5144 | } else { |
5145 | /* Drop. */ | |
5146 | } | |
5147 | ||
5148 | return true; | |
5149 | } | |
5150 | ||
5151 | static const struct ofhooks default_ofhooks = { | |
064af421 BP |
5152 | default_normal_ofhook_cb, |
5153 | NULL, | |
ebe482fd | 5154 | NULL, |
064af421 BP |
5155 | NULL |
5156 | }; |