Global replace of Nicira Networks.
[sliver-openvswitch.git] / datapath / vlan.h
1 /*
2  * Copyright (c) 2007-2011 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #ifndef VLAN_H
20 #define VLAN_H 1
21
22 #include <linux/if_vlan.h>
23 #include <linux/skbuff.h>
24 #include <linux/version.h>
25
26 /**
27  * DOC: VLAN tag manipulation.
28  *
29  * &struct sk_buff handling of VLAN tags has evolved over time:
30  *
31  * In 2.6.26 and earlier, VLAN tags did not have any generic representation in
32  * an skb, other than as a raw 802.1Q header inside the packet data.
33  *
34  * In 2.6.27 &struct sk_buff added a @vlan_tci member.  Between 2.6.27 and
35  * 2.6.32, its value was the raw contents of the 802.1Q TCI field, or zero if
36  * no 802.1Q header was present.  This worked OK except for the corner case of
37  * an 802.1Q header with an all-0-bits TCI, which could not be represented.
38  *
39  * In 2.6.33, @vlan_tci semantics changed.  Now, if an 802.1Q header is
40  * present, then the VLAN_TAG_PRESENT bit is always set.  This fixes the
41  * all-0-bits TCI corner case.
42  *
43  * For compatibility we emulate the 2.6.33+ behavior on earlier kernel
44  * versions.  The client must not access @vlan_tci directly.  Instead, use
45  * vlan_get_tci() to read it or vlan_set_tci() to write it, with semantics
46  * equivalent to those on 2.6.33+.
47  */
48
49 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
50 #define NEED_VLAN_FIELD
51 #endif
52
53 #ifndef NEED_VLAN_FIELD
54 static inline void vlan_copy_skb_tci(struct sk_buff *skb) { }
55
56 static inline u16 vlan_get_tci(struct sk_buff *skb)
57 {
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
59         if (skb->vlan_tci)
60                 return skb->vlan_tci | VLAN_TAG_PRESENT;
61 #endif
62         return skb->vlan_tci;
63 }
64
65 static inline void vlan_set_tci(struct sk_buff *skb, u16 vlan_tci)
66 {
67 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
68         vlan_tci &= ~VLAN_TAG_PRESENT;
69 #endif
70         skb->vlan_tci = vlan_tci;
71 }
72 #else
73 void vlan_copy_skb_tci(struct sk_buff *skb);
74 u16 vlan_get_tci(struct sk_buff *skb);
75 void vlan_set_tci(struct sk_buff *skb, u16 vlan_tci);
76
77 #undef vlan_tx_tag_present
78 bool vlan_tx_tag_present(struct sk_buff *skb);
79
80 #undef vlan_tx_tag_get
81 u16 vlan_tx_tag_get(struct sk_buff *skb);
82
83 #define __vlan_hwaccel_put_tag rpl__vlan_hwaccel_put_tag
84 struct sk_buff *__vlan_hwaccel_put_tag(struct sk_buff *skb, u16 vlan_tci);
85 #endif /* NEED_VLAN_FIELD */
86
87 static inline int vlan_deaccel_tag(struct sk_buff *skb)
88 {
89         if (!vlan_tx_tag_present(skb))
90                 return 0;
91
92         skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb));
93         if (unlikely(!skb))
94                 return -ENOMEM;
95
96         vlan_set_tci(skb, 0);
97         return 0;
98 }
99
100 #endif /* vlan.h */