Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
Software for White Rabbit PTP Core
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
32
Issues
32
List
Board
Labels
Milestones
Merge Requests
7
Merge Requests
7
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
image/svg+xml
Discourse
Discourse
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Projects
Software for White Rabbit PTP Core
Commits
49d5fe99
Commit
49d5fe99
authored
Oct 13, 2015
by
Alessandro Rubini
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'pfilter-cleanup'
parents
fc7febc7
808ffdd2
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
501 additions
and
261 deletions
+501
-261
Makefile
Makefile
+16
-5
dev.mk
dev/dev.mk
+14
-0
ep_pfilter.c
dev/ep_pfilter.c
+67
-253
.gitignore
tools/.gitignore
+1
-0
Makefile
tools/Makefile
+1
-0
genramvhd.c
tools/genramvhd.c
+6
-3
pfilter-builder.c
tools/pfilter-builder.c
+396
-0
No files found.
Makefile
View file @
49d5fe99
...
...
@@ -14,6 +14,7 @@ SIZE = $(CROSS_COMPILE)size
AUTOCONF
=
$(CURDIR)
/include/generated/autoconf.h
PPSI
=
ppsi
PPSI_CONFIG
=
ppsi/include/generated/autoconf.h
# we miss CONFIG_ARCH_LM32 as we have no other archs by now
obj-y
=
arch
/lm32/crt0.o
arch
/lm32/irq.o
...
...
@@ -55,6 +56,14 @@ CFLAGS_PLATFORM = -mmultiply-enabled -mbarrel-shift-enabled
LDFLAGS_PLATFORM
=
-mmultiply-enabled
-mbarrel-shift-enabled
\
-nostdlib
-T
$
(
LDS-y
)
# packet-filter rules depend on configuration; default is rules-plain
pfilter-y
:=
rules-plain.bin
pfilter-$(CONFIG_ETHERBONE)
:
= rules-ebone.bin
pfilter-$(CONFIG_NIC_PFILTER)
:
= rules-e+nic.bin
export
pfilter-y
all
:
include
shell/shell.mk
include
lib/lib.mk
include
pp_printf/printf.mk
...
...
@@ -73,6 +82,7 @@ obj-y += check-error.o
# add system check functions like stack overflow and check reset
obj-y
+=
system_checks.o
# WR node has SDB support, WR switch does not
obj-$(CONFIG_WR_NODE)
+=
sdb-lib/libsdbfs.a
cflags-$(CONFIG_WR_NODE)
+=
-Isdb-lib
...
...
@@ -138,22 +148,23 @@ config.o: .config
./tools/genraminit
$*
.bin 0
>
$@
%.vhd
:
tools %.bin
./tools/genramvhd
-s
`
.
./.config
;
echo
$$
CONFIG_RAMSIZE
`
$*
.bin
>
$@
./tools/genramvhd
-s
$(CONFIG_RAMSIZE)
$*
.bin
>
$@
%.mif
:
tools %.bin
./tools/genrammif
$*
.bin
`
.
./.config
;
echo
$$
CONFIG_RAMSIZE
`
>
$@
./tools/genrammif
$*
.bin
$(CONFIG_RAMSIZE)
>
$@
$(AUTOCONF)
:
silentoldconfig
clean
:
rm
-f
$(OBJS)
$(OUTPUT)
.elf
$(OUTPUT)
.bin
$(OUTPUT)
.ram
\
$(LDS)
.depend
$(LDS)
rules-
*
.bin
.depend
$(MAKE)
-C
$(PPSI)
clean
$(MAKE)
-C
sdb-lib clean
$(MAKE)
-C
tools clean
%.o
:
%.c
${
CC
}
$(CFLAGS)
$(PTPD_CFLAGS)
$(INCLUDE_DIR)
$(LIB_DIR)
-c
$*
.c
-o
$@
${
CC
}
$(CFLAGS)
$(PTPD_CFLAGS)
$(INCLUDE_DIR)
$(LIB_DIR)
\
-include
$(PPSI_CONFIG)
-c
$*
.c
-o
$@
tools
:
$(MAKE)
-C
tools
...
...
@@ -186,4 +197,4 @@ defconfig:
.depend
:
$(wildcard *.c [^pt]/*.c)
$(CC)
$(CFLAGS)
-DSDBFS_BIG_ENDIAN
-MM
$^
>
$@
-include
.depend
\ No newline at end of file
-include
.depend
dev/dev.mk
View file @
49d5fe99
obj-$(CONFIG_WR_NODE) += \
dev/endpoint.o \
dev/ep_pfilter.o \
dev/pfilter-rules.o \
dev/i2c.o \
dev/minic.o \
dev/pps_gen.o \
...
...
@@ -19,3 +20,16 @@ obj-$(CONFIG_W1) += dev/w1.o dev/w1-hw.o dev/w1-shell.o
obj-$(CONFIG_W1) += dev/w1-temp.o dev/w1-eeprom.o
obj-$(CONFIG_UART) += dev/uart.o
obj-$(CONFIG_UART_SW) += dev/uart-sw.o
# Filter rules are selected according to configuration, see toplevel Makefile,
# but the filename is reflected in symbol names, so use a symlink here.
dev/pfilter-rules.o: $(pfilter-y)
ln -sf $(pfilter-y) rules-pfilter.bin
$(OBJCOPY) -I binary -O elf32-lm32 -B lm32 rules-pfilter.bin $@
$(pfilter-y): tools/pfilter-builder
$^
tools/pfilter-builder:
$(MAKE) -C tools pfilter-builder
dev/ep_pfilter.c
View file @
49d5fe99
/*
* This work is part of the White Rabbit project
*
* Copyright (C) 2011 CERN (www.cern.ch)
* Copyright (C) 2011
,2014
CERN (www.cern.ch)
* Copyright (C) 2012 GSI (www.gsi.de)
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
* Author: Wesley W. Terpstra <w.terpstra@gsi.de>
* Author: Alessandro rubini (for the build-time creation of the array)
*
* Released according to the GNU GPL, version 2 or any later version.
*/
/* Endpoint Packet Filter/Classifier driver
A little explanation: The WR core needs to classify the incoming packets into
two (or more categories):
- PTP, ARP, DHCP packets, which should go to the WRCore CPU packet queue (mini-nic)
- Other packets matching user's provided pattern, which shall go to the external fabric
port - for example to Etherbone, host network controller, etc.
- packets to be dropped (used neither by the WR Core or the user application)
WR Endpoint (WR MAC) inside the WR Core therefore contains a simple microprogrammable
packet filter/classifier. The classifier processes the incoming packet, and assigns it
to one of 8 classes (an 8-bit word, where each bit corresponds to a particular class) or
eventually drops it. Hardware implementation of the unit is a simple VLIW processor with
32 single-bit registers (0 - 31). The registers are organized as follows:
- 0: don't touch (always 0)
- 1 - 22: general purpose registers
- 23: drop packet flag: if 1 at the end of the packet processing, the packet will be dropped.
- 24..31: packet class (class 0 = reg 24, class 7 = reg 31).
Program memory has 64 36-bit words. Packet filtering program is restarted every time a new packet comes.
There are 5 possible instructions:
1. CMP offset, value, mask, oper, Rd:
------------------------------------------
* Rd = Rd oper ((((uint16_t *)packet) [offset] & mask) == value)
Examples:
* CMP 3, 0xcafe, 0xffff, MOV, Rd
will compare the 3rd word of the packet (bytes 6, 7) against 0xcafe and if the words are equal,
1 will be written to Rd register.
* CMP 4, 0xbabe, 0xffff, AND, Rd
will do the same with the 4th word and write to Rd its previous value ANDed with the result
of the comparison. Effectively, Rd now will be 1 only if bytes [6..9] of the payload contain word
0xcafebabe.
Note that the mask value is nibble-granular. That means you can choose a particular
set of nibbles within a word to be compared, but not an arbitrary set of bits (e.g. 0xf00f, 0xff00
and 0xf0f0 masks are ok, but 0x8001 is wrong.
2. BTST offset, bit_number, oper, Rd
------------------------------------------
* Rd = Rd oper (((uint16_t *)packet) [offset] & (1<<bit_number) ? 1 : 0)
Examples:
* BTST 3, 10, MOV, 11
will write 1 to reg 11 if the 10th bit in the 3rd word of the packet is set (and 0 if it's clear)
3. Logic opearations:
-----------------------------------------
* LOGIC2 Rd, Ra, OPER Rb - 2 argument logic (Rd = Ra OPER Rb). If the operation is MOV or NOT, Ra is
taken as the source register.
* LOGIC3 Rd, Ra, OPER Rb, OPER2, Rc - 3 argument logic Rd = (Ra OPER Rb) OPER2 Rc.
4. Misc
-----------------------------------------
FIN instruction terminates the program.
NOP executes a dummy instruction (LOGIC2 0, 0, AND, 0)
IMPORTANT:
- the program counter is advanved each time a 16-bit words of the packet arrives.
- the CPU doesn't have any interlocks to simplify the HW, so you can't compare the
10th word when PC = 2. Max comparison offset is always equal to the address of the instruction.
- Code may contain up to 64 operations, but it must classify shorter packets faster than in
32 instructions (there's no flow throttling)
*/
/*
* The packet filter documentation is moved to tools/pfilter-builder.c, where
* the actual packet filter rules are created
*/
#include <stdio.h>
#include "board.h"
#include <endpoint.h>
#include <hw/endpoint_regs.h>
#define PFILTER_MAX_CODE_SIZE 32
extern
uint32_t
_binary_rules_pfilter_bin_start
[];
extern
uint32_t
_binary_rules_pfilter_bin_end
[];
#define pfilter_dbg
(x, ...)
/* nothing */
#define pfilter_dbg
pp_printf
extern
volatile
struct
EP_WB
*
EP
;
static
const
uint64_t
PF_MODE_LOGIC
=
(
1ULL
<<
34
);
static
const
uint64_t
PF_MODE_CMP
=
0ULL
;
static
int
code_pos
;
static
uint64_t
code_buf
[
32
];
/* begins assembling a new packet filter program */
static
void
pfilter_new
(
void
)
{
code_pos
=
0
;
}
static
void
check_size
(
void
)
{
if
(
code_pos
==
PFILTER_MAX_CODE_SIZE
-
1
)
{
pfilter_dbg
(
"microcode: code too big (max size: %d)
\n
"
,
PFILTER_MAX_CODE_SIZE
);
}
}
static
void
check_reg_range
(
int
val
,
int
minval
,
int
maxval
,
char
*
name
)
{
if
(
val
<
minval
||
val
>
maxval
)
{
pfilter_dbg
(
"microcode: %s register out of range (%d to %d)"
,
name
,
minval
,
maxval
);
}
}
static
void
pfilter_cmp
(
int
offset
,
int
value
,
int
mask
,
pfilter_op_t
op
,
int
rd
)
{
uint64_t
ir
;
check_size
();
if
(
offset
>
code_pos
)
pfilter_dbg
(
"microcode: comparison offset is bigger than current PC. Insert some nops before comparing"
);
check_reg_range
(
rd
,
1
,
15
,
"ra/rd"
);
ir
=
(
PF_MODE_CMP
|
((
uint64_t
)
offset
<<
7
)
|
((
mask
&
0x1
)
?
(
1ULL
<<
29
)
:
0
)
|
((
mask
&
0x10
)
?
(
1ULL
<<
30
)
:
0
)
|
((
mask
&
0x100
)
?
(
1ULL
<<
31
)
:
0
)
|
((
mask
&
0x1000
)
?
(
1ULL
<<
32
)
:
0
))
|
op
|
(
rd
<<
3
);
ir
=
ir
|
((
uint64_t
)
value
&
0xffffULL
)
<<
13
;
code_buf
[
code_pos
++
]
=
ir
;
}
static
void
pfilter_nop
(
void
)
{
uint64_t
ir
;
check_size
();
ir
=
PF_MODE_LOGIC
;
code_buf
[
code_pos
++
]
=
ir
;
}
// rd = ra op rb
static
void
pfilter_logic2
(
int
rd
,
int
ra
,
pfilter_op_t
op
,
int
rb
)
{
uint64_t
ir
;
check_size
();
check_reg_range
(
ra
,
0
,
31
,
"ra"
);
check_reg_range
(
rb
,
0
,
31
,
"rb"
);
check_reg_range
(
rd
,
1
,
31
,
"rd"
);
ir
=
((
uint64_t
)
ra
<<
8
)
|
((
uint64_t
)
rb
<<
13
)
|
(((
uint64_t
)
rd
&
0xf
)
<<
3
)
|
(((
uint64_t
)
rd
&
0x10
)
?
(
1ULL
<<
7
)
:
0
)
|
(
uint64_t
)
op
;
ir
=
ir
|
PF_MODE_LOGIC
|
(
3ULL
<<
23
);
code_buf
[
code_pos
++
]
=
ir
;
}
static
void
pfilter_logic3
(
int
rd
,
int
ra
,
pfilter_op_t
op
,
int
rb
,
pfilter_op_t
op2
,
int
rc
)
static
uint32_t
swap32
(
uint32_t
v
)
{
uint64_t
ir
;
check_size
();
check_reg_range
(
ra
,
0
,
31
,
"ra"
);
check_reg_range
(
rb
,
0
,
31
,
"rb"
);
check_reg_range
(
rc
,
0
,
31
,
"rc"
);
check_reg_range
(
rd
,
1
,
31
,
"rd"
);
uint32_t
res
;
ir
=
(
ra
<<
8
)
|
(
rb
<<
13
)
|
(
rc
<<
18
)
|
((
rd
&
0xf
)
<<
3
)
|
((
rd
&
0x10
)
?
(
1
<<
7
)
:
0
)
|
op
;
ir
=
ir
|
PF_MODE_LOGIC
|
(
op2
<<
23
);
code_buf
[
code_pos
++
]
=
ir
;
res
=
(
v
&
0xff000000
)
>>
24
;
res
|=
(
v
&
0x00ff0000
)
>>
8
;
res
|=
(
v
&
0x0000ff00
)
<<
8
;
res
|=
(
v
&
0x000000ff
)
<<
24
;
return
res
;
}
/* Terminates the microcode, loads it to the endpoint and enables the pfilter */
static
void
pfilter_load
(
void
)
void
pfilter_init_default
(
void
)
{
/* Use shorter names to avoid getting mad */
uint32_t
*
vini
=
_binary_rules_pfilter_bin_start
;
uint32_t
*
vend
=
_binary_rules_pfilter_bin_end
;
uint32_t
m
,
*
v
;
uint64_t
cmd_word
;
int
i
;
code_buf
[
code_pos
++
]
=
(
1ULL
<<
35
);
// insert FIN instruction
static
int
inited
;
/*
* The array of words starts with 0x11223344 so we
* can fix endianness. Do it.
*/
v
=
vini
;
m
=
v
[
0
];
if
(
m
!=
0x11223344
)
for
(
v
=
vini
;
v
<
vend
;
v
++
)
*
v
=
swap32
(
*
v
);
v
=
vini
;
if
(
v
[
0
]
!=
0x11223344
)
{
pp_printf
(
"pfilter: wrong magic number (got 0x%x)
\n
"
,
m
);
return
;
}
v
++
;
/*
* First time: be extra-careful that the rule-set is ok. But if
* we change MAC address, this is re-called, and v[] is already changed
*/
if
(
!
inited
)
{
if
(
(((
v
[
2
]
>>
13
)
&
0xffff
)
!=
0x1234
)
||
(((
v
[
4
]
>>
13
)
&
0xffff
)
!=
0x5678
)
||
(((
v
[
6
]
>>
13
)
&
0xffff
)
!=
0x9abc
))
{
pp_printf
(
"pfilter: wrong rule-set, can't apply
\n
"
);
return
;
}
inited
++
;
}
/* Patch the local MAC address in place, in the first three instructions */
v
[
0
]
&=
~
(
0xffff
<<
13
);
v
[
2
]
&=
~
(
0xffff
<<
13
);
v
[
4
]
&=
~
(
0xffff
<<
13
);
v
[
0
]
|=
(
EP
->
MACH
>>
0
)
&
0xffff
<<
13
;
v
[
2
]
|=
(
EP
->
MACL
>>
16
)
&
0xffff
<<
13
;
v
[
4
]
|=
(
EP
->
MACL
>>
0
)
&
0xffff
<<
13
;
EP
->
PFCR0
=
0
;
// disable pfilter
for
(
i
=
0
;
i
<
code_pos
;
i
++
)
{
for
(
i
=
0
;
v
<
vend
;
v
+=
2
,
i
++
)
{
uint32_t
cr0
,
cr1
;
cr1
=
EP_PFCR1_MM_DATA_LSB_W
(
code_buf
[
i
]
&
0xfff
);
cr0
=
EP_PFCR0_MM_ADDR_W
(
i
)
|
EP_PFCR0_MM_DATA_MSB_W
(
code_buf
[
i
]
>>
12
)
|
cmd_word
=
v
[
0
]
|
((
uint64_t
)
v
[
1
]
<<
32
);
pfilter_dbg
(
"pos %02i: %x.%08x
\n
"
,
i
,
(
uint32_t
)(
cmd_word
>>
32
),
(
uint32_t
)(
cmd_word
));
cr1
=
EP_PFCR1_MM_DATA_LSB_W
(
cmd_word
&
0xfff
);
cr0
=
EP_PFCR0_MM_ADDR_W
(
i
)
|
EP_PFCR0_MM_DATA_MSB_W
(
cmd_word
>>
12
)
|
EP_PFCR0_MM_WRITE_MASK
;
EP
->
PFCR1
=
cr1
;
...
...
@@ -206,86 +103,3 @@ static void pfilter_load(void)
EP
->
PFCR0
=
EP_PFCR0_ENABLE
;
}
/* sample packet filter initialization:
- redirects broadcasts and PTP packets to the WR Core
- redirects unicasts addressed to self with ethertype 0xa0a0 to the external fabric */
#define R_CLASS(x) (24 + x)
#define R_DROP 23
void
pfilter_init_default
()
{
pfilter_new
();
pfilter_nop
();
pfilter_cmp
(
0
,
0xffff
,
0xffff
,
MOV
,
1
);
pfilter_cmp
(
1
,
0xffff
,
0xffff
,
AND
,
1
);
pfilter_cmp
(
2
,
0xffff
,
0xffff
,
AND
,
1
);
/* r1 = 1 when dst mac is broadcast */
pfilter_cmp
(
0
,
0x011b
,
0xffff
,
MOV
,
2
);
pfilter_cmp
(
1
,
0x1900
,
0xffff
,
AND
,
2
);
pfilter_cmp
(
2
,
0x0000
,
0xffff
,
AND
,
2
);
/* r2 = 1 when dst mac is PTP multicast (01:1b:19:00:00:00) */
pfilter_cmp
(
0
,
EP
->
MACH
&
0xffff
,
0xffff
,
MOV
,
3
);
pfilter_cmp
(
1
,
EP
->
MACL
>>
16
,
0xffff
,
AND
,
3
);
pfilter_cmp
(
2
,
EP
->
MACL
&
0xffff
,
0xffff
,
AND
,
3
);
/* r3 = 1 when the packet is unicast to our own MAC */
pfilter_cmp
(
6
,
0x0800
,
0xffff
,
MOV
,
4
);
/* r4 = 1 when ethertype = IPv4 */
pfilter_cmp
(
6
,
0x88f7
,
0xffff
,
MOV
,
5
);
/* r5 = 1 when ethertype = PTPv2 */
pfilter_cmp
(
6
,
0x0806
,
0xffff
,
MOV
,
6
);
/* r6 = 1 when ethertype = ARP */
pfilter_cmp
(
6
,
0xdbff
,
0xffff
,
MOV
,
9
);
/* r9 = 1 when ethertype = streamer */
/* Ethernet = 14 bytes, Offset to type in IP: 8 bytes = 22/2 = 11 */
pfilter_cmp
(
11
,
0x0001
,
0x00ff
,
MOV
,
7
);
/* r7 = 1 when IP type = ICMP */
pfilter_cmp
(
11
,
0x0011
,
0x00ff
,
MOV
,
8
);
/* r8 = 1 when IP type = UDP */
#ifdef CONFIG_ETHERBONE
pfilter_logic3
(
10
,
3
,
OR
,
0
,
AND
,
4
);
/* r10 = IP(unicast) */
pfilter_logic3
(
11
,
1
,
OR
,
3
,
AND
,
4
);
/* r11 = IP(unicast+broadcast) */
pfilter_logic3
(
14
,
1
,
AND
,
6
,
OR
,
5
);
/* r14 = ARP(broadcast) or PTPv2 */
pfilter_logic3
(
15
,
10
,
AND
,
7
,
OR
,
14
);
/* r15 = ICMP/IP(unicast) or ARP(broadcast) or PTPv2 */
/* Ethernet = 14 bytes, IPv4 = 20 bytes, offset to dport: 2 = 36/2 = 18 */
pfilter_cmp
(
18
,
0x0044
,
0xffff
,
MOV
,
14
);
/* r14 = 1 when dport = BOOTPC */
pfilter_logic3
(
14
,
14
,
AND
,
8
,
AND
,
11
);
/* r14 = BOOTP/UDP/IP(unicast|broadcast) */
pfilter_logic2
(
15
,
14
,
OR
,
15
);
/* r15 = BOOTP/UDP/IP(unicast|broadcast) or ICMP/IP(unicast) or ARP(broadcast) or PTPv2 */
#ifdef CONFIG_NIC_PFILTER
pfilter_cmp
(
18
,
0xebd0
,
0xffff
,
MOV
,
6
);
/* r6 = 1 when dport = ETHERBONE */
//pfilter_cmp(21,0x4e6f,0xffff,MOV,9); /* r9 = 1 when magic number = ETHERBONE */
//pfilter_logic2(6,6,AND,9);
pfilter_logic2
(
R_CLASS
(
0
),
15
,
MOV
,
0
);
/* class 0: ICMP/IP(unicast) or ARP(broadcast) or PTPv2 => PTP LM32 core */
pfilter_logic2
(
R_CLASS
(
5
),
6
,
OR
,
0
);
/* class 5: Etherbone packet => Etherbone Core */
pfilter_logic3
(
R_CLASS
(
7
),
15
,
OR
,
6
,
NOT
,
0
);
/* class 7: Rest => NIC Core */
#else
pfilter_logic3
(
20
,
11
,
AND
,
8
,
OR
,
15
);
/* r16 = Something we accept */
pfilter_logic3
(
R_DROP
,
20
,
OR
,
9
,
NOT
,
0
);
/* None match? drop */
pfilter_logic2
(
R_CLASS
(
7
),
11
,
AND
,
8
);
/* class 7: UDP/IP(unicast|broadcast) => external fabric */
pfilter_logic2
(
R_CLASS
(
6
),
1
,
AND
,
9
);
/* class 6: streamer broadcasts => external fabric */
pfilter_logic2
(
R_CLASS
(
0
),
15
,
MOV
,
0
);
/* class 0: ICMP/IP(unicast) or ARP(broadcast) or PTPv2 => PTP LM32 core */
#endif
#else
pfilter_logic3
(
10
,
3
,
OR
,
2
,
AND
,
5
);
/* r10 = PTP (multicast or unicast) */
pfilter_logic2
(
11
,
1
,
AND
,
9
);
/* r11 = streamer broadcast */
pfilter_logic3
(
12
,
10
,
OR
,
11
,
NOT
,
0
);
/* r12 = all non-PTP and non-streamer traffic */
pfilter_logic2
(
R_CLASS
(
7
),
12
,
MOV
,
0
);
/* class 7: all non PTP and non-streamer
traffic => external fabric */
pfilter_logic2
(
R_CLASS
(
6
),
11
,
MOV
,
0
);
/* class 6: streamer broadcasts =>
external fabric */
pfilter_logic2
(
R_CLASS
(
0
),
10
,
MOV
,
0
);
/* class 0: PTP frames => LM32 */
#endif
pfilter_load
();
}
tools/.gitignore
View file @
49d5fe99
...
...
@@ -8,3 +8,4 @@ eb-w1-write
sdb-wrpc.bin
flash-read
flash-write
pfilter-builder
tools/Makefile
View file @
49d5fe99
...
...
@@ -5,6 +5,7 @@ CFLAGS = -Wall -ggdb -I../include
LDFLAGS
=
-lutil
ALL
=
genraminit genramvhd genrammif wrpc-uart-sw
ALL
+=
wrpc-w1-read wrpc-w1-write
ALL
+=
pfilter-builder
ifneq
($(EB),no)
ALL
+=
eb-w1-write
...
...
tools/genramvhd.c
View file @
49d5fe99
...
...
@@ -42,12 +42,12 @@ void help()
}
/* We don't want localized versions from ctype.h */
inline
int
my_isalpha
(
char
c
)
static
int
my_isalpha
(
char
c
)
{
return
(
c
>=
'a'
&&
c
<=
'z'
)
||
(
c
>=
'A'
&&
c
<=
'Z'
);
}
inline
int
my_isok
(
char
c
)
static
int
my_isok
(
char
c
)
{
return
c
==
'_'
||
my_isalpha
(
c
)
||
(
c
>=
'0'
&&
c
<=
'9'
);
}
...
...
@@ -124,9 +124,12 @@ int main(int argc, char **argv)
fprintf
(
stderr
,
"%s: expecting one non-optional argument: <filename>
\n
"
,
program
);
return
1
;
error
=
1
;
}
if
(
error
)
return
1
;
filename
=
argv
[
optind
];
/* Confirm the filename exists */
...
...
tools/pfilter-builder.c
0 → 100644
View file @
49d5fe99
/*
* This work is part of the White Rabbit project
*
* Copyright (C) 2011 CERN (www.cern.ch)
* Copyright (C) 2012 GSI (www.gsi.de)
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
* Author: Wesley W. Terpstra <w.terpstra@gsi.de>
*
* Released according to the GNU GPL, version 2 or any later version.
*/
/* Endpoint Packet Filter/Classifier driver
A little explanation: The WR core needs to classify the incoming packets into
two (or more categories):
- PTP, ARP, DHCP packets, which should go to the WRCore CPU packet queue (mini-nic)
- Other packets matching user's provided pattern, which shall go to the external fabric
port - for example to Etherbone, host network controller, etc.
- packets to be dropped (used neither by the WR Core or the user application)
0. Introduction
------------------------------------------
WR Endpoint (WR MAC) inside the WR Core therefore contains a simple microprogrammable
packet filter/classifier. The classifier processes the incoming packet, and assigns it
to one of 8 classes (an 8-bit word, where each bit corresponds to a particular class) or
eventually drops it. Hardware implementation of the unit is a simple VLIW processor with
32 single-bit registers (0 - 31). The registers are organized as follows:
- 0: don't touch (always 0)
- 1 - 22: general purpose registers
- 23: drop packet flag: if 1 at the end of the packet processing, the packet will be dropped.
- 24..31: packet class (class 0 = reg 24, class 7 = reg 31). -- see 2. below for "routing" rules.
Program memory has 64 36-bit words. Packet filtering program is restarted every time a new packet comes.
There are 5 possible instructions.
1. Instructions
------------------------------------------
1.1 CMP offset, value, mask, oper, Rd:
------------------------------------------
* Rd = Rd oper ((((uint16_t *)packet) [offset] & mask) == value)
Examples:
* CMP 3, 0xcafe, 0xffff, MOV, Rd
will compare the 3rd word of the packet (bytes 6, 7) against 0xcafe and if the words are equal,
1 will be written to Rd register.
* CMP 4, 0xbabe, 0xffff, AND, Rd
will do the same with the 4th word and write to Rd its previous value ANDed with the result
of the comparison. Effectively, Rd now will be 1 only if bytes [6..9] of the payload contain word
0xcafebabe.
Note that the mask value is nibble-granular. That means you can choose a particular
set of nibbles within a word to be compared, but not an arbitrary set of bits (e.g. 0xf00f, 0xff00
and 0xf0f0 masks are ok, but 0x8001 is wrong.
1.2. BTST offset, bit_number, oper, Rd
------------------------------------------
* Rd = Rd oper (((uint16_t *)packet) [offset] & (1<<bit_number) ? 1 : 0)
Examples:
* BTST 3, 10, MOV, 11
will write 1 to reg 11 if the 10th bit in the 3rd word of the packet is set (and 0 if it's clear)
1.3. Logic opearations:
-----------------------------------------
* LOGIC2 Rd, Ra, OPER Rb - 2 argument logic (Rd = Ra OPER Rb). If the operation is MOV or NOT, Ra is
taken as the source register.
* LOGIC3 Rd, Ra, OPER Rb, OPER2, Rc - 3 argument logic Rd = (Ra OPER Rb) OPER2 Rc.
1.4. Misc
-----------------------------------------
FIN instruction terminates the program.
NOP executes a dummy instruction (LOGIC2 0, 0, AND, 0)
IMPORTANT:
- the program counter is advanved each time a 16-bit words of the packet arrives.
- the CPU doesn't have any interlocks to simplify the HW, so you can't compare the
10th word when PC = 2. Max comparison offset is always equal to the address of the instruction.
- Code may contain up to 64 operations, but it must classify shorter packets faster than in
32 instructions (there's no flow throttling)
2. How the frame is routed after the pfilter
-----------------------------------------
After the input pipeline is over, the endpoint looks at the DROP and CLASS bits
set by the packet filter. There are two possible output ports: one associated with
classes 0..3 (to the cpu) and one associated to class 4..7 (external fabric).
These are the rules, in strict priority order.
- If "DROP" is set, the frame is dropped irrespective of the rest. Done.
- If at least one bit is set in the first set (bits 0..3), the frame goes to CPU. Done.
- If at least one bit is set in the second set (bits 4..7) the frame goes to fabric. Done.
- No class is set, the frame goes to the fabric.
If, in the future or other implementations, the same pfilter is used with a differnet
set of bits connected to the output ports (e.g. three ports), ports are processed
from lowest-number to highest-number, and if no bit is set it goes to the last.
Please note that the "class" is actually a bitmask; it's ok to set more than one bit
in a single nibble, and the downstream user will find both set (for CPU we have the
class in the status register).
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <endpoint.h>
/* for operations and type pfilter_op_t */
#define PFILTER_MAX_CODE_SIZE 32
#define pfilter_dbg(x, ...)
/* nothing */
char
*
prgname
;
extern
volatile
struct
EP_WB
*
EP
;
static
const
uint64_t
PF_MODE_LOGIC
=
(
1ULL
<<
34
);
static
const
uint64_t
PF_MODE_CMP
=
0ULL
;
static
int
code_pos
;
static
uint64_t
code_buf
[
PFILTER_MAX_CODE_SIZE
];
enum
pf_regs
{
R_ZERO
=
1024
,
R_1
,
R_2
,
R_3
,
R_4
,
R_5
,
R_6
,
R_7
,
R_8
,
R_9
,
R_10
,
R_11
,
R_12
,
R_13
,
R_14
,
R_15
,
R_16
,
R_17
,
R_18
,
R_19
,
R_20
,
R_21
,
R_22
,
R_DROP
,
R_C0
,
R_C1
,
R_C2
,
R_C3
,
R_C4
,
R_C5
,
R_C6
,
R_C7
};
#define R_CLASS(x) (R_C0 + x)
/* Give also "symbolic" names, to the roles of each register. */
enum
pf_symbolic_regs
{
/* The first set is used for straight comparisons */
FRAME_BROADCAST
=
R_1
,
FRAME_PTP_MCAST
,
FRAME_OUR_MAC
,
FRAME_TYPE_IPV4
,
FRAME_TYPE_PTP2
,
FRAME_TYPE_ARP
,
FRAME_ICMP
,
FRAME_UDP
,
FRAME_TYPE_STREAMER
,
/* An ethtype by Tom, used in gateware */
FRAME_PORT_ETHERBONE
,
/* These are results of logic over the previous bits */
FRAME_IP_UNI
,
FRAME_IP_OK
,
/* unicast or broadcast */
FRAME_PTP_OK
,
FRAME_STREAMER_BCAST
,
/* A temporary register, and the CPU target */
R_TMP
,
FRAME_FOR_CPU
,
/* must be last */
};
int
v1
[
R_C7
-
(
R_ZERO
+
31
)];
/* fails if we lost a register */
int
v2
[(
R_ZERO
+
31
)
-
R_C7
];
/* fails if we added a register */
int
v3
[
0
-
((
int
)
FRAME_FOR_CPU
>
R_22
)];
/* begins assembling a new packet filter program */
static
void
pfilter_new
(
void
)
{
code_pos
=
0
;
}
static
void
check_size
(
void
)
{
if
(
code_pos
==
PFILTER_MAX_CODE_SIZE
-
1
)
{
fprintf
(
stderr
,
"%s: microcode too big (max size: %d)
\n
"
,
prgname
,
PFILTER_MAX_CODE_SIZE
);
exit
(
1
);
}
}
static
void
check_reg_range
(
int
val
,
int
minval
,
int
maxval
,
char
*
name
)
{
if
(
val
<
minval
||
val
>
maxval
)
{
fprintf
(
stderr
,
"%s: register
\"
%s
\"
out of range (%d to %d)"
,
prgname
,
name
,
minval
,
maxval
);
exit
(
1
);
}
}
static
void
pfilter_cmp
(
int
offset
,
int
value
,
int
mask
,
pfilter_op_t
op
,
int
rd
)
{
uint64_t
ir
;
check_size
();
if
(
offset
>
code_pos
)
{
fprintf
(
stderr
,
"%s: comparison offset is bigger than current PC. Insert some nops before comparing"
,
prgname
);
exit
(
1
);
}
check_reg_range
(
rd
,
R_1
,
R_15
,
"ra/rd"
);
rd
-=
R_ZERO
;
ir
=
(
PF_MODE_CMP
|
((
uint64_t
)
offset
<<
7
)
|
((
mask
&
0x1
)
?
(
1ULL
<<
29
)
:
0
)
|
((
mask
&
0x10
)
?
(
1ULL
<<
30
)
:
0
)
|
((
mask
&
0x100
)
?
(
1ULL
<<
31
)
:
0
)
|
((
mask
&
0x1000
)
?
(
1ULL
<<
32
)
:
0
))
|
op
|
(
rd
<<
3
);
ir
=
ir
|
((
uint64_t
)
value
&
0xffffULL
)
<<
13
;
code_buf
[
code_pos
++
]
=
ir
;
}
static
void
pfilter_nop
(
void
)
{
uint64_t
ir
;
check_size
();
ir
=
PF_MODE_LOGIC
;
code_buf
[
code_pos
++
]
=
ir
;
}
// rd = ra op rb
static
void
pfilter_logic2
(
int
rd
,
int
ra
,
pfilter_op_t
op
,
int
rb
)
{
uint64_t
ir
;
check_size
();
check_reg_range
(
ra
,
R_ZERO
,
R_C7
,
"ra"
);
ra
-=
R_ZERO
;
check_reg_range
(
rb
,
R_ZERO
,
R_C7
,
"rb"
);
rb
-=
R_ZERO
;
check_reg_range
(
rd
,
R_1
,
R_C7
,
"rd"
);
rd
-=
R_ZERO
;
ir
=
((
uint64_t
)
ra
<<
8
)
|
((
uint64_t
)
rb
<<
13
)
|
(((
uint64_t
)
rd
&
0xf
)
<<
3
)
|
(((
uint64_t
)
rd
&
0x10
)
?
(
1ULL
<<
7
)
:
0
)
|
(
uint64_t
)
op
;
ir
=
ir
|
PF_MODE_LOGIC
|
(
3ULL
<<
23
);
code_buf
[
code_pos
++
]
=
ir
;
}
static
void
pfilter_logic3
(
int
rd
,
int
ra
,
pfilter_op_t
op
,
int
rb
,
pfilter_op_t
op2
,
int
rc
)
{
uint64_t
ir
;
check_size
();
check_reg_range
(
ra
,
R_ZERO
,
R_C7
,
"ra"
);
ra
-=
R_ZERO
;
check_reg_range
(
rb
,
R_ZERO
,
R_C7
,
"rb"
);
rb
-=
R_ZERO
;
check_reg_range
(
rc
,
R_ZERO
,
R_C7
,
"rc"
);
rc
-=
R_ZERO
;
check_reg_range
(
rd
,
R_1
,
R_C7
,
"rd"
);
rd
-=
R_ZERO
;
ir
=
(
ra
<<
8
)
|
(
rb
<<
13
)
|
(
rc
<<
18
)
|
((
rd
&
0xf
)
<<
3
)
|
((
rd
&
0x10
)
?
(
1
<<
7
)
:
0
)
|
op
;
ir
=
ir
|
PF_MODE_LOGIC
|
(
op2
<<
23
);
code_buf
[
code_pos
++
]
=
ir
;
}
/* Terminates the microcode, creating the output file */
static
void
pfilter_output
(
char
*
fname
)
{
uint32_t
v1
,
v2
;
int
i
;
FILE
*
f
;
code_buf
[
code_pos
++
]
=
(
1ULL
<<
35
);
// insert FIN instruction
f
=
fopen
(
fname
,
"w"
);
if
(
!
f
)
{
fprintf
(
stderr
,
"%s: %s: %s
\n
"
,
prgname
,
fname
,
strerror
(
errno
));
exit
(
1
);
}
/* First write a magic word, so the target can check endianness */
v1
=
0x11223344
;
fwrite
(
&
v1
,
sizeof
(
v1
),
1
,
f
);
pfilter_dbg
(
"Writing
\"
%s
\"\n
"
,
fname
);
for
(
i
=
0
;
i
<
code_pos
;
i
++
)
{
pfilter_dbg
(
" pos %02i: %x.%08x
\n
"
,
i
,
(
uint32_t
)(
code_buf
[
i
]
>>
32
),
(
uint32_t
)(
code_buf
[
i
]));
/* Explicitly write the LSB first */
v1
=
code_buf
[
i
];
v2
=
code_buf
[
i
]
>>
32
;
fwrite
(
&
v1
,
sizeof
(
v1
),
1
,
f
);
fwrite
(
&
v2
,
sizeof
(
v2
),
1
,
f
);
}
fclose
(
f
);
}
/* We generate all supported rule-sets, those that used to be ifdef'd */
#define MODE_ETHERBONE 1
#define MODE_NIC_PFILTER 2
void
pfilter_init
(
int
mode
,
char
*
fname
)
{
pfilter_new
();
pfilter_nop
();
/*
* Make three sets of comparisons over the destination address.
* After these 9 instructions, the whole Eth header is available.
*/
pfilter_cmp
(
0
,
0x1234
,
0xffff
,
MOV
,
FRAME_OUR_MAC
);
/* Use fake MAC: 12:34:56:78:9a:bc */
pfilter_cmp
(
1
,
0x5678
,
0xffff
,
AND
,
FRAME_OUR_MAC
);
pfilter_cmp
(
2
,
0x9abc
,
0xffff
,
AND
,
FRAME_OUR_MAC
);
/* set when our MAC */
pfilter_cmp
(
0
,
0xffff
,
0xffff
,
MOV
,
FRAME_BROADCAST
);
pfilter_cmp
(
1
,
0xffff
,
0xffff
,
AND
,
FRAME_BROADCAST
);
pfilter_cmp
(
2
,
0xffff
,
0xffff
,
AND
,
FRAME_BROADCAST
);
/* set when dst mac is broadcast */
pfilter_cmp
(
0
,
0x011b
,
0xffff
,
MOV
,
FRAME_PTP_MCAST
);
pfilter_cmp
(
1
,
0x1900
,
0xffff
,
AND
,
FRAME_PTP_MCAST
);
pfilter_cmp
(
2
,
0x0000
,
0xffff
,
AND
,
FRAME_PTP_MCAST
);
/* set when dst mac is PTP multicast (01:1b:19:00:00:00) */
/* Identify some Ethertypes used later */
pfilter_cmp
(
6
,
0x0800
,
0xffff
,
MOV
,
FRAME_TYPE_IPV4
);
pfilter_cmp
(
6
,
0x88f7
,
0xffff
,
MOV
,
FRAME_TYPE_PTP2
);
pfilter_cmp
(
6
,
0x0806
,
0xffff
,
MOV
,
FRAME_TYPE_ARP
);
pfilter_cmp
(
6
,
0xdbff
,
0xffff
,
MOV
,
FRAME_TYPE_STREAMER
);
/* Ethernet = 14 bytes, Offset to type in IP: 8 bytes = 22/2 = 11 */
pfilter_cmp
(
11
,
0x0001
,
0x00ff
,
MOV
,
FRAME_ICMP
);
pfilter_cmp
(
11
,
0x0011
,
0x00ff
,
MOV
,
FRAME_UDP
);
if
(
mode
&
MODE_ETHERBONE
)
{
/* Mark bits for unicast to us, and for unicast-to-us-or-broadcast */
pfilter_logic3
(
FRAME_IP_UNI
,
FRAME_OUR_MAC
,
OR
,
R_ZERO
,
AND
,
FRAME_TYPE_IPV4
);
pfilter_logic3
(
FRAME_IP_OK
,
FRAME_BROADCAST
,
OR
,
FRAME_OUR_MAC
,
AND
,
FRAME_TYPE_IPV4
);
/* Make a selection for the CPU, that is later still added-to */
pfilter_logic3
(
R_TMP
,
FRAME_BROADCAST
,
AND
,
FRAME_TYPE_ARP
,
OR
,
FRAME_TYPE_PTP2
);
pfilter_logic3
(
FRAME_FOR_CPU
,
FRAME_IP_UNI
,
AND
,
FRAME_ICMP
,
OR
,
R_TMP
);
/* Ethernet = 14 bytes, IPv4 = 20 bytes, offset to dport: 2 = 36/2 = 18 */
pfilter_cmp
(
18
,
0x0044
,
0xffff
,
MOV
,
R_TMP
);
/* R_TMP now means dport = BOOTPC */
pfilter_logic3
(
R_TMP
,
R_TMP
,
AND
,
FRAME_UDP
,
AND
,
FRAME_IP_OK
);
/* BOOTPC and UDP and IP(unicast|broadcast) */
pfilter_logic2
(
FRAME_FOR_CPU
,
R_TMP
,
OR
,
FRAME_FOR_CPU
);
if
(
mode
&
MODE_NIC_PFILTER
)
{
pfilter_cmp
(
18
,
0xebd0
,
0xffff
,
MOV
,
FRAME_PORT_ETHERBONE
);
/* Here we had a commented-out check for magic (offset 21, value 0x4e6f) */
pfilter_logic2
(
R_CLASS
(
0
),
FRAME_FOR_CPU
,
MOV
,
R_ZERO
);
pfilter_logic2
(
R_CLASS
(
5
),
FRAME_PORT_ETHERBONE
,
OR
,
R_ZERO
);
/* class 5: Etherbone packet => Etherbone Core */
pfilter_logic3
(
R_CLASS
(
7
),
FRAME_FOR_CPU
,
OR
,
FRAME_PORT_ETHERBONE
,
NOT
,
R_ZERO
);
/* class 7: Rest => NIC Core */
}
else
{
pfilter_logic3
(
R_TMP
,
FRAME_IP_OK
,
AND
,
FRAME_UDP
,
OR
,
FRAME_FOR_CPU
);
/* Something we accept: cpu+udp or streamer */
pfilter_logic3
(
R_DROP
,
R_TMP
,
OR
,
FRAME_TYPE_STREAMER
,
NOT
,
R_ZERO
);
/* None match? drop */
pfilter_logic2
(
R_CLASS
(
7
),
FRAME_IP_OK
,
AND
,
FRAME_UDP
);
/* class 7: UDP/IP(unicast|broadcast) => external fabric */
pfilter_logic2
(
R_CLASS
(
6
),
FRAME_BROADCAST
,
AND
,
FRAME_TYPE_STREAMER
);
/* class 6: streamer broadcasts => external fabric */
pfilter_logic2
(
R_CLASS
(
0
),
FRAME_FOR_CPU
,
MOV
,
R_ZERO
);
/* class 0: all selected for CPU earlier */
}
}
else
{
/* not etherbone */
pfilter_logic3
(
FRAME_PTP_OK
,
FRAME_OUR_MAC
,
OR
,
FRAME_PTP_MCAST
,
AND
,
FRAME_TYPE_PTP2
);
pfilter_logic2
(
FRAME_STREAMER_BCAST
,
FRAME_BROADCAST
,
AND
,
FRAME_TYPE_STREAMER
);
pfilter_logic3
(
R_TMP
,
FRAME_PTP_OK
,
OR
,
FRAME_STREAMER_BCAST
,
NOT
,
R_ZERO
);
/* R_TMP = everything else */
pfilter_logic2
(
R_CLASS
(
7
),
R_TMP
,
MOV
,
R_ZERO
);
/* class 7: all non PTP and non-streamer traffic => external fabric */
pfilter_logic2
(
R_CLASS
(
6
),
FRAME_STREAMER_BCAST
,
MOV
,
R_ZERO
);
/* class 6: streamer broadcasts => external fabric */
pfilter_logic2
(
R_CLASS
(
0
),
FRAME_PTP_OK
,
MOV
,
R_ZERO
);
/* class 0: PTP frames => LM32 */
}
pfilter_output
(
fname
);
}
int
main
(
int
argc
,
char
**
argv
)
/* no arguments used currently */
{
prgname
=
argv
[
0
];
pfilter_init
(
0
,
"rules-plain.bin"
);
pfilter_init
(
MODE_ETHERBONE
,
"rules-ebone.bin"
);
pfilter_init
(
MODE_ETHERBONE
|
MODE_NIC_PFILTER
,
"rules-e+nic.bin"
);
exit
(
0
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment