]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif-trace.c
ofproto-dpif-xlate: Fix load balancing for select groups with MPLS.
[mirror_ovs.git] / ofproto / ofproto-dpif-trace.c
CommitLineData
d13ee228 1/*
2d9b49dd 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
d13ee228
BP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
18
19#include "ofproto-dpif-trace.h"
20
0f2f05bb 21#include "conntrack.h"
d13ee228
BP
22#include "dpif.h"
23#include "ofproto-dpif-xlate.h"
d13ee228
BP
24#include "unixctl.h"
25
2d9b49dd
BP
26static void oftrace_node_destroy(struct oftrace_node *);
27
28/* Creates a new oftrace_node, populates it with the given 'type' and a copy of
29 * 'text', and appends it to list 'super'. The caller retains ownership of
30 * 'text'. */
31struct oftrace_node *
32oftrace_report(struct ovs_list *super, enum oftrace_node_type type,
33 const char *text)
d13ee228 34{
2d9b49dd
BP
35 struct oftrace_node *node = xmalloc(sizeof *node);
36 ovs_list_push_back(super, &node->node);
37 node->type = type;
38 node->text = xstrdup(text);
39 ovs_list_init(&node->subs);
d13ee228 40
2d9b49dd 41 return node;
d13ee228
BP
42}
43
2d9b49dd
BP
44static bool
45oftrace_node_type_is_terminal(enum oftrace_node_type type)
d13ee228 46{
2d9b49dd
BP
47 switch (type) {
48 case OFT_ACTION:
49 case OFT_DETAIL:
50 case OFT_WARN:
51 case OFT_ERROR:
fac4786a 52 case OFT_BUCKET:
2d9b49dd
BP
53 return true;
54
55 case OFT_BRIDGE:
56 case OFT_TABLE:
57 case OFT_THAW:
58 return false;
d13ee228 59 }
2d9b49dd
BP
60
61 OVS_NOT_REACHED();
d13ee228
BP
62}
63
64static void
2d9b49dd 65oftrace_node_list_destroy(struct ovs_list *nodes)
d13ee228 66{
2d9b49dd
BP
67 if (nodes) {
68 struct oftrace_node *node, *next;
69 LIST_FOR_EACH_SAFE (node, next, node, nodes) {
70 ovs_list_remove(&node->node);
71 oftrace_node_destroy(node);
72 }
d13ee228 73 }
d13ee228
BP
74}
75
76static void
2d9b49dd 77oftrace_node_destroy(struct oftrace_node *node)
d13ee228 78{
2d9b49dd
BP
79 if (node) {
80 oftrace_node_list_destroy(&node->subs);
81 free(node->text);
82 free(node);
83 }
d13ee228
BP
84}
85
e6bc8e74
YHW
86bool
87oftrace_add_recirc_node(struct ovs_list *recirc_queue,
88 enum oftrace_recirc_type type, const struct flow *flow,
5fdd80cc
YHW
89 const struct dp_packet *packet, uint32_t recirc_id,
90 const uint16_t zone)
e6bc8e74
YHW
91{
92 if (!recirc_id_node_find_and_ref(recirc_id)) {
93 return false;
94 }
95
96 struct oftrace_recirc_node *node = xmalloc(sizeof *node);
97 ovs_list_push_back(recirc_queue, &node->node);
98
99 node->type = type;
100 node->recirc_id = recirc_id;
101 node->flow = *flow;
102 node->flow.recirc_id = recirc_id;
5fdd80cc 103 node->flow.ct_zone = zone;
e6bc8e74
YHW
104 node->packet = packet ? dp_packet_clone(packet) : NULL;
105
106 return true;
107}
108
109static void
110oftrace_recirc_node_destroy(struct oftrace_recirc_node *node)
111{
112 if (node) {
113 recirc_free_id(node->recirc_id);
114 dp_packet_delete(node->packet);
115 free(node);
116 }
117}
118
0f2f05bb
YHW
119static void
120oftrace_push_ct_state(struct ovs_list *next_ct_states, uint32_t ct_state)
121{
122 struct oftrace_next_ct_state *next_ct_state =
123 xmalloc(sizeof *next_ct_state);
124 next_ct_state->state = ct_state;
125 ovs_list_push_back(next_ct_states, &next_ct_state->node);
126}
127
128static uint32_t
129oftrace_pop_ct_state(struct ovs_list *next_ct_states)
130{
131 struct oftrace_next_ct_state *s;
132 LIST_FOR_EACH_POP (s, node, next_ct_states) {
36e67140
YHW
133 uint32_t state = s->state;
134 free(s);
135 return state;
0f2f05bb
YHW
136 }
137 OVS_NOT_REACHED();
138}
139
d13ee228 140static void
2d9b49dd
BP
141oftrace_node_print_details(struct ds *output,
142 const struct ovs_list *nodes, int level)
d13ee228 143{
2d9b49dd
BP
144 const struct oftrace_node *sub;
145 LIST_FOR_EACH (sub, node, nodes) {
146 if (sub->type == OFT_BRIDGE) {
147 ds_put_char(output, '\n');
148 }
d13ee228 149
2d9b49dd
BP
150 bool more = (sub->node.next != nodes
151 || oftrace_node_type_is_terminal(sub->type));
152
153 ds_put_char_multiple(output, ' ', (level + more) * 4);
154 switch (sub->type) {
155 case OFT_DETAIL:
156 ds_put_format(output, " -> %s\n", sub->text);
157 break;
158 case OFT_WARN:
159 ds_put_format(output, " >> %s\n", sub->text);
160 break;
161 case OFT_ERROR:
162 ds_put_format(output, " >>>> %s <<<<\n", sub->text);
163 break;
164 case OFT_BRIDGE:
165 ds_put_format(output, "%s\n", sub->text);
166 ds_put_char_multiple(output, ' ', (level + more) * 4);
167 ds_put_char_multiple(output, '-', strlen(sub->text));
168 ds_put_char(output, '\n');
169 break;
170 case OFT_TABLE:
fac4786a 171 case OFT_BUCKET:
2d9b49dd
BP
172 case OFT_THAW:
173 case OFT_ACTION:
174 ds_put_format(output, "%s\n", sub->text);
175 break;
d13ee228 176 }
d13ee228 177
2d9b49dd 178 oftrace_node_print_details(output, &sub->subs, level + more + more);
d13ee228 179 }
d13ee228
BP
180}
181
182/* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
183 * forms are supported:
184 *
b490b189
BP
185 * - [options] [dpname] odp_flow [packet]
186 * - [options] bridge br_flow [packet]
d13ee228
BP
187 *
188 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
189 * returns a nonnull malloced error message. */
190static char * OVS_WARN_UNUSED_RESULT
191parse_flow_and_packet(int argc, const char *argv[],
192 struct ofproto_dpif **ofprotop, struct flow *flow,
0f2f05bb 193 struct dp_packet **packetp,
b490b189
BP
194 struct ovs_list *next_ct_states,
195 bool *consistent)
d13ee228
BP
196{
197 const struct dpif_backer *backer = NULL;
198 const char *error = NULL;
199 char *m_err = NULL;
200 struct simap port_names = SIMAP_INITIALIZER(&port_names);
b490b189 201 struct dp_packet *packet = NULL;
6f068379
BP
202 uint8_t *l7 = NULL;
203 size_t l7_len = 64;
d13ee228
BP
204 struct ofpbuf odp_key;
205 struct ofpbuf odp_mask;
206
207 ofpbuf_init(&odp_key, 0);
208 ofpbuf_init(&odp_mask, 0);
209
b490b189
BP
210 const char *args[3];
211 int n_args = 0;
212 bool generate_packet = false;
213 if (consistent) {
214 *consistent = false;
d13ee228 215 }
b490b189
BP
216 for (int i = 1; i < argc; i++) {
217 const char *arg = argv[i];
218 if (!strcmp(arg, "-generate") || !strcmp(arg, "--generate")) {
219 generate_packet = true;
6f068379
BP
220 } else if (!strcmp(arg, "--l7")) {
221 if (i + 1 >= argc) {
222 m_err = xasprintf("Missing argument for option %s", arg);
223 goto exit;
224 }
225
226 struct dp_packet payload;
227 memset(&payload, 0, sizeof payload);
228 dp_packet_init(&payload, 0);
229 if (dp_packet_put_hex(&payload, argv[++i], NULL)[0] != '\0') {
230 dp_packet_uninit(&payload);
231 error = "Trailing garbage in packet data";
232 goto exit;
233 }
234 free(l7);
235 l7_len = dp_packet_size(&payload);
236 l7 = dp_packet_steal_data(&payload);
237 } else if (!strcmp(arg, "--l7-len")) {
238 if (i + 1 >= argc) {
239 m_err = xasprintf("Missing argument for option %s", arg);
240 goto exit;
241 }
242 free(l7);
243 l7 = NULL;
244 l7_len = atoi(argv[++i]);
245 if (l7_len > 64000) {
246 m_err = xasprintf("%s: too much L7 data", argv[i]);
247 goto exit;
248 }
b490b189
BP
249 } else if (consistent
250 && (!strcmp(arg, "-consistent") ||
251 !strcmp(arg, "--consistent"))) {
252 *consistent = true;
253 } else if (!strcmp(arg, "--ct-next")) {
254 if (i + 1 >= argc) {
255 m_err = xasprintf("Missing argument for option %s", arg);
256 goto exit;
257 }
d13ee228 258
b490b189
BP
259 uint32_t ct_state;
260 struct ds ds = DS_EMPTY_INITIALIZER;
261 if (!parse_ct_state(argv[++i], 0, &ct_state, &ds)
262 || !validate_ct_state(ct_state, &ds)) {
263 m_err = ds_steal_cstr(&ds);
264 goto exit;
265 }
266 oftrace_push_ct_state(next_ct_states, ct_state);
267 } else if (arg[0] == '-') {
268 m_err = xasprintf("%s: unknown option", arg);
269 goto exit;
270 } else if (n_args >= ARRAY_SIZE(args)) {
271 m_err = xstrdup("too many arguments");
0f2f05bb 272 goto exit;
b490b189
BP
273 } else {
274 args[n_args++] = arg;
0f2f05bb 275 }
b490b189 276 }
0f2f05bb 277
b490b189
BP
278 /* 'args' must now have one of the following forms:
279 *
280 * odp_flow
281 * dpname odp_flow
282 * bridge br_flow
283 * odp_flow packet
284 * dpname odp_flow packet
285 * bridge br_flow packet
286 *
287 * Parse the packet if it's there. Note that:
288 *
289 * - If there is one argument, there cannot be a packet.
290 *
291 * - If there are three arguments, there must be a packet.
292 *
293 * If there is a packet, we strip it off.
294 */
295 if (!generate_packet && n_args > 1) {
296 error = eth_from_hex(args[n_args - 1], &packet);
297 if (!error) {
298 n_args--;
299 } else if (n_args > 2) {
300 /* The 3-argument form must end in a hex string. */
0f2f05bb
YHW
301 goto exit;
302 }
b490b189 303 error = NULL;
0f2f05bb
YHW
304 }
305
b490b189
BP
306 /* We stripped off the packet if there was one, so 'args' now has one of
307 * the following forms:
308 *
309 * odp_flow
310 * dpname odp_flow
311 * bridge br_flow
312 *
313 * Before we parse the flow, try to identify the backer, then use that
314 * backer to assemble a collection of port names. The port names are
315 * useful so that the user can specify ports by name instead of number in
316 * the flow. */
317 if (n_args == 2) {
318 /* args[0] might be dpname. */
d13ee228 319 const char *dp_type;
b490b189
BP
320 if (!strncmp(args[0], "ovs-", 4)) {
321 dp_type = args[0] + 4;
d13ee228 322 } else {
b490b189 323 dp_type = args[0];
d13ee228
BP
324 }
325 backer = shash_find_data(&all_dpif_backers, dp_type);
b490b189
BP
326 } else if (n_args == 1) {
327 /* Pick default backer. */
d13ee228
BP
328 struct shash_node *node;
329 if (shash_count(&all_dpif_backers) == 1) {
330 node = shash_first(&all_dpif_backers);
331 backer = node->data;
332 }
333 } else {
334 error = "Syntax error";
335 goto exit;
336 }
337 if (backer && backer->dpif) {
338 struct dpif_port dpif_port;
339 struct dpif_port_dump port_dump;
340 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
341 simap_put(&port_names, dpif_port.name,
342 odp_to_u32(dpif_port.port_no));
343 }
344 }
345
346 /* Parse the flow and determine whether a datapath or
347 * bridge is specified. If function odp_flow_key_from_string()
348 * returns 0, the flow is a odp_flow. If function
349 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
b490b189 350 if (!odp_flow_from_string(args[n_args - 1], &port_names,
d13ee228
BP
351 &odp_key, &odp_mask)) {
352 if (!backer) {
353 error = "Cannot find the datapath";
354 goto exit;
355 }
356
357 if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
358 error = "Failed to parse datapath flow key";
359 goto exit;
360 }
361
362 *ofprotop = xlate_lookup_ofproto(backer, flow,
363 &flow->in_port.ofp_port);
364 if (*ofprotop == NULL) {
365 error = "Invalid datapath flow";
366 goto exit;
367 }
368
369 flow->tunnel.metadata.tab = ofproto_get_tun_tab(&(*ofprotop)->up);
370
371 /* Convert Geneve options to OpenFlow format now. This isn't actually
372 * required in order to get the right results since the ofproto xlate
373 * actions will handle this for us. However, converting now ensures
374 * that our formatting code will always be able to consistently print
375 * in OpenFlow format, which is what we use here. */
376 if (flow->tunnel.flags & FLOW_TNL_F_UDPIF) {
377 struct flow_tnl tnl;
378 int err;
379
380 memcpy(&tnl, &flow->tunnel, sizeof tnl);
381 err = tun_metadata_from_geneve_udpif(flow->tunnel.metadata.tab,
382 &tnl, &tnl, &flow->tunnel);
383 if (err) {
384 error = "Failed to parse Geneve options";
385 goto exit;
386 }
387 }
388 } else {
389 char *err;
390
b490b189 391 if (n_args != 2) {
d13ee228
BP
392 error = "Must specify bridge name";
393 goto exit;
394 }
395
b490b189 396 *ofprotop = ofproto_dpif_lookup_by_name(args[0]);
d13ee228 397 if (!*ofprotop) {
b490b189 398 m_err = xasprintf("%s: unknown bridge", args[0]);
d13ee228
BP
399 goto exit;
400 }
401
50f96b10
BP
402 struct ofputil_port_map map = OFPUTIL_PORT_MAP_INITIALIZER(&map);
403 const struct ofport *ofport;
404 HMAP_FOR_EACH (ofport, hmap_node, &(*ofprotop)->up.ports) {
405 ofputil_port_map_put(&map, ofport->ofp_port,
406 netdev_get_name(ofport->netdev));
407 }
d13ee228
BP
408 err = parse_ofp_exact_flow(flow, NULL,
409 ofproto_get_tun_tab(&(*ofprotop)->up),
b490b189 410 args[n_args - 1], &map);
50f96b10 411 ofputil_port_map_destroy(&map);
d13ee228
BP
412 if (err) {
413 m_err = xasprintf("Bad openflow flow syntax: %s", err);
414 free(err);
415 goto exit;
416 }
417 }
418
b490b189
BP
419 if (generate_packet) {
420 /* Generate a packet, as requested. */
421 packet = dp_packet_new(0);
6f068379 422 flow_compose(packet, flow, l7, l7_len);
b490b189
BP
423 } else if (packet) {
424 /* Use the metadata from the flow and the packet argument to
425 * reconstruct the flow. */
426 pkt_metadata_from_flow(&packet->md, flow);
427 flow_extract(packet, flow);
d13ee228
BP
428 }
429
430exit:
431 if (error && !m_err) {
432 m_err = xstrdup(error);
433 }
434 if (m_err) {
435 dp_packet_delete(packet);
436 packet = NULL;
437 }
438 *packetp = packet;
439 ofpbuf_uninit(&odp_key);
440 ofpbuf_uninit(&odp_mask);
441 simap_destroy(&port_names);
6f068379 442 free(l7);
d13ee228
BP
443 return m_err;
444}
445
0f2f05bb
YHW
446static void
447free_ct_states(struct ovs_list *ct_states)
448{
449 while (!ovs_list_is_empty(ct_states)) {
450 oftrace_pop_ct_state(ct_states);
451 }
452}
453
d13ee228
BP
454static void
455ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
456 void *aux OVS_UNUSED)
457{
458 struct ofproto_dpif *ofproto;
459 struct dp_packet *packet;
460 char *error;
461 struct flow flow;
0f2f05bb 462 struct ovs_list next_ct_states = OVS_LIST_INITIALIZER(&next_ct_states);
d13ee228 463
0f2f05bb 464 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet,
b490b189 465 &next_ct_states, NULL);
d13ee228
BP
466 if (!error) {
467 struct ds result;
468
469 ds_init(&result);
0f2f05bb
YHW
470 ofproto_trace(ofproto, &flow, packet, NULL, 0, &next_ct_states,
471 &result);
d13ee228
BP
472 unixctl_command_reply(conn, ds_cstr(&result));
473 ds_destroy(&result);
474 dp_packet_delete(packet);
475 } else {
476 unixctl_command_reply_error(conn, error);
477 free(error);
478 }
0f2f05bb 479 free_ct_states(&next_ct_states);
d13ee228
BP
480}
481
482static void
483ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
484 const char *argv[], void *aux OVS_UNUSED)
485{
486 enum ofputil_protocol usable_protocols;
487 struct ofproto_dpif *ofproto;
488 bool enforce_consistency;
489 struct ofpbuf ofpacts;
490 struct dp_packet *packet;
491 struct ds result;
67210a55 492 struct match match;
d13ee228 493 uint16_t in_port;
0f2f05bb 494 struct ovs_list next_ct_states = OVS_LIST_INITIALIZER(&next_ct_states);
d13ee228
BP
495
496 /* Three kinds of error return values! */
497 enum ofperr retval;
498 char *error;
499
500 packet = NULL;
501 ds_init(&result);
502 ofpbuf_init(&ofpacts, 0);
503
504 /* Parse actions. */
efefbcae
BP
505 struct ofpact_parse_params pp = {
506 .port_map = NULL,
507 .ofpacts = &ofpacts,
508 .usable_protocols = &usable_protocols,
509 };
510 error = ofpacts_parse_actions(argv[--argc], &pp);
d13ee228
BP
511 if (error) {
512 unixctl_command_reply_error(conn, error);
513 free(error);
514 goto exit;
515 }
516
0f2f05bb 517 error = parse_flow_and_packet(argc, argv, &ofproto, &match.flow, &packet,
b490b189 518 &next_ct_states, &enforce_consistency);
d13ee228
BP
519 if (error) {
520 unixctl_command_reply_error(conn, error);
521 free(error);
522 goto exit;
523 }
67210a55 524 match_wc_init(&match, &match.flow);
d13ee228
BP
525
526 /* Do the same checks as handle_packet_out() in ofproto.c.
527 *
528 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
529 * strictly correct because these actions aren't in any table, but it's OK
530 * because it 'table_id' is used only to check goto_table instructions, but
531 * packet-outs take a list of actions and therefore it can't include
532 * instructions.
533 *
534 * We skip the "meter" check here because meter is an instruction, not an
535 * action, and thus cannot appear in ofpacts. */
67210a55 536 in_port = ofp_to_u16(match.flow.in_port.ofp_port);
d13ee228
BP
537 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
538 unixctl_command_reply_error(conn, "invalid in_port");
539 goto exit;
540 }
ae6f7530
BP
541
542 struct ofpact_check_params cp = {
543 .match = &match,
544 .max_ports = u16_to_ofp(ofproto->up.max_ports),
545 .table_id = 0,
546 .n_tables = ofproto->up.n_tables,
547 };
548 retval = ofpacts_check_consistency(
549 ofpacts.data, ofpacts.size,
550 enforce_consistency ? usable_protocols : 0, &cp);
d13ee228 551 if (!retval) {
d61973d6 552 ovs_mutex_lock(&ofproto_mutex);
d13ee228
BP
553 retval = ofproto_check_ofpacts(&ofproto->up, ofpacts.data,
554 ofpacts.size);
d61973d6 555 ovs_mutex_unlock(&ofproto_mutex);
d13ee228
BP
556 }
557
558 if (retval) {
559 ds_clear(&result);
560 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
561 unixctl_command_reply_error(conn, ds_cstr(&result));
562 goto exit;
563 }
564
67210a55 565 ofproto_trace(ofproto, &match.flow, packet,
0f2f05bb 566 ofpacts.data, ofpacts.size, &next_ct_states, &result);
d13ee228
BP
567 unixctl_command_reply(conn, ds_cstr(&result));
568
569exit:
570 ds_destroy(&result);
571 dp_packet_delete(packet);
572 ofpbuf_uninit(&ofpacts);
0f2f05bb 573 free_ct_states(&next_ct_states);
d13ee228
BP
574}
575
418a7a84
BP
576static void
577explain_slow_path(enum slow_path_reason slow, struct ds *output)
578{
579 ds_put_cstr(output, "\nThis flow is handled by the userspace "
580 "slow path because it:");
581 for (; slow; slow = zero_rightmost_1bit(slow)) {
582 enum slow_path_reason bit = rightmost_1bit(slow);
583 ds_put_format(output, "\n - %s.",
584 slow_path_reason_to_explanation(bit));
585 }
586}
587
588/* Copies ODP actions from 'in' to 'out', dropping OVS_ACTION_ATTR_OUTPUT and
589 * OVS_ACTION_ATTR_RECIRC along the way. */
590static void
591prune_output_actions(const struct ofpbuf *in, struct ofpbuf *out)
592{
593 const struct nlattr *a;
594 unsigned int left;
595 NL_ATTR_FOR_EACH (a, left, in->data, in->size) {
596 if (a->nla_type == OVS_ACTION_ATTR_CLONE) {
597 struct ofpbuf in_nested;
598 nl_attr_get_nested(a, &in_nested);
599
600 size_t ofs = nl_msg_start_nested(out, OVS_ACTION_ATTR_CLONE);
601 prune_output_actions(&in_nested, out);
602 nl_msg_end_nested(out, ofs);
603 } else if (a->nla_type != OVS_ACTION_ATTR_OUTPUT &&
604 a->nla_type != OVS_ACTION_ATTR_RECIRC) {
605 ofpbuf_put(out, a, NLA_ALIGN(a->nla_len));
606 }
607 }
608}
609
610/* Executes all of the datapath actions, except for any OVS_ACTION_ATTR_OUTPUT
611 * and OVS_ACTION_ATTR_RECIRC actions, in 'actions' on 'packet', which has the
612 * given 'flow', on 'dpif'. The actions have slow path reason 'slow' (if any).
613 * Appends any error message to 'output'.
614 *
615 * With output and recirculation actions dropped, the only remaining side
616 * effects are from OVS_ACTION_ATTR_USERSPACE actions for executing actions to
617 * send a packet to an OpenFlow controller, IPFIX, NetFlow, and sFlow, etc. */
618static void
619execute_actions_except_outputs(struct dpif *dpif,
620 const struct dp_packet *packet,
621 const struct flow *flow,
622 const struct ofpbuf *actions,
623 enum slow_path_reason slow,
624 struct ds *output)
625{
626 struct ofpbuf pruned_actions;
627 ofpbuf_init(&pruned_actions, 0);
628 prune_output_actions(actions, &pruned_actions);
629
630 struct dpif_execute execute = {
631 .actions = pruned_actions.data,
632 .actions_len = pruned_actions.size,
633 .needs_help = (slow & SLOW_ACTION) != 0,
634 .flow = flow,
635 .packet = dp_packet_clone_with_headroom(packet, 2),
636 };
637 int error = dpif_execute(dpif, &execute);
638 if (error) {
639 ds_put_format(output, "\nAction execution failed (%s)\n.",
640 ovs_strerror(error));
641 }
642 dp_packet_delete(execute.packet);
643 ofpbuf_uninit(&pruned_actions);
644}
645
d13ee228 646static void
e6bc8e74
YHW
647ofproto_trace__(struct ofproto_dpif *ofproto, const struct flow *flow,
648 const struct dp_packet *packet, struct ovs_list *recirc_queue,
649 const struct ofpact ofpacts[], size_t ofpacts_len,
650 struct ds *output)
d13ee228 651{
2d9b49dd
BP
652 struct ofpbuf odp_actions;
653 ofpbuf_init(&odp_actions, 0);
d13ee228 654
2d9b49dd
BP
655 struct xlate_in xin;
656 struct flow_wildcards wc;
657 struct ovs_list trace = OVS_LIST_INITIALIZER(&trace);
658 xlate_in_init(&xin, ofproto,
d13ee228
BP
659 ofproto_dpif_get_tables_version(ofproto), flow,
660 flow->in_port.ofp_port, NULL, ntohs(flow->tcp_flags),
2d9b49dd
BP
661 packet, &wc, &odp_actions);
662 xin.ofpacts = ofpacts;
663 xin.ofpacts_len = ofpacts_len;
664 xin.trace = &trace;
e6bc8e74 665 xin.recirc_queue = recirc_queue;
2d9b49dd
BP
666
667 /* Copy initial flow out of xin.flow. It differs from '*flow' because
668 * xlate_in_init() initializes actset_output to OFPP_UNSET. */
669 struct flow initial_flow = xin.flow;
670 ds_put_cstr(output, "Flow: ");
50f96b10 671 flow_format(output, &initial_flow, NULL);
2d9b49dd
BP
672 ds_put_char(output, '\n');
673
674 struct xlate_out xout;
675 enum xlate_error error = xlate_actions(&xin, &xout);
676
677 oftrace_node_print_details(output, &trace, 0);
678
679 ds_put_cstr(output, "\nFinal flow: ");
680 if (flow_equal(&initial_flow, &xin.flow)) {
681 ds_put_cstr(output, "unchanged");
682 } else {
50f96b10 683 flow_format(output, &xin.flow, NULL);
2d9b49dd
BP
684 }
685 ds_put_char(output, '\n');
d13ee228 686
2d9b49dd
BP
687 ds_put_cstr(output, "Megaflow: ");
688 struct match match;
689 match_init(&match, flow, &wc);
50f96b10 690 match_format(&match, NULL, output, OFP_DEFAULT_PRIORITY);
2d9b49dd 691 ds_put_char(output, '\n');
d13ee228 692
2d9b49dd 693 ds_put_cstr(output, "Datapath actions: ");
0722f341 694 format_odp_actions(output, odp_actions.data, odp_actions.size, NULL);
d13ee228
BP
695
696 if (error != XLATE_OK) {
2d9b49dd
BP
697 ds_put_format(output,
698 "\nTranslation failed (%s), packet is dropped.\n",
d13ee228 699 xlate_strerror(error));
418a7a84
BP
700 } else {
701 if (xout.slow) {
702 explain_slow_path(xout.slow, output);
703 }
704 if (packet) {
705 execute_actions_except_outputs(ofproto->backer->dpif, packet,
706 &initial_flow, &odp_actions,
707 xout.slow, output);
d13ee228
BP
708 }
709 }
710
418a7a84 711
2d9b49dd
BP
712 xlate_out_uninit(&xout);
713 ofpbuf_uninit(&odp_actions);
714 oftrace_node_list_destroy(&trace);
d13ee228
BP
715}
716
e6bc8e74
YHW
717/* Implements a "trace" through 'ofproto''s flow table, appending a textual
718 * description of the results to 'output'.
719 *
720 * The trace follows a packet with the specified 'flow' through the flow
721 * table. 'packet' may be nonnull to trace an actual packet, with consequent
722 * side effects (if it is nonnull then its flow must be 'flow').
723 *
724 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
725 * trace, otherwise the actions are determined by a flow table lookup. */
d1ea2cc3 726void
e6bc8e74
YHW
727ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
728 const struct dp_packet *packet,
729 const struct ofpact ofpacts[], size_t ofpacts_len,
0f2f05bb 730 struct ovs_list *next_ct_states, struct ds *output)
e6bc8e74
YHW
731{
732 struct ovs_list recirc_queue = OVS_LIST_INITIALIZER(&recirc_queue);
733 ofproto_trace__(ofproto, flow, packet, &recirc_queue,
734 ofpacts, ofpacts_len, output);
735
736 struct oftrace_recirc_node *recirc_node;
737 LIST_FOR_EACH_POP (recirc_node, node, &recirc_queue) {
738 ds_put_cstr(output, "\n\n");
739 ds_put_char_multiple(output, '=', 79);
740 ds_put_format(output, "\nrecirc(%#"PRIx32")",
741 recirc_node->recirc_id);
0f2f05bb 742
e6bc8e74 743 if (recirc_node->type == OFT_RECIRC_CONNTRACK) {
0f2f05bb
YHW
744 uint32_t ct_state;
745 if (ovs_list_is_empty(next_ct_states)) {
746 ct_state = CS_TRACKED | CS_NEW;
747 ds_put_cstr(output, " - resume conntrack with default "
748 "ct_state=trk|new (use --ct-next to customize)");
749 } else {
750 ct_state = oftrace_pop_ct_state(next_ct_states);
751 struct ds s = DS_EMPTY_INITIALIZER;
752 format_flags(&s, ct_state_to_string, ct_state, '|');
753 ds_put_format(output, " - resume conntrack with ct_state=%s",
754 ds_cstr(&s));
755 ds_destroy(&s);
756 }
757 recirc_node->flow.ct_state = ct_state;
e6bc8e74
YHW
758 }
759 ds_put_char(output, '\n');
760 ds_put_char_multiple(output, '=', 79);
761 ds_put_cstr(output, "\n\n");
762
763 ofproto_trace__(ofproto, &recirc_node->flow, recirc_node->packet,
764 &recirc_queue, ofpacts, ofpacts_len, output);
765 oftrace_recirc_node_destroy(recirc_node);
766 }
767}
768
d13ee228
BP
769void
770ofproto_dpif_trace_init(void)
771{
772 static bool registered;
773 if (registered) {
774 return;
775 }
776 registered = true;
777
778 unixctl_command_register(
779 "ofproto/trace",
0f2f05bb
YHW
780 "{[dp_name] odp_flow | bridge br_flow} [OPTIONS...] "
781 "[-generate|packet]", 1, INT_MAX, ofproto_unixctl_trace, NULL);
d13ee228
BP
782 unixctl_command_register(
783 "ofproto/trace-packet-out",
0f2f05bb
YHW
784 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [OPTIONS...] "
785 "[-generate|packet] actions",
786 2, INT_MAX, ofproto_unixctl_trace_actions, NULL);
d13ee228 787}