net: use SPDX tags
[dpdk.git] / lib / librte_net / rte_net.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  */
4
5 #include <stdint.h>
6
7 #include <rte_mbuf.h>
8 #include <rte_mbuf_ptype.h>
9 #include <rte_byteorder.h>
10 #include <rte_ether.h>
11 #include <rte_ip.h>
12 #include <rte_tcp.h>
13 #include <rte_udp.h>
14 #include <rte_sctp.h>
15 #include <rte_gre.h>
16 #include <rte_net.h>
17
18 /* get l3 packet type from ip6 next protocol */
19 static uint32_t
20 ptype_l3_ip6(uint8_t ip6_proto)
21 {
22         static const uint32_t ip6_ext_proto_map[256] = {
23                 [IPPROTO_HOPOPTS] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
24                 [IPPROTO_ROUTING] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
25                 [IPPROTO_FRAGMENT] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
26                 [IPPROTO_ESP] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
27                 [IPPROTO_AH] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
28                 [IPPROTO_DSTOPTS] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
29         };
30
31         return RTE_PTYPE_L3_IPV6 + ip6_ext_proto_map[ip6_proto];
32 }
33
34 /* get l3 packet type from ip version and header length */
35 static uint32_t
36 ptype_l3_ip(uint8_t ipv_ihl)
37 {
38         static const uint32_t ptype_l3_ip_proto_map[256] = {
39                 [0x45] = RTE_PTYPE_L3_IPV4,
40                 [0x46] = RTE_PTYPE_L3_IPV4_EXT,
41                 [0x47] = RTE_PTYPE_L3_IPV4_EXT,
42                 [0x48] = RTE_PTYPE_L3_IPV4_EXT,
43                 [0x49] = RTE_PTYPE_L3_IPV4_EXT,
44                 [0x4A] = RTE_PTYPE_L3_IPV4_EXT,
45                 [0x4B] = RTE_PTYPE_L3_IPV4_EXT,
46                 [0x4C] = RTE_PTYPE_L3_IPV4_EXT,
47                 [0x4D] = RTE_PTYPE_L3_IPV4_EXT,
48                 [0x4E] = RTE_PTYPE_L3_IPV4_EXT,
49                 [0x4F] = RTE_PTYPE_L3_IPV4_EXT,
50         };
51
52         return ptype_l3_ip_proto_map[ipv_ihl];
53 }
54
55 /* get l4 packet type from proto */
56 static uint32_t
57 ptype_l4(uint8_t proto)
58 {
59         static const uint32_t ptype_l4_proto[256] = {
60                 [IPPROTO_UDP] = RTE_PTYPE_L4_UDP,
61                 [IPPROTO_TCP] = RTE_PTYPE_L4_TCP,
62                 [IPPROTO_SCTP] = RTE_PTYPE_L4_SCTP,
63         };
64
65         return ptype_l4_proto[proto];
66 }
67
68 /* get inner l3 packet type from ip6 next protocol */
69 static uint32_t
70 ptype_inner_l3_ip6(uint8_t ip6_proto)
71 {
72         static const uint32_t ptype_inner_ip6_ext_proto_map[256] = {
73                 [IPPROTO_HOPOPTS] = RTE_PTYPE_INNER_L3_IPV6_EXT -
74                         RTE_PTYPE_INNER_L3_IPV6,
75                 [IPPROTO_ROUTING] = RTE_PTYPE_INNER_L3_IPV6_EXT -
76                         RTE_PTYPE_INNER_L3_IPV6,
77                 [IPPROTO_FRAGMENT] = RTE_PTYPE_INNER_L3_IPV6_EXT -
78                         RTE_PTYPE_INNER_L3_IPV6,
79                 [IPPROTO_ESP] = RTE_PTYPE_INNER_L3_IPV6_EXT -
80                         RTE_PTYPE_INNER_L3_IPV6,
81                 [IPPROTO_AH] = RTE_PTYPE_INNER_L3_IPV6_EXT -
82                         RTE_PTYPE_INNER_L3_IPV6,
83                 [IPPROTO_DSTOPTS] = RTE_PTYPE_INNER_L3_IPV6_EXT -
84                         RTE_PTYPE_INNER_L3_IPV6,
85         };
86
87         return RTE_PTYPE_INNER_L3_IPV6 +
88                 ptype_inner_ip6_ext_proto_map[ip6_proto];
89 }
90
91 /* get inner l3 packet type from ip version and header length */
92 static uint32_t
93 ptype_inner_l3_ip(uint8_t ipv_ihl)
94 {
95         static const uint32_t ptype_inner_l3_ip_proto_map[256] = {
96                 [0x45] = RTE_PTYPE_INNER_L3_IPV4,
97                 [0x46] = RTE_PTYPE_INNER_L3_IPV4_EXT,
98                 [0x47] = RTE_PTYPE_INNER_L3_IPV4_EXT,
99                 [0x48] = RTE_PTYPE_INNER_L3_IPV4_EXT,
100                 [0x49] = RTE_PTYPE_INNER_L3_IPV4_EXT,
101                 [0x4A] = RTE_PTYPE_INNER_L3_IPV4_EXT,
102                 [0x4B] = RTE_PTYPE_INNER_L3_IPV4_EXT,
103                 [0x4C] = RTE_PTYPE_INNER_L3_IPV4_EXT,
104                 [0x4D] = RTE_PTYPE_INNER_L3_IPV4_EXT,
105                 [0x4E] = RTE_PTYPE_INNER_L3_IPV4_EXT,
106                 [0x4F] = RTE_PTYPE_INNER_L3_IPV4_EXT,
107         };
108
109         return ptype_inner_l3_ip_proto_map[ipv_ihl];
110 }
111
112 /* get inner l4 packet type from proto */
113 static uint32_t
114 ptype_inner_l4(uint8_t proto)
115 {
116         static const uint32_t ptype_inner_l4_proto[256] = {
117                 [IPPROTO_UDP] = RTE_PTYPE_INNER_L4_UDP,
118                 [IPPROTO_TCP] = RTE_PTYPE_INNER_L4_TCP,
119                 [IPPROTO_SCTP] = RTE_PTYPE_INNER_L4_SCTP,
120         };
121
122         return ptype_inner_l4_proto[proto];
123 }
124
125 /* get the tunnel packet type if any, update proto and off. */
126 static uint32_t
127 ptype_tunnel(uint16_t *proto, const struct rte_mbuf *m,
128         uint32_t *off)
129 {
130         switch (*proto) {
131         case IPPROTO_GRE: {
132                 static const uint8_t opt_len[16] = {
133                         [0x0] = 4,
134                         [0x1] = 8,
135                         [0x2] = 8,
136                         [0x8] = 8,
137                         [0x3] = 12,
138                         [0x9] = 12,
139                         [0xa] = 12,
140                         [0xb] = 16,
141                 };
142                 const struct gre_hdr *gh;
143                 struct gre_hdr gh_copy;
144                 uint16_t flags;
145
146                 gh = rte_pktmbuf_read(m, *off, sizeof(*gh), &gh_copy);
147                 if (unlikely(gh == NULL))
148                         return 0;
149
150                 flags = rte_be_to_cpu_16(*(const uint16_t *)gh);
151                 flags >>= 12;
152                 if (opt_len[flags] == 0)
153                         return 0;
154
155                 *off += opt_len[flags];
156                 *proto = gh->proto;
157                 if (*proto == rte_cpu_to_be_16(ETHER_TYPE_TEB))
158                         return RTE_PTYPE_TUNNEL_NVGRE;
159                 else
160                         return RTE_PTYPE_TUNNEL_GRE;
161         }
162         case IPPROTO_IPIP:
163                 *proto = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
164                 return RTE_PTYPE_TUNNEL_IP;
165         case IPPROTO_IPV6:
166                 *proto = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
167                 return RTE_PTYPE_TUNNEL_IP; /* IP is also valid for IPv6 */
168         default:
169                 return 0;
170         }
171 }
172
173 /* get the ipv4 header length */
174 static uint8_t
175 ip4_hlen(const struct ipv4_hdr *hdr)
176 {
177         return (hdr->version_ihl & 0xf) * 4;
178 }
179
180 /* parse ipv6 extended headers, update offset and return next proto */
181 static uint16_t
182 skip_ip6_ext(uint16_t proto, const struct rte_mbuf *m, uint32_t *off,
183         int *frag)
184 {
185         struct ext_hdr {
186                 uint8_t next_hdr;
187                 uint8_t len;
188         };
189         const struct ext_hdr *xh;
190         struct ext_hdr xh_copy;
191         unsigned int i;
192
193         *frag = 0;
194
195 #define MAX_EXT_HDRS 5
196         for (i = 0; i < MAX_EXT_HDRS; i++) {
197                 switch (proto) {
198                 case IPPROTO_HOPOPTS:
199                 case IPPROTO_ROUTING:
200                 case IPPROTO_DSTOPTS:
201                         xh = rte_pktmbuf_read(m, *off, sizeof(*xh),
202                                 &xh_copy);
203                         if (xh == NULL)
204                                 return 0;
205                         *off += (xh->len + 1) * 8;
206                         proto = xh->next_hdr;
207                         break;
208                 case IPPROTO_FRAGMENT:
209                         xh = rte_pktmbuf_read(m, *off, sizeof(*xh),
210                                 &xh_copy);
211                         if (xh == NULL)
212                                 return 0;
213                         *off += 8;
214                         proto = xh->next_hdr;
215                         *frag = 1;
216                         return proto; /* this is always the last ext hdr */
217                 case IPPROTO_NONE:
218                         return 0;
219                 default:
220                         return proto;
221                 }
222         }
223         return 0;
224 }
225
226 /* parse mbuf data to get packet type */
227 uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
228         struct rte_net_hdr_lens *hdr_lens, uint32_t layers)
229 {
230         struct rte_net_hdr_lens local_hdr_lens;
231         const struct ether_hdr *eh;
232         struct ether_hdr eh_copy;
233         uint32_t pkt_type = RTE_PTYPE_L2_ETHER;
234         uint32_t off = 0;
235         uint16_t proto;
236
237         if (hdr_lens == NULL)
238                 hdr_lens = &local_hdr_lens;
239
240         eh = rte_pktmbuf_read(m, off, sizeof(*eh), &eh_copy);
241         if (unlikely(eh == NULL))
242                 return 0;
243         proto = eh->ether_type;
244         off = sizeof(*eh);
245         hdr_lens->l2_len = off;
246
247         if ((layers & RTE_PTYPE_L2_MASK) == 0)
248                 return 0;
249
250         if (proto == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
251                 goto l3; /* fast path if packet is IPv4 */
252
253         if (proto == rte_cpu_to_be_16(ETHER_TYPE_VLAN)) {
254                 const struct vlan_hdr *vh;
255                 struct vlan_hdr vh_copy;
256
257                 pkt_type = RTE_PTYPE_L2_ETHER_VLAN;
258                 vh = rte_pktmbuf_read(m, off, sizeof(*vh), &vh_copy);
259                 if (unlikely(vh == NULL))
260                         return pkt_type;
261                 off += sizeof(*vh);
262                 hdr_lens->l2_len += sizeof(*vh);
263                 proto = vh->eth_proto;
264         } else if (proto == rte_cpu_to_be_16(ETHER_TYPE_QINQ)) {
265                 const struct vlan_hdr *vh;
266                 struct vlan_hdr vh_copy;
267
268                 pkt_type = RTE_PTYPE_L2_ETHER_QINQ;
269                 vh = rte_pktmbuf_read(m, off + sizeof(*vh), sizeof(*vh),
270                         &vh_copy);
271                 if (unlikely(vh == NULL))
272                         return pkt_type;
273                 off += 2 * sizeof(*vh);
274                 hdr_lens->l2_len += 2 * sizeof(*vh);
275                 proto = vh->eth_proto;
276         }
277
278  l3:
279         if ((layers & RTE_PTYPE_L3_MASK) == 0)
280                 return pkt_type;
281
282         if (proto == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
283                 const struct ipv4_hdr *ip4h;
284                 struct ipv4_hdr ip4h_copy;
285
286                 ip4h = rte_pktmbuf_read(m, off, sizeof(*ip4h), &ip4h_copy);
287                 if (unlikely(ip4h == NULL))
288                         return pkt_type;
289
290                 pkt_type |= ptype_l3_ip(ip4h->version_ihl);
291                 hdr_lens->l3_len = ip4_hlen(ip4h);
292                 off += hdr_lens->l3_len;
293
294                 if ((layers & RTE_PTYPE_L4_MASK) == 0)
295                         return pkt_type;
296
297                 if (ip4h->fragment_offset & rte_cpu_to_be_16(
298                                 IPV4_HDR_OFFSET_MASK | IPV4_HDR_MF_FLAG)) {
299                         pkt_type |= RTE_PTYPE_L4_FRAG;
300                         hdr_lens->l4_len = 0;
301                         return pkt_type;
302                 }
303                 proto = ip4h->next_proto_id;
304                 pkt_type |= ptype_l4(proto);
305         } else if (proto == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
306                 const struct ipv6_hdr *ip6h;
307                 struct ipv6_hdr ip6h_copy;
308                 int frag = 0;
309
310                 ip6h = rte_pktmbuf_read(m, off, sizeof(*ip6h), &ip6h_copy);
311                 if (unlikely(ip6h == NULL))
312                         return pkt_type;
313
314                 proto = ip6h->proto;
315                 hdr_lens->l3_len = sizeof(*ip6h);
316                 off += hdr_lens->l3_len;
317                 pkt_type |= ptype_l3_ip6(proto);
318                 if ((pkt_type & RTE_PTYPE_L3_MASK) == RTE_PTYPE_L3_IPV6_EXT) {
319                         proto = skip_ip6_ext(proto, m, &off, &frag);
320                         hdr_lens->l3_len = off - hdr_lens->l2_len;
321                 }
322                 if (proto == 0)
323                         return pkt_type;
324
325                 if ((layers & RTE_PTYPE_L4_MASK) == 0)
326                         return pkt_type;
327
328                 if (frag) {
329                         pkt_type |= RTE_PTYPE_L4_FRAG;
330                         hdr_lens->l4_len = 0;
331                         return pkt_type;
332                 }
333                 pkt_type |= ptype_l4(proto);
334         }
335
336         if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP) {
337                 hdr_lens->l4_len = sizeof(struct udp_hdr);
338                 return pkt_type;
339         } else if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP) {
340                 const struct tcp_hdr *th;
341                 struct tcp_hdr th_copy;
342
343                 th = rte_pktmbuf_read(m, off, sizeof(*th), &th_copy);
344                 if (unlikely(th == NULL))
345                         return pkt_type & (RTE_PTYPE_L2_MASK |
346                                 RTE_PTYPE_L3_MASK);
347                 hdr_lens->l4_len = (th->data_off & 0xf0) >> 2;
348                 return pkt_type;
349         } else if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP) {
350                 hdr_lens->l4_len = sizeof(struct sctp_hdr);
351                 return pkt_type;
352         } else {
353                 uint32_t prev_off = off;
354
355                 hdr_lens->l4_len = 0;
356
357                 if ((layers & RTE_PTYPE_TUNNEL_MASK) == 0)
358                         return pkt_type;
359
360                 pkt_type |= ptype_tunnel(&proto, m, &off);
361                 hdr_lens->tunnel_len = off - prev_off;
362         }
363
364         /* same job for inner header: we need to duplicate the code
365          * because the packet types do not have the same value.
366          */
367         if ((layers & RTE_PTYPE_INNER_L2_MASK) == 0)
368                 return pkt_type;
369
370         hdr_lens->inner_l2_len = 0;
371         if (proto == rte_cpu_to_be_16(ETHER_TYPE_TEB)) {
372                 eh = rte_pktmbuf_read(m, off, sizeof(*eh), &eh_copy);
373                 if (unlikely(eh == NULL))
374                         return pkt_type;
375                 pkt_type |= RTE_PTYPE_INNER_L2_ETHER;
376                 proto = eh->ether_type;
377                 off += sizeof(*eh);
378                 hdr_lens->inner_l2_len = sizeof(*eh);
379         }
380
381         if (proto == rte_cpu_to_be_16(ETHER_TYPE_VLAN)) {
382                 const struct vlan_hdr *vh;
383                 struct vlan_hdr vh_copy;
384
385                 pkt_type &= ~RTE_PTYPE_INNER_L2_MASK;
386                 pkt_type |= RTE_PTYPE_INNER_L2_ETHER_VLAN;
387                 vh = rte_pktmbuf_read(m, off, sizeof(*vh), &vh_copy);
388                 if (unlikely(vh == NULL))
389                         return pkt_type;
390                 off += sizeof(*vh);
391                 hdr_lens->inner_l2_len += sizeof(*vh);
392                 proto = vh->eth_proto;
393         } else if (proto == rte_cpu_to_be_16(ETHER_TYPE_QINQ)) {
394                 const struct vlan_hdr *vh;
395                 struct vlan_hdr vh_copy;
396
397                 pkt_type &= ~RTE_PTYPE_INNER_L2_MASK;
398                 pkt_type |= RTE_PTYPE_INNER_L2_ETHER_QINQ;
399                 vh = rte_pktmbuf_read(m, off + sizeof(*vh), sizeof(*vh),
400                         &vh_copy);
401                 if (unlikely(vh == NULL))
402                         return pkt_type;
403                 off += 2 * sizeof(*vh);
404                 hdr_lens->inner_l2_len += 2 * sizeof(*vh);
405                 proto = vh->eth_proto;
406         }
407
408         if ((layers & RTE_PTYPE_INNER_L3_MASK) == 0)
409                 return pkt_type;
410
411         if (proto == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
412                 const struct ipv4_hdr *ip4h;
413                 struct ipv4_hdr ip4h_copy;
414
415                 ip4h = rte_pktmbuf_read(m, off, sizeof(*ip4h), &ip4h_copy);
416                 if (unlikely(ip4h == NULL))
417                         return pkt_type;
418
419                 pkt_type |= ptype_inner_l3_ip(ip4h->version_ihl);
420                 hdr_lens->inner_l3_len = ip4_hlen(ip4h);
421                 off += hdr_lens->inner_l3_len;
422
423                 if ((layers & RTE_PTYPE_INNER_L4_MASK) == 0)
424                         return pkt_type;
425                 if (ip4h->fragment_offset &
426                                 rte_cpu_to_be_16(IPV4_HDR_OFFSET_MASK |
427                                         IPV4_HDR_MF_FLAG)) {
428                         pkt_type |= RTE_PTYPE_INNER_L4_FRAG;
429                         hdr_lens->inner_l4_len = 0;
430                         return pkt_type;
431                 }
432                 proto = ip4h->next_proto_id;
433                 pkt_type |= ptype_inner_l4(proto);
434         } else if (proto == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
435                 const struct ipv6_hdr *ip6h;
436                 struct ipv6_hdr ip6h_copy;
437                 int frag = 0;
438
439                 ip6h = rte_pktmbuf_read(m, off, sizeof(*ip6h), &ip6h_copy);
440                 if (unlikely(ip6h == NULL))
441                         return pkt_type;
442
443                 proto = ip6h->proto;
444                 hdr_lens->inner_l3_len = sizeof(*ip6h);
445                 off += hdr_lens->inner_l3_len;
446                 pkt_type |= ptype_inner_l3_ip6(proto);
447                 if ((pkt_type & RTE_PTYPE_INNER_L3_MASK) ==
448                                 RTE_PTYPE_INNER_L3_IPV6_EXT) {
449                         uint32_t prev_off;
450
451                         prev_off = off;
452                         proto = skip_ip6_ext(proto, m, &off, &frag);
453                         hdr_lens->inner_l3_len += off - prev_off;
454                 }
455                 if (proto == 0)
456                         return pkt_type;
457
458                 if ((layers & RTE_PTYPE_INNER_L4_MASK) == 0)
459                         return pkt_type;
460
461                 if (frag) {
462                         pkt_type |= RTE_PTYPE_INNER_L4_FRAG;
463                         hdr_lens->inner_l4_len = 0;
464                         return pkt_type;
465                 }
466                 pkt_type |= ptype_inner_l4(proto);
467         }
468
469         if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) == RTE_PTYPE_INNER_L4_UDP) {
470                 hdr_lens->inner_l4_len = sizeof(struct udp_hdr);
471         } else if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) ==
472                         RTE_PTYPE_INNER_L4_TCP) {
473                 const struct tcp_hdr *th;
474                 struct tcp_hdr th_copy;
475
476                 th = rte_pktmbuf_read(m, off, sizeof(*th), &th_copy);
477                 if (unlikely(th == NULL))
478                         return pkt_type & (RTE_PTYPE_INNER_L2_MASK |
479                                 RTE_PTYPE_INNER_L3_MASK);
480                 hdr_lens->inner_l4_len = (th->data_off & 0xf0) >> 2;
481         } else if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) ==
482                         RTE_PTYPE_INNER_L4_SCTP) {
483                 hdr_lens->inner_l4_len = sizeof(struct sctp_hdr);
484         } else {
485                 hdr_lens->inner_l4_len = 0;
486         }
487
488         return pkt_type;
489 }