summaryrefslogtreecommitdiffstats
path: root/src/vlib/init.h
blob: e6235652ad137ad5b637596a7873438982d7689b (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright (c) 2015 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.
 */
/*
 * init.h: mechanism for functions to be called at init/exit.
 *
 * Copyright (c) 2008 Eliot Dresselhaus
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef included_vlib_init_h
#define included_vlib_init_h

#include <vppinfra/error.h>
#include <vppinfra/format.h>
#include <vppinfra/hash.h>

/* Init/exit functions: called at start/end of main routine.  Init
   functions are typically used to register and setup packet
   processing nodes.  */

typedef clib_error_t *(vlib_init_function_t) (struct vlib_main_t * vm);

typedef struct _vlib_init_function_list_elt
{
  struct _vlib_init_function_list_elt *next_init_function;
  vlib_init_function_t *f;
  char *name;
  char **runs_before;
  char **runs_after;
  char **init_order;
} _vlib_init_function_list_elt_t;

/* Configuration functions: called with configuration input just before
   main polling loop starts. */
typedef clib_error_t *(vlib_config_function_t) (struct vlib_main_t * vm,
						unformat_input_t * input);

typedef struct vlib_config_function_runtime_t
{
  /* Function to call.  Set to null once function has already been called. */
  vlib_config_function_t *function;

  /* Input for function. */
  unformat_input_t input;

  /* next config function registration */
  struct vlib_config_function_runtime_t *next_registration;

  /* To be invoked as soon as the clib heap is available */
  u8 is_early;

  /* Name used to distinguish input on command line. */
  char name[32];
} vlib_config_function_runtime_t;

#define VLIB_REMOVE_FROM_LINKED_LIST(first,p,next)              \
{                                                               \
  ASSERT (first);                                               \
  if (first == p)                                               \
      first = (p)->next;                                        \
  else                                                          \
    {                                                           \
      __typeof__ (p) current = first;                           \
      while (current->next)                                     \
	{                                                       \
	  if (current->next == p)                               \
	    {                                                   \
	      current->next = current->next->next;              \
	      break;                                            \
	    }                                                   \
	  current = current->next;                              \
	}                                                       \
      ASSERT (current);                                         \
    }                                                           \
}

#define _VLIB_INIT_FUNCTION_SYMBOL(x, type)	\
  _vlib_##type##_function_##x

#define VLIB_INIT_FUNCTION_SYMBOL(x)		\
  _VLIB_INIT_FUNCTION_SYMBOL(x, init)
#define VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL(x)		\
  _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_enter)
#define VLIB_MAIN_LOOP_EXIT_FUNCTION_SYMBOL(x)	\
  _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_exit)
#define VLIB_CONFIG_FUNCTION_SYMBOL(x)		\
  _VLIB_INIT_FUNCTION_SYMBOL(x, config)

/* Declaration is global (e.g. not static) so that init functions can
   be called from other modules to resolve init function depend. */

