vlan.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #ifndef __BEN_VLAN_802_1Q_INC__
  2. #define __BEN_VLAN_802_1Q_INC__
  3. #include <linux/if_vlan.h>
  4. #include <linux/u64_stats_sync.h>
  5. #include <linux/list.h>
  6. /**
  7. * struct vlan_priority_tci_mapping - vlan egress priority mappings
  8. * @priority: skb priority
  9. * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
  10. * @next: pointer to next struct
  11. */
  12. struct vlan_priority_tci_mapping {
  13. u32 priority;
  14. u16 vlan_qos;
  15. struct vlan_priority_tci_mapping *next;
  16. };
  17. /**
  18. * struct vlan_pcpu_stats - VLAN percpu rx/tx stats
  19. * @rx_packets: number of received packets
  20. * @rx_bytes: number of received bytes
  21. * @rx_multicast: number of received multicast packets
  22. * @tx_packets: number of transmitted packets
  23. * @tx_bytes: number of transmitted bytes
  24. * @syncp: synchronization point for 64bit counters
  25. * @rx_errors: number of rx errors
  26. * @tx_dropped: number of tx drops
  27. */
  28. struct vlan_pcpu_stats {
  29. u64 rx_packets;
  30. u64 rx_bytes;
  31. u64 rx_multicast;
  32. u64 tx_packets;
  33. u64 tx_bytes;
  34. struct u64_stats_sync syncp;
  35. u32 rx_errors;
  36. u32 tx_dropped;
  37. };
  38. struct netpoll;
  39. /**
  40. * struct vlan_dev_priv - VLAN private device data
  41. * @nr_ingress_mappings: number of ingress priority mappings
  42. * @ingress_priority_map: ingress priority mappings
  43. * @nr_egress_mappings: number of egress priority mappings
  44. * @egress_priority_map: hash of egress priority mappings
  45. * @vlan_proto: VLAN encapsulation protocol
  46. * @vlan_id: VLAN identifier
  47. * @flags: device flags
  48. * @real_dev: underlying netdevice
  49. * @real_dev_addr: address of underlying netdevice
  50. * @dent: proc dir entry
  51. * @vlan_pcpu_stats: ptr to percpu rx stats
  52. */
  53. struct vlan_dev_priv {
  54. unsigned int nr_ingress_mappings;
  55. u32 ingress_priority_map[8];
  56. unsigned int nr_egress_mappings;
  57. struct vlan_priority_tci_mapping *egress_priority_map[16];
  58. __be16 vlan_proto;
  59. u16 vlan_id;
  60. u16 flags;
  61. struct net_device *real_dev;
  62. unsigned char real_dev_addr[ETH_ALEN];
  63. struct proc_dir_entry *dent;
  64. struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
  65. #ifdef CONFIG_NET_POLL_CONTROLLER
  66. struct netpoll *netpoll;
  67. #endif
  68. };
  69. static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
  70. {
  71. return netdev_priv(dev);
  72. }
  73. /* if this changes, algorithm will have to be reworked because this
  74. * depends on completely exhausting the VLAN identifier space. Thus
  75. * it gives constant time look-up, but in many cases it wastes memory.
  76. */
  77. #define VLAN_GROUP_ARRAY_SPLIT_PARTS 8
  78. #define VLAN_GROUP_ARRAY_PART_LEN (VLAN_N_VID/VLAN_GROUP_ARRAY_SPLIT_PARTS)
  79. enum vlan_protos {
  80. VLAN_PROTO_8021Q = 0,
  81. VLAN_PROTO_NUM,
  82. };
  83. struct vlan_group {
  84. unsigned int nr_vlan_devs;
  85. struct hlist_node hlist; /* linked list */
  86. struct net_device **vlan_devices_arrays[VLAN_PROTO_NUM]
  87. [VLAN_GROUP_ARRAY_SPLIT_PARTS];
  88. };
  89. struct vlan_info {
  90. struct net_device *real_dev; /* The ethernet(like) device
  91. * the vlan is attached to.
  92. */
  93. struct vlan_group grp;
  94. struct list_head vid_list;
  95. unsigned int nr_vids;
  96. struct rcu_head rcu;
  97. };
  98. static inline unsigned int vlan_proto_idx(__be16 proto)
  99. {
  100. switch (proto) {
  101. case __constant_htons(ETH_P_8021Q):
  102. return VLAN_PROTO_8021Q;
  103. default:
  104. BUG();
  105. }
  106. }
  107. static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,
  108. unsigned int pidx,
  109. u16 vlan_id)
  110. {
  111. struct net_device **array;
  112. array = vg->vlan_devices_arrays[pidx]
  113. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  114. return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
  115. }
  116. static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
  117. __be16 vlan_proto,
  118. u16 vlan_id)
  119. {
  120. return __vlan_group_get_device(vg, vlan_proto_idx(vlan_proto), vlan_id);
  121. }
  122. static inline void vlan_group_set_device(struct vlan_group *vg,
  123. __be16 vlan_proto, u16 vlan_id,
  124. struct net_device *dev)
  125. {
  126. struct net_device **array;
  127. if (!vg)
  128. return;
  129. array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)]
  130. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  131. array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
  132. }
  133. /* Must be invoked with rcu_read_lock or with RTNL. */
  134. static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
  135. __be16 vlan_proto, u16 vlan_id)
  136. {
  137. struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
  138. if (vlan_info)
  139. return vlan_group_get_device(&vlan_info->grp,
  140. vlan_proto, vlan_id);
  141. return NULL;
  142. }
  143. #define vlan_group_for_each_dev(grp, i, dev) \
  144. for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
  145. if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
  146. (i) % VLAN_N_VID)))
  147. /* found in vlan_dev.c */
  148. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  149. u32 skb_prio, u16 vlan_prio);
  150. int vlan_dev_set_egress_priority(const struct net_device *dev,
  151. u32 skb_prio, u16 vlan_prio);
  152. int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
  153. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result);
  154. int vlan_check_real_dev(struct net_device *real_dev,
  155. __be16 protocol, u16 vlan_id);
  156. void vlan_setup(struct net_device *dev);
  157. int register_vlan_dev(struct net_device *dev);
  158. void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
  159. static inline u32 vlan_get_ingress_priority(struct net_device *dev,
  160. u16 vlan_tci)
  161. {
  162. struct vlan_dev_priv *vip = vlan_dev_priv(dev);
  163. return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
  164. }
  165. #ifdef CONFIG_VLAN_8021Q_GVRP
  166. extern int vlan_gvrp_request_join(const struct net_device *dev);
  167. extern void vlan_gvrp_request_leave(const struct net_device *dev);
  168. extern int vlan_gvrp_init_applicant(struct net_device *dev);
  169. extern void vlan_gvrp_uninit_applicant(struct net_device *dev);
  170. extern int vlan_gvrp_init(void);
  171. extern void vlan_gvrp_uninit(void);
  172. #else
  173. static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
  174. static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
  175. static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
  176. static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
  177. static inline int vlan_gvrp_init(void) { return 0; }
  178. static inline void vlan_gvrp_uninit(void) {}
  179. #endif
  180. #ifdef CONFIG_VLAN_8021Q_MVRP
  181. extern int vlan_mvrp_request_join(const struct net_device *dev);
  182. extern void vlan_mvrp_request_leave(const struct net_device *dev);
  183. extern int vlan_mvrp_init_applicant(struct net_device *dev);
  184. extern void vlan_mvrp_uninit_applicant(struct net_device *dev);
  185. extern int vlan_mvrp_init(void);
  186. extern void vlan_mvrp_uninit(void);
  187. #else
  188. static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
  189. static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
  190. static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
  191. static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
  192. static inline int vlan_mvrp_init(void) { return 0; }
  193. static inline void vlan_mvrp_uninit(void) {}
  194. #endif
  195. extern const char vlan_fullname[];
  196. extern const char vlan_version[];
  197. extern int vlan_netlink_init(void);
  198. extern void vlan_netlink_fini(void);
  199. extern struct rtnl_link_ops vlan_link_ops;
  200. extern int vlan_net_id;
  201. struct proc_dir_entry;
  202. struct vlan_net {
  203. /* /proc/net/vlan */
  204. struct proc_dir_entry *proc_vlan_dir;
  205. /* /proc/net/vlan/config */
  206. struct proc_dir_entry *proc_vlan_conf;
  207. /* Determines interface naming scheme. */
  208. unsigned short name_type;
  209. };
  210. #endif /* !(__BEN_VLAN_802_1Q_INC__) */