cli.c revision adff8bfb
1/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17#define _GNU_SOURCE
18#include <fcntl.h>              /* for open */
19#include <sys/ioctl.h>
20#include <sys/socket.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <sys/uio.h>            /* for iovec */
24#include <netinet/in.h>
25
26#include <linux/socket.h>
27#include <linux/if_arp.h>
28#include <linux/if_tun.h>
29
30#include <vlib/vlib.h>
31#include <vnet/vnet.h>
32
33#include <vlib/unix/unix.h>
34#include <vnet/ethernet/ethernet.h>
35
36#if DPDK == 1
37#include <vnet/devices/dpdk/dpdk.h>
38#endif
39
40#include "turbotap.h"
41
42static clib_error_t *
43turbotap_delete_command_fn (vlib_main_t * vm,
44                 unformat_input_t * input,
45                 vlib_cli_command_t * cmd)
46{
47  turbotap_main_t * tr = &turbotap_main;
48  u32 sw_if_index = ~0;
49
50  if (tr->is_disabled)
51    {
52      return clib_error_return (0, "device disabled...");
53    }
54
55  if (unformat (input, "%U", unformat_vnet_sw_interface, tr->vnet_main,
56                &sw_if_index))
57      ;
58  else
59    return clib_error_return (0, "unknown input `%U'",
60                              format_unformat_error, input);
61
62  int rc = vnet_turbotap_delete (vm, sw_if_index);
63
64  if (!rc) {
65    vlib_cli_output (vm, "Deleted.");
66  } else {
67    vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
68  }
69
70  return 0;
71}
72
73VLIB_CLI_COMMAND (turbotap_delete_command, static) = {
74  .path = "turbotap delete",
75  .short_help = "turbotap delete <vpp-tap-intfc-name>",
76  .function = turbotap_delete_command_fn,
77};
78
79static clib_error_t *
80turbotap_connect_command_fn (vlib_main_t * vm,
81                 unformat_input_t * input,
82                 vlib_cli_command_t * cmd)
83{
84  u8 * intfc_name;
85  turbotap_main_t * tr = &turbotap_main;
86  u8 hwaddr[6];
87  u8 *hwaddr_arg = 0;
88  u32 sw_if_index;
89
90  if (tr->is_disabled)
91    {
92      return clib_error_return (0, "device disabled...");
93    }
94
95  if (unformat (input, "%s", &intfc_name))
96    ;
97  else
98    return clib_error_return (0, "unknown input `%U'",
99                              format_unformat_error, input);
100
101  if (unformat(input, "hwaddr %U", unformat_ethernet_address,
102               &hwaddr))
103    hwaddr_arg = hwaddr;
104
105  int rv = vnet_turbotap_connect(vm, intfc_name, hwaddr_arg, &sw_if_index);
106  if (rv) {
107    switch (rv) {
108    case VNET_API_ERROR_SYSCALL_ERROR_1:
109      vlib_cli_output (vm, "Couldn't open /dev/net/turbotap");
110      break;
111
112    case VNET_API_ERROR_SYSCALL_ERROR_2:
113      vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
114      break;
115
116    case VNET_API_ERROR_SYSCALL_ERROR_3:
117      vlib_cli_output (vm, "Couldn't open provisioning socket");
118      break;
119
120    case VNET_API_ERROR_SYSCALL_ERROR_4:
121      vlib_cli_output (vm, "Couldn't get if_index");
122      break;
123
124    case VNET_API_ERROR_SYSCALL_ERROR_5:
125      vlib_cli_output (vm, "Couldn't bind provisioning socket");
126      break;
127
128    case VNET_API_ERROR_SYSCALL_ERROR_6:
129      vlib_cli_output (0, "Couldn't set device non-blocking flag");
130      break;
131
132    case VNET_API_ERROR_SYSCALL_ERROR_7:
133      vlib_cli_output (0, "Couldn't set device MTU");
134      break;
135
136    case VNET_API_ERROR_SYSCALL_ERROR_8:
137      vlib_cli_output (0, "Couldn't get interface flags");
138      break;
139    case VNET_API_ERROR_SYSCALL_ERROR_9:
140      vlib_cli_output (0, "Couldn't set intfc admin state up");
141      break;
142
143    case VNET_API_ERROR_INVALID_REGISTRATION:
144      vlib_cli_output (0, "Invalid registration");
145      break;
146    default:
147      vlib_cli_output (0, "Unknown error: %d", rv);
148      break;
149    }
150    return 0;
151  }
152
153  vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
154  return 0;
155}
156
157VLIB_CLI_COMMAND (turbotap_connect_command, static) = {
158    .path = "turbotap connect",
159    .short_help = "turbotap connect <intfc-name> [hwaddr <addr>]",
160    .function = turbotap_connect_command_fn,
161};
162
163clib_error_t *
164turbotap_cli_init (vlib_main_t * vm)
165{
166  return 0;
167}
168
169VLIB_INIT_FUNCTION (turbotap_cli_init);
170