BIER: coverity fixes
[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 *brps = NULL, 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     vec_add1(brps, brp);
108     bti.bti_hdr_len = bier_hdr_bit_len_to_id(hdr_len);
109     // FIXME
110     bti.bti_type    = BIER_TABLE_MPLS_SPF;
111
112     if (add)
113     {
114         bier_table_route_add(&bti, bp, &brp);
115     }
116     else
117     {
118         bier_table_route_remove(&bti, bp, &brp);
119     }
120
121 done:
122     vec_free(brps);
123     return (error);
124 }
125
126 VLIB_CLI_COMMAND (bier_route_command) = {
127   .path = "bier route",
128   .short_help = "Add/delete BIER Routes",
129   .function = vnet_bier_route_cmd,
130 };
131
132 static clib_error_t *
133 show_bier_fib_command_fn (vlib_main_t * vm,
134                           unformat_input_t * input,
135                           vlib_cli_command_t * cmd)
136 {
137     bier_show_flags_t flags;
138     index_t bti, bei;
139     bier_bp_t bp;
140
141     bp = BIER_BP_INVALID;
142     bti = bei = INDEX_INVALID;
143     flags = BIER_SHOW_BRIEF;
144
145     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
146         if (unformat (input, "%d %d", &bti, &bp))
147         {
148              flags = BIER_SHOW_DETAIL;
149         }
150         else if (unformat (input, "%d", &bti))
151         {
152               flags = BIER_SHOW_DETAIL;
153         }
154         else
155         {
156             break;
157         }
158     }
159
160     if (INDEX_INVALID == bti)
161     {
162         bier_table_show_all(vm, flags);
163     }
164     else
165     {
166         if (!pool_is_free_index(bier_table_pool, bti))
167         {
168             if (BIER_BP_INVALID == bp)
169             {
170                 vlib_cli_output (vm, "%U", format_bier_table, bti, flags);
171             }
172             else
173             {
174                 vlib_cli_output (vm, "%U", format_bier_table_entry, bti, bp);
175             }
176         }
177     }
178     return (NULL);
179 }
180
181 VLIB_CLI_COMMAND (show_bier_fib_command, static) = {
182     .path = "show bier fib",
183     .short_help = "show bier fib [table-index] [bit-position]",
184     .function = show_bier_fib_command_fn,
185 };