#ifndef CLIB_MARCH_VARIANT
#define VLIB_DECLARE_INIT_FUNCTION(x, tag)                                    \
  vlib_init_function_t *_VLIB_INIT_FUNCTION_SYMBOL (x, tag) = x;              \
  static void __vlib_add_##tag##_function_##x (void)                          \
    __attribute__ ((__constructor__));                                        \
  static _vlib_init_function_list_elt_t _vlib_init_function_##tag##_##x;      \
  static void __vlib_add_##tag##_function_##x (void)                          \
  {                                                                           \
    vlib_global_main_t *vgm = vlib_get_global_main ();                        \
    _vlib_init_function_##tag##_##x.next_init_function =                      \
      vgm->tag##_function_registrations;                                      \
    vgm->tag##_function_registrations = &_vlib_init_function_##tag##_##x;     \
    _vlib_init_function_##tag##_##x.f = &x;                                   \
    _vlib_init_function_##tag##_##x.name = #x;                                \
  }                                                                           \
  static void __vlib_rm_##tag##_function_##x (void)                           \
    __attribute__ ((__destructor__));                                         \
  static void __vlib_rm_##tag##_function_##x (void)                           \
  {                                                                           \
    vlib_global_main_t *vgm = vlib_get_global_main ();                        \
    _vlib_init_function_list_elt_t *this, *prev;                              \
    this = vgm->tag##_function_registrations;                                 \
    if (this == 0)                                                            \
      return;                                                                 \
    if (this->f == &x)                                                        \
      {                                                                       \
	vgm->tag##_function_registrations = this->next_init_function;         \
	return;                                                               \
      }                                                                       \
    prev = this;                                                              \
    this = this->next_init_function;                                          \
    while (this)                                                              \
      {                                                                       \
	if (this->f == &x)                                                    \
	  {                                                                   \
	    prev->next_init_function = this->next_init_function;              \
	    return;                                                           \
	  }                                                                   \
	prev = this;                                                          \
	this = this->next_init_function;                                      \
      }                                                                       \
  }                                                                           \
  static _vlib_init_function_list_elt_t _vlib_init_function_##tag##_##x
#else
/* create unused pointer to silence compiler warnings and get whole
   function optimized out */
#define VLIB_DECLARE_INIT_FUNCTION(x, tag)                      \
static __clib_unused void * __clib_unused_##tag##_##x = x
#endif

#define VLIB_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,init)
#define VLIB_WORKER_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,worker_init)

#define VLIB_MAIN_LOOP_ENTER_FUNCTION(x) \
  VLIB_DECLARE_INIT_FUNCTION(x,main_loop_enter)
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x) \
VLIB_DECLARE_INIT_FUNCTION(x,main_loop_exit)

#ifndef CLIB_MARCH_VARIANT
#define VLIB_CONFIG_FUNCTION(x, n, ...)                                       \
  __VA_ARGS__ vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x); \
  static void __vlib_add_config_function_##x (void)                           \
    __attribute__ ((__constructor__));                                        \
  static void __vlib_add_config_function_##x (void)                           \
  {                                                                           \
    vlib_global_main_t *vgm = vlib_get_global_main ();                        \
    VLIB_CONFIG_FUNCTION_SYMBOL (x).next_registration =                       \
      vgm->config_function_registrations;                                     \
    vgm->config_function_registrations = &VLIB_CONFIG_FUNCTION_SYMBOL (x);    \
  }                                                                           \
  static void __vlib_rm_config_function_##x (void)                            \
    __attribute__ ((__destructor__));                                         \
  static void __vlib_rm_config_function_##x (void)                            \
  {                                                                           \
    vlib_global_main_t *vgm = vlib_get_global_main ();                        \
    vlib_config_function_runtime_t *p = &VLIB_CONFIG_FUNCTION_SYMBOL (x);     \
    VLIB_REMOVE_FROM_LINKED_LIST (vgm->config_function_registrations, p,      \
				  next_registration);                         \
  }                                                                           \
  vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x) = {          \
    .name = n,                                                                \
    .function = x,                                                            \
    .is_early = 0,                                                            \
  }
#else
/* create unused pointer to silence compiler warnings and get whole
   function optimized out */
