Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #include <vnet/cdp/cdp_node.h> |
| 16 | #include <vnet/ethernet/packet.h> |
| 17 | |
| 18 | static vlib_node_registration_t cdp_process_node; |
| 19 | |
| 20 | /** \file |
| 21 | |
| 22 | 2 x CDP graph nodes: an "interior" node to process |
| 23 | incoming announcements, and a "process" node to periodically |
| 24 | send announcements. |
| 25 | |
| 26 | The interior node is neither pipelined nor dual-looped, because |
| 27 | it would be very unusual to see more than one CDP packet in |
| 28 | a given input frame. So, it's a very simple / straighforward |
| 29 | example. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * packet counter strings |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 34 | * Dump these counters via the "show error" CLI command |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 35 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 36 | static char *cdp_error_strings[] = { |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 37 | #define _(sym,string) string, |
| 38 | foreach_cdp_error |
| 39 | #undef _ |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * We actually send all cdp pkts to the "error" node after scanning |
| 44 | * them, so the graph node has only one next-index. The "error-drop" |
| 45 | * node automatically bumps our per-node packet counters for us. |
| 46 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 47 | typedef enum |
| 48 | { |
| 49 | CDP_INPUT_NEXT_NORMAL, |
| 50 | CDP_INPUT_N_NEXT, |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 51 | } cdp_next_t; |
| 52 | |
| 53 | /* |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 54 | * Process a frame of cdp packets |
| 55 | * Expect 1 packet / frame |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 56 | */ |
| 57 | static uword |
| 58 | cdp_node_fn (vlib_main_t * vm, |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 59 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 60 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 61 | u32 n_left_from, *from; |
| 62 | cdp_input_trace_t *t0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 63 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 64 | from = vlib_frame_vector_args (frame); /* array of buffer indices */ |
| 65 | n_left_from = frame->n_vectors; /* number of buffer indices */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 66 | |
| 67 | while (n_left_from > 0) |
| 68 | { |
| 69 | u32 bi0; |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 70 | vlib_buffer_t *b0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 71 | u32 next0, error0; |
| 72 | |
| 73 | bi0 = from[0]; |
| 74 | b0 = vlib_get_buffer (vm, bi0); |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 75 | |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 76 | next0 = CDP_INPUT_NEXT_NORMAL; |
| 77 | |
| 78 | /* scan this cdp pkt. error0 is the counter index to bump */ |
| 79 | error0 = cdp_input (vm, b0, bi0); |
| 80 | b0->error = node->errors[error0]; |
| 81 | |
| 82 | /* If this pkt is traced, snapshoot the data */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 83 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 84 | { |
| 85 | int len; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 86 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 87 | len = (b0->current_length < sizeof (t0->data)) |
| 88 | ? b0->current_length : sizeof (t0->data); |
| 89 | t0->len = len; |
| 90 | clib_memcpy (t0->data, vlib_buffer_get_current (b0), len); |
| 91 | } |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 92 | /* push this pkt to the next graph node, always error-drop */ |
| 93 | vlib_set_next_frame_buffer (vm, node, next0, bi0); |
| 94 | |
| 95 | from += 1; |
| 96 | n_left_from -= 1; |
| 97 | } |
| 98 | |
| 99 | return frame->n_vectors; |
| 100 | } |
| 101 | |
| 102 | /* |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 103 | * cdp input graph node declaration |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 104 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 105 | /* *INDENT-OFF* */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 106 | VLIB_REGISTER_NODE (cdp_input_node, static) = { |
| 107 | .function = cdp_node_fn, |
| 108 | .name = "cdp-input", |
| 109 | .vector_size = sizeof (u32), |
| 110 | .type = VLIB_NODE_TYPE_INTERNAL, |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 111 | |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 112 | .n_errors = CDP_N_ERROR, |
| 113 | .error_strings = cdp_error_strings, |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 114 | |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 115 | .format_trace = cdp_input_format_trace, |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 116 | |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 117 | .n_next_nodes = CDP_INPUT_N_NEXT, |
| 118 | .next_nodes = { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 119 | [CDP_INPUT_NEXT_NORMAL] = "error-drop", |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 120 | }, |
| 121 | }; |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 122 | /* *INDENT-ON* */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 123 | |
| 124 | /* |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 125 | * cdp periodic function |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 126 | */ |
| 127 | static uword |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 128 | cdp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 129 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 130 | cdp_main_t *cm = &cdp_main; |
| 131 | f64 poll_time_remaining; |
| 132 | uword event_type, *event_data = 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 133 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 134 | /* So we can send events to the cdp process */ |
| 135 | cm->cdp_process_node_index = cdp_process_node.index; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 136 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 137 | /* Dynamically register the cdp input node with the snap classifier */ |
| 138 | snap_register_input_protocol (vm, "cdp-input", 0xC /* ieee_oui, Cisco */ , |
| 139 | 0x2000 /* protocol CDP */ , |
| 140 | cdp_input_node.index); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 141 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 142 | snap_register_input_protocol (vm, "cdp-input", 0xC /* ieee_oui, Cisco */ , |
| 143 | 0x2004 /* protocol CDP */ , |
| 144 | cdp_input_node.index); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 145 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 146 | #if 0 /* retain for reference */ |
| 147 | /* with the hdlc classifier */ |
| 148 | hdlc_register_input_protocol (vm, HDLC_PROTOCOL_cdp, cdp_input_node.index); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 149 | #endif |
| 150 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 151 | /* with ethernet input (for SRP) */ |
| 152 | ethernet_register_input_type (vm, ETHERNET_TYPE_CDP /* CDP */ , |
| 153 | cdp_input_node.index); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 154 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 155 | poll_time_remaining = 10.0 /* seconds */ ; |
| 156 | while (1) |
| 157 | { |
| 158 | /* sleep until next poll time, or msg serialize event occurs */ |
| 159 | poll_time_remaining = |
| 160 | vlib_process_wait_for_event_or_clock (vm, poll_time_remaining); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 161 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 162 | event_type = vlib_process_get_events (vm, &event_data); |
| 163 | switch (event_type) |
| 164 | { |
| 165 | case ~0: /* no events => timeout */ |
| 166 | break; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 167 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 168 | default: |
| 169 | clib_warning ("BUG: event type 0x%wx", event_type); |
| 170 | break; |
| 171 | } |
| 172 | if (event_data) |
| 173 | _vec_len (event_data) = 0; |
| 174 | |
| 175 | /* peer timeout scan, send announcements */ |
| 176 | if (vlib_process_suspend_time_is_zero (poll_time_remaining)) |
| 177 | { |
| 178 | cdp_periodic (vm); |
| 179 | poll_time_remaining = 10.0; |
| 180 | } |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 183 | return 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 187 | * cdp periodic node declaration |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 188 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 189 | /* *INDENT-OFF* */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 190 | VLIB_REGISTER_NODE (cdp_process_node, static) = { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 191 | .function = cdp_process, |
| 192 | .type = VLIB_NODE_TYPE_PROCESS, |
| 193 | .name = "cdp-process", |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 194 | }; |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 195 | /* *INDENT-ON* */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 196 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 197 | void |
| 198 | vnet_cdp_node_reference (void) |
| 199 | { |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * fd.io coding-style-patch-verification: ON |
| 204 | * |
| 205 | * Local Variables: |
| 206 | * eval: (c-set-style "gnu") |
| 207 | * End: |
| 208 | */ |