IP Multicast FIB (mfib)
[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[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[attr]))
151             *eflags |= (1 << attr);
152     }
153
154     return (old == *eflags ? 0 : 1);
155 }
156
157 clib_error_t *
158 mfib_show_route_flags (vlib_main_t * vm,
159                        unformat_input_t * main_input,
160                        vlib_cli_command_t * cmd)
161 {
162     mfib_entry_attribute_t attr;
163
164     FOR_EACH_MFIB_ATTRIBUTE(attr) {
165         vlib_cli_output(vm, "%s = %s",
166                         mfib_flag_names[attr],
167                         mfib_flag_names_long[attr]);
168     }
169
170     return (NULL);
171 }
172
173 /*?
174  * This command display the set of support flags applicable to the MFIB route
175  */
176 /* *INDENT-OFF* */
177 VLIB_CLI_COMMAND (mfib_route_flags_command, static) =
178 {
179   .path = "sh mfib route flags",
180   .short_help = "Flags applicable to an MFIB route",
181   .function = mfib_show_route_flags,
182   .is_mp_safe = 1,
183 };
184 /* *INDENT-ON* */
185
186 clib_error_t *
187 mfib_show_itf_flags (vlib_main_t * vm,
188                      unformat_input_t * main_input,
189                      vlib_cli_command_t * cmd)
190 {
191     mfib_itf_attribute_t attr;
192
193     FOR_EACH_MFIB_ITF_ATTRIBUTE(attr) {
194         vlib_cli_output(vm, "%s = %s",
195                         mfib_itf_flag_names[attr],
196                         mfib_itf_flag_long_names[attr]);
197     }
198
199     return (NULL);
200 }
201
202 /*?
203  * This command display the set of support flags applicable to the MFIB route
204  */
205 /* *INDENT-OFF* */
206 VLIB_CLI_COMMAND (mfib_itf_flags_command, static) =
207 {
208   .path = "sh mfib itf flags",
209   .short_help = "Flags applicable to an MFIB interfaces",
210   .function = mfib_show_itf_flags,
211   .is_mp_safe = 1,
212 };
213 /* *INDENT-ON* */