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