summaryrefslogtreecommitdiffstats
path: root/src/vnet/devices/af_packet/cli.c
blob: 44dc517911b7f43f97da5c51a3ba041cdcb11f01 (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
/*
 *------------------------------------------------------------------
 * af_packet.c - linux kernel packet interface
 *
 * 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.
 *------------------------------------------------------------------
 */

#include <fcntl.h>		/* for open */
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>		/* for iovec */
#include <netinet/in.h>

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>

#include <vnet/devices/af_packet/af_packet.h>

/**
 * @file
 * @brief CLI for Host Interface Device Driver.
 *
 * This file contains the source code for CLI for the host interface.
 */

static clib_error_t *
af_packet_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
			     vlib_cli_command_t * cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  u8 *host_if_name = NULL;
  u8 hwaddr[6];
  u8 *hw_addr_ptr = 0;
  u32 sw_if_index;
  int r;
  clib_error_t *error = NULL;

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "name %s", &host_if_name))
	;
      else
	if (unformat
	    (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr))
	hw_addr_ptr = hwaddr;
      else
	{
	  error = clib_error_return (0, "unknown input `%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

  if (host_if_name == NULL)
    {
      error = clib_error_return (0, "missing host interface name");
      goto done;
    }

  r = af_packet_create_if (vm, host_if_name, hw_addr_ptr, &sw_if_index);

  if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
    {
      error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
      goto done;
    }

  if (r == VNET_API_ERROR_INVALID_INTERFACE)
    {
      error = clib_error_return (0, "Invalid interface name");
      goto done;
    }

  if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
    {
      error = clib_error_return (0, "Interface elready exists");
      goto done;
    }

  vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
		   sw_if_index);

done:
  vec_free (host_if_name);
  unformat_free (line_input);

  return error;
}

/*?
 * Create a host interface that will attach to a linux AF_PACKET
 * interface, one side of a veth pair. The veth pair must already
 * exist. Once created, a new host interface will exist in VPP
 * with the name '<em>host-<ifname></em>', where '<em><ifname></em>'
 * is the name of the specified veth pair. Use the
 * '<em>show interface</em>' command to display host interface details.
 *
 * This command has the following optional parameters:
 *
 * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either
 * X:X:X:X:X:X unix or X.X.X cisco format.
 *
 * @cliexpar
 * Example of how to create a host interface tied to one side of an
 * existing linux veth pair named vpp1:
 * @cliexstart{create host-interface name vpp1}
 * host-vpp1
 * @cliexend
 * Once the host interface is created, enable the interface using:
 * @cliexcmd{set interface state host-vpp1 up}
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (af_packet_create_command, static) = {
  .path = "create host-interface",
  .short_help = "create host-interface name <ifname> [hw-addr <mac-addr>]",
  .function = af_packet_create_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
			     vlib_cli_command_t * cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  u8 *host_if_name = NULL;
  clib_error_t *error = NULL;

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "name %s", &host_if_name))
	;
      else
	{
	  error = clib_error_return (0, "unknown input `%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

  if (host_if_name == NULL)
    {
      error = clib_error_return (0, "missing host interface name");
      goto done;
    }

  af_packet_delete_if (vm, host_if_name);

done:
  vec_free (host_if_name);
  unformat_free (line_input);

  return error;
}

/*?
 * Delete a host interface. Use the linux interface name to identify
 * the host interface to be deleted. In VPP, host interfaces are
 * named as '<em>host-<ifname></em>', where '<em><ifname></em>'
 * is the name of the linux interface.
 *
 * @cliexpar
 * Example of how to delete a host interface named host-vpp1:
 * @cliexcmd{delete host-interface name vpp1}
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
  .path = "delete host-interface",
  .short_help = "delete host-interface name <ifname>",
  .function = af_packet_delete_command_fn,
};
/* *INDENT-ON* */

clib_error_t *
af_packet_cli_init (vlib_main_t * vm)
{
  return 0;
}

