1 /* Copyright (C) 2017 Cavium, Inc.
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of version 2 of the GNU General Public License
5 * as published by the Free Software Foundation.
9 #include "xdp_sample.bpf.h"
10 #include "xdp_sample_shared.h"
13 #define ETH_P_8021Q 0x8100
14 #define ETH_P_8021AD 0x88A8
24 /* Key for lpm_trie */
41 /* Map for trie implementation */
43 __uint(type, BPF_MAP_TYPE_LPM_TRIE);
45 __uint(value_size, sizeof(struct trie_value));
46 __uint(max_entries, 50);
47 __uint(map_flags, BPF_F_NO_PREALLOC);
48 } lpm_map SEC(".maps");
50 /* Map for ARP table */
52 __uint(type, BPF_MAP_TYPE_HASH);
54 __type(value, __be64);
55 __uint(max_entries, 50);
56 } arp_table SEC(".maps");
58 /* Map to keep the exact match entries in the route table */
60 __uint(type, BPF_MAP_TYPE_HASH);
62 __type(value, struct direct_map);
63 __uint(max_entries, 50);
64 } exact_match SEC(".maps");
67 __uint(type, BPF_MAP_TYPE_DEVMAP);
68 __uint(key_size, sizeof(int));
69 __uint(value_size, sizeof(int));
70 __uint(max_entries, 100);
71 } tx_port SEC(".maps");
74 int xdp_router_ipv4_prog(struct xdp_md *ctx)
76 void *data_end = (void *)(long)ctx->data_end;
77 void *data = (void *)(long)ctx->data;
78 struct ethhdr *eth = data;
79 u64 nh_off = sizeof(*eth);
84 rec = bpf_map_lookup_elem(&rx_cnt, &key);
86 NO_TEAR_INC(rec->processed);
88 if (data + nh_off > data_end)
91 h_proto = eth->h_proto;
92 if (h_proto == bpf_htons(ETH_P_8021Q) ||
93 h_proto == bpf_htons(ETH_P_8021AD)) {
94 struct vlan_hdr *vhdr;
97 nh_off += sizeof(struct vlan_hdr);
98 if (data + nh_off > data_end)
101 h_proto = vhdr->h_vlan_encapsulated_proto;
104 switch (bpf_ntohs(h_proto)) {
107 NO_TEAR_INC(rec->xdp_pass);
110 struct iphdr *iph = data + nh_off;
111 struct direct_map *direct_entry;
112 __be64 *dest_mac, *src_mac;
115 if (iph + 1 > data_end)
118 direct_entry = bpf_map_lookup_elem(&exact_match, &iph->daddr);
120 /* Check for exact match, this would give a faster lookup */
121 if (direct_entry && direct_entry->mac &&
122 direct_entry->arp.mac) {
123 src_mac = &direct_entry->mac;
124 dest_mac = &direct_entry->arp.mac;
125 forward_to = direct_entry->ifindex;
127 struct trie_value *prefix_value;
130 /* Look up in the trie for lpm */
132 key4.b8[4] = iph->daddr & 0xff;
133 key4.b8[5] = (iph->daddr >> 8) & 0xff;
134 key4.b8[6] = (iph->daddr >> 16) & 0xff;
135 key4.b8[7] = (iph->daddr >> 24) & 0xff;
137 prefix_value = bpf_map_lookup_elem(&lpm_map, &key4);
141 forward_to = prefix_value->ifindex;
142 src_mac = &prefix_value->value;
146 dest_mac = bpf_map_lookup_elem(&arp_table, &iph->daddr);
148 if (!prefix_value->gw)
151 dest_mac = bpf_map_lookup_elem(&arp_table,
154 /* Forward the packet to the kernel in
155 * order to trigger ARP discovery for
159 NO_TEAR_INC(rec->xdp_pass);
165 if (src_mac && dest_mac) {
168 __builtin_memcpy(eth->h_dest, dest_mac, ETH_ALEN);
169 __builtin_memcpy(eth->h_source, src_mac, ETH_ALEN);
171 ret = bpf_redirect_map(&tx_port, forward_to, 0);
172 if (ret == XDP_REDIRECT) {
174 NO_TEAR_INC(rec->xdp_redirect);
184 NO_TEAR_INC(rec->xdp_drop);
189 char _license[] SEC("license") = "GPL";