aboutsummaryrefslogtreecommitdiffstats
path: root/src/scvpp/src/sc_vpp_ip.c
blob: 77e79fb36dab220c94c27b95f237c77163936b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2018 PANTHEON.tech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sc_vpp_comm.h"
#include "sc_vpp_ip.h"

#include "sc_vpp_interface.h"

#include <assert.h>

// Use VAPI macros to define symbols
DEFINE_VAPI_MSG_IDS_IP_API_JSON

VAPI_RETVAL_CB(sw_interface_add_del_address);

static vapi_error_e
bin_api_sw_interface_add_del_address(u32 sw_if_index, bool is_add, bool is_ipv6,
                                     bool del_all, u8 address_length,
                                     const char *ip_address)
{
    vapi_msg_sw_interface_add_del_address *mp;
    vapi_error_e rv;

    ARG_CHECK(VAPI_EINVAL, ip_address);

    mp = vapi_alloc_sw_interface_add_del_address(g_vapi_ctx_instance);
    assert(NULL != mp);

    mp->payload.sw_if_index = sw_if_index;
    mp->payload.is_add = is_add;
    mp->payload.is_ipv6 = is_ipv6;
    mp->payload.del_all = del_all;
    mp->payload.address_length = address_length;
    if (sc_aton(ip_address, mp->payload.address, sizeof(mp->payload.address)))
        return VAPI_EINVAL;

    VAPI_CALL(vapi_sw_interface_add_del_address(g_vapi_ctx_instance, mp,
                                        sw_interface_add_del_address_cb, NULL));

    return rv;
}

static vapi_error_e
bin_api_sw_interface_del_all_address(u32 sw_if_index)
{
    vapi_msg_sw_interface_add_del_address *mp;
    vapi_error_e rv;

    mp = vapi_alloc_sw_interface_add_del_address(g_vapi_ctx_instance);
    assert(NULL != mp);

    mp->payload.sw_if_index = sw_if_index;
    mp->payload.is_add = 0;
    mp->payload.del_all = 1;

    VAPI_CALL(vapi_sw_interface_add_del_address(g_vapi_ctx_instance, mp,
                                        sw_interface_add_del_address_cb, NULL));

    return rv;
}

VAPI_COPY_CB(ip_add_del_route)

static vapi_error_e
bin_api_ip_add_del_route(vapi_payload_ip_add_del_route_reply * reply,
                         const char* dst_address, uint8_t dst_address_length,
                         const char* next_hop, uint8_t is_add,
                         uint32_t table_id, const char *interface_name)
{
    sw_interface_details_query_t query = {0};
    vapi_msg_ip_add_del_route *mp;

    ARG_CHECK4(VAPI_EINVAL, reply, dst_address, next_hop, interface_name);

    sw_interface_details_query_set_name(&query, interface_name);

    if (!get_interface_id(&query))
        return VAPI_EINVAL;

    mp = vapi_alloc_ip_add_del_route (g_vapi_ctx_instance, 1);
    assert(NULL != mp);

    //ip route add 2.2.2.2/24 via 5.5.5.5
    //show ip fib table 0 2.2.2.0/24 detail

    mp->payload.is_add = is_add;
    mp->payload.dst_address_length = dst_address_length;
    mp->payload.table_id = table_id;
    mp->payload.next_hop_sw_if_index = query.sw_interface_details.sw_if_index;

    if (sc_aton(dst_address, mp->payload.dst_address,
        sizeof(mp->payload.dst_address)))
        return VAPI_EINVAL;
    if (sc_aton(next_hop, mp->payload.next_hop_address,
        sizeof(mp->payload.next_hop_address)))
        return VAPI_EINVAL;

    vapi_error_e rv ;
    VAPI_CALL(vapi_ip_add_del_route(g_vapi_ctx_instance, mp, ip_add_del_route_cb, reply));

    return rv;
}

static vapi_error_e
ip_fib_dump_cb(struct vapi_ctx_s *ctx, void *callback_ctx, vapi_error_e rv,
               bool is_last, vapi_payload_ip_fib_details * reply)
{
    if (is_last)
    {
        assert (NULL == reply);
    }
    else
    {
        assert (NULL != reply);

        /*
        typedef struct __attribute__ ((__packed__)) {
            u32 table_id;
            u8 table_name[64];
            u8 address_length;
            u8 address[4];
            u32 count;
            u32 stats_index;
            vapi_type_fib_path path[0];
        } vapi_payload_ip_fib_details;
        */

        for (u32 i = 0; i < reply->count; ++i)
        {
            /*
            typedef struct __attribute__((__packed__)) {
                u32 sw_if_index;
                u32 table_id;
                u8 weight;
                u8 preference;
                u8 is_local;
                u8 is_drop;
                u8 is_udp_encap;
                u8 is_unreach;
                u8 is_prohibit;
                u8 is_resolve_host;
                u8 is_resolve_attached;
                u8 is_dvr;
                u8 is_source_lookup;
                u8 afi;
                u8 next_hop[16];
                u32 next_hop_id;
                u32 rpf_id;
                u32 via_label;
                u8 n_labels;
                vapi_type_fib_mpls_label label_stack[16];
            } vapi_type_fib_path;

   │493         case FIB_PATH_TYPE_ATTACHED_NEXT_HOP:                                                                                                                                                                                        │
b+>│494             s = format (s, "%U", format_ip46_address,                                                                                                                                                                                │
   │495                         &path->attached_next_hop.fp_nh,                                                                                                                                                                              │
   │496                         IP46_TYPE_ANY);

            */

        }
    }

    return VAPI_OK;
}