#define VLIB_CONFIG_FUNCTION(x,n,...)                           \
  static __clib_unused vlib_config_function_runtime_t           \
    VLIB_CONFIG_FUNCTION_SYMBOL (__clib_unused_##x)             \
  = {                                                           \
    .name = n,                                                  \
    .function = x,                                              \
    .is_early = 0,						\
  }
#endif

#ifndef CLIB_MARCH_VARIANT
#define VLIB_EARLY_CONFIG_FUNCTION(x, n, ...)                                 \
  __VA_ARGS__ vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x); \
  static void __vlib_add_config_function_##x (void)                           \
    __attribute__ ((__constructor__));                                        \
  static void __vlib_add_config_function_##x (void)                           \
  {                                                                           \
    vlib_global_main_t *vgm = vlib_get_global_main ();                        \
    VLIB_CONFIG_FUNCTION_SYMBOL (x).next_registration =                       \
      vgm->config_function_registrations;                                     \
    vgm->config_function_registrations = &VLIB_CONFIG_FUNCTION_SYMBOL (x);    \
  }                                                                           \
  static void __vlib_rm_config_function_##x (void)                            \
    __attribute__ ((__destructor__));                                         \
  static void __vlib_rm_config_function_##x (void)                            \
  {                                                                           \
    vlib_global_main_t *vgm = vlib_get_global_main ();                        \
    vlib_config_function_runtime_t *p = &VLIB_CONFIG_FUNCTION_SYMBOL (x);     \
    VLIB_REMOVE_FROM_LINKED_LIST (vgm->config_function_registrations, p,      \
				  next_registration);                         \
  }                                                                           \
  vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x) = {          \
    .name = n,                                                                \
    .function = x,                                                            \
    .is_early = 1,                                                            \
  }
#else
/* create unused pointer to silence compiler warnings and get whole
   function optimized out */
#define VLIB_EARLY_CONFIG_FUNCTION(x,n,...)                     \
  static __clib_unused vlib_config_function_runtime_t           \
    VLIB_CONFIG_FUNCTION_SYMBOL (__clib_unused_##x)             \
  = {                                                           \
    .name = n,                                                  \
    .function = x,                                              \
    .is_early = 1,						\
  }
#endif

/* Call given init function: used for init function dependencies. */
#define vlib_call_init_function(vm, x)                                        \
  ({                                                                          \
    vlib_global_main_t *vgm = &vlib_global_main;                              \
    extern vlib_init_function_t *VLIB_INIT_FUNCTION_SYMBOL (x);               \
    vlib_init_function_t *_f = VLIB_INIT_FUNCTION_SYMBOL (x);                 \
    clib_error_t *_error = 0;                                                 \
    if (!hash_get (vgm->init_functions_called, _f))                           \
      {                                                                       \
	hash_set1 (vgm->init_functions_called, _f);                           \
	_error = _f (vm);                                                     \
      }                                                                       \
    _error;                                                                   \
  })

/* Don't call given init function: used to suppress parts of the netstack */
#define vlib_mark_init_function_complete(vm, x)                               \
  ({                                                                          \
    vlib_global_main_t *vgm = &vlib_global_main;                              \
    extern vlib_init_function_t *VLIB_INIT_FUNCTION_SYMBOL (x);               \
    vlib_init_function_t *_f = VLIB_INIT_FUNCTION_SYMBOL (x);                 \
    hash_set1 (vgm->init_functions_called, _f);                               \
  })

#define vlib_call_post_graph_init_function(vm, x)                             \
  ({                                                                          \
    vlib_global_main_t *vgm = &vlib_global_main;                              \
    extern vlib_init_function_t *VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x);    \
    vlib_init_function_t *_f = VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x);      \
    clib_error_t *_error = 0;                                                 \
    if (!hash_get (vgm->init_functions_called, _f))                           \
      {                                                                       \
	hash_set1 (vgm->init_functions_called, _f);                           \
	_error = _f (vm);                                                     \
      }                                                                       \
    _error;                                                                   \
  })

#define vlib_call_config_function(vm, x)                                      \
  ({                                                                          \
    vlib_global_main_t *vgm = &vlib_global_main;                              \
    vlib_config_function_runtime_t *_r;                                       \
    clib_error_t *_error = 0;                                                 \
    extern vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x);    \
                                                                              \
    _r = &VLIB_CONFIG_FUNCTION_SYMBOL (x);                                    \
    if (!hash_get (vgm->init_functions_called, _r->function))                 \
      {                                                                       \
	hash_set1 (vgm->init_functions_called, _r->function);                 \
	_error = _r->function (vm, &_r->input);                               \
      }                                                                       \
    _error;                                                                   \
  })

