summaryrefslogtreecommitdiffstats
path: root/vppinfra/Make.defs
blob: dcb51e16ab76dc3d4ecdae7ed737010581f33aee (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
# Copyright (c) 2001, 2002 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.

# Basic toolchain defines.
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
AR = $(CROSS_COMPILE)ar
RANLIB = $(CROSS_COMPILE)ranlib
INSTALL = install

ifneq ($(origin CROSS_COMPILE), undefined)
  IS_CROSS_COMPILE=yes
endif

CLIB_ARCH = $(shell $(CC) -dumpmachine)

# Where to get linux kernel includes.
# By default get linux includes from /usr/include/linux...
KERNEL_PREFIX ?= /usr

# Where to find compiler include directory (since we may
# be using -nostdinc).
CC_PREFIX = $(shell dirname `$(CC) --print-libgcc-file-name`)

# Where to get LIBC includes for cross compiles
LIBC_PREFIX ?= $(CC_PREFIX)/../../../../$(CLIB_ARCH)

# Where to find CLIB includes/libraries for cross compiles
CLIB_PREFIX ?= /usr/local/$(CLIB_ARCH)

OBJ = $(CLIB_ARCH).o
SHARED_OBJ = shared.$(OBJ)
KERNEL_OBJ = kernel.$(OBJ)
MODULE_OBJ = module.$(OBJ)

DEP = $(CLIB_ARCH).d
SHARED_DEP = shared.$(DEP)
KERNEL_DEP = kernel.$(DEP)

STATIC_LIB = $(CLIB_ARCH).a
SHARED_LIB = $(CLIB_ARCH).so
KERNEL_LIB = kernel.$(CLIB_ARCH).a

STATIC_CFLAGS = $(DEFAULT_CFLAGS)
SHARED_CFLAGS = $(STATIC_CFLAGS) -fPIC

# Compile flags common to user/kernel
CLIB_COMMON_CFLAGS += -Wall

DEBUG ?= no
ifeq ($(DEBUG),yes)
	COPTS ?= -g -O0
	CLIB_COMMON_CFLAGS += -DDEBUG
else
	COPTS ?= -O2
endif

CLIB_COMMON_CFLAGS += $(COPTS) 

CLIB_USER_CFLAGS = $(CLIB_COMMON_CFLAGS)

ifeq ($(IS_CROSS_COMPILE),yes)
  CLIB_USER_CFLAGS += -nostdinc
  CLIB_USER_CFLAGS += -idirafter $(CC_PREFIX)/include
  CLIB_USER_CFLAGS += -idirafter $(KERNEL_PREFIX)/include
  CLIB_USER_CFLAGS += -idirafter $(LIBC_PREFIX)/include
  CLIB_COMMON_CFLAGS += -idirafter $(CLIB_PREFIX)/include
endif

STATIC_CFLAGS = $(CLIB_USER_CFLAGS)
SHARED_CFLAGS = $(STATIC_CFLAGS) -fPIC

%.$(SHARED_OBJ): %.c
	$(CC) -c $(SHARED_CFLAGS) -o $@ $<

%.$(OBJ): %.c
	$(CC) -c $(STATIC_CFLAGS) -o $@ $<

# Kernel version of clib

CLIB_KERNEL_CFLAGS = $(CLIB_COMMON_CFLAGS)

CLIB_KERNEL_CFLAGS += -nostdinc
CLIB_KERNEL_CFLAGS += -idirafter $(CC_PREFIX)/include
CLIB_KERNEL_CFLAGS += -idirafter $(KERNEL_PREFIX)/include

# Kernel always uses mheap allocator (no malloc)
CLIB_KERNEL_CFLAGS += -DCLIB_MEM_MHEAP

CLIB_KERNEL_CFLAGS += -D__KERNEL__ -DMODULE -DEXPORT_SYMTAB

CLIB_KERNEL_CFLAGS += -fno-common -fomit-frame-pointer -fno-strict-aliasing

ifeq ($(findstring mips,$(CLIB_ARCH)),mips)
  CLIB_KERNEL_CFLAGS += -G0 \
	-mno-abicalls -fno-pic -mlong-calls \
	-mcpu=r8000 -mips2 -Wa,--trap 
endif

%.$(KERNEL_OBJ): %.c
	$(CC) $(CLIB_KERNEL_CFLAGS) -c -o $@ $<

# Dependencies
%.$(DEP): %.c
	$(CC) $(CLIB_USER_CFLAGS) -c -M $< | sed -e s/.o:/.$(OBJ):/ > $@

%.$(SHARED_DEP): %.c
	$(CC) $(CLIB_USER_CFLAGS) -c -M $< | sed -e s/.o:/.$(SHARED_OBJ):/ > $@

%.$(KERNEL_DEP): %.c
	$(CC) $(CLIB_KERNEL_CFLAGS) -c -M $< | sed -e s/.o:/.$(KERNEL_OBJ):/ > $@
><vnet/ip/ip4_packet.h> #include <vnet/ip/ip6_packet.h> #include <vnet/devices/virtio/virtio.h> virtio_main_t virtio_main; #define _IOCTL(fd,a,...) \ if (ioctl (fd, a, __VA_ARGS__) < 0) \ { \ err = clib_error_return_unix (0, "ioctl(" #a ")"); \ goto error; \ } static clib_error_t * call_read_ready (clib_file_t * uf) { virtio_main_t *nm = &virtio_main; vnet_main_t *vnm = vnet_get_main (); u16 qid = uf->private_data & 0xFFFF; virtio_if_t *vif = vec_elt_at_index (nm->interfaces, uf->private_data >> 16); u64 b; CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b)); if ((qid & 1) == 0) vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid); return 0; } clib_error_t * virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz) { clib_error_t *err = 0; virtio_vring_t *vring; struct vhost_vring_state state = { 0 }; struct vhost_vring_addr addr = { 0 }; struct vhost_vring_file file = { 0 }; clib_file_t t = { 0 }; int i; if (!is_pow2 (sz)) return clib_error_return (0, "ring size must be power of 2"); if (sz > 32768) return clib_error_return (0, "ring size must be 32768 or lower"); if (sz == 0) sz = 256; vec_validate_aligned (vif->vrings, idx, CLIB_CACHE_LINE_BYTES); vring = vec_elt_at_index (vif->vrings, idx); i = sizeof (struct vring_desc) * sz; i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); memset (vring->desc, 0, i); i = sizeof (struct vring_avail) + sz * sizeof (vring->avail->ring[0]); i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); memset (vring->avail, 0, i); // tell kernel that we don't need interrupt vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; i = sizeof (struct vring_used) + sz * sizeof (struct vring_used_elem); i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); memset (vring->used, 0, i); ASSERT (vring->buffers == 0); vec_validate_aligned (vring->buffers, sz, CLIB_CACHE_LINE_BYTES); vring->size = sz; vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); vring->kick_fd = eventfd (0, EFD_CLOEXEC); t.read_function = call_read_ready; t.file_descriptor = vring->call_fd; t.private_data = vif->dev_instance << 16 | idx; vring->call_file_index = clib_file_add (&file_main, &t); state.index = idx; state.num = sz; _IOCTL (vif->fd, VHOST_SET_VRING_NUM, &state); addr.index = idx; addr.flags = 0; addr.desc_user_addr = pointer_to_uword (vring->desc); addr.avail_user_addr = pointer_to_uword (vring->avail); addr.used_user_addr = pointer_to_uword (vring->used); _IOCTL (vif->fd, VHOST_SET_VRING_ADDR, &addr); file.index = idx; file.fd = vring->kick_fd; _IOCTL (vif->fd, VHOST_SET_VRING_KICK, &file); file.fd = vring->call_fd; _IOCTL (vif->fd, VHOST_SET_VRING_CALL, &file); file.fd = vif->tap_fd; _IOCTL (vif->fd, VHOST_NET_SET_BACKEND, &file); error: return err; } static_always_inline void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring) { u16 used = vring->desc_in_use; u16 next = vring->desc_next; u16 mask = vring->size - 1; while (used) { vlib_buffer_free (vm, &vring->buffers[next], 1); next = (next + 1) & mask; used--; } } clib_error_t * virtio_vring_free (vlib_main_t * vm, virtio_if_t * vif, u32 idx) { virtio_vring_t *vring = vec_elt_at_index (vif->vrings, idx); clib_file_del_by_index (&file_main, vring->call_file_index); close (vring->kick_fd); close (vring->call_fd); if (vring->used) { if ((idx & 1) == 1) virtio_free_used_desc (vm, vring); else virtio_free_rx_buffers (vm, vring); clib_mem_free (vring->used); } if (vring->desc) clib_mem_free (vring->desc); if (vring->avail) clib_mem_free (vring->avail); vec_free (vring->buffers); return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */