FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
pci.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * pci.c: Linux user space PCI bus management.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #include <vlib/vlib.h>
41 #include <vlib/pci/pci.h>
42 #include <vlib/unix/unix.h>
43 
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <dirent.h>
48 #include <sys/ioctl.h>
49 #include <net/if.h>
50 #include <linux/ethtool.h>
51 #include <linux/sockios.h>
52 
54 
55 vlib_pci_device_info_t * __attribute__ ((weak))
58 {
59  if (error)
60  *error = clib_error_return (0, "unsupported");
61  return 0;
62 }
63 
64 vlib_pci_addr_t * __attribute__ ((weak)) vlib_pci_get_all_dev_addrs ()
65 {
66  return 0;
67 }
68 
69 static clib_error_t *
71  unformat_input_t * input, vlib_cli_command_t * cmd)
72 {
73  vlib_pci_addr_t *addr = 0, *addrs;
74  int show_all = 0;
75  u8 *s = 0;
76 
78  {
79  if (unformat (input, "all"))
80  show_all = 1;
81  else
82  return clib_error_return (0, "unknown input `%U'",
83  format_unformat_error, input);
84  }
85 
86  vlib_cli_output (vm, "%-13s%-5s%-12s%-14s%-16s%-32s%s",
87  "Address", "Sock", "VID:PID", "Link Speed", "Driver",
88  "Product Name", "Vital Product Data");
89 
90  addrs = vlib_pci_get_all_dev_addrs ();
91 
92  vec_foreach (addr, addrs)
93  {
96 
97  if (!d)
98  continue;
99 
100  if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && !show_all)
101  continue;
102 
103  vec_reset_length (s);
104  if (d->numa_node >= 0)
105  s = format (s, " %d", d->numa_node);
106 
108  vm, "%-13U%-5v%04x:%04x %-14U%-16s%-32v%U", format_vlib_pci_addr,
110  d->driver_name ? (char *) d->driver_name : "", d->product_name,
111  format_vlib_pci_vpd, d->vpd_r, (u8 *) 0);
113  }
114 
115  vec_free (s);
116  vec_free (addrs);
117  return 0;
118 }
119 
120 uword
121 unformat_vlib_pci_addr (unformat_input_t * input, va_list * args)
122 {
123  vlib_pci_addr_t *addr = va_arg (*args, vlib_pci_addr_t *);
124  u32 x[4];
125 
126  if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
127  return 0;
128 
129  addr->domain = x[0];
130  addr->bus = x[1];
131  addr->slot = x[2];
132  addr->function = x[3];
133 
134  return 1;
135 }
136 
137 u8 *
138 format_vlib_pci_addr (u8 * s, va_list * va)
139 {
140  vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
141  return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
142  addr->slot, addr->function);
143 }
144 
145 u8 *
146 format_vlib_pci_link_speed (u8 * s, va_list * va)
147 {
148  vlib_pci_device_info_t *d = va_arg (*va, vlib_pci_device_info_t *);
149  pcie_config_regs_t *r =
151  int width;
152 
153  if (!r)
154  return format (s, "unknown");
155 
156  width = (r->link_status >> 4) & 0x3f;
157 
158  if ((r->link_status & 0xf) == 1)
159  return format (s, "2.5 GT/s x%u", width);
160  if ((r->link_status & 0xf) == 2)
161  return format (s, "5.0 GT/s x%u", width);
162  if ((r->link_status & 0xf) == 3)
163  return format (s, "8.0 GT/s x%u", width);
164  if ((r->link_status & 0xf) == 4)
165  return format (s, "16.0 GT/s x%u", width);
166  return format (s, "unknown");
167 }
168 
169 u8 *
170 format_vlib_pci_vpd (u8 * s, va_list * args)
171 {
172  u8 *data = va_arg (*args, u8 *);
173  u8 *id = va_arg (*args, u8 *);
174  u32 indent = format_get_indent (s);
175  char *string_types[] = { "PN", "EC", "SN", "MN", 0 };
176  uword p = 0;
177  int first_line = 1;
178 
179  if (vec_len (data) < 3)
180  return s;
181 
182  while (p + 3 < vec_len (data))
183  {
184 
185  if (data[p] == 0 && data[p + 1] == 0)
186  return s;
187 
188  if (p + data[p + 2] > vec_len (data))
189  return s;
190 
191  if (id == 0)
192  {
193  int is_string = 0;
194  char **c = string_types;
195 
196  while (c[0])
197  {
198  if (*(u16 *) & data[p] == *(u16 *) c[0])
199  is_string = 1;
200  c++;
201  }
202 
203  if (data[p + 2])
204  {
205  if (!first_line)
206  s = format (s, "\n%U", format_white_space, indent);
207  else
208  {
209  first_line = 0;
210  s = format (s, " ");
211  }
212 
213  s = format (s, "%c%c: ", data[p], data[p + 1]);
214  if (is_string)
215  vec_add (s, data + p + 3, data[p + 2]);
216  else
217  {
218  int i;
219  const int max_bytes = 8;
220  s = format (s, "0x");
221  for (i = 0; i < clib_min (data[p + 2], max_bytes); i++)
222  s = format (s, " %02x", data[p + 3 + i]);
223 
224  if (data[p + 2] > max_bytes)
225  s = format (s, " ...");
226  }
227  }
228  }
229  else if (*(u16 *) & data[p] == *(u16 *) id)
230  {
231  vec_add (s, data + p + 3, data[p + 2]);
232  return s;
233  }
234 
235  p += 3 + data[p + 2];
236  }
237 
238  return s;
239 }
240 
241 
242 /* *INDENT-OFF* */
244  .path = "show pci",
245  .short_help = "show pci [all]",
246  .function = show_pci_fn,
247 };
248 /* *INDENT-ON* */
249 
250 clib_error_t *
252 {
253  vlib_pci_main_t *pm = &pci_main;
254  pm->log_default = vlib_log_register_class ("pci", 0);
255  return 0;
256 }
257 
259 
260 /*
261  * fd.io coding-style-patch-verification: ON
262  *
263  * Local Variables:
264  * eval: (c-set-style "gnu")
265  * End:
266  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
pci_bus_init
clib_error_t * pci_bus_init(vlib_main_t *vm)
Definition: pci.c:251
vlib.h
vlib_pci_get_all_dev_addrs
vlib_pci_addr_t * vlib_pci_get_all_dev_addrs()
Definition: pci.c:1466
vlib_pci_device_info::numa_node
int numa_node
Definition: pci.h:69
vec_add
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:688
format_vlib_pci_addr
u8 * format_vlib_pci_addr(u8 *s, va_list *va)
Definition: pci.c:138
vlib_log_register_class
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:339
vlib_pci_main_t::log_default
vlib_log_class_t log_default
Definition: pci.h:158
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
PCI_CAP_ID_PCIE
@ PCI_CAP_ID_PCIE
Definition: pci_config.h:407
u16
unsigned short u16
Definition: types.h:57
vlib_pci_device_info::vpd_r
u8 * vpd_r
Definition: pci.h:78
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
unformat_input_t
struct _unformat_input_t unformat_input_t
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1089
error
Definition: cJSON.c:88
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
vlib_pci_device_info
Definition: pci.h:60
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
format_vlib_pci_vpd
u8 * format_vlib_pci_vpd(u8 *s, va_list *args)
Definition: pci.c:170
unformat_vlib_pci_addr
uword unformat_vlib_pci_addr(unformat_input_t *input, va_list *args)
Definition: pci.c:121
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
c
svmdb_client_t * c
Definition: vpp_get_metrics.c:48
uword
u64 uword
Definition: types.h:112
format_unformat_error
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
vlib_pci_get_device_info
vlib_pci_device_info_t * vlib_pci_get_device_info(vlib_main_t *vm, vlib_pci_addr_t *addr, clib_error_t **error)
Definition: pci.c:202
clib_min
#define clib_min(x, y)
Definition: clib.h:342
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
data
u8 data[128]
Definition: ipsec_types.api:95
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
format
description fragment has unexpected format
Definition: map.api:433
format_get_indent
static u32 format_get_indent(u8 *s)
Definition: format.h:72
vlib_pci_device_info::vendor_id
u16 vendor_id
Definition: pci.h:73
u32
unsigned int u32
Definition: types.h:88
show_pci_fn
static clib_error_t * show_pci_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: pci.c:70
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
pci.h
PCI_CLASS_NETWORK_ETHERNET
@ PCI_CLASS_NETWORK_ETHERNET
Definition: pci_config.h:59
vlib_pci_device_info::config0
pci_config_type0_regs_t config0
Definition: pci.h:87
vlib_main_t
Definition: main.h:102
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
unix.h
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
i
int i
Definition: flowhash_template.h:376
vlib_pci_device_info::device_id
u16 device_id
Definition: pci.h:74
vlib_pci_main_t
Definition: pci.h:153
pci_config_find_capability
static void * pci_config_find_capability(pci_config_type0_regs_t *t, int cap_type)
Definition: pci_config.h:421
vlib_pci_free_device_info
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
Definition: pci.h:114
vlib_cli_command_t
Definition: cli.h:92
vlib_pci_device_info::product_name
u8 * product_name
Definition: pci.h:77
format_vlib_pci_link_speed
u8 * format_vlib_pci_link_speed(u8 *s, va_list *va)
Definition: pci.c:146
pci_main
vlib_pci_main_t pci_main
Definition: pci.c:53
vlib_pci_device_info::device_class
u16 device_class
Definition: pci.h:72
format_white_space
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
show_pci_command
static vlib_cli_command_t show_pci_command
(constructor) VLIB_CLI_COMMAND (show_pci_command)
Definition: pci.c:243
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
vlib_pci_device_info::driver_name
u8 * driver_name
Definition: pci.h:82