BIER
[vpp.git] / src / vnet / bier / bier_update.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/vnet.h>
17 #include <vnet/mpls/mpls.h>
18
19 #include <vnet/bier/bier_table.h>
20 #include <vnet/bier/bier_types.h>
21 #include <vnet/bier/bier_update.h>
22
23 clib_error_t *
24 vnet_bier_table_cmd (vlib_main_t * vm,
25                      unformat_input_t * input,
26                      vlib_cli_command_t * cmd)
27 {
28     u32 hdr_len, local_label;
29     clib_error_t * error = 0;
30     bier_table_id_t bti = {
31         .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN,
32     };
33     u32 is_add = 0;
34
35     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
36         if (unformat (input, "del")) {
37             is_add = 0;
38         } else if (unformat (input, "add")) {
39             is_add = 1;
40         } else if (unformat (input, "sd %d", &bti.bti_sub_domain)) {
41         } else if (unformat (input, "set %d", &bti.bti_set)) {
42         } else if (unformat (input, "bsl %d", &hdr_len)) {
43         } else if (unformat (input, "mpls %d", &local_label)) {
44         } else {
45             error = unformat_parse_error (input);
46             goto done;
47         }
48     }
49
50     bti.bti_hdr_len = bier_hdr_bit_len_to_id(hdr_len);
51     // FIXME
52     bti.bti_type = BIER_TABLE_MPLS_SPF;
53
54     if (is_add)
55     {
56         bier_table_add_or_lock(&bti, local_label);
57     }
58     else
59     {
60         bier_table_unlock(&bti);
61     }
62
63 done:
64     return (error);
65 }
66
67 VLIB_CLI_COMMAND (bier_table_command) = {
68   .path = "bier table",
69   .short_help = "Add/delete BIER Tables",
70   .function = vnet_bier_table_cmd,
71 };
72
73 clib_error_t *
74 vnet_bier_route_cmd (vlib_main_t * vm,
75                      unformat_input_t * input,
76                      vlib_cli_command_t * cmd)
77 {
78     clib_error_t * error = NULL;
79     fib_route_path_t brp = {
80         .frp_flags = FIB_ROUTE_PATH_BIER_FMASK,
81     };
82     bier_table_id_t bti = {
83     };
84     mpls_label_t out_label;
85     bier_bp_t bp;
86     u32 hdr_len;
87     u32 add = 1;
88
89     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
90         if (unformat (input, "del")) {
91             add = 0;
92         } else if (unformat (input, "sd %d", &bti.bti_sub_domain)) {
93         } else if (unformat (input, "set %d", &bti.bti_set)) {
94         } else if (unformat (input, "bsl %d", &hdr_len)) {
95         } else if (unformat (input, "bp %d", &bp)) {
96         } else if (unformat (input, "v4-nh %U",
97                              unformat_ip46_address,
98                              &brp.frp_addr, 0)) {
99         } else if (unformat (input, "mpls %d", &out_label)) {
100             vec_add1(brp.frp_label_stack, out_label);
101         } else {
102             error = unformat_parse_error (input);
103             goto done;
104         }
105     }
106
107     bti.bti_hdr_len = bier_hdr_bit_len_to_id(hdr_len);
108     // FIXME
109     bti.bti_type    = BIER_TABLE_MPLS_SPF;
110
111     if (add)
112     {
113         bier_table_route_add(&bti, bp, &brp);
114     }
115     else
116     {
117         bier_table_route_remove(&bti, bp, &brp);
118     }
119
120 done:
121     return (error);
122 }
123
124 VLIB_CLI_COMMAND (bier_route_command) = {
125   .path = "bier route",
126   .short_help = "Add/delete BIER Routes",
127   .function = vnet_bier_route_cmd,
128 };
129
130 static clib_error_t *
131 show_bier_fib_command_fn (vlib_main_t * vm,
132                           unformat_input_t * input,
133                           vlib_cli_command_t * cmd)
134 {
135     bier_show_flags_t flags;
136     index_t bti, bei;
137     bier_bp_t bp;
138
139     bp = BIER_BP_INVALID;
140     bti = bei = INDEX_INVALID;
141
142     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
143         if (unformat (input, "%d %d", &bti, &bp))
144         {
145              flags = BIER_SHOW_DETAIL;
146         }
147         else if (unformat (input, "%d", &bti))
148         {
149               flags = BIER_SHOW_DETAIL;
150         }
151         else
152         {
153             break;
154         }
155     }
156
157     if (INDEX_INVALID == bti)
158     {
159         bier_table_show_all(vm, flags);
160     }
161     else
162     {
163         if (!pool_is_free_index(bier_table_pool, bti))
164         {
165             if (BIER_BP_INVALID == bp)
166             {
167                 vlib_cli_output (vm, "%U", format_bier_table, bti, flags);
168             }
169             else
170             {
171                 vlib_cli_output (vm, "%U", format_bier_table_entry, bti, bp);
172             }
173         }
174     }
175     return (NULL);
176 }
177
178 VLIB_CLI_COMMAND (show_bier_fib_command, static) = {
179     .path = "show bier fib",
180     .short_help = "show bier fib [table-index] [bit-position]",
181     .function = show_bier_fib_command_fn,
182 };