linux/tools/testing/selftests/bpf/test_pkt_md_access.c
<<
>>
Prefs
   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
  13int _version SEC("version") = 1;
  14
  15#if  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  16#define TEST_FIELD(TYPE, FIELD, MASK)                                   \
  17        {                                                               \
  18                TYPE tmp = *(volatile TYPE *)&skb->FIELD;               \
  19                if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK))   \
  20                        return TC_ACT_SHOT;                             \
  21        }
  22#else
  23#define TEST_FIELD_OFFSET(a, b) ((sizeof(a) - sizeof(b)) / sizeof(b))
  24#define TEST_FIELD(TYPE, FIELD, MASK)                                   \
  25        {                                                               \
  26                TYPE tmp = *((volatile TYPE *)&skb->FIELD +             \
  27                              TEST_FIELD_OFFSET(skb->FIELD, TYPE));     \
  28                if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK))   \
  29                        return TC_ACT_SHOT;                             \
  30        }
  31#endif
  32
  33SEC("test1")
  34int process(struct __sk_buff *skb)
  35{
  36        TEST_FIELD(__u8,  len, 0xFF);
  37        TEST_FIELD(__u16, len, 0xFFFF);
  38        TEST_FIELD(__u32, len, 0xFFFFFFFF);
  39        TEST_FIELD(__u16, protocol, 0xFFFF);
  40        TEST_FIELD(__u32, protocol, 0xFFFFFFFF);
  41        TEST_FIELD(__u8,  hash, 0xFF);
  42        TEST_FIELD(__u16, hash, 0xFFFF);
  43        TEST_FIELD(__u32, hash, 0xFFFFFFFF);
  44
  45        return TC_ACT_OK;
  46}
  47