VLIB_INIT_FUNCTION (af_packet_cli_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
REPORT_ADDRESS; break; case IGMP_MSG_QUERY: if (group != NULL) clib_memcpy_fast (&ip4->dst_address, &group->key->ip4, sizeof (ip4_address_t)); else ip4->dst_address.as_u32 = IGMP_GENERAL_QUERY_ADDRESS; break; } /* add the router alert options */ option = vlib_buffer_get_current (b); option[0] = 0x80 | 20; // IP4_ROUTER_ALERT_OPTION; option[1] = 4; // length option[2] = option[3] = 0; vlib_buffer_append (b, 4); bk->n_avail -= 4; return (b); } static vlib_buffer_t * igmp_pkt_build_report_v3 (igmp_pkt_build_report_t * br, const igmp_group_t * group) { igmp_membership_report_v3_t *report; vlib_buffer_t *b; b = igmp_pkt_build_ip_header (&br->base, IGMP_MSG_REPORT, group); if (NULL == b) return (NULL); report = vlib_buffer_get_current (b); report->header.type = IGMP_TYPE_membership_report_v3; report->header.code = 0; report->header.checksum = 0; report->unused = 0; vlib_buffer_append (b, sizeof (igmp_membership_report_v3_t)); br->base.n_avail -= sizeof (igmp_membership_report_v3_t); br->base.n_bytes += sizeof (igmp_membership_report_v3_t); return (b); } static void igmp_pkt_tx (igmp_pkt_build_t * bk) { const igmp_config_t *config; vlib_buffer_t *b; vlib_main_t *vm; vlib_frame_t *f; u32 *to_next; u32 ii; vm = vlib_get_main (); config = igmp_config_lookup (bk->sw_if_index); if (NULL == config) return; f = vlib_get_frame_to_node (vm, ip4_rewrite_mcast_node.index); to_next = vlib_frame_vector_args (f); vec_foreach_index (ii, bk->buffers) { b = vlib_get_buffer (vm, bk->buffers[ii]); vnet_buffer (b)->ip.adj_index[VLIB_TX] = config->adj_index; to_next[ii] = bk->buffers[ii]; f->n_vectors++; } vlib_put_frame_to_node (vm, ip4_rewrite_mcast_node.index, f); IGMP_DBG (" ..tx: %U", format_vnet_sw_if_index_name, vnet_get_main (), bk->sw_if_index); vec_free (bk->buffers); bk->buffers = 0; } static vlib_buffer_t * igmp_pkt_build_report_get_active (igmp_pkt_build_report_t * br) { if (NULL == br->base.buffers) return (NULL); return (vlib_get_buffer (vlib_get_main (), br->base.buffers[vec_len (br->base.buffers) - 1])); } static void igmp_pkt_build_report_bake (igmp_pkt_build_report_t * br) { igmp_membership_report_v3_t *igmp; ip4_header_t *ip4; vlib_buffer_t *b; b = igmp_pkt_build_report_get_active (br); b->current_data = 0; ip4 = vlib_buffer_get_current (b); igmp = (igmp_membership_report_v3_t *) (((u32 *) ip4) + 6); igmp->n_groups = clib_host_to_net_u16 (br->n_groups); igmp->header.checksum = ~ip_csum_fold (ip_incremental_checksum (0, igmp, br->base.n_bytes)); ip4->length = clib_host_to_net_u16 (b->current_length); ip4->checksum = ip4_header_checksum (ip4); br->base.n_bytes = br->base.n_avail = br->n_groups = 0; } void igmp_pkt_report_v3_send (igmp_pkt_build_report_t * br) { if (NULL == br->base.buffers) return; igmp_pkt_build_report_bake (br); igmp_pkt_tx (&br->base); } static u32 igmp_pkt_report_v3_get_size (const igmp_group_t * group) { ASSERT (IGMP_FILTER_MODE_INCLUDE == group->router_filter_mode); return ((hash_elts (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE]) * sizeof (ip4_address_t)) + sizeof (igmp_membership_group_v3_t)); } static igmp_membership_group_v3_t * igmp_pkt_report_v3_append_group (igmp_pkt_build_report_t * br, const ip46_address_t * grp, igmp_membership_group_v3_type_t type) { igmp_membership_group_v3_t *igmp_group; vlib_buffer_t *b; b = igmp_pkt_build_report_get_active (br); if (br->base.n_avail < sizeof (igmp_membership_group_v3_t)) { igmp_pkt_build_report_bake (br); b = igmp_pkt_build_report_v3 (br, NULL); if (NULL == b) return (NULL); } br->base.n_avail -= sizeof (igmp_membership_group_v3_t); br->base.n_bytes += sizeof (igmp_membership_group_v3_t); br->n_groups++; br->n_srcs = 0; igmp_group = vlib_buffer_get_current (b); vlib_buffer_append (b, sizeof (igmp_membership_group_v3_t)); igmp_group->type = type; igmp_group->n_aux_u32s = 0; igmp_group->n_src_addresses = 0; igmp_group->group_address.as_u32 = grp->ip4.as_u32; return (igmp_group); } /** * 4.2.16 " If the set of Group Records required in a Report does not fit within * the size limit of a single Report message (as determined by the MTU * of the network on which it will be sent), the Group Records are sent * in as many Report messages as needed to report the entire set. * If a single Group Record contains so many source addresses that it * does not fit within the size limit of a single Report message, if its * Type is not MODE_IS_EXCLUDE or CHANGE_TO_EXCLUDE_MODE, it is split * into multiple Group Records, each containing a different subset of * the source addresses and each sent in a separate Report message. If * its Type is MODE_IS_EXCLUDE or CHANGE_TO_EXCLUDE_MODE, a single Group * Record is sent, containing as many source addresses as can fit, and * the remaining source addresses are not reported; though the choice of * which sources to report is arbitrary, it is preferable to report the * same set of sources in each subsequent report, rather than reporting * different sources each time." */ static igmp_membership_group_v3_t * igmp_pkt_report_v3_append_src (igmp_pkt_build_report_t * br, igmp_membership_group_v3_t * igmp_group, const ip46_address_t * grp, igmp_membership_group_v3_type_t type, const ip46_address_t * src) { vlib_buffer_t *b; b = igmp_pkt_build_report_get_active (br); if (br->base.n_avail < sizeof (ip4_address_t)) { igmp_group->n_src_addresses = clib_host_to_net_u16 (br->n_srcs); igmp_pkt_build_report_bake (br); b = igmp_pkt_build_report_v3 (br, NULL); if (NULL == b) return (NULL); igmp_group = igmp_pkt_report_v3_append_group (br, grp, type); } igmp_group->src_addresses[br->n_srcs].as_u32 = src->ip4.as_u32; br->n_srcs++; br->base.n_avail -= sizeof (ip4_address_t); br->base.n_bytes += sizeof (ip4_address_t); vlib_buffer_append (b, sizeof (ip4_address_t)); return (igmp_group); } void igmp_pkt_report_v3_add_report (igmp_pkt_build_report_t * br, const ip46_address_t * grp, const ip46_address_t * srcs, igmp_membership_group_v3_type_t type) { igmp_membership_group_v3_t *igmp_group; const ip46_address_t *s; vlib_buffer_t *b; b = igmp_pkt_build_report_get_active (br); if (NULL == b) { b = igmp_pkt_build_report_v3 (br, NULL); if (NULL == b) /* failed to allocate buffer */ return; } igmp_group = igmp_pkt_report_v3_append_group (br, grp, type); if (NULL == igmp_group) return; /* *INDENT-OFF* */ vec_foreach(s, srcs) { igmp_group = igmp_pkt_report_v3_append_src(br, igmp_group, grp, type, s); if (NULL == igmp_group) return; }; /* *INDENT-ON* */ igmp_group->n_src_addresses = clib_host_to_net_u16 (br->n_srcs); IGMP_DBG (" ..add-group: %U", format_ip46_address, grp, IP46_TYPE_IP4); } void igmp_pkt_report_v3_add_group (igmp_pkt_build_report_t * br, const igmp_group_t * group, igmp_membership_group_v3_type_t type) { igmp_membership_group_v3_t *igmp_group; vlib_buffer_t *b; igmp_src_t *src; b = igmp_pkt_build_report_get_active (br); if (NULL == b) { b = igmp_pkt_build_report_v3 (br, NULL); if (NULL == b) /* failed to allocate buffer */ return; } /* * if the group won't fit in a partially full buffer, start again */ if ((0 != br->n_groups) && (igmp_pkt_report_v3_get_size (group) > br->base.n_avail)) { igmp_pkt_build_report_bake (br); b = igmp_pkt_build_report_v3 (br, NULL); if (NULL == b) /* failed to allocate buffer */ return; } igmp_group = igmp_pkt_report_v3_append_group (br, group->key, type); /* *INDENT-OFF* */ FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE, ({ igmp_group = igmp_pkt_report_v3_append_src(br, igmp_group, group->key, type, src->key); if (NULL == igmp_group) return; })); /* *INDENT-ON* */ igmp_group->n_src_addresses = clib_host_to_net_u16 (br->n_srcs); IGMP_DBG (" ..add-group: %U srcs:%d", format_igmp_key, group->key, hash_elts (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE])); } void igmp_pkt_build_report_init (igmp_pkt_build_report_t * br, u32 sw_if_index) { clib_memset (br, 0, sizeof (*br)); br->base.sw_if_index = sw_if_index; } static vlib_buffer_t * igmp_pkt_build_query_get_active (igmp_pkt_build_query_t * bq) { if (NULL == bq->base.buffers) return (NULL); return (vlib_get_buffer (vlib_get_main (), bq->base.buffers[vec_len (bq->base.buffers) - 1])); } static vlib_buffer_t * igmp_pkt_build_query_v3 (igmp_pkt_build_query_t * bq, const igmp_group_t * group) { igmp_membership_query_v3_t *query; vlib_buffer_t *b; b = igmp_pkt_build_ip_header (&bq->base, IGMP_MSG_QUERY, group); if (NULL == b) return (NULL); query = vlib_buffer_get_current (b); query->header.type = IGMP_TYPE_membership_query; query->header.code = 0; query->header.checksum = 0; query->qqi_code = 0; query->resv_s_qrv = 0; if (NULL != group) query->group_address.as_u32 = group->key->ip4.as_u32; else query->group_address.as_u32 = 0; vlib_buffer_append (b, sizeof (igmp_membership_query_v3_t)); bq->base.n_avail -= sizeof (igmp_membership_query_v3_t); bq->base.n_bytes += sizeof (igmp_membership_query_v3_t); return (b); } void igmp_pkt_query_v3_add_group (igmp_pkt_build_query_t * bq, const igmp_group_t * group, const ip46_address_t * srcs) { vlib_buffer_t *b; b = igmp_pkt_build_query_get_active (bq); if (NULL == b) { b = igmp_pkt_build_query_v3 (bq, group); if (NULL == b) /* failed to allocate buffer */ return; } if (NULL != srcs) { igmp_membership_query_v3_t *query; const ip46_address_t *src; query = vlib_buffer_get_current (b); vec_foreach (src, srcs) { query->src_addresses[bq->n_srcs++].as_u32 = src->ip4.as_u32; vlib_buffer_append (b, sizeof (ip4_address_t)); bq->base.n_bytes += sizeof (ip4_address_t); bq->base.n_avail += sizeof (ip4_address_t); } } /* * else * general query and we're done */ } static void igmp_pkt_build_query_bake (igmp_pkt_build_query_t * bq) { igmp_membership_query_v3_t *igmp; ip4_header_t *ip4; vlib_buffer_t *b; b = igmp_pkt_build_query_get_active (bq); b->current_data = 0; ip4 = vlib_buffer_get_current (b); // account for options igmp = (igmp_membership_query_v3_t *) (((u32 *) ip4) + 6); igmp->n_src_addresses = clib_host_to_net_u16 (bq->n_srcs); igmp->header.checksum = ~ip_csum_fold (ip_incremental_checksum (0, igmp, bq->base.n_bytes)); ip4->length = clib_host_to_net_u16 (b->current_length); ip4->checksum = ip4_header_checksum (ip4); bq->base.n_bytes = bq->base.n_avail = bq->n_srcs = 0; } void igmp_pkt_query_v3_send (igmp_pkt_build_query_t * bq) { if (NULL == bq->base.buffers) return; igmp_pkt_build_query_bake (bq); igmp_pkt_tx (&bq->base); } void igmp_pkt_build_query_init (igmp_pkt_build_query_t * bq, u32 sw_if_index) { clib_memset (bq, 0, sizeof (*bq)); bq->base.sw_if_index = sw_if_index; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */