Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [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 | #include <vlib/vlib.h> |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vnet/pg/pg.h> |
| 18 | #include <vppinfra/error.h> |
| 19 | #include <srv6-localsid/srv6_localsid_sample.h> |
| 20 | |
| 21 | typedef struct { |
| 22 | u32 localsid_index; |
| 23 | } srv6_localsid_sample_trace_t; |
| 24 | |
| 25 | /* packet trace format function */ |
| 26 | static u8 * format_srv6_localsid_sample_trace (u8 * s, va_list * args) |
| 27 | { |
| 28 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 29 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 30 | srv6_localsid_sample_trace_t * t = va_arg (*args, srv6_localsid_sample_trace_t *); |
| 31 | s = format (s, "SRv6-sample-localsid: localsid_index %d\n", |
| 32 | t->localsid_index); |
| 33 | return s; |
| 34 | } |
| 35 | |
| 36 | vlib_node_registration_t srv6_localsid_sample_node; |
| 37 | |
| 38 | #define foreach_srv6_localsid_counter \ |
| 39 | _(PROCESSED, "srv6-sample-localsid processed packets") \ |
| 40 | _(NO_SRH, "(Error) No SRH.") |
| 41 | |
| 42 | typedef enum { |
| 43 | #define _(sym,str) SRV6_LOCALSID_COUNTER_##sym, |
| 44 | foreach_srv6_localsid_counter |
| 45 | #undef _ |
| 46 | SRV6_LOCALSID_N_COUNTERS, |
| 47 | } srv6_localsid_sample_counters; |
| 48 | |
| 49 | static char * srv6_localsid_counter_strings[] = { |
| 50 | #define _(sym,string) string, |
| 51 | foreach_srv6_localsid_counter |
| 52 | #undef _ |
| 53 | }; |
| 54 | |
| 55 | typedef enum { |
| 56 | SRV6_SAMPLE_LOCALSID_NEXT_ERROR, |
| 57 | SRV6_SAMPLE_LOCALSID_NEXT_IP6LOOKUP, |
| 58 | SRV6_SAMPLE_LOCALSID_N_NEXT, |
| 59 | } srv6_localsid_sample_next_t; |
| 60 | |
| 61 | /** |
| 62 | * @brief Function doing End processing. |
| 63 | */ |
| 64 | //Fixme: support OAM (hop-by-hop header) here! |
| 65 | static_always_inline void |
| 66 | end_srh_processing (vlib_node_runtime_t * node, |
| 67 | vlib_buffer_t * b0, |
| 68 | ip6_header_t * ip0, |
| 69 | ip6_sr_header_t * sr0, |
| 70 | u32 * next0) |
| 71 | { |
| 72 | ip6_address_t *new_dst0; |
| 73 | |
| 74 | if(PREDICT_TRUE(ip0->protocol == IP_PROTOCOL_IPV6_ROUTE)) |
| 75 | { |
| 76 | if(PREDICT_TRUE(sr0->type == ROUTING_HEADER_TYPE_SR)) |
| 77 | { |
| 78 | if(PREDICT_TRUE(sr0->segments_left != 0)) |
| 79 | { |
| 80 | sr0->segments_left -= 1; |
| 81 | new_dst0 = (ip6_address_t *)(sr0->segments); |
| 82 | new_dst0 += sr0->segments_left; |
| 83 | ip0->dst_address.as_u64[0] = new_dst0->as_u64[0]; |
| 84 | ip0->dst_address.as_u64[1] = new_dst0->as_u64[1]; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | *next0 = SRV6_SAMPLE_LOCALSID_NEXT_ERROR; |
| 89 | b0->error = node->errors[SRV6_LOCALSID_COUNTER_NO_SRH]; |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | /* Error. Routing header of type != SR */ |
| 95 | *next0 = SRV6_SAMPLE_LOCALSID_NEXT_ERROR; |
| 96 | b0->error = node->errors[SRV6_LOCALSID_COUNTER_NO_SRH]; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * @brief SRv6 Sample Localsid graph node |
| 103 | * WARNING: YOU MUST DO THE DUAL LOOP |
| 104 | */ |
| 105 | static uword |
| 106 | srv6_localsid_sample_fn (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 107 | { |
| 108 | u32 n_left_from, * from, * to_next; |
| 109 | u32 next_index; |
| 110 | u32 pkts_swapped = 0; |
| 111 | |
| 112 | ip6_sr_main_t * sm = &sr_main; |
| 113 | |
| 114 | from = vlib_frame_vector_args (frame); |
| 115 | n_left_from = frame->n_vectors; |
| 116 | next_index = node->cached_next_index; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 117 | u32 thread_index = vlib_get_thread_index (); |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 118 | |
| 119 | while (n_left_from > 0) |
| 120 | { |
| 121 | u32 n_left_to_next; |
| 122 | |
| 123 | vlib_get_next_frame (vm, node, next_index, |
| 124 | to_next, n_left_to_next); |
| 125 | |
| 126 | while (n_left_from > 0 && n_left_to_next > 0) |
| 127 | { |
| 128 | u32 bi0; |
| 129 | vlib_buffer_t * b0; |
| 130 | ip6_header_t * ip0 = 0; |
| 131 | ip6_sr_header_t * sr0; |
| 132 | u32 next0 = SRV6_SAMPLE_LOCALSID_NEXT_IP6LOOKUP; |
| 133 | ip6_sr_localsid_t *ls0; |
| 134 | srv6_localsid_sample_per_sid_memory_t *ls0_mem; |
| 135 | |
| 136 | bi0 = from[0]; |
| 137 | to_next[0] = bi0; |
| 138 | from += 1; |
| 139 | to_next += 1; |
| 140 | n_left_from -= 1; |
| 141 | n_left_to_next -= 1; |
| 142 | |
| 143 | b0 = vlib_get_buffer (vm, bi0); |
| 144 | ip0 = vlib_buffer_get_current (b0); |
| 145 | sr0 = (ip6_sr_header_t *)(ip0+1); |
| 146 | |
| 147 | /* Lookup the SR End behavior based on IP DA (adj) */ |
| 148 | ls0 = pool_elt_at_index (sm->localsids, vnet_buffer(b0)->ip.adj_index[VLIB_TX]); |
| 149 | ls0_mem = ls0->plugin_mem; |
| 150 | |
| 151 | /* SRH processing */ |
| 152 | end_srh_processing (node, b0, ip0, sr0, &next0); |
| 153 | |
| 154 | /* ==================================================================== */ |
| 155 | /* INSERT CODE HERE */ |
| 156 | /* Example starts here */ |
| 157 | //In this example we are changing the next VRF table by the one in CLI |
| 158 | vnet_buffer(b0)->sw_if_index[VLIB_TX] = ls0_mem->fib_table; |
| 159 | /* Example finishes here */ |
| 160 | /* ==================================================================== */ |
| 161 | |
| 162 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 163 | { |
| 164 | srv6_localsid_sample_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 165 | tr->localsid_index = ls0 - sm->localsids; |
| 166 | } |
| 167 | |
| 168 | /* This increments the SRv6 per LocalSID counters.*/ |
| 169 | vlib_increment_combined_counter |
| 170 | (((next0 == SRV6_SAMPLE_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) : &(sm->sr_ls_valid_counters)), |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 171 | thread_index, |
Pablo Camarillo | fb38095 | 2016-12-07 18:34:18 +0100 | [diff] [blame] | 172 | ls0 - sm->localsids, |
| 173 | 1, vlib_buffer_length_in_chain (vm, b0)); |
| 174 | |
| 175 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 176 | n_left_to_next, bi0, next0); |
| 177 | |
| 178 | pkts_swapped ++; |
| 179 | } |
| 180 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 181 | |
| 182 | } |
| 183 | |
| 184 | return frame->n_vectors; |
| 185 | } |
| 186 | |
| 187 | VLIB_REGISTER_NODE (srv6_localsid_sample_node) = { |
| 188 | .function = srv6_localsid_sample_fn, |
| 189 | .name = "srv6-localsid-sample", |
| 190 | .vector_size = sizeof (u32), |
| 191 | .format_trace = format_srv6_localsid_sample_trace, |
| 192 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 193 | .n_errors = SRV6_LOCALSID_N_COUNTERS, |
| 194 | .error_strings = srv6_localsid_counter_strings, |
| 195 | .n_next_nodes = SRV6_SAMPLE_LOCALSID_N_NEXT, |
| 196 | .next_nodes = { |
| 197 | [SRV6_SAMPLE_LOCALSID_NEXT_IP6LOOKUP] = "ip6-lookup", |
| 198 | [SRV6_SAMPLE_LOCALSID_NEXT_ERROR] = "error-drop", |
| 199 | }, |
| 200 | }; |