aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cdp/cdp_node.c
blob: f9ee251c022da986a880fdbe2bf3ca1a4be8109a (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright (c) 2011-2018 Cisco and/or its affiliates.
 * 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 <cdp/cdp.h>
#include <vnet/ethernet/packet.h>

/** \file

    2 x CDP graph nodes: an "interior" node to process
    incoming announcements, and a "process" node to periodically
    send announcements.

    The interior node is neither pipelined nor dual-looped, because
    it would be very unusual to see more than one CDP packet in
    a given input frame. So, it's a very simple / straightforward
    example.
*/

/*
 * packet counter strings
 * Dump these counters via the "show error" CLI command
 */
static char *cdp_error_strings[] = {
#define _(sym,string) string,
  foreach_cdp_error
#undef _
};

/*
 * We actually send all cdp pkts to the "error" node after scanning
 * them, so the graph node has only one next-index. The "error-drop"
 * node automatically bumps our per-node packet counters for us.
 */
typedef enum
{
  CDP_INPUT_NEXT_NORMAL,
  CDP_INPUT_N_NEXT,
} cdp_next_t;

/*
 * Process a frame of cdp packets
 * Expect 1 packet / frame
 */
static uword
cdp_node_fn (vlib_main_t * vm,
	     vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  u32 n_left_from, *from;
  cdp_input_trace_t *t0;

  from = vlib_frame_vector_args (frame);	/* array of buffer indices */
  n_left_from = frame->n_vectors;	/* number of buffer indices */

  while (n_left_from > 0)
    {
      u32 bi0;
      vlib_buffer_t *b0;
      u32 next0, error0;

      bi0 = from[0];
      b0 = vlib_get_buffer (vm, bi0);

      next0 = CDP_INPUT_NEXT_NORMAL;

      /* scan this cdp pkt. error0 is the counter index to bump */
      error0 = cdp_input (vm, b0, bi0);
      b0->error = node->errors[error0];

      /* If this pkt is traced, snapshoot the data */
      if (b0->flags & VLIB_BUFFER_IS_TRACED)
	{
	  int len;
	  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
	  len = (b0->current_length < sizeof (t0->data))
	    ? b0->current_length : sizeof (t0->data);
	  t0->len = len;
	  clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
	}
      /* push this pkt to the next graph node, always error-drop */
      vlib_set_next_frame_buffer (vm, node, next0, bi0);

      from += 1;
      n_left_from -= 1;
    }

  return frame->n_vectors;
}

/*
 * cdp input graph node declaration
 */
/* *INDENT-OFF* */
VLIB_REGISTER_NODE (cdp_input_node, static) = {
  .function = cdp_node_fn,
  .name = "cdp-input",
  .vector_size = sizeof (u32),
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = CDP_N_ERROR,
  .error_strings = cdp_error_strings,

  .format_trace = cdp_input_format_trace,

  .n_next_nodes = CDP_INPUT_N_NEXT,
  .next_nodes = {
    [CDP_INPUT_NEXT_NORMAL] = "error-drop",
  },
};
/* *INDENT-ON* */

/*
 * cdp periodic function
 */
static uword
cdp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
{
  cdp_main_t *cm = &cdp_main;
  f64 poll_time_remaining;
  uword event_type, *event_data = 0;

  /* Start w/ cdp disabled */
  poll_time_remaining = 86400.0;

  while (1)
    {
      /* sleep until next poll time, or msg serialize event occurs */
      poll_time_remaining =
	vlib_process_wait_for_event_or_clock (vm, poll_time_remaining);

      event_type = vlib_process_get_events (vm, &event_data);
      switch (event_type)
	{
	case ~0:		/* no events => timeout */
	  break;

	case CDP_EVENT_ENABLE:
	  if (!cm->cdp_protocol_registered)
	    {
	      /*
	       * Dynamically register the cdp input node
	       * with the snap classifier
	       */
	      snap_register_input_protocol (vm, "cdp-input",
					    0xC /* ieee_oui, Cisco */ ,
					    0x2000 /* protocol CDP */ ,
					    cdp_input_node.index);

	      snap_register_input_protocol (vm, "cdp-input",
					    0xC /* ieee_oui, Cisco */ ,
					    0x2004 /* protocol CDP */ ,
					    cdp_input_node.index);
#if 0
	      /*
	       * Keep this code for reference...
	       * Register with the hdlc classifier
	       */
	      hdlc_register_input_protocol (vm, HDLC_PROTOCOL_cdp,
					    cdp_input_node.index);
#endif
	      /* with ethernet input (for SRP) */
	      ethernet_register_input_type (vm, ETHERNET_TYPE_CDP /* CDP */ ,
					    cdp_input_node.index);
	      cm->cdp_protocol_registered = 1;
	    }
	  poll_time_remaining = 10.0;
	  break;

	case CDP_EVENT_DISABLE:
	  poll_time_remaining = 86400.0;
	  break;

	default:
	  clib_warning ("BUG: event type 0x%wx", event_type);
	  break;
	}
      vec_reset_length (event_data);

      /* peer timeout scan, send announcements */
      if (vlib_process_suspend_time_is_zero (poll_time_remaining))
	{
	  cdp_periodic (vm);
	  poll_time_remaining = 10.0;
	}
    }

  return 0;
}

void
vnet_cdp_create_periodic_process (cdp_main_t * cmp)
{
  /* Already created the process node? */
  if (cmp->cdp_process_node_index > 0)
    return;

  /* No, create it now and make a note of the node index */
  cmp->cdp_process_node_index = vlib_process_create
    (cmp->vlib_main, "cdp-process",
     cdp_process, 16 /* log2_n_stack_bytes */ );
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
>; else if (c >= 'a' && c <= 'f') hex_digit = c + 10 - 'a'; else if (c >= 'A' && c <= 'F') hex_digit = c + 10 - 'A'; else if (c == ':' && n_colon < 2) n_colon++; else { unformat_put_input (input); break; } /* Too many hex quads. */ if (n_hex_quads >= ARRAY_LEN (hex_quads)) return 0; if (hex_digit < 16) { hex_quad = (hex_quad << 4) | hex_digit; /* Hex quad must fit in 16 bits. */ if (n_hex_digits >= 4) return 0; n_colon = 0; n_hex_digits++; } /* Save position of :: */ if (n_colon == 2) { /* More than one :: ? */ if (double_colon_index < ARRAY_LEN (hex_quads)) return 0; double_colon_index = n_hex_quads; } if (n_colon > 0 && n_hex_digits > 0) { hex_quads[n_hex_quads++] = hex_quad; hex_quad = 0; n_hex_digits = 0; } } if (n_hex_digits > 0) hex_quads[n_hex_quads++] = hex_quad; { word i; /* Expand :: to appropriate number of zero hex quads. */ if (double_colon_index < ARRAY_LEN (hex_quads)) { word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads; for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--) hex_quads[n_zero + i] = hex_quads[i]; for (i = 0; i < n_zero; i++) { ASSERT ((double_colon_index + i) < ARRAY_LEN (hex_quads)); hex_quads[double_colon_index + i] = 0; } n_hex_quads = ARRAY_LEN (hex_quads); } /* Too few hex quads given. */ if (n_hex_quads < ARRAY_LEN (hex_quads)) return 0; for (i = 0; i < ARRAY_LEN (hex_quads); i++) result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]); return 1; } } /* Format an IP6 header. */ u8 * format_ip6_header (u8 * s, va_list * args) { ip6_header_t *ip = va_arg (*args, ip6_header_t *); u32 max_header_bytes = va_arg (*args, u32); u32 i, ip_version, traffic_class, flow_label; uword indent; /* Nothing to do. */ if (max_header_bytes < sizeof (ip[0])) return format (s, "IP header truncated"); indent = format_get_indent (s); indent += 2; s = format (s, "%U: %U -> %U", format_ip_protocol, ip->protocol, format_ip6_address, &ip->src_address, format_ip6_address, &ip->dst_address); i = clib_net_to_host_u32 (ip->ip_version_traffic_class_and_flow_label); ip_version = (i >> 28); traffic_class = (i >> 20) & 0xff; flow_label = i & pow2_mask (20); if (ip_version != 6) s = format (s, "\n%Uversion %d", format_white_space, indent, ip_version); s = format (s, "\n%Utos 0x%02x, flow label 0x%x, hop limit %d, payload length %d", format_white_space, indent, traffic_class, flow_label, ip->hop_limit, clib_net_to_host_u16 (ip->payload_length)); /* Recurse into next protocol layer. */ if (max_header_bytes != 0 && sizeof (ip[0]) < max_header_bytes) { ip_main_t *im = &ip_main; ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol); if (pi && pi->format_header) s = format (s, "\n%U%U", format_white_space, indent - 2, pi->format_header, /* next protocol header */ (void *) (ip + 1), max_header_bytes - sizeof (ip[0])); } return s; } /* Parse an IP6 header. */ uword unformat_ip6_header (unformat_input_t * input, va_list * args) { u8 **result = va_arg (*args, u8 **); ip6_header_t *ip; int old_length; /* Allocate space for IP header. */ { void *p; old_length = vec_len (*result); vec_add2 (*result, p, sizeof (ip[0])); ip = p; } memset (ip, 0, sizeof (ip[0])); ip->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (6 << 28); if (!unformat (input, "%U: %U -> %U", unformat_ip_protocol, &ip->protocol, unformat_ip6_address, &ip->src_address, unformat_ip6_address, &ip->dst_address)) return 0; /* Parse options. */ while (1) { int i; if (unformat (input, "tos %U", unformat_vlib_number, &i)) ip->ip_version_traffic_class_and_flow_label |= clib_host_to_net_u32 ((i & 0xff) << 20); else if (unformat (input, "hop-limit %U", unformat_vlib_number, &i)) ip->hop_limit = i; /* Can't parse input: try next protocol level. */ else break; } /* Recurse into next protocol layer. */ { ip_main_t *im = &ip_main; ip_protocol_info_t *pi = ip_get_protocol_info (im, ip->protocol); if (pi && pi->unformat_header) { if (!unformat_user (input, pi->unformat_header, result)) return 0; /* Result may have moved. */ ip = (void *) *result + old_length; } } ip->payload_length = clib_host_to_net_u16 (vec_len (*result) - (old_length + sizeof (ip[0]))); return 1; } /* Parse an IP46 address. */ uword unformat_ip46_address (unformat_input_t * input, va_list * args) { ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); ip46_type_t type = va_arg (*args, ip46_type_t); if ((type != IP46_TYPE_IP6) && unformat (input, "%U", unformat_ip4_address, &ip46->ip4)) { ip46_address_mask_ip4 (ip46); return 1; } else if ((type != IP46_TYPE_IP4) && unformat (input, "%U", unformat_ip6_address, &ip46->ip6)) { return 1; } return 0; } /* Format an IP46 address. */ u8 * format_ip46_address (u8 * s, va_list * args) { ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); ip46_type_t type = va_arg (*args, ip46_type_t); int is_ip4 = 1; switch (type) { case IP46_TYPE_ANY: is_ip4 = ip46_address_is_ip4 (ip46); break; case IP46_TYPE_IP4: is_ip4 = 1; break; case IP46_TYPE_IP6: is_ip4 = 0; break; } return is_ip4 ? format (s, "%U", format_ip4_address, &ip46->ip4) : format (s, "%U", format_ip6_address, &ip46->ip6); } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */