MFIB; CLI improvements
[vpp.git] / src / vnet / mfib / mfib_types.c
1  /*
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:
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 <vnet/mfib/mfib_types.h>
17
18 #include <vnet/ip/ip.h>
19
20 /**
21  * String names for each flag
22  */
23 static const char *mfib_flag_names[] = MFIB_ENTRY_NAMES_SHORT;
24 static const char *mfib_flag_names_long[] = MFIB_ENTRY_NAMES_LONG;
25
26 static const char *mfib_itf_flag_long_names[] = MFIB_ITF_NAMES_LONG;
27 static const char *mfib_itf_flag_names[] = MFIB_ITF_NAMES_SHORT;
28
29 u8 *
30 format_mfib_prefix (u8 * s, va_list * args)
31 {
32     mfib_prefix_t *fp = va_arg (*args, mfib_prefix_t *);
33
34     /*
35      * protocol specific so it prints ::/0 correctly.
36      */
37     switch (fp->fp_proto)
38     {
39     case FIB_PROTOCOL_IP6:
40     {
41         ip6_address_t p6 = fp->fp_grp_addr.ip6;
42         u32 len = (fp->fp_len > 128 ? 128 : fp->fp_len);
43
44         ip6_address_mask(&p6, &(ip6_main.fib_masks[len]));
45
46         if (ip6_address_is_zero(&fp->fp_src_addr.ip6))
47         {
48             s = format(s, "(*, ");
49         }
50         else
51         {
52             s = format (s, "(%U, ", format_ip6_address, &fp->fp_src_addr.ip6);
53         }
54         s = format (s, "%U", format_ip6_address, &p6);
55         s = format (s, "/%d)", len);
56         break;
57     }
58     case FIB_PROTOCOL_IP4:
59     {
60         ip4_address_t p4 = fp->fp_grp_addr.ip4;
61         u32 len = (fp->fp_len > 32 ? 32 : fp->fp_len);
62
63         p4.as_u32 &= ip4_main.fib_masks[len];
64
65         if (0 == fp->fp_src_addr.ip4.as_u32)
66         {
67             s = format(s, "(*, ");
68         }
69         else
70         {
71             s = format (s, "(%U, ", format_ip4_address, &fp->fp_src_addr.ip4);
72         }
73         s = format (s, "%U", format_ip4_address, &p4);
74         s = format (s, "/%d)", len);
75         break;
76     }
77     case FIB_PROTOCOL_MPLS:
78         break;
79     }
80
81     return (s);
82 }
83
84 u8 *
85 format_mfib_entry_flags (u8 * s, va_list * args)
86 {
87     mfib_entry_attribute_t attr;
88     mfib_entry_flags_t flags;
89
90     flags = va_arg (*args, mfib_entry_flags_t);
91
92     if (MFIB_ENTRY_FLAG_NONE != flags) {
93         s = format(s, " flags:");
94         FOR_EACH_MFIB_ATTRIBUTE(attr) {
95             if ((1<<attr) & flags) {
96                 s = format (s, "%s,", mfib_flag_names_long[attr]);
97             }
98         }
99     }
100
101     return (s);
102 }
103
104 u8 *
105 format_mfib_itf_flags (u8 * s, va_list * args)
106 {
107     mfib_itf_attribute_t attr;
108     mfib_itf_flags_t flags;
109
110     flags = va_arg (*args, mfib_itf_flags_t);
111
112     FOR_EACH_MFIB_ITF_ATTRIBUTE(attr) {
113         if ((1<<attr) & flags) {
114             s = format (s, "%s,", mfib_itf_flag_long_names[attr]);
115         }
116     }
117
118     return (s);
119 }
120
121 uword
122 unformat_mfib_itf_flags (unformat_input_t * input,
123                          va_list * args)
124 {
125     mfib_itf_flags_t old, *iflags = va_arg (*args, mfib_itf_flags_t*);
126     mfib_itf_attribute_t attr;
127
128     old = *iflags;
129     FOR_EACH_MFIB_ITF_ATTRIBUTE(attr) {
130         if (unformat (input, mfib_itf_flag_long_names[attr]))
131             *iflags |= (1 << attr);
132     }
133     FOR_EACH_MFIB_ITF_ATTRIBUTE(attr) {
134         if (unformat (input, mfib_itf_flag_names[attr]))
135             *iflags |= (1 << attr);
136     }
137
138     return (old == *iflags ? 0 : 1);
139 }
140
141 uword
142 unformat_mfib_entry_flags (unformat_input_t * input,
143                            va_list * args)
144 {
145     mfib_entry_flags_t old, *eflags = va_arg (*args, mfib_entry_flags_t*);
146     mfib_entry_attribute_t attr;
147
148     old = *eflags;
149     FOR_EACH_MFIB_ATTRIBUTE(attr) {
150         if (unformat (input, mfib_flag_names_long[attr]))
151             *eflags |= (1 << attr);
152     }
153     FOR_EACH_MFIB_ATTRIBUTE(attr) {
154         if (unformat (input, mfib_flag_names[attr]))
155             *eflags |= (1 << attr);
156     }
157
158     return (old == *eflags ? 0 : 1);
159 }
160
161 clib_error_t *
162 mfib_show_route_flags (vlib_main_t * vm,
163                        unformat_input_t * main_input,
164                        vlib_cli_command_t * cmd)
165 {
166     mfib_entry_attribute_t attr;
167
168     FOR_EACH_MFIB_ATTRIBUTE(attr) {
169         vlib_cli_output(vm, "%s = %s",
170                         mfib_flag_names[attr],
171                         mfib_flag_names_long[attr]);
172     }
173
174     return (NULL);
175 }
176
177 /*?
178  * This command displays the set of supported flags applicable to an MFIB route
179  */
180 /* *INDENT-OFF* */
181 VLIB_CLI_COMMAND (mfib_route_flags_command, static) =
182 {
183   .path = "show mfib route flags",
184   .short_help = "Flags applicable to an MFIB route",
185   .function = mfib_show_route_flags,
186   .is_mp_safe = 1,
187 };
188 /* *INDENT-ON* */
189
190 clib_error_t *
191 mfib_show_itf_flags (vlib_main_t * vm,
192                      unformat_input_t * main_input,
193                      vlib_cli_command_t * cmd)
194 {
195     mfib_itf_attribute_t attr;
196
197     FOR_EACH_MFIB_ITF_ATTRIBUTE(attr) {
198         vlib_cli_output(vm, "%s = %s",
199                         mfib_itf_flag_names[attr],
200                         mfib_itf_flag_long_names[attr]);
201     }
202
203     return (NULL);
204 }
205
206 /*?
207  * This command displays the set of supported flags applicable to an MFIB interface
208  */
209 /* *INDENT-OFF* */
210 VLIB_CLI_COMMAND (mfib_itf_flags_command, static) =
211 {
212   .path = "show mfib itf flags",
213   .short_help = "Flags applicable to an MFIB interfaces",
214   .function = mfib_show_itf_flags,
215   .is_mp_safe = 1,
216 };
217 /* *INDENT-ON* */