2 * Copyright (c) 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
20 #include <vnet/ip/ip6.h>
21 #include <vnet/ip/ip6_hop_by_hop.h>
22 #include <vnet/ip/ip6_hop_by_hop_packet.h>
24 #include <vppinfra/hash.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/elog.h>
28 #include <ioam/lib-pot/pot_util.h>
30 typedef CLIB_PACKED(struct {
31 ip6_hop_by_hop_option_t hdr;
33 #define PROFILE_ID_MASK 0xF
34 u8 reserved_profile_id; /* 4 bits reserved, 4 bits to carry profile id */
39 #define foreach_ip6_hop_by_hop_ioam_pot_stats \
40 _(PROCESSED, "Pkts with ip6 hop-by-hop pot options") \
41 _(PROFILE_MISS, "Pkts with ip6 hop-by-hop pot options but no profile set") \
42 _(PASSED, "Pkts with POT in Policy") \
43 _(FAILED, "Pkts with POT out of Policy")
45 static char * ip6_hop_by_hop_ioam_pot_stats_strings[] = {
46 #define _(sym,string) string,
47 foreach_ip6_hop_by_hop_ioam_pot_stats
52 #define _(sym,str) IP6_IOAM_POT_##sym,
53 foreach_ip6_hop_by_hop_ioam_pot_stats
56 } ip6_ioam_pot_stats_t;
60 u64 counters[ARRAY_LEN(ip6_hop_by_hop_ioam_pot_stats_strings)];
63 vlib_main_t * vlib_main;
64 vnet_main_t * vnet_main;
65 } ip6_hop_by_hop_ioam_pot_main_t;
67 ip6_hop_by_hop_ioam_pot_main_t ip6_hop_by_hop_ioam_pot_main;
70 ip6_ioam_stats_increment_counter (u32 counter_index, u64 increment)
72 ip6_hop_by_hop_ioam_pot_main_t *hm = &ip6_hop_by_hop_ioam_pot_main;
74 hm->counters[counter_index] += increment;
78 static u8 * format_ioam_pot (u8 * s, va_list * args)
80 ioam_pot_option_t * pot0 = va_arg (*args, ioam_pot_option_t *);
81 u64 random, cumulative;
82 random = cumulative = 0;
85 random = clib_net_to_host_u64 (pot0->random);
86 cumulative = clib_net_to_host_u64 (pot0->cumulative);
89 s = format (s, "random = 0x%Lx, Cumulative = 0x%Lx, Index = 0x%x",
90 random, cumulative, pot0 ? pot0->reserved_profile_id : ~0);
95 ip6_hbh_ioam_proof_of_transit_trace_handler (u8 *s, ip6_hop_by_hop_option_t *opt)
97 ioam_pot_option_t *pot;
99 s = format (s, " POT opt present\n");
100 pot = (ioam_pot_option_t *) opt;
101 s = format (s, " %U\n", format_ioam_pot, pot);
106 ip6_hbh_ioam_proof_of_transit_handler (vlib_buffer_t *b,
108 ip6_hop_by_hop_option_t *opt0)
110 ioam_pot_option_t * pot0;
111 u64 random = 0, cumulative = 0;
113 u8 pot_profile_index;
114 pot_profile *pot_profile = 0, *new_profile = 0;
117 pot0 = (ioam_pot_option_t *) opt0;
118 pot_encap = (pot0->random == 0);
119 pot_profile_index = pot_profile_get_active_id();
120 pot_profile = pot_profile_get_active();
121 if (pot_encap && PREDICT_FALSE(!pot_profile))
123 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_PROFILE_MISS, 1);
128 pot0->reserved_profile_id =
129 pot_profile_index & PROFILE_ID_MASK;
130 pot_profile_incr_usage_stats(pot_profile);
133 { /* Non encap node */
134 if (PREDICT_FALSE(pot0->reserved_profile_id !=
135 pot_profile_index || pot_profile == 0))
137 /* New profile announced by encap node. */
139 pot_profile_find(pot0->reserved_profile_id);
140 if (PREDICT_FALSE(new_profile == 0 ||
141 new_profile->valid == 0))
143 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_PROFILE_MISS, 1);
148 pot_profile_index = pot0->reserved_profile_id;
149 pot_profile = new_profile;
150 pot_profile_set_active(pot_profile_index);
151 pot_profile_reset_usage_stats(pot_profile);
154 pot_profile_incr_usage_stats(pot_profile);
157 if (pot0->random == 0)
159 pot0->random = clib_host_to_net_u64(pot_generate_random(pot_profile));
160 pot0->cumulative = 0;
162 random = clib_net_to_host_u64(pot0->random);
163 cumulative = clib_net_to_host_u64(pot0->cumulative);
164 pot0->cumulative = clib_host_to_net_u64(
165 pot_update_cumulative(pot_profile,
168 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_PROCESSED, 1);
174 ip6_hbh_ioam_proof_of_transit_pop_handler (ip6_header_t *ip,
175 ip6_hop_by_hop_option_t *opt0)
177 ioam_pot_option_t * pot0;
181 pot_profile *pot_profile = 0;
184 pot0 = (ioam_pot_option_t *) opt0;
185 random = clib_net_to_host_u64(pot0->random);
186 cumulative = clib_net_to_host_u64(pot0->cumulative);
187 pot_profile = pot_profile_get_active();
188 result = pot_validate (pot_profile,
193 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_PASSED, 1);
197 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_FAILED, 1);
202 int ip6_hop_by_hop_ioam_pot_rewrite_handler (u8 *rewrite_string, u8 rewrite_size)
204 ioam_pot_option_t * pot_option;
205 if (rewrite_string && rewrite_size == sizeof(ioam_pot_option_t))
207 pot_option = (ioam_pot_option_t *)rewrite_string;
208 pot_option->hdr.type = HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT
209 | HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
210 pot_option->hdr.length = sizeof (ioam_pot_option_t) -
211 sizeof (ip6_hop_by_hop_option_t);
217 static clib_error_t *
218 ip6_show_ioam_pot_cmd_fn (vlib_main_t * vm,
219 unformat_input_t * input,
220 vlib_cli_command_t * cmd)
222 ip6_hop_by_hop_ioam_pot_main_t *hm = &ip6_hop_by_hop_ioam_pot_main;
226 for ( i = 0; i < IP6_IOAM_POT_N_STATS; i++)
228 s = format(s, " %s - %lu\n", ip6_hop_by_hop_ioam_pot_stats_strings[i],
232 vlib_cli_output(vm, "%v", s);
238 VLIB_CLI_COMMAND (ip6_show_ioam_pot_cmd, static) = {
239 .path = "show ioam pot",
240 .short_help = "iOAM pot statistics",
241 .function = ip6_show_ioam_pot_cmd_fn,
245 static clib_error_t *
246 ip6_hop_by_hop_ioam_pot_init (vlib_main_t * vm)
248 ip6_hop_by_hop_ioam_pot_main_t * hm = &ip6_hop_by_hop_ioam_pot_main;
249 clib_error_t * error;
251 if ((error = vlib_call_init_function (vm, ip_main_init)))
254 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
257 if ((error = vlib_call_init_function (vm, ip6_hop_by_hop_ioam_init)))
261 hm->vnet_main = vnet_get_main();
262 memset(hm->counters, 0, sizeof(hm->counters));
264 if (ip6_hbh_register_option(HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT, ip6_hbh_ioam_proof_of_transit_handler,
265 ip6_hbh_ioam_proof_of_transit_trace_handler) < 0)
266 return (clib_error_create("registration of HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT failed"));
268 if (ip6_hbh_add_register_option(HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT,
269 sizeof(ioam_pot_option_t),
270 ip6_hop_by_hop_ioam_pot_rewrite_handler) < 0)
271 return (clib_error_create("registration of HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT for rewrite failed"));
273 if (ip6_hbh_pop_register_option(HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT,
274 ip6_hbh_ioam_proof_of_transit_pop_handler) < 0)
275 return (clib_error_create("registration of HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT POP failed"));
280 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_pot_init);