static vapi_error_e
bin_api_ip_fib_dump()
{
    vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx_instance);
    assert(NULL != mp);

    //ip route add 2.2.2.2/24 via 5.5.5.5
    //show ip fib table 0 2.2.2.0/24 detail

    vapi_error_e rv;
    VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx_instance, mp, ip_fib_dump_cb, NULL));

    return rv;
}

VAPI_RETVAL_CB(ip_table_add_del)

static vapi_error_e
bin_api_table_add_del(u8 is_add, u32 table_id)
{
    vapi_msg_ip_table_add_del *mp = vapi_alloc_ip_table_add_del(g_vapi_ctx_instance);
    assert(NULL != mp);

    mp->payload.is_add = is_add;
    mp->payload.table_id = table_id;

    vapi_error_e rv;
    VAPI_CALL(vapi_ip_table_add_del(g_vapi_ctx_instance, mp, ip_table_add_del_cb, NULL));

    return rv;
}

static vapi_error_e
ip_address_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, vapi_error_e rv,
                    bool is_last, vapi_payload_ip_address_details *reply)
{
    ARG_CHECK3(VAPI_EINVAL, ctx, callback_ctx, reply);

    //copy dump reply in callback context
    if (!is_last && callback_ctx) {
        vapi_payload_ip_address_details *passed =
            (vapi_payload_ip_address_details *) callback_ctx;
        *passed = *reply;
    }

    return VAPI_OK;
}

static vapi_error_e
bin_api_ip_address_dump(u32 sw_if_index, bool is_ipv6,
                        vapi_payload_ip_address_details *dctx)
{
    vapi_msg_ip_address_dump *mp;
    vapi_error_e rv;

    mp = vapi_alloc_ip_address_dump(g_vapi_ctx_instance);
    mp->payload.sw_if_index = sw_if_index;
    mp->payload.is_ipv6 = is_ipv6;

    VAPI_CALL(vapi_ip_address_dump(g_vapi_ctx_instance, mp, ip_address_dump_cb,
                                   dctx));
    if (rv != VAPI_OK)
        return rv;

    return VAPI_OK;
}

/*
 * @brief Dump IPv4/IPv6 address from an interface.
 * @param interface_name Name of the interface to dump.
 * @param ip_addr pointer where dump will store IP.
 * @param prefix_len pointer where dump will store prefix
 */
int ipv46_address_dump(const char *interface_name, char *ip_addr,
                       u8 *prefix_len, bool is_ipv6)
{
    vapi_payload_ip_address_details dctx = {0};
    sw_interface_details_query_t query = {0};
    vapi_error_e rv;

    sw_interface_details_query_set_name(&query, interface_name);

    if (!get_interface_id(&query))
        return -EINVAL;

    rv = bin_api_ip_address_dump(query.sw_interface_details.sw_if_index, false, &dctx);
    if (rv != VAPI_OK)
        return -EAGAIN;

    strcpy(ip_addr, sc_ntoa(dctx.ip)); //IP string
    *prefix_len = dctx.prefix_length; //prefix length

    return 0;
}

/**
 * @brief Add or remove IPv4/IPv6 address to/from an interface.
 */
int ipv46_config_add_remove(const char *if_name, const char *addr,
                            uint8_t prefix, bool is_ipv6, bool add)
{
    vapi_error_e rv;
    int rc;

    ARG_CHECK2(-1, if_name, addr);

    /* get interface index */
    sw_interface_details_query_t query = {0};
    sw_interface_details_query_set_name(&query, if_name);
    rc = get_interface_id(&query);
    if (!rc)
        return -EINVAL;

    /* add del addr */
    rv = bin_api_sw_interface_add_del_address(query.sw_interface_details.sw_if_index,
                                              add, is_ipv6, 0, prefix, addr);
    if (rv != VAPI_OK)
        return -EINVAL;

    return 0;
}

/*
 * @brief Add or remove an IP route
 */
int ipv46_config_add_del_route(const char* dst_address, u8 dst_address_length,
                               const char* next_address, u8 is_add,
                               u32 table_id, const char *interface)
{
    vapi_payload_ip_add_del_route_reply reply = {0};
    vapi_error_e rv;

    rv = bin_api_ip_add_del_route(&reply, dst_address, dst_address_length,
                                  next_address, is_add, table_id, interface);
    if (VAPI_OK != rv || reply.retval > 0) {
        return -EINVAL;
    }

    return 0;
}