summaryrefslogtreecommitdiffstats
path: root/extras/emacs/plugin-main-skel.el
blob: db1147e75e5035ba724f6c71836ce40475580824 (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
;;; plugin-main-skel.el - vpp engine plug-in "main.c" skeleton
;;;
;;; Copyright (c) 2016 Cisco and/or its affiliates.
;;; 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.

(require 'skeleton)

(define-skeleton skel-plugin-main
"Insert a plug-in 'main.c' skeleton "
nil
'(if (not (boundp 'plugin-name))
     (setq plugin-name (read-string "Plugin name: ")))
'(setq PLUGIN-NAME (upcase plugin-name))
'(setq capital-oh-en "ON")
"/*
 * " plugin-name ".c - skeleton vpp engine plug-in
 *
 * Copyright (c) <current-year> <your-organization>
 * 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 <vnet/vnet.h>
#include <vnet/plugin/plugin.h>
#include <" plugin-name "/" plugin-name ".h>

#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vpp/app/version.h>

/* define message IDs */
#include <" plugin-name "/" plugin-name "_msg_enum.h>

/* define message structures */
#define vl_typedefs
#include <" plugin-name "/" plugin-name "_all_api_h.h>
#undef vl_typedefs

/* define generated endian-swappers */
#define vl_endianfun
#include <" plugin-name "/" plugin-name "_all_api_h.h>
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include <" plugin-name "/" plugin-name "_all_api_h.h>
#undef vl_printfun

/* Get the API version number */
#define vl_api_version(n,v) static u32 api_version=(v);
#include <" plugin-name "/" plugin-name "_all_api_h.h>
#undef vl_api_version

/*
 * A handy macro to set up a message reply.
 * Assumes that the following variables are available:
 * mp - pointer to request message
 * rmp - pointer to reply message type
 * rv - return value
 */

#define REPLY_MACRO(t)                                          \\
do {                                                            \\
    unix_shared_memory_queue_t * q =                            \\
    vl_api_client_index_to_input_queue (mp->client_index);      \\
    if (!q)                                                     \\
        return;                                                 \\
                                                                \\
    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \\
    rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \\
    rmp->context = mp->context;                                 \\
    rmp->retval = ntohl(rv);                                    \\
                                                                \\
    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \\
} while(0);


/* List of message types that this plugin understands */

#define foreach_" plugin-name "_plugin_api_msg                           \\
_(" PLUGIN-NAME "_ENABLE_DISABLE, " plugin-name "_enable_disable)

/* Action function shared between message handler and debug CLI */

int " plugin-name "_enable_disable (" plugin-name "_main_t * sm, u32 sw_if_index,
                                   int enable_disable)
{
  vnet_sw_interface_t * sw;
  int rv = 0;

  /* Utterly wrong? */
  if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
                          sw_if_index))
    return VNET_API_ERROR_INVALID_SW_IF_INDEX;

  /* Not a physical port? */
  sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
  if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
    return VNET_API_ERROR_INVALID_SW_IF_INDEX;

  vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\",
                               sw_if_index, enable_disable, 0, 0);

  return rv;
}

