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 <vppinfra/hash.h> |
| 17 | #include <vnet/unix/pcap.h> |
| 18 | #include <vnet/srp/srp.h> |
| 19 | #include <vnet/ppp/ppp.h> |
| 20 | #include <vnet/hdlc/hdlc.h> |
| 21 | #include <vnet/srp/packet.h> |
| 22 | |
| 23 | /* |
| 24 | * Generate a set of specific CDP TLVs. |
| 25 | * |
| 26 | * $$$ eventually these need to fish better data from |
| 27 | * other data structures; e.g. the hostname, software version info |
| 28 | * etc. |
| 29 | */ |
| 30 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 31 | static void |
| 32 | add_device_name_tlv (vnet_hw_interface_t * hw, u8 ** t0p) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 33 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 34 | cdp_tlv_t *t = (cdp_tlv_t *) * t0p; |
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 | t->t = htons (CDP_TLV_device_name); |
| 37 | t->l = htons (3 + sizeof (*t)); |
| 38 | clib_memcpy (&t->v, "VPP", 3); |
| 39 | |
| 40 | *t0p += ntohs (t->l); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 43 | static void |
| 44 | add_port_id_tlv (vnet_hw_interface_t * hw, u8 ** t0p) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 45 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 46 | cdp_tlv_t *t = (cdp_tlv_t *) * t0p; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 47 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 48 | t->t = htons (CDP_TLV_port_id); |
| 49 | t->l = htons (vec_len (hw->name) + sizeof (*t)); |
| 50 | clib_memcpy (&t->v, hw->name, vec_len (hw->name)); |
| 51 | *t0p += ntohs (t->l); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 54 | static void |
| 55 | add_version_tlv (vnet_hw_interface_t * hw, u8 ** t0p) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 56 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 57 | cdp_tlv_t *t = (cdp_tlv_t *) * t0p; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 58 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 59 | t->t = htons (CDP_TLV_version); |
| 60 | t->l = htons (12 + sizeof (*t)); |
| 61 | clib_memcpy (&t->v, "VPP Software", 12); |
| 62 | *t0p += ntohs (t->l); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 65 | static void |
| 66 | add_platform_tlv (vnet_hw_interface_t * hw, u8 ** t0p) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 67 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 68 | cdp_tlv_t *t = (cdp_tlv_t *) * t0p; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 69 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 70 | t->t = htons (CDP_TLV_platform); |
| 71 | t->l = htons (2 + sizeof (*t)); |
| 72 | clib_memcpy (&t->v, "SW", 2); |
| 73 | *t0p += ntohs (t->l); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 76 | static void |
| 77 | add_capability_tlv (vnet_hw_interface_t * hw, u8 ** t0p) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 78 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 79 | cdp_tlv_t *t = (cdp_tlv_t *) * t0p; |
| 80 | u32 capabilities; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 81 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 82 | t->t = htons (CDP_TLV_capabilities); |
| 83 | t->l = htons (4 + sizeof (*t)); |
| 84 | capabilities = CDP_ROUTER_DEVICE; |
| 85 | capabilities = htonl (capabilities); |
| 86 | clib_memcpy (&t->v, &capabilities, sizeof (capabilities)); |
| 87 | *t0p += ntohs (t->l); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 90 | static void |
| 91 | add_tlvs (cdp_main_t * cm, vnet_hw_interface_t * hw, u8 ** t0p) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 92 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 93 | add_device_name_tlv (hw, t0p); |
| 94 | add_port_id_tlv (hw, t0p); |
| 95 | add_version_tlv (hw, t0p); |
| 96 | add_platform_tlv (hw, t0p); |
| 97 | add_capability_tlv (hw, t0p); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* |
| 101 | * send a cdp pkt on an ethernet interface |
| 102 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 103 | static void |
| 104 | send_ethernet_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 105 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 106 | u32 *to_next; |
| 107 | ethernet_llc_snap_and_cdp_header_t *h0; |
| 108 | vnet_hw_interface_t *hw; |
| 109 | u32 bi0; |
| 110 | vlib_buffer_t *b0; |
| 111 | u8 *t0; |
| 112 | u16 checksum; |
| 113 | int nbytes_to_checksum; |
| 114 | int i; |
| 115 | vlib_frame_t *f; |
| 116 | vlib_main_t *vm = cm->vlib_main; |
| 117 | vnet_main_t *vnm = cm->vnet_main; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 118 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 119 | for (i = 0; i < count; i++) |
| 120 | { |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 121 | /* |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 122 | * see cdp_periodic_init() to understand what's already painted |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 123 | * into the buffer by the packet template mechanism |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 124 | */ |
| 125 | h0 = vlib_packet_template_get_packet |
| 126 | (vm, &cm->packet_templates[n->packet_template_index], &bi0); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 127 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 128 | /* Add the interface's ethernet source address */ |
| 129 | hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 130 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 131 | clib_memcpy (h0->ethernet.src_address, hw->hw_address, |
| 132 | vec_len (hw->hw_address)); |
Dave Barach | f60a822 | 2016-02-08 16:57:13 -0500 | [diff] [blame] | 133 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 134 | t0 = (u8 *) & h0->cdp.data; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 135 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 136 | /* add TLVs */ |
| 137 | add_tlvs (cm, hw, &t0); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 138 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 139 | /* add the cdp packet checksum */ |
| 140 | nbytes_to_checksum = t0 - (u8 *) & h0->cdp; |
| 141 | checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum); |
| 142 | h0->cdp.checksum = htons (checksum); |
| 143 | |
| 144 | /* Set the outbound packet length */ |
| 145 | b0 = vlib_get_buffer (vm, bi0); |
| 146 | b0->current_length = nbytes_to_checksum + sizeof (*h0) |
| 147 | - sizeof (cdp_hdr_t); |
| 148 | |
| 149 | /* And the outbound interface */ |
| 150 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index; |
| 151 | |
| 152 | /* Set the 802.3 ethernet length */ |
| 153 | h0->ethernet.len = htons (b0->current_length |
| 154 | - sizeof (ethernet_802_3_header_t)); |
| 155 | |
| 156 | /* And output the packet on the correct interface */ |
| 157 | f = vlib_get_frame_to_node (vm, hw->output_node_index); |
| 158 | to_next = vlib_frame_vector_args (f); |
| 159 | to_next[0] = bi0; |
| 160 | f->n_vectors = 1; |
| 161 | |
| 162 | vlib_put_frame_to_node (vm, hw->output_node_index, f); |
| 163 | n->last_sent = vlib_time_now (vm); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * send a cdp pkt on an hdlc interface |
| 169 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 170 | static void |
| 171 | send_hdlc_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 172 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 173 | u32 *to_next; |
| 174 | hdlc_and_cdp_header_t *h0; |
| 175 | vnet_hw_interface_t *hw; |
| 176 | u32 bi0; |
| 177 | vlib_buffer_t *b0; |
| 178 | u8 *t0; |
| 179 | u16 checksum; |
| 180 | int nbytes_to_checksum; |
| 181 | int i; |
| 182 | vlib_frame_t *f; |
| 183 | vlib_main_t *vm = cm->vlib_main; |
| 184 | vnet_main_t *vnm = cm->vnet_main; |
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 | for (i = 0; i < count; i++) |
| 187 | { |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 188 | /* |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 189 | * see cdp_periodic_init() to understand what's already painted |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 190 | * into the buffer by the packet template mechanism |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 191 | */ |
| 192 | h0 = vlib_packet_template_get_packet |
| 193 | (vm, &cm->packet_templates[n->packet_template_index], &bi0); |
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 | hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index); |
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 | t0 = (u8 *) & h0->cdp.data; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 198 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 199 | /* add TLVs */ |
| 200 | add_tlvs (cm, hw, &t0); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 201 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 202 | /* add the cdp packet checksum */ |
| 203 | nbytes_to_checksum = t0 - (u8 *) & h0->cdp; |
| 204 | checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum); |
| 205 | h0->cdp.checksum = htons (checksum); |
| 206 | |
| 207 | /* Set the outbound packet length */ |
| 208 | b0 = vlib_get_buffer (vm, bi0); |
| 209 | b0->current_length = nbytes_to_checksum + sizeof (*h0) |
| 210 | - sizeof (cdp_hdr_t); |
| 211 | |
| 212 | /* And output the packet on the correct interface */ |
| 213 | f = vlib_get_frame_to_node (vm, hw->output_node_index); |
| 214 | to_next = vlib_frame_vector_args (f); |
| 215 | to_next[0] = bi0; |
| 216 | f->n_vectors = 1; |
| 217 | |
| 218 | vlib_put_frame_to_node (vm, hw->output_node_index, f); |
| 219 | n->last_sent = vlib_time_now (vm); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * send a cdp pkt on an srp interface |
| 225 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 226 | static void |
| 227 | send_srp_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 228 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 229 | u32 *to_next; |
| 230 | srp_and_cdp_header_t *h0; |
| 231 | vnet_hw_interface_t *hw; |
| 232 | u32 bi0; |
| 233 | vlib_buffer_t *b0; |
| 234 | u8 *t0; |
| 235 | u16 checksum; |
| 236 | int nbytes_to_checksum; |
| 237 | int i; |
| 238 | vlib_frame_t *f; |
| 239 | vlib_main_t *vm = cm->vlib_main; |
| 240 | vnet_main_t *vnm = cm->vnet_main; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 241 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 242 | for (i = 0; i < count; i++) |
| 243 | { |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 244 | /* |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 245 | * see cdp_periodic_init() to understand what's already painted |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 246 | * into the buffer by the packet template mechanism |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 247 | */ |
| 248 | h0 = vlib_packet_template_get_packet |
| 249 | (vm, &cm->packet_templates[n->packet_template_index], &bi0); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 250 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 251 | hw = vnet_get_sup_hw_interface (vnm, n->sw_if_index); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 252 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 253 | t0 = (u8 *) & h0->cdp.data; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 254 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 255 | /* add TLVs */ |
| 256 | add_tlvs (cm, hw, &t0); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 257 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 258 | /* Add the interface's ethernet source address */ |
| 259 | clib_memcpy (h0->ethernet.src_address, hw->hw_address, |
| 260 | vec_len (hw->hw_address)); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 261 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 262 | /* add the cdp packet checksum */ |
| 263 | nbytes_to_checksum = t0 - (u8 *) & h0->cdp; |
| 264 | checksum = cdp_checksum (&h0->cdp, nbytes_to_checksum); |
| 265 | h0->cdp.checksum = htons (checksum); |
| 266 | |
| 267 | /* Set the outbound packet length */ |
| 268 | b0 = vlib_get_buffer (vm, bi0); |
| 269 | b0->current_length = nbytes_to_checksum + sizeof (*h0) |
| 270 | - sizeof (cdp_hdr_t); |
| 271 | |
| 272 | /* And output the packet on the correct interface */ |
| 273 | f = vlib_get_frame_to_node (vm, hw->output_node_index); |
| 274 | to_next = vlib_frame_vector_args (f); |
| 275 | to_next[0] = bi0; |
| 276 | f->n_vectors = 1; |
| 277 | |
| 278 | vlib_put_frame_to_node (vm, hw->output_node_index, f); |
| 279 | n->last_sent = vlib_time_now (vm); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Decide which cdp packet template to use |
| 285 | */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 286 | static int |
| 287 | pick_packet_template (cdp_main_t * cm, cdp_neighbor_t * n) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 288 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 289 | n->packet_template_index = CDP_PACKET_TEMPLATE_ETHERNET; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 290 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 291 | return 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* Send a cdp neighbor announcement */ |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 295 | static void |
| 296 | send_hello (cdp_main_t * cm, cdp_neighbor_t * n, int count) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 297 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 298 | if (n->packet_template_index == (u8) ~ 0) |
| 299 | { |
| 300 | /* If we don't know how to talk to this peer, don't try again */ |
| 301 | if (pick_packet_template (cm, n)) |
| 302 | { |
| 303 | n->last_sent = 1e70; |
| 304 | return; |
| 305 | } |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 306 | } |
| 307 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 308 | switch (n->packet_template_index) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 309 | { |
| 310 | case CDP_PACKET_TEMPLATE_ETHERNET: |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 311 | send_ethernet_hello (cm, n, count); |
| 312 | break; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 313 | |
| 314 | case CDP_PACKET_TEMPLATE_HDLC: |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 315 | send_hdlc_hello (cm, n, count); |
| 316 | break; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 317 | |
| 318 | case CDP_PACKET_TEMPLATE_SRP: |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 319 | send_srp_hello (cm, n, count); |
| 320 | break; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 321 | |
| 322 | default: |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 323 | ASSERT (0); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 324 | } |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 325 | n->last_sent = vlib_time_now (cm->vlib_main); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 328 | static void |
| 329 | delete_neighbor (cdp_main_t * cm, cdp_neighbor_t * n, int want_broadcast) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 330 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 331 | hash_unset (cm->neighbor_by_sw_if_index, n->sw_if_index); |
| 332 | vec_free (n->device_name); |
| 333 | vec_free (n->version); |
| 334 | vec_free (n->port_id); |
| 335 | vec_free (n->platform); |
| 336 | vec_free (n->last_rx_pkt); |
| 337 | pool_put (cm->neighbors, n); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 338 | } |
| 339 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 340 | void |
| 341 | cdp_periodic (vlib_main_t * vm) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 342 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 343 | cdp_main_t *cm = &cdp_main; |
| 344 | cdp_neighbor_t *n; |
| 345 | f64 now = vlib_time_now (vm); |
| 346 | vnet_sw_interface_t *sw; |
| 347 | static u32 *delete_list = 0; |
| 348 | int i; |
| 349 | static cdp_neighbor_t **n_list = 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 350 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 351 | /* *INDENT-OFF* */ |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 352 | pool_foreach (n, cm->neighbors, |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 353 | ({ |
| 354 | vec_add1 (n_list, n); |
| 355 | })); |
| 356 | /* *INDENT-ON* */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 357 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 358 | /* Across all cdp neighbors known to the system */ |
| 359 | for (i = 0; i < vec_len (n_list); i++) |
| 360 | { |
| 361 | n = n_list[i]; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 362 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 363 | /* "no cdp run" provisioned on the interface? */ |
| 364 | if (n->disabled == 1) |
| 365 | continue; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 366 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 367 | sw = vnet_get_sw_interface (cm->vnet_main, n->sw_if_index); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 368 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 369 | /* Interface shutdown or rx timeout? */ |
| 370 | if (!(sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 371 | || (now > (n->last_heard + (f64) n->ttl_in_seconds))) |
| 372 | /* add to list of neighbors to delete */ |
| 373 | vec_add1 (delete_list, n - cm->neighbors); |
| 374 | else if (n->last_sent == 0.0) |
| 375 | /* First time, send 3 hellos */ |
| 376 | send_hello (cm, n, 3 /* three to begin with */ ); |
| 377 | else if (now > (n->last_sent + (((f64) n->ttl_in_seconds) / 6.0))) |
| 378 | /* Normal keepalive, send one */ |
| 379 | send_hello (cm, n, 1 /* one as a keepalive */ ); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 380 | } |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 381 | |
| 382 | for (i = 0; i < vec_len (delete_list); i++) |
| 383 | { |
| 384 | n = vec_elt_at_index (cm->neighbors, delete_list[i]); |
| 385 | delete_neighbor (cm, n, 1); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 386 | } |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 387 | if (delete_list) |
| 388 | _vec_len (delete_list) = 0; |
| 389 | if (n_list) |
| 390 | _vec_len (n_list) = 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 393 | static clib_error_t * |
| 394 | cdp_periodic_init (vlib_main_t * vm) |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 395 | { |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 396 | cdp_main_t *cm = &cdp_main; |
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 | /* Create the ethernet cdp hello packet template */ |
| 399 | { |
| 400 | ethernet_llc_snap_and_cdp_header_t h; |
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 | memset (&h, 0, sizeof (h)); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 403 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 404 | /* Send to 01:00:0c:cc:cc */ |
| 405 | h.ethernet.dst_address[0] = 0x01; |
| 406 | /* h.ethernet.dst_address[1] = 0x00; (memset) */ |
| 407 | h.ethernet.dst_address[2] = 0x0C; |
| 408 | h.ethernet.dst_address[3] = 0xCC; |
| 409 | h.ethernet.dst_address[4] = 0xCC; |
| 410 | h.ethernet.dst_address[5] = 0xCC; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 411 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 412 | /* leave src address blank (fill in at send time) */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 413 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 414 | /* leave length blank (fill in at send time) */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 415 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 416 | /* LLC */ |
| 417 | h.llc.dst_sap = h.llc.src_sap = 0xAA; /* SNAP */ |
| 418 | h.llc.control = 0x03; /* UI (no extended control bytes) */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 419 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 420 | /* SNAP */ |
| 421 | /* h.snap.oui[0] = 0x00; (memset) */ |
| 422 | /* h.snap.oui[1] = 0x00; (memset) */ |
| 423 | h.snap.oui[2] = 0x0C; /* Cisco = 0x00000C */ |
| 424 | h.snap.protocol = htons (0x2000); /* CDP = 0x2000 */ |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 425 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 426 | /* CDP */ |
| 427 | h.cdp.version = 2; |
| 428 | h.cdp.ttl = 180; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 429 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 430 | vlib_packet_template_init |
| 431 | (vm, &cm->packet_templates[CDP_PACKET_TEMPLATE_ETHERNET], |
| 432 | /* data */ &h, |
| 433 | sizeof (h), |
| 434 | /* alloc chunk size */ 8, |
| 435 | "cdp-ethernet"); |
| 436 | } |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 437 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 438 | #if 0 /* retain for reference */ |
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 | /* Create the hdlc cdp hello packet template */ |
| 441 | { |
| 442 | hdlc_and_cdp_header_t h; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 443 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 444 | memset (&h, 0, sizeof (h)); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 445 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 446 | h.hdlc.address = 0x0f; |
| 447 | /* h.hdlc.control = 0; (memset) */ |
| 448 | h.hdlc.protocol = htons (0x2000); /* CDP = 0x2000 */ |
| 449 | |
| 450 | /* CDP */ |
| 451 | h.cdp.version = 2; |
| 452 | h.cdp.ttl = 180; |
| 453 | |
| 454 | vlib_packet_template_init |
| 455 | (vm, &cm->packet_templates[CDP_PACKET_TEMPLATE_HDLC], |
| 456 | /* data */ &h, |
| 457 | sizeof (h), |
| 458 | /* alloc chunk size */ 8, |
| 459 | "cdp-hdlc"); |
| 460 | } |
| 461 | |
| 462 | /* Create the srp cdp hello packet template */ |
| 463 | { |
| 464 | srp_and_cdp_header_t h; |
| 465 | |
| 466 | memset (&h, 0, sizeof (h)); |
| 467 | |
| 468 | /* Send to 01:00:0c:cc:cc */ |
| 469 | h.ethernet.dst_address[0] = 0x01; |
| 470 | /* h.ethernet.dst_address[1] = 0x00; (memset) */ |
| 471 | h.ethernet.dst_address[2] = 0x0C; |
| 472 | h.ethernet.dst_address[3] = 0xCC; |
| 473 | h.ethernet.dst_address[4] = 0xCC; |
| 474 | h.ethernet.dst_address[5] = 0xCC; |
| 475 | |
| 476 | /* leave src address blank (fill in at send time) */ |
| 477 | |
| 478 | /* The srp header is filled in at xmt */ |
| 479 | h.srp.ttl = 1; |
| 480 | h.srp.priority = 7; |
| 481 | h.srp.mode = SRP_MODE_data; |
| 482 | srp_header_compute_parity (&h.srp); |
| 483 | |
| 484 | /* Inner ring and parity will be set at send time */ |
| 485 | |
| 486 | h.ethernet.type = htons (0x2000); /* CDP = 0x2000 */ |
| 487 | |
| 488 | /* CDP */ |
| 489 | h.cdp.version = 2; |
| 490 | h.cdp.ttl = 180; |
| 491 | |
| 492 | vlib_packet_template_init |
| 493 | (vm, &cm->packet_templates[CDP_PACKET_TEMPLATE_SRP], |
| 494 | /* data */ &h, |
| 495 | sizeof (h), |
| 496 | /* alloc chunk size */ 8, |
| 497 | "cdp-srp"); |
| 498 | } |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 499 | #endif |
| 500 | |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 501 | return 0; |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | VLIB_INIT_FUNCTION (cdp_periodic_init); |
Dave Barach | 861bd21 | 2016-08-09 08:55:49 -0400 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * fd.io coding-style-patch-verification: ON |
| 508 | * |
| 509 | * Local Variables: |
| 510 | * eval: (c-set-style "gnu") |
| 511 | * End: |
| 512 | */ |