Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | |
| 16 | #include <vlib/vlib.h> |
| 17 | #include <vnet/vnet.h> |
| 18 | #include <vppinfra/error.h> |
| 19 | |
| 20 | #include <vnet/lawful-intercept/lawful_intercept.h> |
| 21 | |
| 22 | #include <vppinfra/error.h> |
| 23 | #include <vppinfra/elog.h> |
| 24 | |
Filip Tehlar | c3a0e8d | 2019-03-11 05:53:35 -0700 | [diff] [blame^] | 25 | extern vlib_node_registration_t li_hit_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 27 | typedef struct |
| 28 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | u32 next_index; |
| 30 | } li_hit_trace_t; |
| 31 | |
| 32 | /* packet trace format function */ |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 33 | static u8 * |
| 34 | format_li_hit_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | { |
| 36 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 37 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 38 | li_hit_trace_t *t = va_arg (*args, li_hit_trace_t *); |
| 39 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | s = format (s, "LI_HIT: next index %d", t->next_index); |
| 41 | |
| 42 | return s; |
| 43 | } |
| 44 | |
Dave Barach | 26cd8c1 | 2017-02-23 17:11:26 -0500 | [diff] [blame] | 45 | #define foreach_li_hit_error \ |
| 46 | _(HITS, "LI packets processed") \ |
| 47 | _(NO_COLLECTOR, "No collector configured") \ |
| 48 | _(BUFFER_ALLOCATION_FAILURE, "Buffer allocation failure") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 50 | typedef enum |
| 51 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | #define _(sym,str) LI_HIT_ERROR_##sym, |
| 53 | foreach_li_hit_error |
| 54 | #undef _ |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 55 | LI_HIT_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | } li_hit_error_t; |
| 57 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 58 | static char *li_hit_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | #define _(sym,string) string, |
| 60 | foreach_li_hit_error |
| 61 | #undef _ |
| 62 | }; |
| 63 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 64 | typedef enum |
| 65 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | LI_HIT_NEXT_ETHERNET, |
| 67 | LI_HIT_N_NEXT, |
| 68 | } li_hit_next_t; |
| 69 | |
Filip Tehlar | c3a0e8d | 2019-03-11 05:53:35 -0700 | [diff] [blame^] | 70 | VLIB_NODE_FN (li_hit_node) (vlib_main_t * vm, |
| 71 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | { |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 73 | u32 n_left_from, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | li_hit_next_t next_index; |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 75 | vlib_frame_t *int_frame = 0; |
| 76 | u32 *to_int_next = 0; |
| 77 | li_main_t *lm = &li_main; |
| 78 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | from = vlib_frame_vector_args (frame); |
| 80 | n_left_from = frame->n_vectors; |
| 81 | next_index = node->cached_next_index; |
| 82 | |
| 83 | if (PREDICT_FALSE (vec_len (lm->collectors) == 0)) |
| 84 | { |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 85 | vlib_node_increment_counter (vm, li_hit_node.index, |
| 86 | LI_HIT_ERROR_NO_COLLECTOR, n_left_from); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
| 90 | /* The intercept frame... */ |
| 91 | int_frame = vlib_get_frame_to_node (vm, ip4_lookup_node.index); |
| 92 | to_int_next = vlib_frame_vector_args (int_frame); |
| 93 | } |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 94 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | while (n_left_from > 0) |
| 96 | { |
| 97 | u32 n_left_to_next; |
| 98 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 99 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
| 101 | #if 0 |
| 102 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 103 | { |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 104 | u32 next0 = LI_HIT_NEXT_INTERFACE_OUTPUT; |
| 105 | u32 next1 = LI_HIT_NEXT_INTERFACE_OUTPUT; |
| 106 | u32 sw_if_index0, sw_if_index1; |
| 107 | u8 tmp0[6], tmp1[6]; |
| 108 | ethernet_header_t *en0, *en1; |
| 109 | u32 bi0, bi1; |
| 110 | vlib_buffer_t *b0, *b1; |
| 111 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | /* Prefetch next iteration. */ |
| 113 | { |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 114 | vlib_buffer_t *p2, *p3; |
| 115 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | p2 = vlib_get_buffer (vm, from[2]); |
| 117 | p3 = vlib_get_buffer (vm, from[3]); |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 118 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | vlib_prefetch_buffer_header (p2, LOAD); |
| 120 | vlib_prefetch_buffer_header (p3, LOAD); |
| 121 | |
| 122 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 123 | CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 124 | } |
| 125 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 126 | /* speculatively enqueue b0 and b1 to the current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | to_next[0] = bi0 = from[0]; |
| 128 | to_next[1] = bi1 = from[1]; |
| 129 | from += 2; |
| 130 | to_next += 2; |
| 131 | n_left_from -= 2; |
| 132 | n_left_to_next -= 2; |
| 133 | |
| 134 | b0 = vlib_get_buffer (vm, bi0); |
| 135 | b1 = vlib_get_buffer (vm, bi1); |
| 136 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 137 | /* $$$$$ Dual loop: process 2 x packets here $$$$$ */ |
| 138 | ASSERT (b0->current_data == 0); |
| 139 | ASSERT (b1->current_data == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 141 | en0 = vlib_buffer_get_current (b0); |
| 142 | en1 = vlib_buffer_get_current (b1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 144 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 145 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 147 | /* Send pkt back out the RX interface */ |
| 148 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 149 | vnet_buffer (b1)->sw_if_index[VLIB_TX] = sw_if_index1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 151 | /* $$$$$ End of processing 2 x packets $$$$$ */ |
| 152 | |
| 153 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 154 | { |
| 155 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 156 | { |
| 157 | li_hit_trace_t *t = |
| 158 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 159 | t->sw_if_index = sw_if_index0; |
| 160 | t->next_index = next0; |
| 161 | } |
| 162 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 163 | { |
| 164 | li_hit_trace_t *t = |
| 165 | vlib_add_trace (vm, node, b1, sizeof (*t)); |
| 166 | t->sw_if_index = sw_if_index1; |
| 167 | t->next_index = next1; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* verify speculative enqueues, maybe switch current next frame */ |
| 172 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 173 | to_next, n_left_to_next, |
| 174 | bi0, bi1, next0, next1); |
| 175 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | #endif /* $$$ dual-loop off */ |
| 177 | |
| 178 | while (n_left_from > 0 && n_left_to_next > 0) |
| 179 | { |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 180 | u32 bi0; |
| 181 | vlib_buffer_t *b0; |
| 182 | vlib_buffer_t *c0; |
| 183 | ip4_udp_header_t *iu0; |
| 184 | ip4_header_t *ip0; |
| 185 | udp_header_t *udp0; |
| 186 | u32 next0 = LI_HIT_NEXT_ETHERNET; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 188 | /* speculatively enqueue b0 to the current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | bi0 = from[0]; |
| 190 | to_next[0] = bi0; |
| 191 | from += 1; |
| 192 | to_next += 1; |
| 193 | n_left_from -= 1; |
| 194 | n_left_to_next -= 1; |
| 195 | |
| 196 | b0 = vlib_get_buffer (vm, bi0); |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 197 | if (PREDICT_TRUE (to_int_next != 0)) |
| 198 | { |
| 199 | /* Make an intercept copy. This can fail. */ |
| 200 | c0 = vlib_buffer_copy (vm, b0); |
Dave Barach | 26cd8c1 | 2017-02-23 17:11:26 -0500 | [diff] [blame] | 201 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 202 | if (PREDICT_FALSE (c0 == 0)) |
| 203 | { |
| 204 | vlib_node_increment_counter |
| 205 | (vm, node->node_index, |
| 206 | LI_HIT_ERROR_BUFFER_ALLOCATION_FAILURE, 1); |
| 207 | goto skip; |
| 208 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 210 | vlib_buffer_advance (c0, -sizeof (*iu0)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 212 | iu0 = vlib_buffer_get_current (c0); |
| 213 | ip0 = &iu0->ip4; |
| 214 | |
| 215 | ip0->ip_version_and_header_length = 0x45; |
| 216 | ip0->ttl = 254; |
| 217 | ip0->protocol = IP_PROTOCOL_UDP; |
| 218 | |
| 219 | ip0->src_address.as_u32 = lm->src_addrs[0].as_u32; |
| 220 | ip0->dst_address.as_u32 = lm->collectors[0].as_u32; |
| 221 | ip0->length = vlib_buffer_length_in_chain (vm, c0); |
| 222 | ip0->checksum = ip4_header_checksum (ip0); |
| 223 | |
| 224 | udp0 = &iu0->udp; |
| 225 | udp0->src_port = udp0->dst_port = |
| 226 | clib_host_to_net_u16 (lm->ports[0]); |
| 227 | udp0->checksum = 0; |
| 228 | udp0->length = |
| 229 | clib_net_to_host_u16 (vlib_buffer_length_in_chain (vm, b0)); |
| 230 | |
| 231 | to_int_next[0] = vlib_get_buffer_index (vm, c0); |
| 232 | to_int_next++; |
| 233 | } |
| 234 | |
| 235 | skip: |
| 236 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 237 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 238 | { |
| 239 | li_hit_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 240 | t->next_index = next0; |
| 241 | } |
| 242 | |
| 243 | /* verify speculative enqueue, maybe switch current next frame */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 245 | to_next, n_left_to_next, |
| 246 | bi0, next0); |
| 247 | } |
| 248 | |
| 249 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 250 | } |
| 251 | |
| 252 | if (int_frame) |
| 253 | { |
| 254 | int_frame->n_vectors = frame->n_vectors; |
| 255 | vlib_put_frame_to_node (vm, ip4_lookup_node.index, int_frame); |
| 256 | } |
| 257 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 258 | vlib_node_increment_counter (vm, li_hit_node.index, |
| 259 | LI_HIT_ERROR_HITS, frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | return frame->n_vectors; |
| 261 | } |
| 262 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 263 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | VLIB_REGISTER_NODE (li_hit_node) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | .name = "li-hit", |
| 266 | .vector_size = sizeof (u32), |
| 267 | .format_trace = format_li_hit_trace, |
| 268 | .type = VLIB_NODE_TYPE_INTERNAL, |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 269 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | .n_errors = ARRAY_LEN(li_hit_error_strings), |
| 271 | .error_strings = li_hit_error_strings, |
| 272 | |
| 273 | .n_next_nodes = LI_HIT_N_NEXT, |
| 274 | |
| 275 | /* edit / add dispositions here */ |
| 276 | .next_nodes = { |
| 277 | [LI_HIT_NEXT_ETHERNET] = "ethernet-input-not-l2", |
| 278 | }, |
| 279 | }; |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 280 | /* *INDENT-ON* */ |
Dave Barach | bfdedbd | 2016-01-20 09:11:55 -0500 | [diff] [blame] | 281 | |
satish.karunanithi | 93fbcc6 | 2017-12-06 16:11:59 +0530 | [diff] [blame] | 282 | /* |
| 283 | * fd.io coding-style-patch-verification: ON |
| 284 | * |
| 285 | * Local Variables: |
| 286 | * eval: (c-set-style "gnu") |
| 287 | * End: |
| 288 | */ |