#define vlib_call_main_loop_enter_function(vm, x)                             \
  ({                                                                          \
    vlib_global_main_t *vgm = &vlib_global_main;                              \
    extern vlib_init_function_t *VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL (x);    \
    vlib_init_function_t *_f = VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL (x);      \
    clib_error_t *_error = 0;                                                 \
    if (!hash_get (vgm->init_functions_called, _f))                           \
      {                                                                       \
	hash_set1 (vgm->init_functions_called, _f);                           \
	_error = _f (vm);                                                     \
      }                                                                       \
    _error;                                                                   \
  })

/* External functions. */
clib_error_t *vlib_call_all_init_functions (struct vlib_main_t *vm);
clib_error_t *vlib_call_all_config_functions (struct vlib_main_t *vm,
					      unformat_input_t * input,
					      int is_early);
clib_error_t *vlib_call_all_main_loop_enter_functions (struct vlib_main_t
						       *vm);
clib_error_t *vlib_call_all_main_loop_exit_functions (struct vlib_main_t *vm);
clib_error_t *
vlib_call_init_exit_functions (struct vlib_main_t *vm,
			       _vlib_init_function_list_elt_t **headp,
			       int call_once, int is_global);
clib_error_t *
vlib_call_init_exit_functions_no_sort (struct vlib_main_t *vm,
				       _vlib_init_function_list_elt_t **headp,
				       int call_once, int is_global);
clib_error_t *vlib_sort_init_exit_functions (_vlib_init_function_list_elt_t
					     **);
#define foreach_vlib_module_reference		\
  _ (node_cli)					\
  _ (trace_cli)

