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 <vppinfra/error.h>
18 #include <vnet/ip/ip6.h>
20 #include <vppinfra/hash.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/elog.h>
24 #include <ioam/encap/ip6_ioam_pot.h>
25 #include <ioam/lib-pot/pot_util.h>
27 #define foreach_ip6_hop_by_hop_ioam_pot_stats \
28 _(PROCESSED, "Pkts with ip6 hop-by-hop pot options") \
29 _(PROFILE_MISS, "Pkts with ip6 hop-by-hop pot options but no profile set") \
30 _(PASSED, "Pkts with POT in Policy") \
31 _(FAILED, "Pkts with POT out of Policy")
33 static char * ip6_hop_by_hop_ioam_pot_stats_strings[] = {
34 #define _(sym,string) string,
35 foreach_ip6_hop_by_hop_ioam_pot_stats
40 #define _(sym,str) IP6_IOAM_POT_##sym,
41 foreach_ip6_hop_by_hop_ioam_pot_stats
44 } ip6_ioam_pot_stats_t;
48 u64 counters[ARRAY_LEN(ip6_hop_by_hop_ioam_pot_stats_strings)];
51 vlib_main_t * vlib_main;
52 vnet_main_t * vnet_main;
53 } ip6_hop_by_hop_ioam_pot_main_t;
55 ip6_hop_by_hop_ioam_pot_main_t ip6_hop_by_hop_ioam_pot_main;
58 ip6_ioam_stats_increment_counter (u32 counter_index, u64 increment)
60 ip6_hop_by_hop_ioam_pot_main_t *hm = &ip6_hop_by_hop_ioam_pot_main;
62 hm->counters[counter_index] += increment;
66 static u8 * format_ioam_pot (u8 * s, va_list * args)
68 ioam_pot_option_t * pot0 = va_arg (*args, ioam_pot_option_t *);
69 u64 random, cumulative;
70 random = cumulative = 0;
73 random = clib_net_to_host_u64 (pot0->random);
74 cumulative = clib_net_to_host_u64 (pot0->cumulative);
77 s = format (s, "random = 0x%Lx, Cumulative = 0x%Lx, Index = 0x%x",
78 random, cumulative, pot0 ? pot0->reserved_profile_id : ~0);
83 ip6_hbh_ioam_proof_of_transit_trace_handler (u8 *s, ip6_hop_by_hop_option_t *opt)
85 ioam_pot_option_t *pot;
87 s = format (s, " POT opt present\n");
88 pot = (ioam_pot_option_t *) opt;
89 s = format (s, " %U\n", format_ioam_pot, pot);
94 ip6_hbh_ioam_proof_of_transit_handler (vlib_buffer_t *b,
96 ip6_hop_by_hop_option_t *opt0)
98 ioam_pot_option_t * pot0;
99 u64 random = 0, cumulative = 0;
101 u8 pot_profile_index;
102 pot_profile *pot_profile = 0, *new_profile = 0;
105 pot0 = (ioam_pot_option_t *) opt0;
106 pot_encap = (pot0->random == 0);
107 pot_profile_index = pot_profile_get_active_id();
108 pot_profile = pot_profile_get_active();
109 if (pot_encap && PREDICT_FALSE(!pot_profile))
111 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_PROFILE_MISS, 1);
116 pot0->reserved_profile_id =
117 pot_profile_index & PROFILE_ID_MASK;
118 pot_profile_incr_usage_stats(pot_profile);
121 { /* Non encap node */
122 if (PREDICT_FALSE(pot0->reserved_profile_id !=
123 pot_profile_index || pot_profile == 0))
125 /* New profile announced by encap node. */
127 pot_profile_find(pot0->reserved_profile_id);
128 if (PREDICT_FALSE(new_profile == 0 ||
129 new_profile->valid == 0))
131 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_PROFILE_MISS, 1);
136 pot_profile_index = pot0->reserved_profile_id;
137 pot_profile = new_profile;
138 pot_profile_set_active(pot_profile_index);
139 pot_profile_reset_usage_stats(pot_profile);
142 pot_profile_incr_usage_stats(pot_profile);
145 if (pot0->random == 0)
147 pot0->random = clib_host_to_net_u64(pot_generate_random(pot_profile));
148 pot0->cumulative = 0;
150 random = clib_net_to_host_u64(pot0->random);
151 cumulative = clib_net_to_host_u64(pot0->cumulative);
152 pot0->cumulative = clib_host_to_net_u64(
153 pot_update_cumulative(pot_profile,
156 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_PROCESSED, 1);
162 ip6_hbh_ioam_proof_of_transit_pop_handler (vlib_buffer_t *b, ip6_header_t *ip,
163 ip6_hop_by_hop_option_t *opt0)
165 ioam_pot_option_t * pot0;
169 pot_profile *pot_profile = 0;
172 pot0 = (ioam_pot_option_t *) opt0;
173 random = clib_net_to_host_u64(pot0->random);
174 cumulative = clib_net_to_host_u64(pot0->cumulative);
175 pot_profile = pot_profile_get_active();
176 result = pot_validate (pot_profile,
181 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_PASSED, 1);
185 ip6_ioam_stats_increment_counter (IP6_IOAM_POT_FAILED, 1);
190 int ip6_hop_by_hop_ioam_pot_rewrite_handler (u8 *rewrite_string, u8 *rewrite_size)
192 ioam_pot_option_t * pot_option;
193 if (rewrite_string && *rewrite_size == sizeof(ioam_pot_option_t))
195 pot_option = (ioam_pot_option_t *)rewrite_string;
196 pot_option->hdr.type = HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT
197 | HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
198 pot_option->hdr.length = sizeof (ioam_pot_option_t) -
199 sizeof (ip6_hop_by_hop_option_t);
205 static clib_error_t *
206 ip6_show_ioam_pot_cmd_fn (vlib_main_t * vm,
207 unformat_input_t * input,
208 vlib_cli_command_t * cmd)
210 ip6_hop_by_hop_ioam_pot_main_t *hm = &ip6_hop_by_hop_ioam_pot_main;
214 for ( i = 0; i < IP6_IOAM_POT_N_STATS; i++)
216 s = format(s, " %s - %lu\n", ip6_hop_by_hop_ioam_pot_stats_strings[i],
220 vlib_cli_output(vm, "%v", s);
226 VLIB_CLI_COMMAND (ip6_show_ioam_pot_cmd, static) = {
227 .path = "show ioam pot",
228 .short_help = "iOAM pot statistics",
229 .function = ip6_show_ioam_pot_cmd_fn,
233 static clib_error_t *
234 ip6_hop_by_hop_ioam_pot_init (vlib_main_t * vm)
236 ip6_hop_by_hop_ioam_pot_main_t * hm = &ip6_hop_by_hop_ioam_pot_main;
239 hm->vnet_main = vnet_get_main();
240 clib_memset(hm->counters, 0, sizeof(hm->counters));
242 if (ip6_hbh_register_option(HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT, ip6_hbh_ioam_proof_of_transit_handler,
243 ip6_hbh_ioam_proof_of_transit_trace_handler) < 0)
244 return (clib_error_create("registration of HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT failed"));
246 if (ip6_hbh_add_register_option(HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT,
247 sizeof(ioam_pot_option_t),
248 ip6_hop_by_hop_ioam_pot_rewrite_handler) < 0)
249 return (clib_error_create("registration of HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT for rewrite failed"));
251 if (ip6_hbh_pop_register_option(HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT,
252 ip6_hbh_ioam_proof_of_transit_pop_handler) < 0)
253 return (clib_error_create("registration of HBH_OPTION_TYPE_IOAM_PROOF_OF_TRANSIT POP failed"));
259 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_pot_init) =
261 .runs_after = VLIB_INITS("ip6_hop_by_hop_ioam_init"),