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 | |
| 17 | cdp_main_t cdp_main; |
| 18 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 19 | #define DEBUG_TLV_DUMP 0 /* 1=> dump TLV's to stdout while processing them */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 20 | |
| 21 | /* Reliable multicast messages we use to keep peers updated */ |
| 22 | mc_serialize_msg_t serialize_cdp_neighbor_msg; |
| 23 | mc_serialize_msg_t serialize_cdp_keepalive_msg; |
| 24 | |
| 25 | /* |
| 26 | * ported from an unspecified Cisco cdp implementation. |
| 27 | * Compute / return in HOST byte order. 0 => good checksum. |
| 28 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 29 | u16 |
| 30 | cdp_checksum (void *p, int count) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 31 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 32 | u32 sum; |
| 33 | u16 i, *data; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 34 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 35 | data = p; |
| 36 | sum = 0; |
| 37 | while (count > 1) |
| 38 | { |
| 39 | sum += ntohs (*data); |
| 40 | data++; |
| 41 | count -= 2; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 44 | if (count > 0) |
| 45 | sum += *(char *) data; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 46 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 47 | while (sum >> 16) |
| 48 | { |
| 49 | sum = (sum & 0xFFFF) + (sum >> 16); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 52 | i = (i16) sum; |
| 53 | return (~i); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* TLV handler table */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 57 | typedef struct |
| 58 | { |
| 59 | char *name; |
| 60 | u32 tlv_id; |
| 61 | void *format; |
| 62 | void *process; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 63 | } tlv_handler_t; |
| 64 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 65 | static tlv_handler_t tlv_handlers[]; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 66 | |
| 67 | /* Display a generic TLV as a set of hex bytes */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 68 | static u8 * |
| 69 | format_generic_tlv (u8 * s, va_list * va) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 70 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 71 | cdp_tlv_t *t = va_arg (*va, cdp_tlv_t *); |
| 72 | tlv_handler_t *h = &tlv_handlers[t->t]; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 73 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 74 | s = format (s, "%s(%d): %U\n", h->name, |
| 75 | t->t, format_hex_bytes, t->v, t->l - sizeof (*t)); |
| 76 | return s; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* Ignore / skip a TLV we don't support */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 80 | static cdp_error_t |
| 81 | process_generic_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 82 | { |
| 83 | #if DEBUG_TLV_DUMP > 0 |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 84 | fformat (stdout, "%U", format_generic_tlv, t); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 85 | #endif |
| 86 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 87 | return CDP_ERROR_NONE; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* print a text tlv */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 91 | static u8 * |
| 92 | format_text_tlv (u8 * s, va_list * va) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 93 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 94 | cdp_tlv_t *t = va_arg (*va, cdp_tlv_t *); |
| 95 | tlv_handler_t *h = &tlv_handlers[t->t]; |
| 96 | int i; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 97 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 98 | s = format (s, "%s(%d): ", h->name, t->t); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 99 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 100 | for (i = 0; i < (t->l - sizeof (*t)); i++) |
| 101 | vec_add1 (s, t->v[i]); |
| 102 | |
| 103 | vec_add1 (s, '\n'); |
| 104 | return s; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | #if DEBUG_TLV_DUMP == 0 |
| 108 | /* gcc warning be gone */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 109 | CLIB_UNUSED (static cdp_error_t |
| 110 | process_text_tlv (cdp_main_t * cm, cdp_neighbor_t * n, |
| 111 | cdp_tlv_t * t)); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 112 | #endif |
| 113 | |
| 114 | /* process / skip a generic text TLV that we don't support */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 115 | static cdp_error_t |
| 116 | process_text_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 117 | { |
| 118 | #if DEBUG_TLV_DUMP > 0 |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 119 | fformat (stdout, "%U\n", format_text_tlv, t); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 120 | #endif |
| 121 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 122 | return CDP_ERROR_NONE; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* per-TLV format function definitions */ |
| 126 | #define format_unused_tlv format_generic_tlv |
| 127 | #define format_device_name_tlv format_text_tlv |
| 128 | #define format_address_tlv format_generic_tlv |
| 129 | #define format_port_id_tlv format_text_tlv |
| 130 | #define format_capabilities_tlv format_generic_tlv |
| 131 | #define format_version_tlv format_text_tlv |
| 132 | #define format_platform_tlv format_text_tlv |
| 133 | #define format_ipprefix_tlv format_generic_tlv |
| 134 | #define format_hello_tlv format_generic_tlv |
| 135 | #define format_vtp_domain_tlv format_generic_tlv |
| 136 | #define format_native_vlan_tlv format_generic_tlv |
| 137 | #define format_duplex_tlv format_generic_tlv |
| 138 | #define format_appl_vlan_tlv format_generic_tlv |
| 139 | #define format_trigger_tlv format_generic_tlv |
| 140 | #define format_power_tlv format_generic_tlv |
| 141 | #define format_mtu_tlv format_generic_tlv |
| 142 | #define format_trust_tlv format_generic_tlv |
| 143 | #define format_cos_tlv format_generic_tlv |
| 144 | #define format_sysname_tlv format_generic_tlv |
| 145 | #define format_sysobject_tlv format_generic_tlv |
| 146 | #define format_mgmt_addr_tlv format_generic_tlv |
| 147 | #define format_physical_loc_tlv format_generic_tlv |
| 148 | #define format_mgmt_addr2_tlv format_generic_tlv |
| 149 | #define format_power_requested_tlv format_generic_tlv |
| 150 | #define format_power_available_tlv format_generic_tlv |
| 151 | #define format_port_unidirectional_tlv format_generic_tlv |
| 152 | #define format_unknown_28_tlv format_generic_tlv |
| 153 | #define format_energywise_tlv format_generic_tlv |
| 154 | #define format_unknown_30_tlv format_generic_tlv |
| 155 | #define format_spare_poe_tlv format_generic_tlv |
| 156 | |
| 157 | /* tlv ID=0 is a mistake */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 158 | static cdp_error_t |
| 159 | process_unused_tlv (cdp_main_t * cm, cdp_neighbor_t * n, cdp_tlv_t * t) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 160 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 161 | return CDP_ERROR_BAD_TLV; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* list of text TLV's that we snapshoot */ |
| 165 | #define foreach_text_to_struct_tlv \ |
| 166 | _(device_name,DEBUG_TLV_DUMP) \ |
| 167 | _(version,DEBUG_TLV_DUMP) \ |
| 168 | _(platform,DEBUG_TLV_DUMP) \ |
| 169 | _(port_id,DEBUG_TLV_DUMP) |
| 170 | |
| 171 | #define _(z,dbg) \ |
| 172 | static \ |
| 173 | cdp_error_t process_##z##_tlv (cdp_main_t *cm, cdp_neighbor_t *n, \ |
| 174 | cdp_tlv_t *t) \ |
| 175 | { \ |
| 176 | int i; \ |
| 177 | if (dbg) \ |
| 178 | fformat(stdout, "%U\n", format_text_tlv, t); \ |
| 179 | \ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 180 | if (n->z) \ |
| 181 | _vec_len(n->z) = 0; \ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 182 | \ |
| 183 | for (i = 0; i < (t->l - sizeof (*t)); i++) \ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 184 | vec_add1(n->z, t->v[i]); \ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 185 | \ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 186 | vec_add1(n->z, 0); \ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 187 | \ |
| 188 | return CDP_ERROR_NONE; \ |
| 189 | } |
| 190 | |
| 191 | foreach_text_to_struct_tlv |
| 192 | #undef _ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 193 | #define process_address_tlv process_generic_tlv |
| 194 | #define process_capabilities_tlv process_generic_tlv |
| 195 | #define process_ipprefix_tlv process_generic_tlv |
| 196 | #define process_hello_tlv process_generic_tlv |
| 197 | #define process_vtp_domain_tlv process_generic_tlv |
| 198 | #define process_native_vlan_tlv process_generic_tlv |
| 199 | #define process_duplex_tlv process_generic_tlv |
| 200 | #define process_appl_vlan_tlv process_generic_tlv |
| 201 | #define process_trigger_tlv process_generic_tlv |
| 202 | #define process_power_tlv process_generic_tlv |
| 203 | #define process_mtu_tlv process_generic_tlv |
| 204 | #define process_trust_tlv process_generic_tlv |
| 205 | #define process_cos_tlv process_generic_tlv |
| 206 | #define process_sysname_tlv process_generic_tlv |
| 207 | #define process_sysobject_tlv process_generic_tlv |
| 208 | #define process_mgmt_addr_tlv process_generic_tlv |
| 209 | #define process_physical_loc_tlv process_generic_tlv |
| 210 | #define process_mgmt_addr2_tlv process_generic_tlv |
| 211 | #define process_power_requested_tlv process_generic_tlv |
| 212 | #define process_power_available_tlv process_generic_tlv |
| 213 | #define process_port_unidirectional_tlv process_generic_tlv |
| 214 | #define process_unknown_28_tlv process_generic_tlv |
| 215 | #define process_energywise_tlv process_generic_tlv |
| 216 | #define process_unknown_30_tlv process_generic_tlv |
| 217 | #define process_spare_poe_tlv process_generic_tlv |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 218 | static tlv_handler_t tlv_handlers[] = { |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 219 | #define _(a) {#a, CDP_TLV_##a, format_##a##_tlv, process_##a##_tlv}, |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 220 | foreach_cdp_tlv_type |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 221 | #undef _ |
| 222 | }; |
| 223 | |
| 224 | #if DEBUG_TLV_DUMP == 0 |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 225 | CLIB_UNUSED (static u8 * format_cdp_hdr (u8 * s, va_list * va)); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 226 | #endif |
| 227 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 228 | static u8 * |
| 229 | format_cdp_hdr (u8 * s, va_list * va) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 230 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 231 | cdp_hdr_t *h = va_arg (*va, cdp_hdr_t *); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 232 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 233 | s = format (s, "version %d, ttl %d(secs), cksum 0x%04x\n", |
| 234 | h->version, h->ttl, h->checksum); |
| 235 | return s; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 236 | } |
| 237 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 238 | static cdp_error_t |
| 239 | process_cdp_hdr (cdp_main_t * cm, cdp_neighbor_t * n, cdp_hdr_t * h) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 240 | { |
| 241 | #if DEBUG_TLV_DUMP > 0 |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 242 | fformat (stdout, "%U", format_cdp_hdr, h); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 243 | #endif |
| 244 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 245 | if (h->version != 1 && h->version != 2) |
| 246 | return CDP_ERROR_PROTOCOL_VERSION; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 247 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 248 | n->ttl_in_seconds = h->ttl; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 249 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 250 | return CDP_ERROR_NONE; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* scan a cdp packet; header, then tlv's */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 254 | static int |
| 255 | cdp_packet_scan (cdp_main_t * cm, cdp_neighbor_t * n) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 256 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 257 | u8 *cur = n->last_rx_pkt; |
| 258 | cdp_hdr_t *h; |
| 259 | cdp_tlv_t *tlv; |
| 260 | cdp_error_t e = CDP_ERROR_NONE; |
| 261 | tlv_handler_t *handler; |
| 262 | cdp_error_t (*fp) (cdp_main_t *, cdp_neighbor_t *, cdp_tlv_t *); |
| 263 | u16 computed_checksum; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 264 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 265 | computed_checksum = cdp_checksum (cur, vec_len (cur)); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 266 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 267 | if (computed_checksum) |
| 268 | return CDP_ERROR_CHECKSUM; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 269 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 270 | h = (cdp_hdr_t *) cur; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 271 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 272 | e = process_cdp_hdr (cm, n, h); |
| 273 | if (e) |
| 274 | return e; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 275 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 276 | cur = (u8 *) (h + 1); |
| 277 | |
| 278 | while (cur < n->last_rx_pkt + vec_len (n->last_rx_pkt) - 1) |
| 279 | { |
| 280 | tlv = (cdp_tlv_t *) cur; |
| 281 | tlv->t = ntohs (tlv->t); |
| 282 | tlv->l = ntohs (tlv->l); |
| 283 | if (tlv->t >= ARRAY_LEN (tlv_handlers)) |
| 284 | return CDP_ERROR_BAD_TLV; |
| 285 | handler = &tlv_handlers[tlv->t]; |
| 286 | fp = handler->process; |
| 287 | e = (*fp) (cm, n, tlv); |
| 288 | if (e) |
| 289 | return e; |
| 290 | /* tlv length includes (t, l) */ |
| 291 | cur += tlv->l; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 292 | } |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 293 | |
| 294 | return CDP_ERROR_NONE; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* |
| 298 | * cdp input routine |
| 299 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 300 | cdp_error_t |
| 301 | cdp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 302 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 303 | cdp_main_t *cm = &cdp_main; |
| 304 | cdp_neighbor_t *n; |
| 305 | uword *p, nbytes; |
| 306 | cdp_error_t e; |
| 307 | uword last_packet_signature; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 308 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 309 | /* find or create a neighbor pool entry for the (sw) interface |
| 310 | upon which we received this pkt */ |
| 311 | p = hash_get (cm->neighbor_by_sw_if_index, |
| 312 | vnet_buffer (b0)->sw_if_index[VLIB_RX]); |
| 313 | |
| 314 | if (p == 0) |
| 315 | { |
| 316 | pool_get (cm->neighbors, n); |
| 317 | memset (n, 0, sizeof (*n)); |
| 318 | n->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 319 | n->packet_template_index = (u8) ~ 0; |
| 320 | hash_set (cm->neighbor_by_sw_if_index, n->sw_if_index, |
| 321 | n - cm->neighbors); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | n = pool_elt_at_index (cm->neighbors, p[0]); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 328 | /* |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 329 | * typical clib idiom. Don't repeatedly allocate and free |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 330 | * the per-neighbor rx buffer. Reset its apparent length to zero |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 331 | * and reuse it. |
| 332 | */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 333 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 334 | if (n->last_rx_pkt) |
| 335 | _vec_len (n->last_rx_pkt) = 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 336 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 337 | /* cdp disabled on this interface, we're done */ |
| 338 | if (n->disabled) |
| 339 | return CDP_ERROR_DISABLED; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 340 | |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 341 | /* |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 342 | * Make sure the per-neighbor rx buffer is big enough to hold |
| 343 | * the data we're about to copy |
| 344 | */ |
| 345 | vec_validate (n->last_rx_pkt, vlib_buffer_length_in_chain (vm, b0) - 1); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 346 | |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 347 | /* |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 348 | * Coalesce / copy e the buffer chain into the per-neighbor |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 349 | * rx buffer |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 350 | */ |
| 351 | nbytes = vlib_buffer_contents (vm, bi0, n->last_rx_pkt); |
| 352 | ASSERT (nbytes <= vec_len (n->last_rx_pkt)); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 353 | |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 354 | /* |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 355 | * Compute Jenkins hash of the new packet, decide if we need to |
| 356 | * actually parse through the TLV's. CDP packets are all identical, |
| 357 | * so unless we time out the peer, we don't need to process the packet. |
| 358 | */ |
| 359 | last_packet_signature = |
| 360 | hash_memory (n->last_rx_pkt, vec_len (n->last_rx_pkt), 0xd00b); |
| 361 | |
| 362 | if (n->last_packet_signature_valid && |
| 363 | n->last_packet_signature == last_packet_signature) |
| 364 | { |
| 365 | e = CDP_ERROR_CACHE_HIT; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | /* Actually scan the packet */ |
| 370 | e = cdp_packet_scan (cm, n); |
| 371 | n->last_packet_signature_valid = 1; |
| 372 | n->last_packet_signature = last_packet_signature; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 375 | if (e == CDP_ERROR_NONE) |
| 376 | { |
| 377 | n->last_heard = vlib_time_now (vm); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 378 | } |
| 379 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 380 | return e; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
| 384 | * setup neighbor hash table |
| 385 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 386 | static clib_error_t * |
| 387 | cdp_init (vlib_main_t * vm) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 388 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 389 | clib_error_t *error; |
| 390 | cdp_main_t *cm = &cdp_main; |
| 391 | void vnet_cdp_node_reference (void); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 392 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 393 | vnet_cdp_node_reference (); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 394 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 395 | if ((error = vlib_call_init_function (vm, cdp_periodic_init))) |
| 396 | return error; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 397 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 398 | cm->vlib_main = vm; |
| 399 | cm->vnet_main = vnet_get_main (); |
| 400 | cm->neighbor_by_sw_if_index = hash_create (0, sizeof (uword)); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 401 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 402 | return 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | VLIB_INIT_FUNCTION (cdp_init); |
| 406 | |
| 407 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 408 | static u8 * |
| 409 | format_cdp_neighbors (u8 * s, va_list * va) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 410 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 411 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 412 | cdp_main_t *cm = va_arg (*va, cdp_main_t *); |
| 413 | vnet_main_t *vnm = &vnet_main; |
| 414 | cdp_neighbor_t *n; |
| 415 | vnet_hw_interface_t *hw; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 416 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 417 | s = format (s, |
| 418 | "%=25s %=15s %=25s %=10s\n", |
| 419 | "Our Port", "Peer System", "Peer Port", "Last Heard"); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 420 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 421 | /* *INDENT-OFF* */ |
| 422 | pool_foreach (n, cm->neighbors, |
| 423 | ({ |
| 424 | hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index); |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 425 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 426 | if (n->disabled == 0) |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 427 | s = format (s, "%=25s %=15s %=25s %=10.1f\n", |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 428 | hw->name, n->device_name, n->port_id, |
| 429 | n->last_heard); |
| 430 | })); |
| 431 | /* *INDENT-ON* */ |
| 432 | return s; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | |
| 436 | static clib_error_t * |
| 437 | show_cdp (vlib_main_t * vm, |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 438 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 439 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 440 | cdp_main_t *cm = &cdp_main; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 441 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 442 | vlib_cli_output (vm, "%U\n", format_cdp_neighbors, vm, cm); |
| 443 | |
| 444 | return 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 445 | } |
| 446 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 447 | /* *INDENT-OFF* */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 448 | VLIB_CLI_COMMAND (show_cdp_command, static) = { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 449 | .path = "show cdp", |
| 450 | .short_help = "Show cdp command", |
| 451 | .function = show_cdp, |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 452 | }; |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 453 | /* *INDENT-ON* */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 454 | |
| 455 | |
| 456 | /* |
| 457 | * packet trace format function, very similar to |
| 458 | * cdp_packet_scan except that we call the per TLV format |
| 459 | * functions instead of the per TLV processing functions |
| 460 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 461 | u8 * |
| 462 | cdp_input_format_trace (u8 * s, va_list * args) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 463 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 464 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 465 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 466 | cdp_input_trace_t *t = va_arg (*args, cdp_input_trace_t *); |
| 467 | u8 *cur; |
| 468 | cdp_hdr_t *h; |
| 469 | cdp_tlv_t *tlv; |
| 470 | tlv_handler_t *handler; |
| 471 | u8 *(*fp) (cdp_tlv_t *); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 472 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 473 | cur = t->data; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 474 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 475 | h = (cdp_hdr_t *) cur; |
| 476 | s = format (s, "%U", format_cdp_hdr, h); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 477 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 478 | cur = (u8 *) (h + 1); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 479 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 480 | while (cur < t->data + t->len) |
| 481 | { |
| 482 | tlv = (cdp_tlv_t *) cur; |
| 483 | tlv->t = ntohs (tlv->t); |
| 484 | tlv->l = ntohs (tlv->l); |
| 485 | if (tlv->t >= ARRAY_LEN (tlv_handlers)) |
| 486 | { |
| 487 | s = format (s, "BAD_TLV\n"); |
| 488 | break; |
| 489 | } |
| 490 | handler = &tlv_handlers[tlv->t]; |
| 491 | fp = handler->format; |
| 492 | s = format (s, " %U", fp, tlv); |
| 493 | /* tlv length includes (t, l) */ |
| 494 | cur += tlv->l; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 495 | } |
| 496 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 497 | return s; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 498 | } |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 499 | |
| 500 | /* |
| 501 | * fd.io coding-style-patch-verification: ON |
| 502 | * |
| 503 | * Local Variables: |
| 504 | * eval: (c-set-style "gnu") |
| 505 | * End: |
| 506 | */ |