test_pkt_md_access.c 928 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* Copyright (c) 2017 Facebook
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <stddef.h>
  8. #include <string.h>
  9. #include <linux/bpf.h>
  10. #include <linux/pkt_cls.h>
  11. #include "bpf_helpers.h"
  12. int _version SEC("version") = 1;
  13. #define TEST_FIELD(TYPE, FIELD, MASK) \
  14. { \
  15. TYPE tmp = *(volatile TYPE *)&skb->FIELD; \
  16. if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK)) \
  17. return TC_ACT_SHOT; \
  18. }
  19. SEC("test1")
  20. int process(struct __sk_buff *skb)
  21. {
  22. TEST_FIELD(__u8, len, 0xFF);
  23. TEST_FIELD(__u16, len, 0xFFFF);
  24. TEST_FIELD(__u32, len, 0xFFFFFFFF);
  25. TEST_FIELD(__u16, protocol, 0xFFFF);
  26. TEST_FIELD(__u32, protocol, 0xFFFFFFFF);
  27. TEST_FIELD(__u8, hash, 0xFF);
  28. TEST_FIELD(__u16, hash, 0xFFFF);
  29. TEST_FIELD(__u32, hash, 0xFFFFFFFF);
  30. return TC_ACT_OK;
  31. }