xdp_router_ipv4_kern.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Copyright (C) 2017 Cavium, Inc.
  2. *
  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.
  6. */
  7. #define KBUILD_MODNAME "foo"
  8. #include <uapi/linux/bpf.h>
  9. #include <linux/in.h>
  10. #include <linux/if_ether.h>
  11. #include <linux/if_packet.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/ip.h>
  14. #include <linux/ipv6.h>
  15. #include <bpf/bpf_helpers.h>
  16. #include <linux/slab.h>
  17. #include <net/ip_fib.h>
  18. struct trie_value {
  19. __u8 prefix[4];
  20. __be64 value;
  21. int ifindex;
  22. int metric;
  23. __be32 gw;
  24. };
  25. /* Key for lpm_trie*/
  26. union key_4 {
  27. u32 b32[2];
  28. u8 b8[8];
  29. };
  30. struct arp_entry {
  31. __be64 mac;
  32. __be32 dst;
  33. };
  34. struct direct_map {
  35. struct arp_entry arp;
  36. int ifindex;
  37. __be64 mac;
  38. };
  39. /* Map for trie implementation*/
  40. struct {
  41. __uint(type, BPF_MAP_TYPE_LPM_TRIE);
  42. __uint(key_size, 8);
  43. __uint(value_size, sizeof(struct trie_value));
  44. __uint(max_entries, 50);
  45. __uint(map_flags, BPF_F_NO_PREALLOC);
  46. } lpm_map SEC(".maps");
  47. /* Map for counter*/
  48. struct {
  49. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  50. __type(key, u32);
  51. __type(value, u64);
  52. __uint(max_entries, 256);
  53. } rxcnt SEC(".maps");
  54. /* Map for ARP table*/
  55. struct {
  56. __uint(type, BPF_MAP_TYPE_HASH);
  57. __type(key, __be32);
  58. __type(value, __be64);
  59. __uint(max_entries, 50);
  60. } arp_table SEC(".maps");
  61. /* Map to keep the exact match entries in the route table*/
  62. struct {
  63. __uint(type, BPF_MAP_TYPE_HASH);
  64. __type(key, __be32);
  65. __type(value, struct direct_map);
  66. __uint(max_entries, 50);
  67. } exact_match SEC(".maps");
  68. struct {
  69. __uint(type, BPF_MAP_TYPE_DEVMAP);
  70. __uint(key_size, sizeof(int));
  71. __uint(value_size, sizeof(int));
  72. __uint(max_entries, 100);
  73. } tx_port SEC(".maps");
  74. /* Function to set source and destination mac of the packet */
  75. static inline void set_src_dst_mac(void *data, void *src, void *dst)
  76. {
  77. unsigned short *source = src;
  78. unsigned short *dest = dst;
  79. unsigned short *p = data;
  80. __builtin_memcpy(p, dest, 6);
  81. __builtin_memcpy(p + 3, source, 6);
  82. }
  83. /* Parse IPV4 packet to get SRC, DST IP and protocol */
  84. static inline int parse_ipv4(void *data, u64 nh_off, void *data_end,
  85. __be32 *src, __be32 *dest)
  86. {
  87. struct iphdr *iph = data + nh_off;
  88. if (iph + 1 > data_end)
  89. return 0;
  90. *src = iph->saddr;
  91. *dest = iph->daddr;
  92. return iph->protocol;
  93. }
  94. SEC("xdp_router_ipv4")
  95. int xdp_router_ipv4_prog(struct xdp_md *ctx)
  96. {
  97. void *data_end = (void *)(long)ctx->data_end;
  98. __be64 *dest_mac = NULL, *src_mac = NULL;
  99. void *data = (void *)(long)ctx->data;
  100. struct trie_value *prefix_value;
  101. int rc = XDP_DROP, forward_to;
  102. struct ethhdr *eth = data;
  103. union key_4 key4;
  104. long *value;
  105. u16 h_proto;
  106. u32 ipproto;
  107. u64 nh_off;
  108. nh_off = sizeof(*eth);
  109. if (data + nh_off > data_end)
  110. return rc;
  111. h_proto = eth->h_proto;
  112. if (h_proto == htons(ETH_P_8021Q) || h_proto == htons(ETH_P_8021AD)) {
  113. struct vlan_hdr *vhdr;
  114. vhdr = data + nh_off;
  115. nh_off += sizeof(struct vlan_hdr);
  116. if (data + nh_off > data_end)
  117. return rc;
  118. h_proto = vhdr->h_vlan_encapsulated_proto;
  119. }
  120. if (h_proto == htons(ETH_P_ARP)) {
  121. return XDP_PASS;
  122. } else if (h_proto == htons(ETH_P_IP)) {
  123. struct direct_map *direct_entry;
  124. __be32 src_ip = 0, dest_ip = 0;
  125. ipproto = parse_ipv4(data, nh_off, data_end, &src_ip, &dest_ip);
  126. direct_entry = bpf_map_lookup_elem(&exact_match, &dest_ip);
  127. /* Check for exact match, this would give a faster lookup*/
  128. if (direct_entry && direct_entry->mac && direct_entry->arp.mac) {
  129. src_mac = &direct_entry->mac;
  130. dest_mac = &direct_entry->arp.mac;
  131. forward_to = direct_entry->ifindex;
  132. } else {
  133. /* Look up in the trie for lpm*/
  134. key4.b32[0] = 32;
  135. key4.b8[4] = dest_ip & 0xff;
  136. key4.b8[5] = (dest_ip >> 8) & 0xff;
  137. key4.b8[6] = (dest_ip >> 16) & 0xff;
  138. key4.b8[7] = (dest_ip >> 24) & 0xff;
  139. prefix_value = bpf_map_lookup_elem(&lpm_map, &key4);
  140. if (!prefix_value)
  141. return XDP_DROP;
  142. src_mac = &prefix_value->value;
  143. if (!src_mac)
  144. return XDP_DROP;
  145. dest_mac = bpf_map_lookup_elem(&arp_table, &dest_ip);
  146. if (!dest_mac) {
  147. if (!prefix_value->gw)
  148. return XDP_DROP;
  149. dest_ip = prefix_value->gw;
  150. dest_mac = bpf_map_lookup_elem(&arp_table, &dest_ip);
  151. }
  152. forward_to = prefix_value->ifindex;
  153. }
  154. } else {
  155. ipproto = 0;
  156. }
  157. if (src_mac && dest_mac) {
  158. set_src_dst_mac(data, src_mac, dest_mac);
  159. value = bpf_map_lookup_elem(&rxcnt, &ipproto);
  160. if (value)
  161. *value += 1;
  162. return bpf_redirect_map(&tx_port, forward_to, 0);
  163. }
  164. return rc;
  165. }
  166. char _license[] SEC("license") = "GPL";