internal.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef MPLS_INTERNAL_H
  2. #define MPLS_INTERNAL_H
  3. #include <net/mpls.h>
  4. struct mpls_entry_decoded {
  5. u32 label;
  6. u8 ttl;
  7. u8 tc;
  8. u8 bos;
  9. };
  10. struct mpls_pcpu_stats {
  11. struct mpls_link_stats stats;
  12. struct u64_stats_sync syncp;
  13. };
  14. struct mpls_dev {
  15. int input_enabled;
  16. struct net_device *dev;
  17. struct mpls_pcpu_stats __percpu *stats;
  18. struct ctl_table_header *sysctl;
  19. struct rcu_head rcu;
  20. };
  21. #if BITS_PER_LONG == 32
  22. #define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
  23. do { \
  24. __typeof__(*(mdev)->stats) *ptr = \
  25. raw_cpu_ptr((mdev)->stats); \
  26. local_bh_disable(); \
  27. u64_stats_update_begin(&ptr->syncp); \
  28. ptr->stats.pkts_field++; \
  29. ptr->stats.bytes_field += (len); \
  30. u64_stats_update_end(&ptr->syncp); \
  31. local_bh_enable(); \
  32. } while (0)
  33. #define MPLS_INC_STATS(mdev, field) \
  34. do { \
  35. __typeof__(*(mdev)->stats) *ptr = \
  36. raw_cpu_ptr((mdev)->stats); \
  37. local_bh_disable(); \
  38. u64_stats_update_begin(&ptr->syncp); \
  39. ptr->stats.field++; \
  40. u64_stats_update_end(&ptr->syncp); \
  41. local_bh_enable(); \
  42. } while (0)
  43. #else
  44. #define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
  45. do { \
  46. this_cpu_inc((mdev)->stats->stats.pkts_field); \
  47. this_cpu_add((mdev)->stats->stats.bytes_field, (len)); \
  48. } while (0)
  49. #define MPLS_INC_STATS(mdev, field) \
  50. this_cpu_inc((mdev)->stats->stats.field)
  51. #endif
  52. struct sk_buff;
  53. #define LABEL_NOT_SPECIFIED (1 << 20)
  54. #define MAX_NEW_LABELS 2
  55. /* This maximum ha length copied from the definition of struct neighbour */
  56. #define VIA_ALEN_ALIGN sizeof(unsigned long)
  57. #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, VIA_ALEN_ALIGN))
  58. enum mpls_payload_type {
  59. MPT_UNSPEC, /* IPv4 or IPv6 */
  60. MPT_IPV4 = 4,
  61. MPT_IPV6 = 6,
  62. /* Other types not implemented:
  63. * - Pseudo-wire with or without control word (RFC4385)
  64. * - GAL (RFC5586)
  65. */
  66. };
  67. struct mpls_nh { /* next hop label forwarding entry */
  68. struct net_device __rcu *nh_dev;
  69. unsigned int nh_flags;
  70. u32 nh_label[MAX_NEW_LABELS];
  71. u8 nh_labels;
  72. u8 nh_via_alen;
  73. u8 nh_via_table;
  74. };
  75. /* The route, nexthops and vias are stored together in the same memory
  76. * block:
  77. *
  78. * +----------------------+
  79. * | mpls_route |
  80. * +----------------------+
  81. * | mpls_nh 0 |
  82. * +----------------------+
  83. * | ... |
  84. * +----------------------+
  85. * | mpls_nh n-1 |
  86. * +----------------------+
  87. * | alignment padding |
  88. * +----------------------+
  89. * | via[rt_max_alen] 0 |
  90. * +----------------------+
  91. * | ... |
  92. * +----------------------+
  93. * | via[rt_max_alen] n-1 |
  94. * +----------------------+
  95. */
  96. struct mpls_route { /* next hop label forwarding entry */
  97. struct rcu_head rt_rcu;
  98. u8 rt_protocol;
  99. u8 rt_payload_type;
  100. u8 rt_max_alen;
  101. unsigned int rt_nhn;
  102. unsigned int rt_nhn_alive;
  103. struct mpls_nh rt_nh[0];
  104. };
  105. #define for_nexthops(rt) { \
  106. int nhsel; struct mpls_nh *nh; \
  107. for (nhsel = 0, nh = (rt)->rt_nh; \
  108. nhsel < (rt)->rt_nhn; \
  109. nh++, nhsel++)
  110. #define change_nexthops(rt) { \
  111. int nhsel; struct mpls_nh *nh; \
  112. for (nhsel = 0, nh = (struct mpls_nh *)((rt)->rt_nh); \
  113. nhsel < (rt)->rt_nhn; \
  114. nh++, nhsel++)
  115. #define endfor_nexthops(rt) }
  116. static inline struct mpls_shim_hdr mpls_entry_encode(u32 label, unsigned ttl, unsigned tc, bool bos)
  117. {
  118. struct mpls_shim_hdr result;
  119. result.label_stack_entry =
  120. cpu_to_be32((label << MPLS_LS_LABEL_SHIFT) |
  121. (tc << MPLS_LS_TC_SHIFT) |
  122. (bos ? (1 << MPLS_LS_S_SHIFT) : 0) |
  123. (ttl << MPLS_LS_TTL_SHIFT));
  124. return result;
  125. }
  126. static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *hdr)
  127. {
  128. struct mpls_entry_decoded result;
  129. unsigned entry = be32_to_cpu(hdr->label_stack_entry);
  130. result.label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
  131. result.ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
  132. result.tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
  133. result.bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
  134. return result;
  135. }
  136. static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
  137. {
  138. return rcu_dereference_rtnl(dev->mpls_ptr);
  139. }
  140. int nla_put_labels(struct sk_buff *skb, int attrtype, u8 labels,
  141. const u32 label[]);
  142. int nla_get_labels(const struct nlattr *nla, u32 max_labels, u8 *labels,
  143. u32 label[]);
  144. int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
  145. u8 via[]);
  146. bool mpls_output_possible(const struct net_device *dev);
  147. unsigned int mpls_dev_mtu(const struct net_device *dev);
  148. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu);
  149. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  150. const struct sk_buff *skb);
  151. #endif /* MPLS_INTERNAL_H */