/* Dummy function to get node_cli.c linked in. */
#define _(x) void vlib_##x##_reference (void);
foreach_vlib_module_reference
#undef _
#define VLIB_INITS(...)  (char*[]) { __VA_ARGS__, 0}
#endif /* included_vlib_init_h */
/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
/span> vec_len (file_name)); if (!unformat (&input, "uio%d", &l->uio_minor)) abort (); unformat_free (&input); return 0; } static clib_error_t * linux_pci_uio_read_ready (unix_file_t * uf) { vlib_pci_main_t *pm = &pci_main; vlib_pci_device_t *d; int __attribute__ ((unused)) rv; u32 icount; rv = read (uf->file_descriptor, &icount, 4); d = pool_elt_at_index (pm->pci_devs, uf->private_data); if (d->interrupt_handler) d->interrupt_handler (d); vlib_pci_intr_enable (d); return /* no error */ 0; } static clib_error_t * linux_pci_uio_error_ready (unix_file_t * uf) { u32 error_index = (u32) uf->private_data; return clib_error_return (0, "pci device %d: error", error_index); } static void add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev) { vlib_pci_main_t *pm = &pci_main; linux_pci_main_t *lpm = &linux_pci_main; linux_pci_device_t *l; pool_get (lpm->linux_pci_devices, l); l[0] = pdev[0]; l->dev_dir_name = vec_dup (l->dev_dir_name); dev->os_handle = l - lpm->linux_pci_devices; { u8 *uio_dir = format (0, "%s/uio", l->dev_dir_name); foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */ 1); vec_free (uio_dir); } { char *uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0); l->uio_fd = open (uio_name, O_RDWR); if (l->uio_fd < 0) clib_unix_error ("open `%s'", uio_name); vec_free (uio_name); } { unix_file_t template = { 0 }; unix_main_t *um = &unix_main; template.read_function = linux_pci_uio_read_ready; template.file_descriptor = l->uio_fd; template.error_function = linux_pci_uio_error_ready; template.private_data = dev - pm->pci_devs; l->unix_file_index = unix_file_add (um, &template); } } static void linux_pci_device_free (linux_pci_device_t * l) { int i; for (i = 0; i < vec_len (l->resource_fds); i++) if (l->resource_fds[i] > 0) close (l->resource_fds[i]); if (l->config_fd > 0) close (l->config_fd); if (l->uio_fd > 0) close (l->uio_fd); vec_free (l->resource_fds); vec_free (l->dev_dir_name); } /* Configuration space read/write. */ clib_error_t * vlib_pci_read_write_config (vlib_pci_device_t * dev, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes) { linux_pci_main_t *lpm = &linux_pci_main; linux_pci_device_t *p; int n; p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle); if (read_or_write == VLIB_READ) n = pread (p->config_fd, data, n_bytes, address); else n = pwrite (p->config_fd, data, n_bytes, address); if (n != n_bytes) return clib_error_return_unix (0, "%s", read_or_write == VLIB_READ ? "read" : "write"); return 0; } static clib_error_t * os_map_pci_resource_internal (uword os_handle, u32 resource, u8 * addr, void **result) { linux_pci_main_t *pm = &linux_pci_main; linux_pci_device_t *p; struct stat stat_buf; u8 *file_name; int fd; clib_error_t *error; int flags = MAP_SHARED; error = 0; p = pool_elt_at_index (pm->linux_pci_devices, os_handle); file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0); fd = open ((char *) file_name, O_RDWR); if (fd < 0) { error = clib_error_return_unix (0, "open `%s'", file_name); goto done; } if (fstat (fd, &stat_buf) < 0) { error = clib_error_return_unix (0, "fstat `%s'", file_name); goto done; } vec_validate (p->resource_fds, resource); p->resource_fds[resource] = fd; if (addr != 0) flags |= MAP_FIXED; *result = mmap (addr, /* size */ stat_buf.st_size, PROT_READ | PROT_WRITE, flags, /* file */ fd, /* offset */ 0); if (*result == (void *) -1) { error = clib_error_return_unix (0, "mmap `%s'", file_name); goto done; } done: if (error) { if (fd >= 0) close (fd); } vec_free (file_name); return error; } clib_error_t * vlib_pci_map_resource (vlib_pci_device_t * dev, u32 resource, void **result) { return (os_map_pci_resource_internal (dev->os_handle, resource, 0 /* addr */ , result)); } clib_error_t * vlib_pci_map_resource_fixed (vlib_pci_device_t * dev, u32 resource, u8 * addr, void **result) { return (os_map_pci_resource_internal (dev->os_handle, resource, addr, result)); } void vlib_pci_free_device (vlib_pci_device_t * dev) { linux_pci_main_t *pm = &linux_pci_main; linux_pci_device_t *l; l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle); linux_pci_device_free (l); pool_put (pm->linux_pci_devices, l); } pci_device_registration_t * __attribute__ ((unused)) pci_device_next_registered (pci_device_registration_t * r) { uword i; /* Null vendor id marks end of initialized list. */ for (i = 0; r->supported_devices[i].vendor_id != 0; i++) ; return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0])); } static clib_error_t * init_device_from_registered (vlib_main_t * vm, vlib_pci_device_t * dev, linux_pci_device_t * pdev) { vlib_pci_main_t *pm = &pci_main; pci_device_registration_t *r; pci_device_id_t *i; clib_error_t *error; r = pm->pci_device_registrations; while (r) { for (i = r->supported_devices; i->vendor_id != 0; i++) if (i->vendor_id == dev->vendor_id && i->device_id == dev->device_id) { error = vlib_pci_bind_to_uio (dev, "uio_pci_generic"); if (error) { clib_error_report (error); continue; } add_device (dev, pdev); dev->interrupt_handler = r->interrupt_handler; return r->init_function (vm, dev); } r = r->next_registration; } /* No driver, close the PCI config-space FD */ close (pdev->config_fd); return 0; } static clib_error_t * init_device (vlib_main_t * vm, vlib_pci_device_t * dev, linux_pci_device_t * pdev) { return init_device_from_registered (vm, dev, pdev); } static clib_error_t * scan_device (void *arg, u8 * dev_dir_name, u8 * ignored) { vlib_main_t *vm = arg; vlib_pci_main_t *pm = &pci_main; int fd; u8 *f; clib_error_t *error = 0; vlib_pci_device_t *dev; linux_pci_device_t pdev = { 0 }; u32 tmp; f = format (0, "%v/config%c", dev_dir_name, 0); fd = open ((char *) f, O_RDWR); /* Try read-only access if write fails. */ if (fd < 0) fd = open ((char *) f, O_RDONLY); if (fd < 0) { error = clib_error_return_unix (0, "open `%s'", f); goto done; } pool_get (pm->pci_devs, dev); /* You can only read more that 64 bytes of config space as root; so we try to read the full space but fall back to just the first 64 bytes. */ if (read (fd, &dev->config_data, sizeof (dev->config_data)) != sizeof (dev->config_data) && read (fd, &dev->config0, sizeof (dev->config0)) != sizeof (dev->config0)) { pool_put (pm->pci_devs, dev); error = clib_error_return_unix (0, "read `%s'", f); close (fd); goto done; } { static pci_config_header_t all_ones; if (all_ones.vendor_id == 0) memset (&all_ones, ~0, sizeof (all_ones)); if (!memcmp (&dev->config0.header, &all_ones, sizeof (all_ones))) { pool_put (pm->pci_devs, dev); error = clib_error_return (0, "invalid PCI config for `%s'", f); close (fd); goto done; } } if (dev->config0.header.header_type == 0) pci_config_type0_little_to_host (&dev->config0); else pci_config_type1_little_to_host (&dev->config1); /* Parse bus, dev, function from directory name. */ { unformat_input_t input; unformat_init_string (&input, (char *) dev_dir_name, vec_len (dev_dir_name)); if (!unformat (&input, "/sys/bus/pci/devices/%U", unformat_vlib_pci_addr, &dev->bus_address)) abort (); unformat_free (&input); } pdev.config_fd = fd; pdev.dev_dir_name = dev_dir_name; hash_set (pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32, dev - pm->pci_devs); error = init_device (vm, dev, &pdev); vec_reset_length (f); f = format (f, "%v/vpd%c", dev_dir_name, 0); fd = open ((char *) f, O_RDONLY); if (fd >= 0) { while (1) { u8 tag[3]; u8 *data = 0; int len; if (read (fd, &tag, 3) != 3) break; if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91) break; len = (tag[2] << 8) | tag[1]; vec_validate (data, len); if (read (fd, data, len) != len) { vec_free (data); break; } if (tag[0] == 0x82) dev->product_name = data; else if (tag[0] == 0x90) dev->vpd_r = data; else if (tag[0] == 0x91) dev->vpd_w = data; data = 0; } close (fd); } vec_reset_length (f); f = format (f, "%v/driver%c", dev_dir_name, 0); dev->driver_name = vlib_sysfs_link_to_name ((char *) f); dev->numa_node = -1; vec_reset_length (f); f = format (f, "%v/numa_node%c", dev_dir_name, 0); vlib_sysfs_read ((char *) f, "%u", &dev->numa_node); vec_reset_length (f); f = format (f, "%v/class%c", dev_dir_name, 0); vlib_sysfs_read ((char *) f, "0x%x", &tmp); dev->device_class = tmp >> 8; vec_reset_length (f); f = format (f, "%v/vendor%c", dev_dir_name, 0); vlib_sysfs_read ((char *) f, "0x%x", &tmp); dev->vendor_id = tmp; vec_reset_length (f); f = format (f, "%v/device%c", dev_dir_name, 0); vlib_sysfs_read ((char *) f, "0x%x", &tmp); dev->device_id = tmp; done: vec_free (f); return error; } clib_error_t * linux_pci_init (vlib_main_t * vm) { vlib_pci_main_t *pm = &pci_main; clib_error_t *error; pm->vlib_main = vm; if ((error = vlib_call_init_function (vm, unix_input_init))) return error; ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32)); pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword)); error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm, /* scan_dirs */ 0); /* Complain and continue. might not be root, etc. */ if (error) clib_error_report (error); return error; } VLIB_INIT_FUNCTION (linux_pci_init); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */