summaryrefslogtreecommitdiffstats
path: root/src/plugins/tracedump/graph_test.c
blob: 37dfbcdcaa01638126f740352a2354fb1295b26b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2020 cisco
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vppinfra/error.h>
#include <vppinfra/time_range.h>
#include <vnet/ethernet/ethernet.h>
#include <vpp-api/client/stat_client.h>

#define __plugin_msg_base graph_test_main.msg_id_base
#include <vlibapi/vat_helper_macros.h>

#include <vnet/format_fns.h>
#include <tracedump/graph.api_enum.h>
#include <tracedump/graph.api_types.h>
#include <vlibmemory/vlib.api_types.h>

typedef struct
{
  u16 msg_id_base;
  u32 ping_id;
  vat_main_t *vat_main;
} graph_test_main_t;

graph_test_main_t graph_test_main;


uword
api_unformat_node_index (unformat_input_t * input, va_list * args)
{
  u32 *result = va_arg (*args, u32 *);

  return unformat (input, "%u", result);
}


static void
vl_api_graph_node_get_reply_t_handler (vl_api_graph_node_get_reply_t * mp)
{
  vat_main_t *vam = &vat_main;

  clib_warning ("Next node index: %u\n", mp->cursor);
  vam->result_ready = 1;
}

int
api_graph_node_get (vat_main_t * vam)
{
  graph_test_main_t *gtm = &graph_test_main;
  unformat_input_t *i = vam->input;
  vl_api_graph_node_get_t *mp;
  vl_api_control_ping_t *mp_ping;
  u32 node_index;
  char *node_name;
  u32 flags;
  bool want_arcs;

  if (vam->json_output)
    {
      clib_warning ("JSON output not supported for graph_node_get");
      return -99;
    }

  node_index = ~0;
  node_name = 0;
  flags = 0;
  want_arcs = false;

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "node_index %u", &node_index))
	;
      else if (unformat (i, "node_name %s", &node_name))
	;
      else if (unformat (i, "want_arcs"))
	want_arcs = true;
      else if (unformat (i, "trace_supported"))
	flags |= NODE_FLAG_TRACE_SUPPORTED;
      else if (unformat (i, "input"))
	flags |= NODE_FLAG_TRACE_SUPPORTED;
      else if (unformat (i, "drop"))
	flags |= NODE_FLAG_IS_DROP;
      else if (unformat (i, "ouptput"))
	flags |= NODE_FLAG_IS_OUTPUT;
      else if (unformat (i, "punt"))
	flags |= NODE_FLAG_IS_PUNT;
      else if (unformat (i, "handoff"))
	flags |= NODE_FLAG_IS_HANDOFF;
      else if (unformat (i, "no_free"))
	flags |= NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH;
      else if (unformat (i, "polling"))
	flags |= NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE;
      else if (unformat (i, "interrupt"))
	flags |= NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE;
      else
	{
	  clib_warning ("Unknown input: %U\n", format_unformat_error, i);
	  return -99;
	}
    }

  M (GRAPH_NODE_GET, mp);
  mp->index = htonl (node_index);
  mp->flags = htonl (flags);
  mp->want_arcs = want_arcs;

  if (node_name && node_name[0])
    clib_strncpy ((char *) mp->name, node_name, sizeof (mp->name) - 1);

  int ret = 0;
  S (mp);

  if (!gtm->ping_id)
    gtm->ping_id =
      vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));

  mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
  mp_ping->_vl_msg_id = htons (gtm->ping_id);
  mp_ping->client_index = vam->my_client_index;

  S (mp_ping);
  W (ret);

  return ret;
}

void
vl_api_graph_node_details_t_handler (vl_api_graph_node_details_t * mp)
{
  vat_main_t *vam = &vat_main;
  u32 n_arcs;
  int i;

  fformat (vam->ofp,
	   "Node: %s  Index:%d  Flags:0x%x\n",
	   mp->name, ntohl (mp->index), ntohl (mp->flags));

  n_arcs = ntohl (mp->n_arcs);
  for (i = 0; i < n_arcs; ++i)
    {
      u32 node_index = ntohl (mp->arcs_out[i]);
      fformat (vam->ofp, "    next: %d\n", node_index);
    }
}

