nsh.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #ifndef __NET_NSH_H
  2. #define __NET_NSH_H 1
  3. #include <linux/skbuff.h>
  4. /*
  5. * Network Service Header:
  6. * 0 1 2 3
  7. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  8. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  9. * |Ver|O|U| TTL | Length |U|U|U|U|MD Type| Next Protocol |
  10. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  11. * | Service Path Identifier (SPI) | Service Index |
  12. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  13. * | |
  14. * ~ Mandatory/Optional Context Headers ~
  15. * | |
  16. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  17. *
  18. * Version: The version field is used to ensure backward compatibility
  19. * going forward with future NSH specification updates. It MUST be set
  20. * to 0x0 by the sender, in this first revision of NSH. Given the
  21. * widespread implementation of existing hardware that uses the first
  22. * nibble after an MPLS label stack for ECMP decision processing, this
  23. * document reserves version 01b and this value MUST NOT be used in
  24. * future versions of the protocol. Please see [RFC7325] for further
  25. * discussion of MPLS-related forwarding requirements.
  26. *
  27. * O bit: Setting this bit indicates an Operations, Administration, and
  28. * Maintenance (OAM) packet. The actual format and processing of SFC
  29. * OAM packets is outside the scope of this specification (see for
  30. * example [I-D.ietf-sfc-oam-framework] for one approach).
  31. *
  32. * The O bit MUST be set for OAM packets and MUST NOT be set for non-OAM
  33. * packets. The O bit MUST NOT be modified along the SFP.
  34. *
  35. * SF/SFF/SFC Proxy/Classifier implementations that do not support SFC
  36. * OAM procedures SHOULD discard packets with O bit set, but MAY support
  37. * a configurable parameter to enable forwarding received SFC OAM
  38. * packets unmodified to the next element in the chain. Forwarding OAM
  39. * packets unmodified by SFC elements that do not support SFC OAM
  40. * procedures may be acceptable for a subset of OAM functions, but can
  41. * result in unexpected outcomes for others, thus it is recommended to
  42. * analyze the impact of forwarding an OAM packet for all OAM functions
  43. * prior to enabling this behavior. The configurable parameter MUST be
  44. * disabled by default.
  45. *
  46. * TTL: Indicates the maximum SFF hops for an SFP. This field is used
  47. * for service plane loop detection. The initial TTL value SHOULD be
  48. * configurable via the control plane; the configured initial value can
  49. * be specific to one or more SFPs. If no initial value is explicitly
  50. * provided, the default initial TTL value of 63 MUST be used. Each SFF
  51. * involved in forwarding an NSH packet MUST decrement the TTL value by
  52. * 1 prior to NSH forwarding lookup. Decrementing by 1 from an incoming
  53. * value of 0 shall result in a TTL value of 63. The packet MUST NOT be
  54. * forwarded if TTL is, after decrement, 0.
  55. *
  56. * All other flag fields, marked U, are unassigned and available for
  57. * future use, see Section 11.2.1. Unassigned bits MUST be set to zero
  58. * upon origination, and MUST be ignored and preserved unmodified by
  59. * other NSH supporting elements. Elements which do not understand the
  60. * meaning of any of these bits MUST NOT modify their actions based on
  61. * those unknown bits.
  62. *
  63. * Length: The total length, in 4-byte words, of NSH including the Base
  64. * Header, the Service Path Header, the Fixed Length Context Header or
  65. * Variable Length Context Header(s). The length MUST be 0x6 for MD
  66. * Type equal to 0x1, and MUST be 0x2 or greater for MD Type equal to
  67. * 0x2. The length of the NSH header MUST be an integer multiple of 4
  68. * bytes, thus variable length metadata is always padded out to a
  69. * multiple of 4 bytes.
  70. *
  71. * MD Type: Indicates the format of NSH beyond the mandatory Base Header
  72. * and the Service Path Header. MD Type defines the format of the
  73. * metadata being carried.
  74. *
  75. * 0x0 - This is a reserved value. Implementations SHOULD silently
  76. * discard packets with MD Type 0x0.
  77. *
  78. * 0x1 - This indicates that the format of the header includes a fixed
  79. * length Context Header (see Figure 4 below).
  80. *
  81. * 0x2 - This does not mandate any headers beyond the Base Header and
  82. * Service Path Header, but may contain optional variable length Context
  83. * Header(s). The semantics of the variable length Context Header(s)
  84. * are not defined in this document. The format of the optional
  85. * variable length Context Headers is provided in Section 2.5.1.
  86. *
  87. * 0xF - This value is reserved for experimentation and testing, as per
  88. * [RFC3692]. Implementations not explicitly configured to be part of
  89. * an experiment SHOULD silently discard packets with MD Type 0xF.
  90. *
  91. * Next Protocol: indicates the protocol type of the encapsulated data.
  92. * NSH does not alter the inner payload, and the semantics on the inner
  93. * protocol remain unchanged due to NSH service function chaining.
  94. * Please see the IANA Considerations section below, Section 11.2.5.
  95. *
  96. * This document defines the following Next Protocol values:
  97. *
  98. * 0x1: IPv4
  99. * 0x2: IPv6
  100. * 0x3: Ethernet
  101. * 0x4: NSH
  102. * 0x5: MPLS
  103. * 0xFE: Experiment 1
  104. * 0xFF: Experiment 2
  105. *
  106. * Packets with Next Protocol values not supported SHOULD be silently
  107. * dropped by default, although an implementation MAY provide a
  108. * configuration parameter to forward them. Additionally, an
  109. * implementation not explicitly configured for a specific experiment
  110. * [RFC3692] SHOULD silently drop packets with Next Protocol values 0xFE
  111. * and 0xFF.
  112. *
  113. * Service Path Identifier (SPI): Identifies a service path.
  114. * Participating nodes MUST use this identifier for Service Function
  115. * Path selection. The initial classifier MUST set the appropriate SPI
  116. * for a given classification result.
  117. *
  118. * Service Index (SI): Provides location within the SFP. The initial
  119. * classifier for a given SFP SHOULD set the SI to 255, however the
  120. * control plane MAY configure the initial value of SI as appropriate
  121. * (i.e., taking into account the length of the service function path).
  122. * The Service Index MUST be decremented by a value of 1 by Service
  123. * Functions or by SFC Proxy nodes after performing required services
  124. * and the new decremented SI value MUST be used in the egress packet's
  125. * NSH. The initial Classifier MUST send the packet to the first SFF in
  126. * the identified SFP for forwarding along an SFP. If re-classification
  127. * occurs, and that re-classification results in a new SPI, the
  128. * (re)classifier is, in effect, the initial classifier for the
  129. * resultant SPI.
  130. *
  131. * The SI is used in conjunction the with Service Path Identifier for
  132. * Service Function Path Selection and for determining the next SFF/SF
  133. * in the path. The SI is also valuable when troubleshooting or
  134. * reporting service paths. Additionally, while the TTL field is the
  135. * main mechanism for service plane loop detection, the SI can also be
  136. * used for detecting service plane loops.
  137. *
  138. * When the Base Header specifies MD Type = 0x1, a Fixed Length Context
  139. * Header (16-bytes) MUST be present immediately following the Service
  140. * Path Header. The value of a Fixed Length Context
  141. * Header that carries no metadata MUST be set to zero.
  142. *
  143. * When the base header specifies MD Type = 0x2, zero or more Variable
  144. * Length Context Headers MAY be added, immediately following the
  145. * Service Path Header (see Figure 5). Therefore, Length = 0x2,
  146. * indicates that only the Base Header followed by the Service Path
  147. * Header are present. The optional Variable Length Context Headers
  148. * MUST be of an integer number of 4-bytes. The base header Length
  149. * field MUST be used to determine the offset to locate the original
  150. * packet or frame for SFC nodes that require access to that
  151. * information.
  152. *
  153. * The format of the optional variable length Context Headers
  154. *
  155. * 0 1 2 3
  156. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  157. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  158. * | Metadata Class | Type |U| Length |
  159. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  160. * | Variable Metadata |
  161. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  162. *
  163. * Metadata Class (MD Class): Defines the scope of the 'Type' field to
  164. * provide a hierarchical namespace. The IANA Considerations
  165. * Section 11.2.4 defines how the MD Class values can be allocated to
  166. * standards bodies, vendors, and others.
  167. *
  168. * Type: Indicates the explicit type of metadata being carried. The
  169. * definition of the Type is the responsibility of the MD Class owner.
  170. *
  171. * Unassigned bit: One unassigned bit is available for future use. This
  172. * bit MUST NOT be set, and MUST be ignored on receipt.
  173. *
  174. * Length: Indicates the length of the variable metadata, in bytes. In
  175. * case the metadata length is not an integer number of 4-byte words,
  176. * the sender MUST add pad bytes immediately following the last metadata
  177. * byte to extend the metadata to an integer number of 4-byte words.
  178. * The receiver MUST round up the length field to the nearest 4-byte
  179. * word boundary, to locate and process the next field in the packet.
  180. * The receiver MUST access only those bytes in the metadata indicated
  181. * by the length field (i.e., actual number of bytes) and MUST ignore
  182. * the remaining bytes up to the nearest 4-byte word boundary. The
  183. * Length may be 0 or greater.
  184. *
  185. * A value of 0 denotes a Context Header without a Variable Metadata
  186. * field.
  187. *
  188. * [0] https://datatracker.ietf.org/doc/draft-ietf-sfc-nsh/
  189. */
  190. /**
  191. * struct nsh_md1_ctx - Keeps track of NSH context data
  192. * @nshc<1-4>: NSH Contexts.
  193. */
  194. struct nsh_md1_ctx {
  195. __be32 context[4];
  196. };
  197. struct nsh_md2_tlv {
  198. __be16 md_class;
  199. u8 type;
  200. u8 length;
  201. u8 md_value[];
  202. };
  203. struct nshhdr {
  204. __be16 ver_flags_ttl_len;
  205. u8 mdtype;
  206. u8 np;
  207. __be32 path_hdr;
  208. union {
  209. struct nsh_md1_ctx md1;
  210. struct nsh_md2_tlv md2;
  211. };
  212. };
  213. /* Masking NSH header fields. */
  214. #define NSH_VER_MASK 0xc000
  215. #define NSH_VER_SHIFT 14
  216. #define NSH_FLAGS_MASK 0x3000
  217. #define NSH_FLAGS_SHIFT 12
  218. #define NSH_TTL_MASK 0x0fc0
  219. #define NSH_TTL_SHIFT 6
  220. #define NSH_LEN_MASK 0x003f
  221. #define NSH_LEN_SHIFT 0
  222. #define NSH_MDTYPE_MASK 0x0f
  223. #define NSH_MDTYPE_SHIFT 0
  224. #define NSH_SPI_MASK 0xffffff00
  225. #define NSH_SPI_SHIFT 8
  226. #define NSH_SI_MASK 0x000000ff
  227. #define NSH_SI_SHIFT 0
  228. /* MD Type Registry. */
  229. #define NSH_M_TYPE1 0x01
  230. #define NSH_M_TYPE2 0x02
  231. #define NSH_M_EXP1 0xFE
  232. #define NSH_M_EXP2 0xFF
  233. /* NSH Base Header Length */
  234. #define NSH_BASE_HDR_LEN 8
  235. /* NSH MD Type 1 header Length. */
  236. #define NSH_M_TYPE1_LEN 24
  237. /* NSH header maximum Length. */
  238. #define NSH_HDR_MAX_LEN 256
  239. /* NSH context headers maximum Length. */
  240. #define NSH_CTX_HDRS_MAX_LEN 248
  241. static inline struct nshhdr *nsh_hdr(struct sk_buff *skb)
  242. {
  243. return (struct nshhdr *)skb_network_header(skb);
  244. }
  245. static inline u16 nsh_hdr_len(const struct nshhdr *nsh)
  246. {
  247. return ((ntohs(nsh->ver_flags_ttl_len) & NSH_LEN_MASK)
  248. >> NSH_LEN_SHIFT) << 2;
  249. }
  250. static inline u8 nsh_get_ver(const struct nshhdr *nsh)
  251. {
  252. return (ntohs(nsh->ver_flags_ttl_len) & NSH_VER_MASK)
  253. >> NSH_VER_SHIFT;
  254. }
  255. static inline u8 nsh_get_flags(const struct nshhdr *nsh)
  256. {
  257. return (ntohs(nsh->ver_flags_ttl_len) & NSH_FLAGS_MASK)
  258. >> NSH_FLAGS_SHIFT;
  259. }
  260. static inline u8 nsh_get_ttl(const struct nshhdr *nsh)
  261. {
  262. return (ntohs(nsh->ver_flags_ttl_len) & NSH_TTL_MASK)
  263. >> NSH_TTL_SHIFT;
  264. }
  265. static inline void __nsh_set_xflag(struct nshhdr *nsh, u16 xflag, u16 xmask)
  266. {
  267. nsh->ver_flags_ttl_len
  268. = (nsh->ver_flags_ttl_len & ~htons(xmask)) | htons(xflag);
  269. }
  270. static inline void nsh_set_flags_and_ttl(struct nshhdr *nsh, u8 flags, u8 ttl)
  271. {
  272. __nsh_set_xflag(nsh, ((flags << NSH_FLAGS_SHIFT) & NSH_FLAGS_MASK) |
  273. ((ttl << NSH_TTL_SHIFT) & NSH_TTL_MASK),
  274. NSH_FLAGS_MASK | NSH_TTL_MASK);
  275. }
  276. static inline void nsh_set_flags_ttl_len(struct nshhdr *nsh, u8 flags,
  277. u8 ttl, u8 len)
  278. {
  279. len = len >> 2;
  280. __nsh_set_xflag(nsh, ((flags << NSH_FLAGS_SHIFT) & NSH_FLAGS_MASK) |
  281. ((ttl << NSH_TTL_SHIFT) & NSH_TTL_MASK) |
  282. ((len << NSH_LEN_SHIFT) & NSH_LEN_MASK),
  283. NSH_FLAGS_MASK | NSH_TTL_MASK | NSH_LEN_MASK);
  284. }
  285. #endif /* __NET_NSH_H */