aboutsummaryrefslogtreecommitdiffstats
path: root/src/scvpp/src/sc_vpp_interface.c
blob: 73b29ae5c163715bb976ca358db00e50f6891c21 (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
/*
 * 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 <assert.h>
#include <stdbool.h>

#include <vapi/l2.api.vapi.h>

#include "sc_vpp_comm.h"
#include "sc_vpp_interface.h"


// Use VAPI macros to define symbols
DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON
DEFINE_VAPI_MSG_IDS_L2_API_JSON;

void sw_interface_details_query_set_name(sw_interface_details_query_t * query,
                                         const char * interface_name)
{
    assert(query && interface_name);

    memset(query, 0, sizeof(*query));

    strncpy((char*) query->sw_interface_details.interface_name, interface_name,
            sizeof(query->sw_interface_details.interface_name));
}

static vapi_error_e
sw_interface_dump_cb(struct vapi_ctx_s *ctx, void *callback_ctx,
                     vapi_error_e rv, bool is_last,
                     vapi_payload_sw_interface_details *reply)
{
    vapi_payload_sw_interface_details *passed;

    ARG_CHECK(-EINVAL, callback_ctx);

    passed = (vapi_payload_sw_interface_details *) callback_ctx;

    //Interface is found if index of query equals index of reply
    if (passed->sw_if_index != reply->sw_if_index)
        return -EINVAL;

    //copy
    *passed = *reply;

    return VAPI_OK;
}

static vapi_error_e
bin_api_sw_interface_dump(vapi_payload_sw_interface_details *details)
{
    vapi_msg_sw_interface_dump *mp;
    vapi_error_e rv;

    mp = vapi_alloc_sw_interface_dump(g_vapi_ctx_instance);

    mp->payload.name_filter_valid = 0;
    memset(mp->payload.name_filter, 0, sizeof(mp->payload.name_filter));
    assert(NULL != mp);

    VAPI_CALL(vapi_sw_interface_dump(g_vapi_ctx_instance, mp, sw_interface_dump_cb, details));
    if (!rv)
        return -EINVAL;

    return rv;
}

static vapi_error_e
interface_dump_all_cb(struct vapi_ctx_s *ctx, void *callback_ctx,
                          vapi_error_e rv, bool is_last,
                          vapi_payload_sw_interface_details * reply)
{
    dump_all_ctx *dctx = callback_ctx;

    if (is_last)
        return VAPI_OK;

    if(dctx->capacity == 0 && dctx->intfcArray == NULL) {
        dctx->capacity = 10;
        dctx->intfcArray = (vpp_interface_t*)malloc( sizeof(vpp_interface_t)*dctx->capacity );
    }
    if(dctx->num_ifs >= dctx->capacity-1) {
        dctx->capacity += 10;
        dctx->intfcArray = (vpp_interface_t*)realloc(dctx->intfcArray, sizeof(vpp_interface_t)*dctx->capacity );
    }

    vpp_interface_t * iface = &dctx->intfcArray[dctx->num_ifs];

    iface->sw_if_index = reply->sw_if_index;
    strncpy(iface->interface_name, reply->interface_name, VPP_INTFC_NAME_LEN);
    iface->l2_address_length = reply->l2_address_length;
    memcpy(iface->l2_address, reply->l2_address, reply->l2_address_length );
    iface->link_speed = reply->link_speed;

    iface->link_mtu = reply->link_mtu;
    iface->admin_up_down = reply->admin_up_down;
    iface->link_up_down = reply->link_up_down;

    dctx->num_ifs += 1;

    return VAPI_OK;
}

int interface_dump_all(dump_all_ctx * dctx)
{
    vapi_msg_sw_interface_dump *dump;
    vapi_error_e rv;

    ARG_CHECK(-1, dctx);

    if(dctx == NULL)
      return -1;

    dctx->intfcArray = NULL;
    dctx->capacity = 0;
    dctx->num_ifs = 0;

    dump = vapi_alloc_sw_interface_dump(g_vapi_ctx_instance);

    dump->payload.name_filter_valid = 0;
    memset(dump->payload.name_filter, 0, sizeof(dump->payload.name_filter));
    while (VAPI_EAGAIN ==
           (rv =
            vapi_sw_interface_dump(g_vapi_ctx_instance, dump, interface_dump_all_cb,
                                   dctx)));

    return dctx->num_ifs;
}

static vapi_error_e
get_interface_id_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
                      vapi_error_e rv, bool is_last,
                      vapi_payload_sw_interface_details * reply)
{
    sw_interface_details_query_t *dctx = callback_ctx;
    assert(dctx);

    if (!dctx->interface_found)
    {
        if (is_last)
        {
            assert(NULL == reply);
        }
        else
        {
            assert(NULL != reply);

            if (0 == strcmp((const char*)dctx->sw_interface_details.interface_name,
                            (const char*)reply->interface_name))
            {
                dctx->interface_found = true;
                dctx->sw_interface_details = *reply;
            }
        }
    }

    return VAPI_OK;
}

// return error code instead of boolean
int get_interface_id(sw_interface_details_query_t * sw_interface_details_query)
{
    vapi_error_e rv;

    ARG_CHECK(false, sw_interface_details_query);

    sw_interface_details_query->interface_found = false;

    vapi_msg_sw_interface_dump *mp = vapi_alloc_sw_interface_dump (g_vapi_ctx_instance);
    assert(NULL != mp);

    mp->payload.name_filter_valid = true;
    memcpy(mp->payload.name_filter, sw_interface_details_query->sw_interface_details.interface_name,
           sizeof(mp->payload.name_filter));

    VAPI_CALL(vapi_sw_interface_dump(g_vapi_ctx_instance, mp, get_interface_id_cb, sw_interface_details_query));
    if (VAPI_OK != rv)
        return false;

    return sw_interface_details_query->interface_found;
}

int get_interface_name(sw_interface_details_query_t *query)
{
    vapi_error_e rv;

    ARG_CHECK(-EINVAL, query);

    query->interface_found = false;

    rv = bin_api_sw_interface_dump(&query->sw_interface_details);
    if (rv == VAPI_OK)
        query->interface_found = true;

    return query->interface_found;
}

VAPI_RETVAL_CB(sw_interface_set_flags);

static vapi_error_e
bin_api_sw_interface_set_flags(uint32_t if_index, uint8_t up)
{
    vapi_msg_sw_interface_set_flags *mp = vapi_alloc_sw_interface_set_flags (g_vapi_ctx_instance);
    assert(NULL != mp);

    mp->payload.sw_if_index = if_index;
    mp->payload.admin_up_down = up;

    vapi_error_e rv;
    VAPI_CALL(vapi_sw_interface_set_flags(g_vapi_ctx_instance, mp, sw_interface_set_flags_cb, NULL));

    return rv;
}

VAPI_RETVAL_CB(sw_interface_set_l2_bridge);

//set interface l2 bridge <interface> <bridge-domain-id> [bvi|uu-fwd] [shg]
static vapi_error_e
bin_api_sw_interface_set_l2_bridge(u32 bd_id, u32 rx_sw_if_index, bool enable)
{
    vapi_msg_sw_interface_set_l2_bridge *mp;
    vapi_error_e rv;

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

    mp->payload.enable = enable;
    mp->payload.bd_id = bd_id;
    mp->payload.rx_sw_if_index = rx_sw_if_index;

    VAPI_CALL(vapi_sw_interface_set_l2_bridge (g_vapi_ctx_instance, mp,
                                        sw_interface_set_l2_bridge_cb, NULL));

    return rv;
}

int interface_enable(const char *interface_name, const bool enable)
{
    ARG_CHECK(-1, interface_name);

    int rc = 0;
    sw_interface_details_query_t query = {0};
    sw_interface_details_query_set_name(&query, interface_name);

    rc = get_interface_id(&query);
    if (!rc)
        return -1;

    rc = bin_api_sw_interface_set_flags(query.sw_interface_details.sw_if_index,
                                        enable);
    if (VAPI_OK != rc)
        return -1;

    return 0;
}