void
vl_api_graph_node_details_t_handler_json (vl_api_graph_node_details_t * mp)
{
  clib_error ("graph_node_details JSON not supported");
}

/* Override generated plugin register symbol */
#define vat_plugin_register graph_test_vat_plugin_register
#include <tracedump/graph.api_test.c>

static clib_error_t *
graph_api_hookup_shim (vlib_main_t * vm)
{
  graph_test_vat_plugin_register (&vat_main);
  return 0;
}

VLIB_API_INIT_FUNCTION (graph_api_hookup_shim);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
ass="n">t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0])); t1->entry_index = vnet_buffer (b1)->ip.adj_index[VLIB_TX]; t1->fib_index = vec_elt (im->mfib_index_by_sw_if_index, vnet_buffer(b1)->sw_if_index[VLIB_RX]); } from += 2; n_left -= 2; } while (n_left >= 1) { mfib_forward_lookup_trace_t * t0; vlib_buffer_t * b0; u32 bi0; bi0 = from[0]; b0 = vlib_get_buffer (vm, bi0); if (b0->flags & VLIB_BUFFER_IS_TRACED) { t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); t0->entry_index = vnet_buffer (b0)->ip.adj_index[VLIB_TX]; t0->fib_index = vec_elt (im->mfib_index_by_sw_if_index, vnet_buffer(b0)->sw_if_index[VLIB_RX]); } from += 1; n_left -= 1; } } typedef enum mfib_forward_lookup_next_t_ { MFIB_FORWARD_LOOKUP_NEXT_RPF, MFIB_FORWARD_LOOKUP_N_NEXT, } mfib_forward_lookup_next_t; static uword mfib_forward_lookup (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame, int is_v4) { u32 n_left_from, n_left_to_next, * from, * to_next; from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; while (n_left_from > 0) { vlib_get_next_frame (vm, node, MFIB_FORWARD_LOOKUP_NEXT_RPF, to_next, n_left_to_next); while (n_left_from > 0 && n_left_to_next > 0) { fib_node_index_t mfei0; vlib_buffer_t * p0; u32 fib_index0; u32 pi0; pi0 = from[0]; to_next[0] = pi0; from += 1; to_next += 1; n_left_to_next -= 1; n_left_from -= 1; p0 = vlib_get_buffer (vm, pi0); if (is_v4) { ip4_header_t * ip0; fib_index0 = vec_elt (ip4_main.mfib_index_by_sw_if_index, vnet_buffer(p0)->sw_if_index[VLIB_RX]); ip0 = vlib_buffer_get_current (p0); mfei0 = ip4_mfib_table_lookup(ip4_mfib_get(fib_index0), &ip0->src_address, &ip0->dst_address, 64); } else { ip6_header_t * ip0; fib_index0 = vec_elt (ip6_main.mfib_index_by_sw_if_index, vnet_buffer(p0)->sw_if_index[VLIB_RX]); ip0 = vlib_buffer_get_current (p0); mfei0 = ip6_mfib_table_fwd_lookup(ip6_mfib_get(fib_index0), &ip0->src_address, &ip0->dst_address); } vnet_buffer (p0)->ip.adj_index[VLIB_TX] = mfei0; } vlib_put_next_frame(vm, node, MFIB_FORWARD_LOOKUP_NEXT_RPF, n_left_to_next); } if (node->flags & VLIB_NODE_FLAG_TRACE) mfib_forward_lookup_trace(vm, node, frame); return frame->n_vectors; } VLIB_NODE_FN (ip4_mfib_forward_lookup_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { return (mfib_forward_lookup (vm, node, frame, 1)); } VLIB_REGISTER_NODE (ip4_mfib_forward_lookup_node) = { .name = "ip4-mfib-forward-lookup", .vector_size = sizeof (u32), .format_trace = format_mfib_forward_lookup_trace, .n_next_nodes = MFIB_FORWARD_LOOKUP_N_NEXT, .next_nodes = { [MFIB_FORWARD_LOOKUP_NEXT_RPF] = "ip4-mfib-forward-rpf", }, }; VLIB_NODE_FN (ip6_mfib_forward_lookup_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { return (mfib_forward_lookup (vm, node, frame, 0)); } VLIB_REGISTER_NODE (ip6_mfib_forward_lookup_node) = { .name = "ip6-mfib-forward-lookup", .vector_size = sizeof (u32), .format_trace = format_mfib_forward_lookup_trace, .n_next_nodes = MFIB_FORWARD_LOOKUP_N_NEXT, .next_nodes = { [MFIB_FORWARD_LOOKUP_NEXT_RPF] = "ip6-mfib-forward-rpf", }, }; typedef struct mfib_forward_rpf_trace_t_ { u32 entry_index; u32 sw_if_index; mfib_itf_flags_t itf_flags; } mfib_forward_rpf_trace_t; typedef enum mfib_forward_rpf_next_t_ { MFIB_FORWARD_RPF_NEXT_DROP, MFIB_FORWARD_RPF_N_NEXT, } mfib_forward_rpf_next_t; static u8 * format_mfib_forward_rpf_trace (u8 * s, va_list * args) { CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); mfib_forward_rpf_trace_t * t = va_arg (*args, mfib_forward_rpf_trace_t *); s = format (s, "entry %d", t->entry_index); s = format (s, " itf %d", t->sw_if_index); s = format (s, " flags %U", format_mfib_itf_flags, t->itf_flags); return s; } static int mfib_forward_connected_check (vlib_buffer_t * b0, u32 sw_if_index, int is_v4) { /* * Lookup the source of the IP packet in the * FIB. return true if the entry is attached. */ index_t lbi0; if (is_v4) { load_balance_t *lb0; ip4_header_t *ip0; ip0 = vlib_buffer_get_current(b0); lbi0 = ip4_fib_forwarding_lookup( ip4_fib_table_get_index_for_sw_if_index( sw_if_index), &ip0->src_address); lb0 = load_balance_get(lbi0); return (FIB_ENTRY_FLAG_ATTACHED & lb0->lb_fib_entry_flags); } else { ASSERT(0); } return (0); } static void mfib_forward_itf_signal (vlib_main_t *vm, const mfib_entry_t *mfe, mfib_itf_t *mfi, vlib_buffer_t *b0) { mfib_itf_flags_t old_flags; old_flags = clib_atomic_fetch_or(&mfi->mfi_flags, MFIB_ITF_FLAG_SIGNAL_PRESENT); if (!(old_flags & MFIB_ITF_FLAG_SIGNAL_PRESENT)) { /* * we were the lucky ones to set the signal present flag */ if (!(old_flags & MFIB_ITF_FLAG_DONT_PRESERVE)) { /* * preserve a copy of the packet for the control * plane to examine. * Only allow one preserved packet at at time, since * when the signal present flag is cleared so is the * preserved packet. */ mfib_signal_push(mfe, mfi, b0); } else { /* * The control plane just wants the signal, not the packet as well */ mfib_signal_push(mfe, mfi, NULL); } } /* * else * there is already a signal present on this interface that the * control plane has not yet acknowledged */ } always_inline uword mfib_forward_rpf (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame, int is_v4) { u32 n_left_from, n_left_to_next, * from, * to_next; mfib_forward_rpf_next_t next; vlib_node_runtime_t *error_node; if (is_v4) error_node = vlib_node_get_runtime (vm, ip4_input_node.index); else error_node = vlib_node_get_runtime (vm, ip6_input_node.index); from = vlib_frame_vector_args (frame); n_left_from = frame->n_vectors; next = MFIB_FORWARD_RPF_NEXT_DROP; while (n_left_from > 0) { vlib_get_next_frame (vm, node, next, to_next, n_left_to_next); while (n_left_from > 0 && n_left_to_next > 0) { fib_node_index_t mfei0; const mfib_entry_t *mfe0; mfib_itf_t *mfi0; vlib_buffer_t * b0; u32 pi0, next0; mfib_itf_flags_t iflags0; mfib_entry_flags_t eflags0; u8 error0; pi0 = from[0]; to_next[0] = pi0; from += 1; to_next += 1; n_left_to_next -= 1; n_left_from -= 1; error0 = IP4_ERROR_NONE; b0 = vlib_get_buffer (vm, pi0); mfei0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX]; mfe0 = mfib_entry_get(mfei0); mfi0 = mfib_entry_get_itf(mfe0, vnet_buffer(b0)->sw_if_index[VLIB_RX]); /* * throughout this function we are 'PREDICT' optimising * for the case of throughput traffic that is not replicated * to the host stack nor sets local flags */ /* * If the mfib entry has a configured RPF-ID check that * in preference to an interface based RPF */ if (MFIB_RPF_ID_NONE != mfe0->mfe_rpf_id) { iflags0 = (mfe0->mfe_rpf_id == vnet_buffer(b0)->ip.rpf_id ? MFIB_ITF_FLAG_ACCEPT : MFIB_ITF_FLAG_NONE); } else { if (PREDICT_TRUE(NULL != mfi0)) { iflags0 = mfi0->mfi_flags; } else { iflags0 = MFIB_ITF_FLAG_NONE; } } eflags0 = mfe0->mfe_flags; if (PREDICT_FALSE(eflags0 & MFIB_ENTRY_FLAG_CONNECTED)) { /* * lookup the source in the unicast FIB - check it * matches a connected. */ if (mfib_forward_connected_check( b0, vnet_buffer(b0)->sw_if_index[VLIB_RX], is_v4)) { mfib_forward_itf_signal(vm, mfe0, mfi0, b0); } } if (PREDICT_FALSE((eflags0 & MFIB_ENTRY_FLAG_SIGNAL) ^ (iflags0 & MFIB_ITF_FLAG_NEGATE_SIGNAL))) { /* * Entry signal XOR interface negate-signal */ if (NULL != mfi0) { mfib_forward_itf_signal(vm, mfe0, mfi0, b0); } } if (PREDICT_TRUE((iflags0 & MFIB_ITF_FLAG_ACCEPT) || (eflags0 & MFIB_ENTRY_FLAG_ACCEPT_ALL_ITF))) { /* * This interface is accepting packets for the matching entry */ next0 = mfe0->mfe_rep.dpoi_next_node; vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mfe0->mfe_rep.dpoi_index; } else { next0 = MFIB_FORWARD_RPF_NEXT_DROP; error0 = IP4_ERROR_RPF_FAILURE; } b0->error = error0 ? error_node->errors[error0] : 0; if (b0->flags & VLIB_BUFFER_IS_TRACED) { mfib_forward_rpf_trace_t *t0; t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); t0->entry_index = mfei0; t0->itf_flags = iflags0; if (NULL == mfi0) { t0->sw_if_index = ~0; } else { t0->sw_if_index = mfi0->mfi_sw_if_index; } } vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next, pi0, next0); } vlib_put_next_frame(vm, node, next, n_left_to_next); } return frame->n_vectors; } VLIB_NODE_FN (ip4_mfib_forward_rpf_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { return (mfib_forward_rpf(vm, node, frame, 1)); } VLIB_REGISTER_NODE (ip4_mfib_forward_rpf_node) = { .name = "ip4-mfib-forward-rpf", .vector_size = sizeof (u32), .format_trace = format_mfib_forward_rpf_trace, .n_next_nodes = MFIB_FORWARD_RPF_N_NEXT, .next_nodes = { [MFIB_FORWARD_RPF_NEXT_DROP] = "ip4-drop", }, }; VLIB_NODE_FN (ip6_mfib_forward_rpf_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { return (mfib_forward_rpf(vm, node, frame, 0)); } VLIB_REGISTER_NODE (ip6_mfib_forward_rpf_node) = { .name = "ip6-mfib-forward-rpf", .vector_size = sizeof (u32), .format_trace = format_mfib_forward_rpf_trace, .n_next_nodes = MFIB_FORWARD_RPF_N_NEXT, .next_nodes = { [MFIB_FORWARD_RPF_NEXT_DROP] = "ip6-drop", }, };