static clib_error_t *
" plugin-name "_enable_disable_command_fn (vlib_main_t * vm,
                                   unformat_input_t * input,
                                   vlib_cli_command_t * cmd)
{
  " plugin-name "_main_t * sm = &" plugin-name "_main;
  u32 sw_if_index = ~0;
  int enable_disable = 1;

  int rv;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
    {
      if (unformat (input, \"disable\"))
        enable_disable = 0;
      else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
                         sm->vnet_main, &sw_if_index))
        ;
      else
        break;
  }

  if (sw_if_index == ~0)
    return clib_error_return (0, \"Please specify an interface...\");

  rv = " plugin-name "_enable_disable (sm, sw_if_index, enable_disable);

  switch(rv) 
    {
  case 0:
    break;

  case VNET_API_ERROR_INVALID_SW_IF_INDEX:
    return clib_error_return
      (0, \"Invalid interface, only works on physical ports\");
    break;

  case VNET_API_ERROR_UNIMPLEMENTED:
    return clib_error_return (0, \"Device driver doesn't support redirection\");
    break;

  default:
    return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\",
                              rv);
    }
  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) = 
{
  .path = \"" plugin-name " enable-disable\",
  .short_help =
  \"" plugin-name " enable-disable <interface-name> [disable]\",
  .function = " plugin-name "_enable_disable_command_fn,
};
/* *INDENT-ON* */

/* API message handler */
static void vl_api_" plugin-name "_enable_disable_t_handler
(vl_api_" plugin-name "_enable_disable_t * mp)
{
  vl_api_" plugin-name "_enable_disable_reply_t * rmp;
  " plugin-name "_main_t * sm = &" plugin-name "_main;
  int rv;

  rv = " plugin-name "_enable_disable (sm, ntohl(mp->sw_if_index),
                                      (int) (mp->enable_disable));

  REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
}

/* Set up the API message handling tables */
static clib_error_t *
" plugin-name "_plugin_api_hookup (vlib_main_t *vm)
{
  " plugin-name "_main_t * sm = &" plugin-name "_main;
#define _(N,n)                                                  \\
    vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \\
                           #n,					\\
                           vl_api_##n##_t_handler,              \\
                           vl_noop_handler,                     \\
                           vl_api_##n##_t_endian,               \\
                           vl_api_##n##_t_print,                \\
                           sizeof(vl_api_##n##_t), 1);
    foreach_" plugin-name "_plugin_api_msg;
#undef _

    return 0;
}

#define vl_msg_name_crc_list
#include <" plugin-name "/" plugin-name "_all_api_h.h>
#undef vl_msg_name_crc_list

static void
setup_message_id_table (" plugin-name "_main_t * sm, api_main_t * am)
{
#define _(id,n,crc) \
  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
  foreach_vl_msg_name_crc_" plugin-name" ;
#undef _
}

static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
{
  " plugin-name "_main_t * sm = &" plugin-name "_main;
  clib_error_t * error = 0;
  u8 * name;

  name = format (0, \"" plugin-name "_%08x%c\", api_version, 0);

  /* Ask for a correctly-sized block of API message decode slots */
  sm->msg_id_base = vl_msg_api_get_msg_ids
      ((char *) name, VL_MSG_FIRST_AVAILABLE);

  error = " plugin-name "_plugin_api_hookup (vm);

  /* Add our API messages to the global name_crc hash table */
  setup_message_id_table (sm, &api_main);

  vec_free(name);

  return error;
}

VLIB_INIT_FUNCTION (" plugin-name "_init);

/* *INDENT-OFF* */
VNET_FEATURE_INIT (" plugin-name ", static) =
{
  .arc_name = \"device-input\",
  .node_name = \"" plugin-name "\",
  .runs_before = VNET_FEATURES (\"ethernet-input\"),
};
/* *INDENT-ON */

/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () = 
{
  .version = VPP_BUILD_VER,
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: " capital-oh-en "
 *
 * Local Variables:
 * eval: (c-set-style \"gnu\")
 * End:
 */
")
class="n">args->error = clib_error_return (0, "interface already exists"); return; } } else { int tries = 1000; while (--tries) { args->id = tm->last_used_interface_id++; p = hash_get (tm->dev_instance_by_interface_id, args->id); if (!p) break; } if (!tries) { args->rv = VNET_API_ERROR_UNSPECIFIED; args->error = clib_error_return (0, "cannot find free interface id"); return; } } memset (&ifr, 0, sizeof (ifr)); pool_get (vim->interfaces, vif); vif->dev_instance = vif - vim->interfaces; vif->tap_fd = -1; vif->id = args->id; hash_set (tm->dev_instance_by_interface_id, vif->id, vif->dev_instance); if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) { args->rv = VNET_API_ERROR_SYSCALL_ERROR_1; args->error = clib_error_return_unix (0, "open '/dev/vhost-net'"); goto error; } _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features); if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0) { args->rv = VNET_API_ERROR_UNSUPPORTED; args->error = clib_error_return (0, "vhost-net backend doesn't support " "VIRTIO_NET_F_MRG_RXBUF feature"); goto error; } if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0) { args->rv = VNET_API_ERROR_UNSUPPORTED; args->error = clib_error_return (0, "vhost-net backend doesn't support " "VIRTIO_RING_F_INDIRECT_DESC feature"); goto error; } if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0) { args->rv = VNET_API_ERROR_UNSUPPORTED; args->error = clib_error_return (0, "vhost-net backend doesn't support " "VIRTIO_F_VERSION_1 features"); goto error; } vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF; vif->features |= 1ULL << VIRTIO_F_VERSION_1; vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC; _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features); if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) { args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); goto error; } ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR; _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr); vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name); unsigned int offload = 0; hdrsz = sizeof (struct virtio_net_hdr_v1); _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); _IOCTL (vif->fd, VHOST_SET_OWNER, 0); /* if namespace is specified, all further netlink messages should be excuted after we change our net namespace */ if (args->host_namespace) { int fd; old_netns_fd = open ("/proc/self/ns/net", O_RDONLY); if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1) { args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; args->error = clib_error_return_unix (0, "open_netns_fd '%s'", args->host_namespace); goto error; } args->error = vnet_netlink_set_link_netns (vif->ifindex, fd, (char *) args->host_if_name); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } if (setns (fd, CLONE_NEWNET) == -1) { args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; args->error = clib_error_return_unix (0, "setns '%s'", args->host_namespace); goto error; } close (fd); if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0) { args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; args->error = clib_error_return_unix (0, "if_nametoindex '%s'", args->host_if_name); goto error; } } else { if (args->host_if_name) { args->error = vnet_netlink_set_link_name (vif->ifindex, (char *) args->host_if_name); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } } } if (!ethernet_mac_address_is_zero (args->host_mac_addr)) { args->error = vnet_netlink_set_link_addr (vif->ifindex, args->host_mac_addr); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } } if (args->host_bridge) { args->error = vnet_netlink_set_link_master (vif->ifindex, (char *) args->host_bridge); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } } if (args->host_ip4_prefix_len) { args->error = vnet_netlink_add_ip4_addr (vif->ifindex, &args->host_ip4_addr, args->host_ip4_prefix_len); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } } if (args->host_ip6_prefix_len) { args->error = vnet_netlink_add_ip6_addr (vif->ifindex, &args->host_ip6_addr, args->host_ip6_prefix_len); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } } args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } if (args->host_ip4_gw_set) { args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } } if (args->host_ip6_gw_set) { args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); if (args->error) { args->rv = VNET_API_ERROR_NETLINK_ERROR; goto error; } } /* switch back to old net namespace */ if (args->host_namespace) { if (setns (old_netns_fd, CLONE_NEWNET) == -1) { args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; args->error = clib_error_return_unix (0, "setns '%s'", args->host_namespace); goto error; } } /* Set vhost memory table */ i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); vhost_mem = clib_mem_alloc (i); memset (vhost_mem, 0, i); vhost_mem->nregions = 1; vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz))) { args->rv = VNET_API_ERROR_INIT_FAILED; goto error; } if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz))) { args->rv = VNET_API_ERROR_INIT_FAILED; goto error; } if (!args->mac_addr_set) { f64 now = vlib_time_now (vm); u32 rnd; rnd = (u32) (now * 1e6); rnd = random_u32 (&rnd); memcpy (args->mac_addr + 2, &rnd, sizeof (rnd)); args->mac_addr[0] = 2; args->mac_addr[1] = 0xfe; } vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256; vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256; vif->host_if_name = args->host_if_name; args->host_if_name = 0; vif->net_ns = args->host_namespace; args->host_namespace = 0; vif->host_bridge = args->host_bridge; args->host_bridge = 0; clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6); vif->host_ip4_prefix_len = args->host_ip4_prefix_len; vif->host_ip6_prefix_len = args->host_ip6_prefix_len; if (args->host_ip4_prefix_len) clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); if (args->host_ip6_prefix_len) clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); args->error = ethernet_register_interface (vnm, virtio_device_class.index, vif->dev_instance, args->mac_addr, &vif->hw_if_index, virtio_eth_flag_change); if (args->error) { args->rv = VNET_API_ERROR_INVALID_REGISTRATION; goto error; } sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); vif->sw_if_index = sw->sw_if_index; args->sw_if_index = vif->sw_if_index; hw = vnet_get_hw_interface (vnm, vif->hw_if_index); hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, virtio_input_node.index); vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, VNET_HW_INTERFACE_RX_MODE_DEFAULT); vif->per_interface_next_index = ~0; vif->type = VIRTIO_IF_TYPE_TAP; vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; vnet_hw_interface_set_flags (vnm, vif->hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP); goto done; error: if (err) { ASSERT (args->error == 0); args->error = err; args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; } if (vif->tap_fd != -1) close (vif->tap_fd); if (vif->fd != -1) close (vif->fd); vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); memset (vif, 0, sizeof (virtio_if_t)); pool_put (vim->interfaces, vif); done: if (vhost_mem) clib_mem_free (vhost_mem); if (old_netns_fd != -1) close (old_netns_fd); } int tap_delete_if (vlib_main_t * vm, u32 sw_if_index) { vnet_main_t *vnm = vnet_get_main (); virtio_main_t *mm = &virtio_main; tap_main_t *tm = &tap_main; int i; virtio_if_t *vif; vnet_hw_interface_t *hw; hw = vnet_get_sup_hw_interface (vnm, sw_if_index); if (hw == NULL || virtio_device_class.index != hw->dev_class_index) return VNET_API_ERROR_INVALID_SW_IF_INDEX; vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); /* bring down the interface */ vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); ethernet_delete_interface (vnm, vif->hw_if_index); vif->hw_if_index = ~0; if (vif->tap_fd != -1) close (vif->tap_fd); if (vif->fd != -1) close (vif->fd); vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); vec_free (vif->vrings); hash_unset (tm->dev_instance_by_interface_id, vif->id); memset (vif, 0, sizeof (*vif)); pool_put (mm->interfaces, vif); return 0; } int tap_dump_ifs (tap_interface_details_t ** out_tapids) { vnet_main_t *vnm = vnet_get_main (); virtio_main_t *mm = &virtio_main; virtio_if_t *vif; vnet_hw_interface_t *hi; tap_interface_details_t *r_tapids = NULL; tap_interface_details_t *tapid = NULL; /* *INDENT-OFF* */ pool_foreach (vif, mm->interfaces, vec_add2(r_tapids, tapid, 1); memset (tapid, 0, sizeof (*tapid)); tapid->id = vif->id; tapid->sw_if_index = vif->sw_if_index; hi = vnet_get_hw_interface (vnm, vif->hw_if_index); clib_memcpy(tapid->dev_name, hi->name, MIN (ARRAY_LEN (tapid->dev_name) - 1, strlen ((const char *) hi->name))); tapid->rx_ring_sz = vif->rx_ring_sz; tapid->tx_ring_sz = vif->tx_ring_sz; clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6); if (vif->host_if_name) { clib_memcpy(tapid->host_if_name, vif->host_if_name, MIN (ARRAY_LEN (tapid->host_if_name) - 1, strlen ((const char *) vif->host_if_name))); } if (vif->net_ns) { clib_memcpy(tapid->host_namespace, vif->net_ns, MIN (ARRAY_LEN (tapid->host_namespace) - 1, strlen ((const char *) vif->net_ns))); } if (vif->host_bridge) { clib_memcpy(tapid->host_bridge, vif->host_bridge, MIN (ARRAY_LEN (tapid->host_bridge) - 1, strlen ((const char *) vif->host_bridge))); } if (vif->host_ip4_prefix_len) clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4); tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; if (vif->host_ip6_prefix_len) clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16); tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; ); /* *INDENT-ON* */ *out_tapids = r_tapids; return 0; } static clib_error_t * tap_init (vlib_main_t * vm) { tap_main_t *tm = &tap_main; tm->dev_instance_by_interface_id = hash_create (0, sizeof (uword)); return 0; } VLIB_INIT_FUNCTION (tap_init); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */