FD.io VPP  v17.01.1-3-gc6833f8
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 static clib_error_t *
57  unformat_input_t * input, vlib_cli_command_t * cmd)
58 {
61  int show_all = 0;
62  u8 *s = 0;
63 
65  {
66  if (unformat (input, "all"))
67  show_all = 1;
68  else
69  return clib_error_return (0, "unknown input `%U'",
70  format_unformat_error, input);
71  }
72 
73  vlib_cli_output (vm, "%-13s%-7s%-12s%-15s%-20s%-40s",
74  "Address", "Socket", "VID:PID", "Link Speed", "Driver",
75  "Product Name");
76 
77  /* *INDENT-OFF* */
78  pool_foreach (d, pm->pci_devs, ({
79 
80  if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && !show_all)
81  continue;
82 
83  vec_reset_length (s);
84 
85  if (d->numa_node >= 0)
86  s = format (s, " %d", d->numa_node);
87 
88  vlib_cli_output (vm, "%-13U%-7v%04x:%04x %-15U%-20s%-40v",
89  format_vlib_pci_addr, &d->bus_address, s,
90  d->vendor_id, d->device_id,
91  format_vlib_pci_link_speed, d,
92  d->driver_name ? (char *) d->driver_name : "",
93  d->product_name);
94  }));
95 /* *INDENT-ON* */
96 
97  vec_free (s);
98  return 0;
99 }
100 
101 uword
102 unformat_vlib_pci_addr (unformat_input_t * input, va_list * args)
103 {
104  vlib_pci_addr_t *addr = va_arg (*args, vlib_pci_addr_t *);
105  u32 x[4];
106 
107  if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
108  return 0;
109 
110  addr->domain = x[0];
111  addr->bus = x[1];
112  addr->slot = x[2];
113  addr->function = x[3];
114 
115  return 1;
116 }
117 
118 u8 *
119 format_vlib_pci_addr (u8 * s, va_list * va)
120 {
121  vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
122  return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
123  addr->slot, addr->function);
124 }
125 
126 u8 *
127 format_vlib_pci_handle (u8 * s, va_list * va)
128 {
129  vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
130  return format (s, "%x/%x/%x", addr->bus, addr->slot, addr->function);
131 }
132 
133 u8 *
134 format_vlib_pci_link_speed (u8 * s, va_list * va)
135 {
136  vlib_pci_device_t *d = va_arg (*va, vlib_pci_device_t *);
137  pcie_config_regs_t *r =
139  int width;
140 
141  if (!r)
142  return format (s, "unknown");
143 
144  width = (r->link_status >> 4) & 0x3f;
145 
146  if ((r->link_status & 0xf) == 1)
147  return format (s, "2.5 GT/s x%u", width);
148  if ((r->link_status & 0xf) == 2)
149  return format (s, "5.0 GT/s x%u", width);
150  if ((r->link_status & 0xf) == 3)
151  return format (s, "8.0 GT/s x%u", width);
152  return format (s, "unknown");
153 }
154 
155 
156 /* *INDENT-OFF* */
157 VLIB_CLI_COMMAND (show_pci_command, static) = {
158  .path = "show pci",
159  .short_help = "show pci [all]",
160  .function = show_pci_fn,
161 };
162 /* *INDENT-ON* */
163 
164 clib_error_t *
166 {
167  return 0;
168 }
169 
171 
172 /*
173  * fd.io coding-style-patch-verification: ON
174  *
175  * Local Variables:
176  * eval: (c-set-style "gnu")
177  * End:
178  */
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
clib_error_t * pci_bus_init(vlib_main_t *vm)
Definition: pci.c:165
static void * pci_config_find_capability(pci_config_type0_regs_t *t, int cap_type)
Definition: pci_config.h:415
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
u8 * format_vlib_pci_link_speed(u8 *s, va_list *va)
Definition: pci.c:134
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:348
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
vlib_pci_device_t * pci_devs
Definition: pci.h:116
static clib_error_t * show_pci_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: pci.c:56
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
uword unformat_vlib_pci_addr(unformat_input_t *input, va_list *args)
Definition: pci.c:102
vlib_pci_main_t pci_main
Definition: pci.c:53
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
pci_config_type0_regs_t config0
Definition: pci.h:63
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
u8 * format_vlib_pci_addr(u8 *s, va_list *va)
Definition: pci.c:119
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
u8 * format_vlib_pci_handle(u8 *s, va_list *va)
Definition: pci.c:127
vhost_vring_addr_t addr
Definition: vhost-user.h:81
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t