1 /*
2  * Copyright 2010 Tilera Corporation. All Rights Reserved.
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
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, GOOD TITLE or
11  *   NON INFRINGEMENT.  See the GNU General Public License for
12  *   more details.
13  */
14 
15 /**
16  * NetIO interface structures and macros.
17  */
18 
19 #ifndef __NETIO_INTF_H__
20 #define __NETIO_INTF_H__
21 
22 #include <hv/netio_errors.h>
23 
24 #ifdef __KERNEL__
25 #include <linux/types.h>
26 #else
27 #include <stdint.h>
28 #endif
29 
30 #if !defined(__HV__) && !defined(__BOGUX__) && !defined(__KERNEL__)
31 #include <assert.h>
32 #define netio_assert assert  /**< Enable assertions from macros */
33 #else
34 #define netio_assert(...) ((void)(0))  /**< Disable assertions from macros */
35 #endif
36 
37 /*
38  * If none of these symbols are defined, we're building libnetio in an
39  * environment where we have pthreads, so we'll enable locking.
40  */
41 #if !defined(__HV__) && !defined(__BOGUX__) && !defined(__KERNEL__) && \
42     !defined(__NEWLIB__)
43 #define _NETIO_PTHREAD       /**< Include a mutex in netio_queue_t below */
44 
45 /*
46  * If NETIO_UNLOCKED is defined, we don't do use per-cpu locks on
47  * per-packet NetIO operations.  We still do pthread locking on things
48  * like netio_input_register, though.  This is used for building
49  * libnetio_unlocked.
50  */
51 #ifndef NETIO_UNLOCKED
52 
53 /* Avoid PLT overhead by using our own inlined per-cpu lock. */
54 #include <sched.h>
55 typedef int _netio_percpu_mutex_t;
56 
57 static __inline int
_netio_percpu_mutex_init(_netio_percpu_mutex_t * lock)58 _netio_percpu_mutex_init(_netio_percpu_mutex_t* lock)
59 {
60   *lock = 0;
61   return 0;
62 }
63 
64 static __inline int
_netio_percpu_mutex_lock(_netio_percpu_mutex_t * lock)65 _netio_percpu_mutex_lock(_netio_percpu_mutex_t* lock)
66 {
67   while (__builtin_expect(__insn_tns(lock), 0))
68     sched_yield();
69   return 0;
70 }
71 
72 static __inline int
_netio_percpu_mutex_unlock(_netio_percpu_mutex_t * lock)73 _netio_percpu_mutex_unlock(_netio_percpu_mutex_t* lock)
74 {
75   *lock = 0;
76   return 0;
77 }
78 
79 #else /* NETIO_UNLOCKED */
80 
81 /* Don't do any locking for per-packet NetIO operations. */
82 typedef int _netio_percpu_mutex_t;
83 #define _netio_percpu_mutex_init(L)
84 #define _netio_percpu_mutex_lock(L)
85 #define _netio_percpu_mutex_unlock(L)
86 
87 #endif /* NETIO_UNLOCKED */
88 #endif /* !__HV__, !__BOGUX, !__KERNEL__, !__NEWLIB__ */
89 
90 /** How many tiles can register for a given queue.
91  *  @ingroup setup */
92 #define NETIO_MAX_TILES_PER_QUEUE  64
93 
94 
95 /** Largest permissible queue identifier.
96  *  @ingroup setup  */
97 #define NETIO_MAX_QUEUE_ID        255
98 
99 
100 #ifndef __DOXYGEN__
101 
102 /* Metadata packet checksum/ethertype flags. */
103 
104 /** The L4 checksum has not been calculated. */
105 #define _NETIO_PKT_NO_L4_CSUM_SHIFT           0
106 #define _NETIO_PKT_NO_L4_CSUM_RMASK           1
107 #define _NETIO_PKT_NO_L4_CSUM_MASK \
108          (_NETIO_PKT_NO_L4_CSUM_RMASK << _NETIO_PKT_NO_L4_CSUM_SHIFT)
109 
110 /** The L3 checksum has not been calculated. */
111 #define _NETIO_PKT_NO_L3_CSUM_SHIFT           1
112 #define _NETIO_PKT_NO_L3_CSUM_RMASK           1
113 #define _NETIO_PKT_NO_L3_CSUM_MASK \
114          (_NETIO_PKT_NO_L3_CSUM_RMASK << _NETIO_PKT_NO_L3_CSUM_SHIFT)
115 
116 /** The L3 checksum is incorrect (or perhaps has not been calculated). */
117 #define _NETIO_PKT_BAD_L3_CSUM_SHIFT          2
118 #define _NETIO_PKT_BAD_L3_CSUM_RMASK          1
119 #define _NETIO_PKT_BAD_L3_CSUM_MASK \
120          (_NETIO_PKT_BAD_L3_CSUM_RMASK << _NETIO_PKT_BAD_L3_CSUM_SHIFT)
121 
122 /** The Ethernet packet type is unrecognized. */
123 #define _NETIO_PKT_TYPE_UNRECOGNIZED_SHIFT    3
124 #define _NETIO_PKT_TYPE_UNRECOGNIZED_RMASK    1
125 #define _NETIO_PKT_TYPE_UNRECOGNIZED_MASK \
126          (_NETIO_PKT_TYPE_UNRECOGNIZED_RMASK << \
127           _NETIO_PKT_TYPE_UNRECOGNIZED_SHIFT)
128 
129 /* Metadata packet type flags. */
130 
131 /** Where the packet type bits are; this field is the index into
132  *  _netio_pkt_info. */
133 #define _NETIO_PKT_TYPE_SHIFT        4
134 #define _NETIO_PKT_TYPE_RMASK        0x3F
135 
136 /** How many VLAN tags the packet has, and, if we have two, which one we
137  *  actually grouped on.  A VLAN within a proprietary (Marvell or Broadcom)
138  *  tag is counted here. */
139 #define _NETIO_PKT_VLAN_SHIFT        4
140 #define _NETIO_PKT_VLAN_RMASK        0x3
141 #define _NETIO_PKT_VLAN_MASK \
142          (_NETIO_PKT_VLAN_RMASK << _NETIO_PKT_VLAN_SHIFT)
143 #define _NETIO_PKT_VLAN_NONE         0   /* No VLAN tag. */
144 #define _NETIO_PKT_VLAN_ONE          1   /* One VLAN tag. */
145 #define _NETIO_PKT_VLAN_TWO_OUTER    2   /* Two VLAN tags, outer one used. */
146 #define _NETIO_PKT_VLAN_TWO_INNER    3   /* Two VLAN tags, inner one used. */
147 
148 /** Which proprietary tags the packet has. */
149 #define _NETIO_PKT_TAG_SHIFT         6
150 #define _NETIO_PKT_TAG_RMASK         0x3
151 #define _NETIO_PKT_TAG_MASK \
152           (_NETIO_PKT_TAG_RMASK << _NETIO_PKT_TAG_SHIFT)
153 #define _NETIO_PKT_TAG_NONE          0   /* No proprietary tags. */
154 #define _NETIO_PKT_TAG_MRVL          1   /* Marvell HyperG.Stack tags. */
155 #define _NETIO_PKT_TAG_MRVL_EXT      2   /* HyperG.Stack extended tags. */
156 #define _NETIO_PKT_TAG_BRCM          3   /* Broadcom HiGig tags. */
157 
158 /** Whether a packet has an LLC + SNAP header. */
159 #define _NETIO_PKT_SNAP_SHIFT        8
160 #define _NETIO_PKT_SNAP_RMASK        0x1
161 #define _NETIO_PKT_SNAP_MASK \
162           (_NETIO_PKT_SNAP_RMASK << _NETIO_PKT_SNAP_SHIFT)
163 
164 /* NOTE: Bits 9 and 10 are unused. */
165 
166 /** Length of any custom data before the L2 header, in words. */
167 #define _NETIO_PKT_CUSTOM_LEN_SHIFT  11
168 #define _NETIO_PKT_CUSTOM_LEN_RMASK  0x1F
169 #define _NETIO_PKT_CUSTOM_LEN_MASK \
170           (_NETIO_PKT_CUSTOM_LEN_RMASK << _NETIO_PKT_CUSTOM_LEN_SHIFT)
171 
172 /** The L4 checksum is incorrect (or perhaps has not been calculated). */
173 #define _NETIO_PKT_BAD_L4_CSUM_SHIFT 16
174 #define _NETIO_PKT_BAD_L4_CSUM_RMASK 0x1
175 #define _NETIO_PKT_BAD_L4_CSUM_MASK \
176           (_NETIO_PKT_BAD_L4_CSUM_RMASK << _NETIO_PKT_BAD_L4_CSUM_SHIFT)
177 
178 /** Length of the L2 header, in words. */
179 #define _NETIO_PKT_L2_LEN_SHIFT  17
180 #define _NETIO_PKT_L2_LEN_RMASK  0x1F
181 #define _NETIO_PKT_L2_LEN_MASK \
182           (_NETIO_PKT_L2_LEN_RMASK << _NETIO_PKT_L2_LEN_SHIFT)
183 
184 
185 /* Flags in minimal packet metadata. */
186 
187 /** We need an eDMA checksum on this packet. */
188 #define _NETIO_PKT_NEED_EDMA_CSUM_SHIFT            0
189 #define _NETIO_PKT_NEED_EDMA_CSUM_RMASK            1
190 #define _NETIO_PKT_NEED_EDMA_CSUM_MASK \
191          (_NETIO_PKT_NEED_EDMA_CSUM_RMASK << _NETIO_PKT_NEED_EDMA_CSUM_SHIFT)
192 
193 /* Data within the packet information table. */
194 
195 /* Note that, for efficiency, code which uses these fields assumes that none
196  * of the shift values below are zero.  See uses below for an explanation. */
197 
198 /** Offset within the L2 header of the innermost ethertype (in halfwords). */
199 #define _NETIO_PKT_INFO_ETYPE_SHIFT       6
200 #define _NETIO_PKT_INFO_ETYPE_RMASK    0x1F
201 
202 /** Offset within the L2 header of the VLAN tag (in halfwords). */
203 #define _NETIO_PKT_INFO_VLAN_SHIFT       11
204 #define _NETIO_PKT_INFO_VLAN_RMASK     0x1F
205 
206 #endif
207 
208 
209 /** The size of a memory buffer representing a small packet.
210  *  @ingroup egress */
211 #define SMALL_PACKET_SIZE 256
212 
213 /** The size of a memory buffer representing a large packet.
214  *  @ingroup egress */
215 #define LARGE_PACKET_SIZE 2048
216 
217 /** The size of a memory buffer representing a jumbo packet.
218  *  @ingroup egress */
219 #define JUMBO_PACKET_SIZE (12 * 1024)
220 
221 
222 /* Common ethertypes.
223  * @ingroup ingress */
224 /** @{ */
225 /** The ethertype of IPv4. */
226 #define ETHERTYPE_IPv4 (0x0800)
227 /** The ethertype of ARP. */
228 #define ETHERTYPE_ARP (0x0806)
229 /** The ethertype of VLANs. */
230 #define ETHERTYPE_VLAN (0x8100)
231 /** The ethertype of a Q-in-Q header. */
232 #define ETHERTYPE_Q_IN_Q (0x9100)
233 /** The ethertype of IPv6. */
234 #define ETHERTYPE_IPv6 (0x86DD)
235 /** The ethertype of MPLS. */
236 #define ETHERTYPE_MPLS (0x8847)
237 /** @} */
238 
239 
240 /** The possible return values of NETIO_PKT_STATUS.
241  * @ingroup ingress
242  */
243 typedef enum
244 {
245   /** No problems were detected with this packet. */
246   NETIO_PKT_STATUS_OK,
247   /** The packet is undersized; this is expected behavior if the packet's
248     * ethertype is unrecognized, but otherwise the packet is likely corrupt. */
249   NETIO_PKT_STATUS_UNDERSIZE,
250   /** The packet is oversized and some trailing bytes have been discarded.
251       This is expected behavior for short packets, since it's impossible to
252       precisely determine the amount of padding which may have been added to
253       them to make them meet the minimum Ethernet packet size. */
254   NETIO_PKT_STATUS_OVERSIZE,
255   /** The packet was judged to be corrupt by hardware (for instance, it had
256       a bad CRC, or part of it was discarded due to lack of buffer space in
257       the I/O shim) and should be discarded. */
258   NETIO_PKT_STATUS_BAD
259 } netio_pkt_status_t;
260 
261 
262 /** Log2 of how many buckets we have. */
263 #define NETIO_LOG2_NUM_BUCKETS (10)
264 
265 /** How many buckets we have.
266  * @ingroup ingress */
267 #define NETIO_NUM_BUCKETS (1 << NETIO_LOG2_NUM_BUCKETS)
268 
269 
270 /**
271  * @brief A group-to-bucket identifier.
272  *
273  * @ingroup setup
274  *
275  * This tells us what to do with a given group.
276  */
277 typedef union {
278   /** The header broken down into bits. */
279   struct {
280     /** Whether we should balance on L4, if available */
281     unsigned int __balance_on_l4:1;
282     /** Whether we should balance on L3, if available */
283     unsigned int __balance_on_l3:1;
284     /** Whether we should balance on L2, if available */
285     unsigned int __balance_on_l2:1;
286     /** Reserved for future use */
287     unsigned int __reserved:1;
288     /** The base bucket to use to send traffic */
289     unsigned int __bucket_base:NETIO_LOG2_NUM_BUCKETS;
290     /** The mask to apply to the balancing value. This must be one less
291      * than a power of two, e.g. 0x3 or 0xFF.
292      */
293     unsigned int __bucket_mask:NETIO_LOG2_NUM_BUCKETS;
294     /** Pad to 32 bits */
295     unsigned int __padding:(32 - 4 - 2 * NETIO_LOG2_NUM_BUCKETS);
296   } bits;
297   /** To send out the IDN. */
298   unsigned int word;
299 }
300 netio_group_t;
301 
302 
303 /**
304  * @brief A VLAN-to-bucket identifier.
305  *
306  * @ingroup setup
307  *
308  * This tells us what to do with a given VLAN.
309  */
310 typedef netio_group_t netio_vlan_t;
311 
312 
313 /**
314  * A bucket-to-queue mapping.
315  * @ingroup setup
316  */
317 typedef unsigned char netio_bucket_t;
318 
319 
320 /**
321  * A packet size can always fit in a netio_size_t.
322  * @ingroup setup
323  */
324 typedef unsigned int netio_size_t;
325 
326 
327 /**
328  * @brief Ethernet standard (ingress) packet metadata.
329  *
330  * @ingroup ingress
331  *
332  * This is additional data associated with each packet.
333  * This structure is opaque and accessed through the @ref ingress.
334  *
335  * Also, the buffer population operation currently assumes that standard
336  * metadata is at least as large as minimal metadata, and will need to be
337  * modified if that is no longer the case.
338  */
339 typedef struct
340 {
341 #ifdef __DOXYGEN__
342   /** This structure is opaque. */
343   unsigned char opaque[24];
344 #else
345   /** The overall ordinal of the packet */
346   unsigned int __packet_ordinal;
347   /** The ordinal of the packet within the group */
348   unsigned int __group_ordinal;
349   /** The best flow hash IPP could compute. */
350   unsigned int __flow_hash;
351   /** Flags pertaining to checksum calculation, packet type, etc. */
352   unsigned int __flags;
353   /** The first word of "user data". */
354   unsigned int __user_data_0;
355   /** The second word of "user data". */
356   unsigned int __user_data_1;
357 #endif
358 }
359 netio_pkt_metadata_t;
360 
361 
362 /** To ensure that the L3 header is aligned mod 4, the L2 header should be
363  * aligned mod 4 plus 2, since every supported L2 header is 4n + 2 bytes
364  * long.  The standard way to do this is to simply add 2 bytes of padding
365  * before the L2 header.
366  */
367 #define NETIO_PACKET_PADDING 2
368 
369 
370 
371 /**
372  * @brief Ethernet minimal (egress) packet metadata.
373  *
374  * @ingroup egress
375  *
376  * This structure represents information about packets which have
377  * been processed by @ref netio_populate_buffer() or
378  * @ref netio_populate_prepend_buffer().  This structure is opaque
379  * and accessed through the @ref egress.
380  *
381  * @internal This structure is actually copied into the memory used by
382  * standard metadata, which is assumed to be large enough.
383  */
384 typedef struct
385 {
386 #ifdef __DOXYGEN__
387   /** This structure is opaque. */
388   unsigned char opaque[14];
389 #else
390   /** The offset of the L2 header from the start of the packet data. */
391   unsigned short l2_offset;
392   /** The offset of the L3 header from the start of the packet data. */
393   unsigned short l3_offset;
394   /** Where to write the checksum. */
395   unsigned char csum_location;
396   /** Where to start checksumming from. */
397   unsigned char csum_start;
398   /** Flags pertaining to checksum calculation etc. */
399   unsigned short flags;
400   /** The L2 length of the packet. */
401   unsigned short l2_length;
402   /** The checksum with which to seed the checksum generator. */
403   unsigned short csum_seed;
404   /** How much to checksum. */
405   unsigned short csum_length;
406 #endif
407 }
408 netio_pkt_minimal_metadata_t;
409 
410 
411 #ifndef __DOXYGEN__
412 
413 /**
414  * @brief An I/O notification header.
415  *
416  * This is the first word of data received from an I/O shim in a notification
417  * packet. It contains framing and status information.
418  */
419 typedef union
420 {
421   unsigned int word; /**< The whole word. */
422   /** The various fields. */
423   struct
424   {
425     unsigned int __channel:7;    /**< Resource channel. */
426     unsigned int __type:4;       /**< Type. */
427     unsigned int __ack:1;        /**< Whether an acknowledgement is needed. */
428     unsigned int __reserved:1;   /**< Reserved. */
429     unsigned int __protocol:1;   /**< A protocol-specific word is added. */
430     unsigned int __status:2;     /**< Status of the transfer. */
431     unsigned int __framing:2;    /**< Framing of the transfer. */
432     unsigned int __transfer_size:14; /**< Transfer size in bytes (total). */
433   } bits;
434 }
435 __netio_pkt_notif_t;
436 
437 
438 /**
439  * Returns the base address of the packet.
440  */
441 #define _NETIO_PKT_HANDLE_BASE(p) \
442   ((unsigned char*)((p).word & 0xFFFFFFC0))
443 
444 /**
445  * Returns the base address of the packet.
446  */
447 #define _NETIO_PKT_BASE(p) \
448   _NETIO_PKT_HANDLE_BASE(p->__packet)
449 
450 /**
451  * @brief An I/O notification packet (second word)
452  *
453  * This is the second word of data received from an I/O shim in a notification
454  * packet.  This is the virtual address of the packet buffer, plus some flag
455  * bits.  (The virtual address of the packet is always 256-byte aligned so we
456  * have room for 8 bits' worth of flags in the low 8 bits.)
457  *
458  * @internal
459  * NOTE: The low two bits must contain "__queue", so the "packet size"
460  * (SIZE_SMALL, SIZE_LARGE, or SIZE_JUMBO) can be determined quickly.
461  *
462  * If __addr or __offset are moved, _NETIO_PKT_BASE
463  * (defined right below this) must be changed.
464  */
465 typedef union
466 {
467   unsigned int word; /**< The whole word. */
468   /** The various fields. */
469   struct
470   {
471     /** Which queue the packet will be returned to once it is sent back to
472         the IPP.  This is one of the SIZE_xxx values. */
473     unsigned int __queue:2;
474 
475     /** The IPP handle of the sending IPP. */
476     unsigned int __ipp_handle:2;
477 
478     /** Reserved for future use. */
479     unsigned int __reserved:1;
480 
481     /** If 1, this packet has minimal (egress) metadata; otherwise, it
482         has standard (ingress) metadata. */
483     unsigned int __minimal:1;
484 
485     /** Offset of the metadata within the packet.  This value is multiplied
486      *  by 64 and added to the base packet address to get the metadata
487      *  address.  Note that this field is aligned within the word such that
488      *  you can easily extract the metadata address with a 26-bit mask. */
489     unsigned int __offset:2;
490 
491     /** The top 24 bits of the packet's virtual address. */
492     unsigned int __addr:24;
493   } bits;
494 }
495 __netio_pkt_handle_t;
496 
497 #endif /* !__DOXYGEN__ */
498 
499 
500 /**
501  * @brief A handle for an I/O packet's storage.
502  * @ingroup ingress
503  *
504  * netio_pkt_handle_t encodes the concept of a ::netio_pkt_t with its
505  * packet metadata removed.  It is a much smaller type that exists to
506  * facilitate applications where the full ::netio_pkt_t type is too
507  * large, such as those that cache enormous numbers of packets or wish
508  * to transmit packet descriptors over the UDN.
509  *
510  * Because there is no metadata, most ::netio_pkt_t operations cannot be
511  * performed on a netio_pkt_handle_t.  It supports only
512  * netio_free_handle() (to free the buffer) and
513  * NETIO_PKT_CUSTOM_DATA_H() (to access a pointer to its contents).
514  * The application must acquire any additional metadata it wants from the
515  * original ::netio_pkt_t and record it separately.
516  *
517  * A netio_pkt_handle_t can be extracted from a ::netio_pkt_t by calling
518  * NETIO_PKT_HANDLE().  An invalid handle (analogous to NULL) can be
519  * created by assigning the value ::NETIO_PKT_HANDLE_NONE. A handle can
520  * be tested for validity with NETIO_PKT_HANDLE_IS_VALID().
521  */
522 typedef struct
523 {
524   unsigned int word; /**< Opaque bits. */
525 } netio_pkt_handle_t;
526 
527 /**
528  * @brief A packet descriptor.
529  *
530  * @ingroup ingress
531  * @ingroup egress
532  *
533  * This data structure represents a packet.  The structure is manipulated
534  * through the @ref ingress and the @ref egress.
535  *
536  * While the contents of a netio_pkt_t are opaque, the structure itself is
537  * portable.  This means that it may be shared between all tiles which have
538  * done a netio_input_register() call for the interface on which the pkt_t
539  * was initially received (via netio_get_packet()) or retrieved (via
540  * netio_get_buffer()).  The contents of a netio_pkt_t can be transmitted to
541  * another tile via shared memory, or via a UDN message, or by other means.
542  * The destination tile may then use the pkt_t as if it had originally been
543  * received locally; it may read or write the packet's data, read its
544  * metadata, free the packet, send the packet, transfer the netio_pkt_t to
545  * yet another tile, and so forth.
546  *
547  * Once a netio_pkt_t has been transferred to a second tile, the first tile
548  * should not reference the original copy; in particular, if more than one
549  * tile frees or sends the same netio_pkt_t, the IPP's packet free lists will
550  * become corrupted.  Note also that each tile which reads or modifies
551  * packet data must obey the memory coherency rules outlined in @ref input.
552  */
553 typedef struct
554 {
555 #ifdef __DOXYGEN__
556   /** This structure is opaque. */
557   unsigned char opaque[32];
558 #else
559   /** For an ingress packet (one with standard metadata), this is the
560    *  notification header we got from the I/O shim.  For an egress packet
561    *  (one with minimal metadata), this word is zero if the packet has not
562    *  been populated, and nonzero if it has. */
563   __netio_pkt_notif_t __notif_header;
564 
565   /** Virtual address of the packet buffer, plus state flags. */
566   __netio_pkt_handle_t __packet;
567 
568   /** Metadata associated with the packet. */
569   netio_pkt_metadata_t __metadata;
570 #endif
571 }
572 netio_pkt_t;
573 
574 
575 #ifndef __DOXYGEN__
576 
577 #define __NETIO_PKT_NOTIF_HEADER(pkt) ((pkt)->__notif_header)
578 #define __NETIO_PKT_IPP_HANDLE(pkt) ((pkt)->__packet.bits.__ipp_handle)
579 #define __NETIO_PKT_QUEUE(pkt) ((pkt)->__packet.bits.__queue)
580 #define __NETIO_PKT_NOTIF_HEADER_M(mda, pkt) ((pkt)->__notif_header)
581 #define __NETIO_PKT_IPP_HANDLE_M(mda, pkt) ((pkt)->__packet.bits.__ipp_handle)
582 #define __NETIO_PKT_MINIMAL(pkt) ((pkt)->__packet.bits.__minimal)
583 #define __NETIO_PKT_QUEUE_M(mda, pkt) ((pkt)->__packet.bits.__queue)
584 #define __NETIO_PKT_FLAGS_M(mda, pkt) ((mda)->__flags)
585 
586 /* Packet information table, used by the attribute access functions below. */
587 extern const uint16_t _netio_pkt_info[];
588 
589 #endif /* __DOXYGEN__ */
590 
591 
592 #ifndef __DOXYGEN__
593 /* These macros are deprecated and will disappear in a future MDE release. */
594 #define NETIO_PKT_GOOD_CHECKSUM(pkt) \
595   NETIO_PKT_L4_CSUM_CORRECT(pkt)
596 #define NETIO_PKT_GOOD_CHECKSUM_M(mda, pkt) \
597   NETIO_PKT_L4_CSUM_CORRECT_M(mda, pkt)
598 #endif /* __DOXYGEN__ */
599 
600 
601 /* Packet attribute access functions. */
602 
603 /** Return a pointer to the metadata for a packet.
604  * @ingroup ingress
605  *
606  * Calling this function once and passing the result to other retrieval
607  * functions with a "_M" suffix usually improves performance.  This
608  * function must be called on an 'ingress' packet (i.e. one retrieved
609  * by @ref netio_get_packet(), on which @ref netio_populate_buffer() or
610  * @ref netio_populate_prepend_buffer have not been called). Use of this
611  * function on an 'egress' packet will cause an assertion failure.
612  *
613  * @param[in] pkt Packet on which to operate.
614  * @return A pointer to the packet's standard metadata.
615  */
616 static __inline netio_pkt_metadata_t*
NETIO_PKT_METADATA(netio_pkt_t * pkt)617 NETIO_PKT_METADATA(netio_pkt_t* pkt)
618 {
619   netio_assert(!pkt->__packet.bits.__minimal);
620   return &pkt->__metadata;
621 }
622 
623 
624 /** Return a pointer to the minimal metadata for a packet.
625  * @ingroup egress
626  *
627  * Calling this function once and passing the result to other retrieval
628  * functions with a "_MM" suffix usually improves performance.  This
629  * function must be called on an 'egress' packet (i.e. one on which
630  * @ref netio_populate_buffer() or @ref netio_populate_prepend_buffer()
631  * have been called, or one retrieved by @ref netio_get_buffer()). Use of
632  * this function on an 'ingress' packet will cause an assertion failure.
633  *
634  * @param[in] pkt Packet on which to operate.
635  * @return A pointer to the packet's standard metadata.
636  */
637 static __inline netio_pkt_minimal_metadata_t*
NETIO_PKT_MINIMAL_METADATA(netio_pkt_t * pkt)638 NETIO_PKT_MINIMAL_METADATA(netio_pkt_t* pkt)
639 {
640   netio_assert(pkt->__packet.bits.__minimal);
641   return (netio_pkt_minimal_metadata_t*) &pkt->__metadata;
642 }
643 
644 
645 /** Determine whether a packet has 'minimal' metadata.
646  * @ingroup pktfuncs
647  *
648  * This function will return nonzero if the packet is an 'egress'
649  * packet (i.e. one on which @ref netio_populate_buffer() or
650  * @ref netio_populate_prepend_buffer() have been called, or one
651  * retrieved by @ref netio_get_buffer()), and zero if the packet
652  * is an 'ingress' packet (i.e. one retrieved by @ref netio_get_packet(),
653  * which has not been converted into an 'egress' packet).
654  *
655  * @param[in] pkt Packet on which to operate.
656  * @return Nonzero if the packet has minimal metadata.
657  */
658 static __inline unsigned int
NETIO_PKT_IS_MINIMAL(netio_pkt_t * pkt)659 NETIO_PKT_IS_MINIMAL(netio_pkt_t* pkt)
660 {
661   return pkt->__packet.bits.__minimal;
662 }
663 
664 
665 /** Return a handle for a packet's storage.
666  * @ingroup pktfuncs
667  *
668  * @param[in] pkt Packet on which to operate.
669  * @return A handle for the packet's storage.
670  */
671 static __inline netio_pkt_handle_t
NETIO_PKT_HANDLE(netio_pkt_t * pkt)672 NETIO_PKT_HANDLE(netio_pkt_t* pkt)
673 {
674   netio_pkt_handle_t h;
675   h.word = pkt->__packet.word;
676   return h;
677 }
678 
679 
680 /** A special reserved value indicating the absence of a packet handle.
681  *
682  * @ingroup pktfuncs
683  */
684 #define NETIO_PKT_HANDLE_NONE ((netio_pkt_handle_t) { 0 })
685 
686 
687 /** Test whether a packet handle is valid.
688  *
689  * Applications may wish to use the reserved value NETIO_PKT_HANDLE_NONE
690  * to indicate no packet at all.  This function tests to see if a packet
691  * handle is a real handle, not this special reserved value.
692  *
693  * @ingroup pktfuncs
694  *
695  * @param[in] handle Handle on which to operate.
696  * @return One if the packet handle is valid, else zero.
697  */
698 static __inline unsigned int
NETIO_PKT_HANDLE_IS_VALID(netio_pkt_handle_t handle)699 NETIO_PKT_HANDLE_IS_VALID(netio_pkt_handle_t handle)
700 {
701   return handle.word != 0;
702 }
703 
704 
705 
706 /** Return a pointer to the start of the packet's custom header.
707  *  A custom header may or may not be present, depending upon the IPP; its
708  *  contents and alignment are also IPP-dependent.  Currently, none of the
709  *  standard IPPs supplied by Tilera produce a custom header.  If present,
710  *  the custom header precedes the L2 header in the packet buffer.
711  * @ingroup ingress
712  *
713  * @param[in] handle Handle on which to operate.
714  * @return A pointer to start of the packet.
715  */
716 static __inline unsigned char*
NETIO_PKT_CUSTOM_DATA_H(netio_pkt_handle_t handle)717 NETIO_PKT_CUSTOM_DATA_H(netio_pkt_handle_t handle)
718 {
719   return _NETIO_PKT_HANDLE_BASE(handle) + NETIO_PACKET_PADDING;
720 }
721 
722 
723 /** Return the length of the packet's custom header.
724  *  A custom header may or may not be present, depending upon the IPP; its
725  *  contents and alignment are also IPP-dependent.  Currently, none of the
726  *  standard IPPs supplied by Tilera produce a custom header.  If present,
727  *  the custom header precedes the L2 header in the packet buffer.
728  *
729  * @ingroup ingress
730  *
731  * @param[in] mda Pointer to packet's standard metadata.
732  * @param[in] pkt Packet on which to operate.
733  * @return The length of the packet's custom header, in bytes.
734  */
735 static __inline netio_size_t
NETIO_PKT_CUSTOM_HEADER_LENGTH_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)736 NETIO_PKT_CUSTOM_HEADER_LENGTH_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
737 {
738   /*
739    * Note that we effectively need to extract a quantity from the flags word
740    * which is measured in words, and then turn it into bytes by shifting
741    * it left by 2.  We do this all at once by just shifting right two less
742    * bits, and shifting the mask up two bits.
743    */
744   return ((mda->__flags >> (_NETIO_PKT_CUSTOM_LEN_SHIFT - 2)) &
745           (_NETIO_PKT_CUSTOM_LEN_RMASK << 2));
746 }
747 
748 
749 /** Return the length of the packet, starting with the custom header.
750  *  A custom header may or may not be present, depending upon the IPP; its
751  *  contents and alignment are also IPP-dependent.  Currently, none of the
752  *  standard IPPs supplied by Tilera produce a custom header.  If present,
753  *  the custom header precedes the L2 header in the packet buffer.
754  * @ingroup ingress
755  *
756  * @param[in] mda Pointer to packet's standard metadata.
757  * @param[in] pkt Packet on which to operate.
758  * @return The length of the packet, in bytes.
759  */
760 static __inline netio_size_t
NETIO_PKT_CUSTOM_LENGTH_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)761 NETIO_PKT_CUSTOM_LENGTH_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
762 {
763   return (__NETIO_PKT_NOTIF_HEADER(pkt).bits.__transfer_size -
764           NETIO_PACKET_PADDING);
765 }
766 
767 
768 /** Return a pointer to the start of the packet's custom header.
769  *  A custom header may or may not be present, depending upon the IPP; its
770  *  contents and alignment are also IPP-dependent.  Currently, none of the
771  *  standard IPPs supplied by Tilera produce a custom header.  If present,
772  *  the custom header precedes the L2 header in the packet buffer.
773  * @ingroup ingress
774  *
775  * @param[in] mda Pointer to packet's standard metadata.
776  * @param[in] pkt Packet on which to operate.
777  * @return A pointer to start of the packet.
778  */
779 static __inline unsigned char*
NETIO_PKT_CUSTOM_DATA_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)780 NETIO_PKT_CUSTOM_DATA_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
781 {
782   return NETIO_PKT_CUSTOM_DATA_H(NETIO_PKT_HANDLE(pkt));
783 }
784 
785 
786 /** Return the length of the packet's L2 (Ethernet plus VLAN or SNAP) header.
787  * @ingroup ingress
788  *
789  * @param[in] mda Pointer to packet's standard metadata.
790  * @param[in] pkt Packet on which to operate.
791  * @return The length of the packet's L2 header, in bytes.
792  */
793 static __inline netio_size_t
NETIO_PKT_L2_HEADER_LENGTH_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)794 NETIO_PKT_L2_HEADER_LENGTH_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
795 {
796   /*
797    * Note that we effectively need to extract a quantity from the flags word
798    * which is measured in words, and then turn it into bytes by shifting
799    * it left by 2.  We do this all at once by just shifting right two less
800    * bits, and shifting the mask up two bits.  We then add two bytes.
801    */
802   return ((mda->__flags >> (_NETIO_PKT_L2_LEN_SHIFT - 2)) &
803           (_NETIO_PKT_L2_LEN_RMASK << 2)) + 2;
804 }
805 
806 
807 /** Return the length of the packet, starting with the L2 (Ethernet) header.
808  * @ingroup ingress
809  *
810  * @param[in] mda Pointer to packet's standard metadata.
811  * @param[in] pkt Packet on which to operate.
812  * @return The length of the packet, in bytes.
813  */
814 static __inline netio_size_t
NETIO_PKT_L2_LENGTH_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)815 NETIO_PKT_L2_LENGTH_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
816 {
817   return (NETIO_PKT_CUSTOM_LENGTH_M(mda, pkt) -
818           NETIO_PKT_CUSTOM_HEADER_LENGTH_M(mda,pkt));
819 }
820 
821 
822 /** Return a pointer to the start of the packet's L2 (Ethernet) header.
823  * @ingroup ingress
824  *
825  * @param[in] mda Pointer to packet's standard metadata.
826  * @param[in] pkt Packet on which to operate.
827  * @return A pointer to start of the packet.
828  */
829 static __inline unsigned char*
NETIO_PKT_L2_DATA_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)830 NETIO_PKT_L2_DATA_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
831 {
832   return (NETIO_PKT_CUSTOM_DATA_M(mda, pkt) +
833           NETIO_PKT_CUSTOM_HEADER_LENGTH_M(mda, pkt));
834 }
835 
836 
837 /** Retrieve the length of the packet, starting with the L3 (generally,
838  *  the IP) header.
839  * @ingroup ingress
840  *
841  * @param[in] mda Pointer to packet's standard metadata.
842  * @param[in] pkt Packet on which to operate.
843  * @return Length of the packet's L3 header and data, in bytes.
844  */
845 static __inline netio_size_t
NETIO_PKT_L3_LENGTH_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)846 NETIO_PKT_L3_LENGTH_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
847 {
848   return (NETIO_PKT_L2_LENGTH_M(mda, pkt) -
849           NETIO_PKT_L2_HEADER_LENGTH_M(mda,pkt));
850 }
851 
852 
853 /** Return a pointer to the packet's L3 (generally, the IP) header.
854  * @ingroup ingress
855  *
856  * Note that we guarantee word alignment of the L3 header.
857  *
858  * @param[in] mda Pointer to packet's standard metadata.
859  * @param[in] pkt Packet on which to operate.
860  * @return A pointer to the packet's L3 header.
861  */
862 static __inline unsigned char*
NETIO_PKT_L3_DATA_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)863 NETIO_PKT_L3_DATA_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
864 {
865   return (NETIO_PKT_L2_DATA_M(mda, pkt) +
866           NETIO_PKT_L2_HEADER_LENGTH_M(mda, pkt));
867 }
868 
869 
870 /** Return the ordinal of the packet.
871  * @ingroup ingress
872  *
873  * Each packet is given an ordinal number when it is delivered by the IPP.
874  * In the medium term, the ordinal is unique and monotonically increasing,
875  * being incremented by 1 for each packet; the ordinal of the first packet
876  * delivered after the IPP starts is zero.  (Since the ordinal is of finite
877  * size, given enough input packets, it will eventually wrap around to zero;
878  * in the long term, therefore, ordinals are not unique.)  The ordinals
879  * handed out by different IPPs are not disjoint, so two packets from
880  * different IPPs may have identical ordinals.  Packets dropped by the
881  * IPP or by the I/O shim are not assigned ordinals.
882  *
883  * @param[in] mda Pointer to packet's standard metadata.
884  * @param[in] pkt Packet on which to operate.
885  * @return The packet's per-IPP packet ordinal.
886  */
887 static __inline unsigned int
NETIO_PKT_ORDINAL_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)888 NETIO_PKT_ORDINAL_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
889 {
890   return mda->__packet_ordinal;
891 }
892 
893 
894 /** Return the per-group ordinal of the packet.
895  * @ingroup ingress
896  *
897  * Each packet is given a per-group ordinal number when it is
898  * delivered by the IPP. By default, the group is the packet's VLAN,
899  * although IPP can be recompiled to use different values.  In
900  * the medium term, the ordinal is unique and monotonically
901  * increasing, being incremented by 1 for each packet; the ordinal of
902  * the first packet distributed to a particular group is zero.
903  * (Since the ordinal is of finite size, given enough input packets,
904  * it will eventually wrap around to zero; in the long term,
905  * therefore, ordinals are not unique.)  The ordinals handed out by
906  * different IPPs are not disjoint, so two packets from different IPPs
907  * may have identical ordinals; similarly, packets distributed to
908  * different groups may have identical ordinals.  Packets dropped by
909  * the IPP or by the I/O shim are not assigned ordinals.
910  *
911  * @param[in] mda Pointer to packet's standard metadata.
912  * @param[in] pkt Packet on which to operate.
913  * @return The packet's per-IPP, per-group ordinal.
914  */
915 static __inline unsigned int
NETIO_PKT_GROUP_ORDINAL_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)916 NETIO_PKT_GROUP_ORDINAL_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
917 {
918   return mda->__group_ordinal;
919 }
920 
921 
922 /** Return the VLAN ID assigned to the packet.
923  * @ingroup ingress
924  *
925  * This value is usually contained within the packet header.
926  *
927  * This value will be zero if the packet does not have a VLAN tag, or if
928  * this value was not extracted from the packet.
929  *
930  * @param[in] mda Pointer to packet's standard metadata.
931  * @param[in] pkt Packet on which to operate.
932  * @return The packet's VLAN ID.
933  */
934 static __inline unsigned short
NETIO_PKT_VLAN_ID_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)935 NETIO_PKT_VLAN_ID_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
936 {
937   int vl = (mda->__flags >> _NETIO_PKT_VLAN_SHIFT) & _NETIO_PKT_VLAN_RMASK;
938   unsigned short* pkt_p;
939   int index;
940   unsigned short val;
941 
942   if (vl == _NETIO_PKT_VLAN_NONE)
943     return 0;
944 
945   pkt_p = (unsigned short*) NETIO_PKT_L2_DATA_M(mda, pkt);
946   index = (mda->__flags >> _NETIO_PKT_TYPE_SHIFT) & _NETIO_PKT_TYPE_RMASK;
947 
948   val = pkt_p[(_netio_pkt_info[index] >> _NETIO_PKT_INFO_VLAN_SHIFT) &
949               _NETIO_PKT_INFO_VLAN_RMASK];
950 
951 #ifdef __TILECC__
952   return (__insn_bytex(val) >> 16) & 0xFFF;
953 #else
954   return (__builtin_bswap32(val) >> 16) & 0xFFF;
955 #endif
956 }
957 
958 
959 /** Return the ethertype of the packet.
960  * @ingroup ingress
961  *
962  * This value is usually contained within the packet header.
963  *
964  * This value is reliable if @ref NETIO_PKT_ETHERTYPE_RECOGNIZED_M()
965  * returns true, and otherwise, may not be well defined.
966  *
967  * @param[in] mda Pointer to packet's standard metadata.
968  * @param[in] pkt Packet on which to operate.
969  * @return The packet's ethertype.
970  */
971 static __inline unsigned short
NETIO_PKT_ETHERTYPE_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)972 NETIO_PKT_ETHERTYPE_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
973 {
974   unsigned short* pkt_p = (unsigned short*) NETIO_PKT_L2_DATA_M(mda, pkt);
975   int index = (mda->__flags >> _NETIO_PKT_TYPE_SHIFT) & _NETIO_PKT_TYPE_RMASK;
976 
977   unsigned short val =
978     pkt_p[(_netio_pkt_info[index] >> _NETIO_PKT_INFO_ETYPE_SHIFT) &
979           _NETIO_PKT_INFO_ETYPE_RMASK];
980 
981   return __builtin_bswap32(val) >> 16;
982 }
983 
984 
985 /** Return the flow hash computed on the packet.
986  * @ingroup ingress
987  *
988  * For TCP and UDP packets, this hash is calculated by hashing together
989  * the "5-tuple" values, specifically the source IP address, destination
990  * IP address, protocol type, source port and destination port.
991  * The hash value is intended to be helpful for millions of distinct
992  * flows.
993  *
994  * For IPv4 or IPv6 packets which are neither TCP nor UDP, the flow hash is
995  * derived by hashing together the source and destination IP addresses.
996  *
997  * For MPLS-encapsulated packets, the flow hash is derived by hashing
998  * the first MPLS label.
999  *
1000  * For all other packets the flow hash is computed from the source
1001  * and destination Ethernet addresses.
1002  *
1003  * The hash is symmetric, meaning it produces the same value if the
1004  * source and destination are swapped. The only exceptions are
1005  * tunneling protocols 0x04 (IP in IP Encapsulation), 0x29 (Simple
1006  * Internet Protocol), 0x2F (General Routing Encapsulation) and 0x32
1007  * (Encap Security Payload), which use only the destination address
1008  * since the source address is not meaningful.
1009  *
1010  * @param[in] mda Pointer to packet's standard metadata.
1011  * @param[in] pkt Packet on which to operate.
1012  * @return The packet's 32-bit flow hash.
1013  */
1014 static __inline unsigned int
NETIO_PKT_FLOW_HASH_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1015 NETIO_PKT_FLOW_HASH_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1016 {
1017   return mda->__flow_hash;
1018 }
1019 
1020 
1021 /** Return the first word of "user data" for the packet.
1022  *
1023  * The contents of the user data words depend on the IPP.
1024  *
1025  * When using the standard ipp1, ipp2, or ipp4 sub-drivers, the first
1026  * word of user data contains the least significant bits of the 64-bit
1027  * arrival cycle count (see @c get_cycle_count_low()).
1028  *
1029  * See the <em>System Programmer's Guide</em> for details.
1030  *
1031  * @ingroup ingress
1032  *
1033  * @param[in] mda Pointer to packet's standard metadata.
1034  * @param[in] pkt Packet on which to operate.
1035  * @return The packet's first word of "user data".
1036  */
1037 static __inline unsigned int
NETIO_PKT_USER_DATA_0_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1038 NETIO_PKT_USER_DATA_0_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1039 {
1040   return mda->__user_data_0;
1041 }
1042 
1043 
1044 /** Return the second word of "user data" for the packet.
1045  *
1046  * The contents of the user data words depend on the IPP.
1047  *
1048  * When using the standard ipp1, ipp2, or ipp4 sub-drivers, the second
1049  * word of user data contains the most significant bits of the 64-bit
1050  * arrival cycle count (see @c get_cycle_count_high()).
1051  *
1052  * See the <em>System Programmer's Guide</em> for details.
1053  *
1054  * @ingroup ingress
1055  *
1056  * @param[in] mda Pointer to packet's standard metadata.
1057  * @param[in] pkt Packet on which to operate.
1058  * @return The packet's second word of "user data".
1059  */
1060 static __inline unsigned int
NETIO_PKT_USER_DATA_1_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1061 NETIO_PKT_USER_DATA_1_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1062 {
1063   return mda->__user_data_1;
1064 }
1065 
1066 
1067 /** Determine whether the L4 (TCP/UDP) checksum was calculated.
1068  * @ingroup ingress
1069  *
1070  * @param[in] mda Pointer to packet's standard metadata.
1071  * @param[in] pkt Packet on which to operate.
1072  * @return Nonzero if the L4 checksum was calculated.
1073  */
1074 static __inline unsigned int
NETIO_PKT_L4_CSUM_CALCULATED_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1075 NETIO_PKT_L4_CSUM_CALCULATED_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1076 {
1077   return !(mda->__flags & _NETIO_PKT_NO_L4_CSUM_MASK);
1078 }
1079 
1080 
1081 /** Determine whether the L4 (TCP/UDP) checksum was calculated and found to
1082  *  be correct.
1083  * @ingroup ingress
1084  *
1085  * @param[in] mda Pointer to packet's standard metadata.
1086  * @param[in] pkt Packet on which to operate.
1087  * @return Nonzero if the checksum was calculated and is correct.
1088  */
1089 static __inline unsigned int
NETIO_PKT_L4_CSUM_CORRECT_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1090 NETIO_PKT_L4_CSUM_CORRECT_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1091 {
1092   return !(mda->__flags &
1093            (_NETIO_PKT_BAD_L4_CSUM_MASK | _NETIO_PKT_NO_L4_CSUM_MASK));
1094 }
1095 
1096 
1097 /** Determine whether the L3 (IP) checksum was calculated.
1098  * @ingroup ingress
1099  *
1100  * @param[in] mda Pointer to packet's standard metadata.
1101  * @param[in] pkt Packet on which to operate.
1102  * @return Nonzero if the L3 (IP) checksum was calculated.
1103 */
1104 static __inline unsigned int
NETIO_PKT_L3_CSUM_CALCULATED_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1105 NETIO_PKT_L3_CSUM_CALCULATED_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1106 {
1107   return !(mda->__flags & _NETIO_PKT_NO_L3_CSUM_MASK);
1108 }
1109 
1110 
1111 /** Determine whether the L3 (IP) checksum was calculated and found to be
1112  *  correct.
1113  * @ingroup ingress
1114  *
1115  * @param[in] mda Pointer to packet's standard metadata.
1116  * @param[in] pkt Packet on which to operate.
1117  * @return Nonzero if the checksum was calculated and is correct.
1118  */
1119 static __inline unsigned int
NETIO_PKT_L3_CSUM_CORRECT_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1120 NETIO_PKT_L3_CSUM_CORRECT_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1121 {
1122   return !(mda->__flags &
1123            (_NETIO_PKT_BAD_L3_CSUM_MASK | _NETIO_PKT_NO_L3_CSUM_MASK));
1124 }
1125 
1126 
1127 /** Determine whether the ethertype was recognized and L3 packet data was
1128  *  processed.
1129  * @ingroup ingress
1130  *
1131  * @param[in] mda Pointer to packet's standard metadata.
1132  * @param[in] pkt Packet on which to operate.
1133  * @return Nonzero if the ethertype was recognized and L3 packet data was
1134  *   processed.
1135  */
1136 static __inline unsigned int
NETIO_PKT_ETHERTYPE_RECOGNIZED_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1137 NETIO_PKT_ETHERTYPE_RECOGNIZED_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1138 {
1139   return !(mda->__flags & _NETIO_PKT_TYPE_UNRECOGNIZED_MASK);
1140 }
1141 
1142 
1143 /** Retrieve the status of a packet and any errors that may have occurred
1144  * during ingress processing (length mismatches, CRC errors, etc.).
1145  * @ingroup ingress
1146  *
1147  * Note that packets for which @ref NETIO_PKT_ETHERTYPE_RECOGNIZED()
1148  * returns zero are always reported as underlength, as there is no a priori
1149  * means to determine their length.  Normally, applications should use
1150  * @ref NETIO_PKT_BAD_M() instead of explicitly checking status with this
1151  * function.
1152  *
1153  * @param[in] mda Pointer to packet's standard metadata.
1154  * @param[in] pkt Packet on which to operate.
1155  * @return The packet's status.
1156  */
1157 static __inline netio_pkt_status_t
NETIO_PKT_STATUS_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1158 NETIO_PKT_STATUS_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1159 {
1160   return (netio_pkt_status_t) __NETIO_PKT_NOTIF_HEADER(pkt).bits.__status;
1161 }
1162 
1163 
1164 /** Report whether a packet is bad (i.e., was shorter than expected based on
1165  *  its headers, or had a bad CRC).
1166  * @ingroup ingress
1167  *
1168  * Note that this function does not verify L3 or L4 checksums.
1169  *
1170  * @param[in] mda Pointer to packet's standard metadata.
1171  * @param[in] pkt Packet on which to operate.
1172  * @return Nonzero if the packet is bad and should be discarded.
1173  */
1174 static __inline unsigned int
NETIO_PKT_BAD_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1175 NETIO_PKT_BAD_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1176 {
1177   return ((NETIO_PKT_STATUS_M(mda, pkt) & 1) &&
1178           (NETIO_PKT_ETHERTYPE_RECOGNIZED_M(mda, pkt) ||
1179            NETIO_PKT_STATUS_M(mda, pkt) == NETIO_PKT_STATUS_BAD));
1180 }
1181 
1182 
1183 /** Return the length of the packet, starting with the L2 (Ethernet) header.
1184  * @ingroup egress
1185  *
1186  * @param[in] mmd Pointer to packet's minimal metadata.
1187  * @param[in] pkt Packet on which to operate.
1188  * @return The length of the packet, in bytes.
1189  */
1190 static __inline netio_size_t
NETIO_PKT_L2_LENGTH_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1191 NETIO_PKT_L2_LENGTH_MM(netio_pkt_minimal_metadata_t* mmd, netio_pkt_t* pkt)
1192 {
1193   return mmd->l2_length;
1194 }
1195 
1196 
1197 /** Return the length of the L2 (Ethernet) header.
1198  * @ingroup egress
1199  *
1200  * @param[in] mmd Pointer to packet's minimal metadata.
1201  * @param[in] pkt Packet on which to operate.
1202  * @return The length of the packet's L2 header, in bytes.
1203  */
1204 static __inline netio_size_t
NETIO_PKT_L2_HEADER_LENGTH_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1205 NETIO_PKT_L2_HEADER_LENGTH_MM(netio_pkt_minimal_metadata_t* mmd,
1206                               netio_pkt_t* pkt)
1207 {
1208   return mmd->l3_offset - mmd->l2_offset;
1209 }
1210 
1211 
1212 /** Return the length of the packet, starting with the L3 (IP) header.
1213  * @ingroup egress
1214  *
1215  * @param[in] mmd Pointer to packet's minimal metadata.
1216  * @param[in] pkt Packet on which to operate.
1217  * @return Length of the packet's L3 header and data, in bytes.
1218  */
1219 static __inline netio_size_t
NETIO_PKT_L3_LENGTH_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1220 NETIO_PKT_L3_LENGTH_MM(netio_pkt_minimal_metadata_t* mmd, netio_pkt_t* pkt)
1221 {
1222   return (NETIO_PKT_L2_LENGTH_MM(mmd, pkt) -
1223           NETIO_PKT_L2_HEADER_LENGTH_MM(mmd, pkt));
1224 }
1225 
1226 
1227 /** Return a pointer to the packet's L3 (generally, the IP) header.
1228  * @ingroup egress
1229  *
1230  * Note that we guarantee word alignment of the L3 header.
1231  *
1232  * @param[in] mmd Pointer to packet's minimal metadata.
1233  * @param[in] pkt Packet on which to operate.
1234  * @return A pointer to the packet's L3 header.
1235  */
1236 static __inline unsigned char*
NETIO_PKT_L3_DATA_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1237 NETIO_PKT_L3_DATA_MM(netio_pkt_minimal_metadata_t* mmd, netio_pkt_t* pkt)
1238 {
1239   return _NETIO_PKT_BASE(pkt) + mmd->l3_offset;
1240 }
1241 
1242 
1243 /** Return a pointer to the packet's L2 (Ethernet) header.
1244  * @ingroup egress
1245  *
1246  * @param[in] mmd Pointer to packet's minimal metadata.
1247  * @param[in] pkt Packet on which to operate.
1248  * @return A pointer to start of the packet.
1249  */
1250 static __inline unsigned char*
NETIO_PKT_L2_DATA_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1251 NETIO_PKT_L2_DATA_MM(netio_pkt_minimal_metadata_t* mmd, netio_pkt_t* pkt)
1252 {
1253   return _NETIO_PKT_BASE(pkt) + mmd->l2_offset;
1254 }
1255 
1256 
1257 /** Retrieve the status of a packet and any errors that may have occurred
1258  * during ingress processing (length mismatches, CRC errors, etc.).
1259  * @ingroup ingress
1260  *
1261  * Note that packets for which @ref NETIO_PKT_ETHERTYPE_RECOGNIZED()
1262  * returns zero are always reported as underlength, as there is no a priori
1263  * means to determine their length.  Normally, applications should use
1264  * @ref NETIO_PKT_BAD() instead of explicitly checking status with this
1265  * function.
1266  *
1267  * @param[in] pkt Packet on which to operate.
1268  * @return The packet's status.
1269  */
1270 static __inline netio_pkt_status_t
NETIO_PKT_STATUS(netio_pkt_t * pkt)1271 NETIO_PKT_STATUS(netio_pkt_t* pkt)
1272 {
1273   netio_assert(!pkt->__packet.bits.__minimal);
1274 
1275   return (netio_pkt_status_t) __NETIO_PKT_NOTIF_HEADER(pkt).bits.__status;
1276 }
1277 
1278 
1279 /** Report whether a packet is bad (i.e., was shorter than expected based on
1280  *  its headers, or had a bad CRC).
1281  * @ingroup ingress
1282  *
1283  * Note that this function does not verify L3 or L4 checksums.
1284  *
1285  * @param[in] pkt Packet on which to operate.
1286  * @return Nonzero if the packet is bad and should be discarded.
1287  */
1288 static __inline unsigned int
NETIO_PKT_BAD(netio_pkt_t * pkt)1289 NETIO_PKT_BAD(netio_pkt_t* pkt)
1290 {
1291   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1292 
1293   return NETIO_PKT_BAD_M(mda, pkt);
1294 }
1295 
1296 
1297 /** Return the length of the packet's custom header.
1298  *  A custom header may or may not be present, depending upon the IPP; its
1299  *  contents and alignment are also IPP-dependent.  Currently, none of the
1300  *  standard IPPs supplied by Tilera produce a custom header.  If present,
1301  *  the custom header precedes the L2 header in the packet buffer.
1302  * @ingroup pktfuncs
1303  *
1304  * @param[in] pkt Packet on which to operate.
1305  * @return The length of the packet's custom header, in bytes.
1306  */
1307 static __inline netio_size_t
NETIO_PKT_CUSTOM_HEADER_LENGTH(netio_pkt_t * pkt)1308 NETIO_PKT_CUSTOM_HEADER_LENGTH(netio_pkt_t* pkt)
1309 {
1310   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1311 
1312   return NETIO_PKT_CUSTOM_HEADER_LENGTH_M(mda, pkt);
1313 }
1314 
1315 
1316 /** Return the length of the packet, starting with the custom header.
1317  *  A custom header may or may not be present, depending upon the IPP; its
1318  *  contents and alignment are also IPP-dependent.  Currently, none of the
1319  *  standard IPPs supplied by Tilera produce a custom header.  If present,
1320  *  the custom header precedes the L2 header in the packet buffer.
1321  * @ingroup pktfuncs
1322  *
1323  * @param[in] pkt Packet on which to operate.
1324  * @return  The length of the packet, in bytes.
1325  */
1326 static __inline netio_size_t
NETIO_PKT_CUSTOM_LENGTH(netio_pkt_t * pkt)1327 NETIO_PKT_CUSTOM_LENGTH(netio_pkt_t* pkt)
1328 {
1329   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1330 
1331   return NETIO_PKT_CUSTOM_LENGTH_M(mda, pkt);
1332 }
1333 
1334 
1335 /** Return a pointer to the packet's custom header.
1336  *  A custom header may or may not be present, depending upon the IPP; its
1337  *  contents and alignment are also IPP-dependent.  Currently, none of the
1338  *  standard IPPs supplied by Tilera produce a custom header.  If present,
1339  *  the custom header precedes the L2 header in the packet buffer.
1340  * @ingroup pktfuncs
1341  *
1342  * @param[in] pkt Packet on which to operate.
1343  * @return A pointer to start of the packet.
1344  */
1345 static __inline unsigned char*
NETIO_PKT_CUSTOM_DATA(netio_pkt_t * pkt)1346 NETIO_PKT_CUSTOM_DATA(netio_pkt_t* pkt)
1347 {
1348   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1349 
1350   return NETIO_PKT_CUSTOM_DATA_M(mda, pkt);
1351 }
1352 
1353 
1354 /** Return the length of the packet's L2 (Ethernet plus VLAN or SNAP) header.
1355  * @ingroup pktfuncs
1356  *
1357  * @param[in] pkt Packet on which to operate.
1358  * @return The length of the packet's L2 header, in bytes.
1359  */
1360 static __inline netio_size_t
NETIO_PKT_L2_HEADER_LENGTH(netio_pkt_t * pkt)1361 NETIO_PKT_L2_HEADER_LENGTH(netio_pkt_t* pkt)
1362 {
1363   if (NETIO_PKT_IS_MINIMAL(pkt))
1364   {
1365     netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1366 
1367     return NETIO_PKT_L2_HEADER_LENGTH_MM(mmd, pkt);
1368   }
1369   else
1370   {
1371     netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1372 
1373     return NETIO_PKT_L2_HEADER_LENGTH_M(mda, pkt);
1374   }
1375 }
1376 
1377 
1378 /** Return the length of the packet, starting with the L2 (Ethernet) header.
1379  * @ingroup pktfuncs
1380  *
1381  * @param[in] pkt Packet on which to operate.
1382  * @return  The length of the packet, in bytes.
1383  */
1384 static __inline netio_size_t
NETIO_PKT_L2_LENGTH(netio_pkt_t * pkt)1385 NETIO_PKT_L2_LENGTH(netio_pkt_t* pkt)
1386 {
1387   if (NETIO_PKT_IS_MINIMAL(pkt))
1388   {
1389     netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1390 
1391     return NETIO_PKT_L2_LENGTH_MM(mmd, pkt);
1392   }
1393   else
1394   {
1395     netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1396 
1397     return NETIO_PKT_L2_LENGTH_M(mda, pkt);
1398   }
1399 }
1400 
1401 
1402 /** Return a pointer to the packet's L2 (Ethernet) header.
1403  * @ingroup pktfuncs
1404  *
1405  * @param[in] pkt Packet on which to operate.
1406  * @return A pointer to start of the packet.
1407  */
1408 static __inline unsigned char*
NETIO_PKT_L2_DATA(netio_pkt_t * pkt)1409 NETIO_PKT_L2_DATA(netio_pkt_t* pkt)
1410 {
1411   if (NETIO_PKT_IS_MINIMAL(pkt))
1412   {
1413     netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1414 
1415     return NETIO_PKT_L2_DATA_MM(mmd, pkt);
1416   }
1417   else
1418   {
1419     netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1420 
1421     return NETIO_PKT_L2_DATA_M(mda, pkt);
1422   }
1423 }
1424 
1425 
1426 /** Retrieve the length of the packet, starting with the L3 (generally, the IP)
1427  * header.
1428  * @ingroup pktfuncs
1429  *
1430  * @param[in] pkt Packet on which to operate.
1431  * @return Length of the packet's L3 header and data, in bytes.
1432  */
1433 static __inline netio_size_t
NETIO_PKT_L3_LENGTH(netio_pkt_t * pkt)1434 NETIO_PKT_L3_LENGTH(netio_pkt_t* pkt)
1435 {
1436   if (NETIO_PKT_IS_MINIMAL(pkt))
1437   {
1438     netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1439 
1440     return NETIO_PKT_L3_LENGTH_MM(mmd, pkt);
1441   }
1442   else
1443   {
1444     netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1445 
1446     return NETIO_PKT_L3_LENGTH_M(mda, pkt);
1447   }
1448 }
1449 
1450 
1451 /** Return a pointer to the packet's L3 (generally, the IP) header.
1452  * @ingroup pktfuncs
1453  *
1454  * Note that we guarantee word alignment of the L3 header.
1455  *
1456  * @param[in] pkt Packet on which to operate.
1457  * @return A pointer to the packet's L3 header.
1458  */
1459 static __inline unsigned char*
NETIO_PKT_L3_DATA(netio_pkt_t * pkt)1460 NETIO_PKT_L3_DATA(netio_pkt_t* pkt)
1461 {
1462   if (NETIO_PKT_IS_MINIMAL(pkt))
1463   {
1464     netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1465 
1466     return NETIO_PKT_L3_DATA_MM(mmd, pkt);
1467   }
1468   else
1469   {
1470     netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1471 
1472     return NETIO_PKT_L3_DATA_M(mda, pkt);
1473   }
1474 }
1475 
1476 
1477 /** Return the ordinal of the packet.
1478  * @ingroup ingress
1479  *
1480  * Each packet is given an ordinal number when it is delivered by the IPP.
1481  * In the medium term, the ordinal is unique and monotonically increasing,
1482  * being incremented by 1 for each packet; the ordinal of the first packet
1483  * delivered after the IPP starts is zero.  (Since the ordinal is of finite
1484  * size, given enough input packets, it will eventually wrap around to zero;
1485  * in the long term, therefore, ordinals are not unique.)  The ordinals
1486  * handed out by different IPPs are not disjoint, so two packets from
1487  * different IPPs may have identical ordinals.  Packets dropped by the
1488  * IPP or by the I/O shim are not assigned ordinals.
1489  *
1490  *
1491  * @param[in] pkt Packet on which to operate.
1492  * @return The packet's per-IPP packet ordinal.
1493  */
1494 static __inline unsigned int
NETIO_PKT_ORDINAL(netio_pkt_t * pkt)1495 NETIO_PKT_ORDINAL(netio_pkt_t* pkt)
1496 {
1497   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1498 
1499   return NETIO_PKT_ORDINAL_M(mda, pkt);
1500 }
1501 
1502 
1503 /** Return the per-group ordinal of the packet.
1504  * @ingroup ingress
1505  *
1506  * Each packet is given a per-group ordinal number when it is
1507  * delivered by the IPP. By default, the group is the packet's VLAN,
1508  * although IPP can be recompiled to use different values.  In
1509  * the medium term, the ordinal is unique and monotonically
1510  * increasing, being incremented by 1 for each packet; the ordinal of
1511  * the first packet distributed to a particular group is zero.
1512  * (Since the ordinal is of finite size, given enough input packets,
1513  * it will eventually wrap around to zero; in the long term,
1514  * therefore, ordinals are not unique.)  The ordinals handed out by
1515  * different IPPs are not disjoint, so two packets from different IPPs
1516  * may have identical ordinals; similarly, packets distributed to
1517  * different groups may have identical ordinals.  Packets dropped by
1518  * the IPP or by the I/O shim are not assigned ordinals.
1519  *
1520  * @param[in] pkt Packet on which to operate.
1521  * @return The packet's per-IPP, per-group ordinal.
1522  */
1523 static __inline unsigned int
NETIO_PKT_GROUP_ORDINAL(netio_pkt_t * pkt)1524 NETIO_PKT_GROUP_ORDINAL(netio_pkt_t* pkt)
1525 {
1526   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1527 
1528   return NETIO_PKT_GROUP_ORDINAL_M(mda, pkt);
1529 }
1530 
1531 
1532 /** Return the VLAN ID assigned to the packet.
1533  * @ingroup ingress
1534  *
1535  * This is usually also contained within the packet header.  If the packet
1536  * does not have a VLAN tag, the VLAN ID returned by this function is zero.
1537  *
1538  * @param[in] pkt Packet on which to operate.
1539  * @return The packet's VLAN ID.
1540  */
1541 static __inline unsigned short
NETIO_PKT_VLAN_ID(netio_pkt_t * pkt)1542 NETIO_PKT_VLAN_ID(netio_pkt_t* pkt)
1543 {
1544   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1545 
1546   return NETIO_PKT_VLAN_ID_M(mda, pkt);
1547 }
1548 
1549 
1550 /** Return the ethertype of the packet.
1551  * @ingroup ingress
1552  *
1553  * This value is reliable if @ref NETIO_PKT_ETHERTYPE_RECOGNIZED()
1554  * returns true, and otherwise, may not be well defined.
1555  *
1556  * @param[in] pkt Packet on which to operate.
1557  * @return The packet's ethertype.
1558  */
1559 static __inline unsigned short
NETIO_PKT_ETHERTYPE(netio_pkt_t * pkt)1560 NETIO_PKT_ETHERTYPE(netio_pkt_t* pkt)
1561 {
1562   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1563 
1564   return NETIO_PKT_ETHERTYPE_M(mda, pkt);
1565 }
1566 
1567 
1568 /** Return the flow hash computed on the packet.
1569  * @ingroup ingress
1570  *
1571  * For TCP and UDP packets, this hash is calculated by hashing together
1572  * the "5-tuple" values, specifically the source IP address, destination
1573  * IP address, protocol type, source port and destination port.
1574  * The hash value is intended to be helpful for millions of distinct
1575  * flows.
1576  *
1577  * For IPv4 or IPv6 packets which are neither TCP nor UDP, the flow hash is
1578  * derived by hashing together the source and destination IP addresses.
1579  *
1580  * For MPLS-encapsulated packets, the flow hash is derived by hashing
1581  * the first MPLS label.
1582  *
1583  * For all other packets the flow hash is computed from the source
1584  * and destination Ethernet addresses.
1585  *
1586  * The hash is symmetric, meaning it produces the same value if the
1587  * source and destination are swapped. The only exceptions are
1588  * tunneling protocols 0x04 (IP in IP Encapsulation), 0x29 (Simple
1589  * Internet Protocol), 0x2F (General Routing Encapsulation) and 0x32
1590  * (Encap Security Payload), which use only the destination address
1591  * since the source address is not meaningful.
1592  *
1593  * @param[in] pkt Packet on which to operate.
1594  * @return The packet's 32-bit flow hash.
1595  */
1596 static __inline unsigned int
NETIO_PKT_FLOW_HASH(netio_pkt_t * pkt)1597 NETIO_PKT_FLOW_HASH(netio_pkt_t* pkt)
1598 {
1599   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1600 
1601   return NETIO_PKT_FLOW_HASH_M(mda, pkt);
1602 }
1603 
1604 
1605 /** Return the first word of "user data" for the packet.
1606  *
1607  * The contents of the user data words depend on the IPP.
1608  *
1609  * When using the standard ipp1, ipp2, or ipp4 sub-drivers, the first
1610  * word of user data contains the least significant bits of the 64-bit
1611  * arrival cycle count (see @c get_cycle_count_low()).
1612  *
1613  * See the <em>System Programmer's Guide</em> for details.
1614  *
1615  * @ingroup ingress
1616  *
1617  * @param[in] pkt Packet on which to operate.
1618  * @return The packet's first word of "user data".
1619  */
1620 static __inline unsigned int
NETIO_PKT_USER_DATA_0(netio_pkt_t * pkt)1621 NETIO_PKT_USER_DATA_0(netio_pkt_t* pkt)
1622 {
1623   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1624 
1625   return NETIO_PKT_USER_DATA_0_M(mda, pkt);
1626 }
1627 
1628 
1629 /** Return the second word of "user data" for the packet.
1630  *
1631  * The contents of the user data words depend on the IPP.
1632  *
1633  * When using the standard ipp1, ipp2, or ipp4 sub-drivers, the second
1634  * word of user data contains the most significant bits of the 64-bit
1635  * arrival cycle count (see @c get_cycle_count_high()).
1636  *
1637  * See the <em>System Programmer's Guide</em> for details.
1638  *
1639  * @ingroup ingress
1640  *
1641  * @param[in] pkt Packet on which to operate.
1642  * @return The packet's second word of "user data".
1643  */
1644 static __inline unsigned int
NETIO_PKT_USER_DATA_1(netio_pkt_t * pkt)1645 NETIO_PKT_USER_DATA_1(netio_pkt_t* pkt)
1646 {
1647   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1648 
1649   return NETIO_PKT_USER_DATA_1_M(mda, pkt);
1650 }
1651 
1652 
1653 /** Determine whether the L4 (TCP/UDP) checksum was calculated.
1654  * @ingroup ingress
1655  *
1656  * @param[in] pkt Packet on which to operate.
1657  * @return Nonzero if the L4 checksum was calculated.
1658  */
1659 static __inline unsigned int
NETIO_PKT_L4_CSUM_CALCULATED(netio_pkt_t * pkt)1660 NETIO_PKT_L4_CSUM_CALCULATED(netio_pkt_t* pkt)
1661 {
1662   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1663 
1664   return NETIO_PKT_L4_CSUM_CALCULATED_M(mda, pkt);
1665 }
1666 
1667 
1668 /** Determine whether the L4 (TCP/UDP) checksum was calculated and found to
1669  *  be correct.
1670  * @ingroup ingress
1671  *
1672  * @param[in] pkt Packet on which to operate.
1673  * @return Nonzero if the checksum was calculated and is correct.
1674  */
1675 static __inline unsigned int
NETIO_PKT_L4_CSUM_CORRECT(netio_pkt_t * pkt)1676 NETIO_PKT_L4_CSUM_CORRECT(netio_pkt_t* pkt)
1677 {
1678   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1679 
1680   return NETIO_PKT_L4_CSUM_CORRECT_M(mda, pkt);
1681 }
1682 
1683 
1684 /** Determine whether the L3 (IP) checksum was calculated.
1685  * @ingroup ingress
1686  *
1687  * @param[in] pkt Packet on which to operate.
1688  * @return Nonzero if the L3 (IP) checksum was calculated.
1689 */
1690 static __inline unsigned int
NETIO_PKT_L3_CSUM_CALCULATED(netio_pkt_t * pkt)1691 NETIO_PKT_L3_CSUM_CALCULATED(netio_pkt_t* pkt)
1692 {
1693   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1694 
1695   return NETIO_PKT_L3_CSUM_CALCULATED_M(mda, pkt);
1696 }
1697 
1698 
1699 /** Determine whether the L3 (IP) checksum was calculated and found to be
1700  *  correct.
1701  * @ingroup ingress
1702  *
1703  * @param[in] pkt Packet on which to operate.
1704  * @return Nonzero if the checksum was calculated and is correct.
1705  */
1706 static __inline unsigned int
NETIO_PKT_L3_CSUM_CORRECT(netio_pkt_t * pkt)1707 NETIO_PKT_L3_CSUM_CORRECT(netio_pkt_t* pkt)
1708 {
1709   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1710 
1711   return NETIO_PKT_L3_CSUM_CORRECT_M(mda, pkt);
1712 }
1713 
1714 
1715 /** Determine whether the Ethertype was recognized and L3 packet data was
1716  *  processed.
1717  * @ingroup ingress
1718  *
1719  * @param[in] pkt Packet on which to operate.
1720  * @return Nonzero if the Ethertype was recognized and L3 packet data was
1721  *   processed.
1722  */
1723 static __inline unsigned int
NETIO_PKT_ETHERTYPE_RECOGNIZED(netio_pkt_t * pkt)1724 NETIO_PKT_ETHERTYPE_RECOGNIZED(netio_pkt_t* pkt)
1725 {
1726   netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1727 
1728   return NETIO_PKT_ETHERTYPE_RECOGNIZED_M(mda, pkt);
1729 }
1730 
1731 
1732 /** Set an egress packet's L2 length, using a metadata pointer to speed the
1733  * computation.
1734  * @ingroup egress
1735  *
1736  * @param[in,out] mmd Pointer to packet's minimal metadata.
1737  * @param[in] pkt Packet on which to operate.
1738  * @param[in] len Packet L2 length, in bytes.
1739  */
1740 static __inline void
NETIO_PKT_SET_L2_LENGTH_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt,int len)1741 NETIO_PKT_SET_L2_LENGTH_MM(netio_pkt_minimal_metadata_t* mmd, netio_pkt_t* pkt,
1742                            int len)
1743 {
1744   mmd->l2_length = len;
1745 }
1746 
1747 
1748 /** Set an egress packet's L2 length.
1749  * @ingroup egress
1750  *
1751  * @param[in,out] pkt Packet on which to operate.
1752  * @param[in] len Packet L2 length, in bytes.
1753  */
1754 static __inline void
NETIO_PKT_SET_L2_LENGTH(netio_pkt_t * pkt,int len)1755 NETIO_PKT_SET_L2_LENGTH(netio_pkt_t* pkt, int len)
1756 {
1757   netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1758 
1759   NETIO_PKT_SET_L2_LENGTH_MM(mmd, pkt, len);
1760 }
1761 
1762 
1763 /** Set an egress packet's L2 header length, using a metadata pointer to
1764  *  speed the computation.
1765  * @ingroup egress
1766  *
1767  * It is not normally necessary to call this routine; only the L2 length,
1768  * not the header length, is needed to transmit a packet.  It may be useful if
1769  * the egress packet will later be processed by code which expects to use
1770  * functions like @ref NETIO_PKT_L3_DATA() to get a pointer to the L3 payload.
1771  *
1772  * @param[in,out] mmd Pointer to packet's minimal metadata.
1773  * @param[in] pkt Packet on which to operate.
1774  * @param[in] len Packet L2 header length, in bytes.
1775  */
1776 static __inline void
NETIO_PKT_SET_L2_HEADER_LENGTH_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt,int len)1777 NETIO_PKT_SET_L2_HEADER_LENGTH_MM(netio_pkt_minimal_metadata_t* mmd,
1778                                   netio_pkt_t* pkt, int len)
1779 {
1780   mmd->l3_offset = mmd->l2_offset + len;
1781 }
1782 
1783 
1784 /** Set an egress packet's L2 header length.
1785  * @ingroup egress
1786  *
1787  * It is not normally necessary to call this routine; only the L2 length,
1788  * not the header length, is needed to transmit a packet.  It may be useful if
1789  * the egress packet will later be processed by code which expects to use
1790  * functions like @ref NETIO_PKT_L3_DATA() to get a pointer to the L3 payload.
1791  *
1792  * @param[in,out] pkt Packet on which to operate.
1793  * @param[in] len Packet L2 header length, in bytes.
1794  */
1795 static __inline void
NETIO_PKT_SET_L2_HEADER_LENGTH(netio_pkt_t * pkt,int len)1796 NETIO_PKT_SET_L2_HEADER_LENGTH(netio_pkt_t* pkt, int len)
1797 {
1798   netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1799 
1800   NETIO_PKT_SET_L2_HEADER_LENGTH_MM(mmd, pkt, len);
1801 }
1802 
1803 
1804 /** Set up an egress packet for hardware checksum computation, using a
1805  *  metadata pointer to speed the operation.
1806  * @ingroup egress
1807  *
1808  *  NetIO provides the ability to automatically calculate a standard
1809  *  16-bit Internet checksum on transmitted packets.  The application
1810  *  may specify the point in the packet where the checksum starts, the
1811  *  number of bytes to be checksummed, and the two bytes in the packet
1812  *  which will be replaced with the completed checksum.  (If the range
1813  *  of bytes to be checksummed includes the bytes to be replaced, the
1814  *  initial values of those bytes will be included in the checksum.)
1815  *
1816  *  For some protocols, the packet checksum covers data which is not present
1817  *  in the packet, or is at least not contiguous to the main data payload.
1818  *  For instance, the TCP checksum includes a "pseudo-header" which includes
1819  *  the source and destination IP addresses of the packet.  To accommodate
1820  *  this, the checksum engine may be "seeded" with an initial value, which
1821  *  the application would need to compute based on the specific protocol's
1822  *  requirements.  Note that the seed is given in host byte order (little-
1823  *  endian), not network byte order (big-endian); code written to compute a
1824  *  pseudo-header checksum in network byte order will need to byte-swap it
1825  *  before use as the seed.
1826  *
1827  *  Note that the checksum is computed as part of the transmission process,
1828  *  so it will not be present in the packet upon completion of this routine.
1829  *
1830  * @param[in,out] mmd Pointer to packet's minimal metadata.
1831  * @param[in] pkt Packet on which to operate.
1832  * @param[in] start Offset within L2 packet of the first byte to include in
1833  *   the checksum.
1834  * @param[in] length Number of bytes to include in the checksum.
1835  *   the checksum.
1836  * @param[in] location Offset within L2 packet of the first of the two bytes
1837  *   to be replaced with the calculated checksum.
1838  * @param[in] seed Initial value of the running checksum before any of the
1839  *   packet data is added.
1840  */
1841 static __inline void
NETIO_PKT_DO_EGRESS_CSUM_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt,int start,int length,int location,uint16_t seed)1842 NETIO_PKT_DO_EGRESS_CSUM_MM(netio_pkt_minimal_metadata_t* mmd,
1843                             netio_pkt_t* pkt, int start, int length,
1844                             int location, uint16_t seed)
1845 {
1846   mmd->csum_start = start;
1847   mmd->csum_length = length;
1848   mmd->csum_location = location;
1849   mmd->csum_seed = seed;
1850   mmd->flags |= _NETIO_PKT_NEED_EDMA_CSUM_MASK;
1851 }
1852 
1853 
1854 /** Set up an egress packet for hardware checksum computation.
1855  * @ingroup egress
1856  *
1857  *  NetIO provides the ability to automatically calculate a standard
1858  *  16-bit Internet checksum on transmitted packets.  The application
1859  *  may specify the point in the packet where the checksum starts, the
1860  *  number of bytes to be checksummed, and the two bytes in the packet
1861  *  which will be replaced with the completed checksum.  (If the range
1862  *  of bytes to be checksummed includes the bytes to be replaced, the
1863  *  initial values of those bytes will be included in the checksum.)
1864  *
1865  *  For some protocols, the packet checksum covers data which is not present
1866  *  in the packet, or is at least not contiguous to the main data payload.
1867  *  For instance, the TCP checksum includes a "pseudo-header" which includes
1868  *  the source and destination IP addresses of the packet.  To accommodate
1869  *  this, the checksum engine may be "seeded" with an initial value, which
1870  *  the application would need to compute based on the specific protocol's
1871  *  requirements.  Note that the seed is given in host byte order (little-
1872  *  endian), not network byte order (big-endian); code written to compute a
1873  *  pseudo-header checksum in network byte order will need to byte-swap it
1874  *  before use as the seed.
1875  *
1876  *  Note that the checksum is computed as part of the transmission process,
1877  *  so it will not be present in the packet upon completion of this routine.
1878  *
1879  * @param[in,out] pkt Packet on which to operate.
1880  * @param[in] start Offset within L2 packet of the first byte to include in
1881  *   the checksum.
1882  * @param[in] length Number of bytes to include in the checksum.
1883  *   the checksum.
1884  * @param[in] location Offset within L2 packet of the first of the two bytes
1885  *   to be replaced with the calculated checksum.
1886  * @param[in] seed Initial value of the running checksum before any of the
1887  *   packet data is added.
1888  */
1889 static __inline void
NETIO_PKT_DO_EGRESS_CSUM(netio_pkt_t * pkt,int start,int length,int location,uint16_t seed)1890 NETIO_PKT_DO_EGRESS_CSUM(netio_pkt_t* pkt, int start, int length,
1891                          int location, uint16_t seed)
1892 {
1893   netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1894 
1895   NETIO_PKT_DO_EGRESS_CSUM_MM(mmd, pkt, start, length, location, seed);
1896 }
1897 
1898 
1899 /** Return the number of bytes which could be prepended to a packet, using a
1900  *  metadata pointer to speed the operation.
1901  *  See @ref netio_populate_prepend_buffer() to get a full description of
1902  *  prepending.
1903  *
1904  * @param[in,out] mda Pointer to packet's standard metadata.
1905  * @param[in] pkt Packet on which to operate.
1906  */
1907 static __inline int
NETIO_PKT_PREPEND_AVAIL_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)1908 NETIO_PKT_PREPEND_AVAIL_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
1909 {
1910   return (pkt->__packet.bits.__offset << 6) +
1911          NETIO_PKT_CUSTOM_HEADER_LENGTH_M(mda, pkt);
1912 }
1913 
1914 
1915 /** Return the number of bytes which could be prepended to a packet, using a
1916  *  metadata pointer to speed the operation.
1917  *  See @ref netio_populate_prepend_buffer() to get a full description of
1918  *  prepending.
1919  * @ingroup egress
1920  *
1921  * @param[in,out] mmd Pointer to packet's minimal metadata.
1922  * @param[in] pkt Packet on which to operate.
1923  */
1924 static __inline int
NETIO_PKT_PREPEND_AVAIL_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1925 NETIO_PKT_PREPEND_AVAIL_MM(netio_pkt_minimal_metadata_t* mmd, netio_pkt_t* pkt)
1926 {
1927   return (pkt->__packet.bits.__offset << 6) + mmd->l2_offset;
1928 }
1929 
1930 
1931 /** Return the number of bytes which could be prepended to a packet.
1932  *  See @ref netio_populate_prepend_buffer() to get a full description of
1933  *  prepending.
1934  * @ingroup egress
1935  *
1936  * @param[in] pkt Packet on which to operate.
1937  */
1938 static __inline int
NETIO_PKT_PREPEND_AVAIL(netio_pkt_t * pkt)1939 NETIO_PKT_PREPEND_AVAIL(netio_pkt_t* pkt)
1940 {
1941   if (NETIO_PKT_IS_MINIMAL(pkt))
1942   {
1943     netio_pkt_minimal_metadata_t* mmd = NETIO_PKT_MINIMAL_METADATA(pkt);
1944 
1945     return NETIO_PKT_PREPEND_AVAIL_MM(mmd, pkt);
1946   }
1947   else
1948   {
1949     netio_pkt_metadata_t* mda = NETIO_PKT_METADATA(pkt);
1950 
1951     return NETIO_PKT_PREPEND_AVAIL_M(mda, pkt);
1952   }
1953 }
1954 
1955 
1956 /** Flush a packet's minimal metadata from the cache, using a metadata pointer
1957  *  to speed the operation.
1958  * @ingroup egress
1959  *
1960  * @param[in] mmd Pointer to packet's minimal metadata.
1961  * @param[in] pkt Packet on which to operate.
1962  */
1963 static __inline void
NETIO_PKT_FLUSH_MINIMAL_METADATA_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1964 NETIO_PKT_FLUSH_MINIMAL_METADATA_MM(netio_pkt_minimal_metadata_t* mmd,
1965                                     netio_pkt_t* pkt)
1966 {
1967 }
1968 
1969 
1970 /** Invalidate a packet's minimal metadata from the cache, using a metadata
1971  *  pointer to speed the operation.
1972  * @ingroup egress
1973  *
1974  * @param[in] mmd Pointer to packet's minimal metadata.
1975  * @param[in] pkt Packet on which to operate.
1976  */
1977 static __inline void
NETIO_PKT_INV_MINIMAL_METADATA_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1978 NETIO_PKT_INV_MINIMAL_METADATA_MM(netio_pkt_minimal_metadata_t* mmd,
1979                                   netio_pkt_t* pkt)
1980 {
1981 }
1982 
1983 
1984 /** Flush and then invalidate a packet's minimal metadata from the cache,
1985  *  using a metadata pointer to speed the operation.
1986  * @ingroup egress
1987  *
1988  * @param[in] mmd Pointer to packet's minimal metadata.
1989  * @param[in] pkt Packet on which to operate.
1990  */
1991 static __inline void
NETIO_PKT_FLUSH_INV_MINIMAL_METADATA_MM(netio_pkt_minimal_metadata_t * mmd,netio_pkt_t * pkt)1992 NETIO_PKT_FLUSH_INV_MINIMAL_METADATA_MM(netio_pkt_minimal_metadata_t* mmd,
1993                                         netio_pkt_t* pkt)
1994 {
1995 }
1996 
1997 
1998 /** Flush a packet's metadata from the cache, using a metadata pointer
1999  *  to speed the operation.
2000  * @ingroup ingress
2001  *
2002  * @param[in] mda Pointer to packet's minimal metadata.
2003  * @param[in] pkt Packet on which to operate.
2004  */
2005 static __inline void
NETIO_PKT_FLUSH_METADATA_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)2006 NETIO_PKT_FLUSH_METADATA_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
2007 {
2008 }
2009 
2010 
2011 /** Invalidate a packet's metadata from the cache, using a metadata
2012  *  pointer to speed the operation.
2013  * @ingroup ingress
2014  *
2015  * @param[in] mda Pointer to packet's metadata.
2016  * @param[in] pkt Packet on which to operate.
2017  */
2018 static __inline void
NETIO_PKT_INV_METADATA_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)2019 NETIO_PKT_INV_METADATA_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
2020 {
2021 }
2022 
2023 
2024 /** Flush and then invalidate a packet's metadata from the cache,
2025  *  using a metadata pointer to speed the operation.
2026  * @ingroup ingress
2027  *
2028  * @param[in] mda Pointer to packet's metadata.
2029  * @param[in] pkt Packet on which to operate.
2030  */
2031 static __inline void
NETIO_PKT_FLUSH_INV_METADATA_M(netio_pkt_metadata_t * mda,netio_pkt_t * pkt)2032 NETIO_PKT_FLUSH_INV_METADATA_M(netio_pkt_metadata_t* mda, netio_pkt_t* pkt)
2033 {
2034 }
2035 
2036 
2037 /** Flush a packet's minimal metadata from the cache.
2038  * @ingroup egress
2039  *
2040  * @param[in] pkt Packet on which to operate.
2041  */
2042 static __inline void
NETIO_PKT_FLUSH_MINIMAL_METADATA(netio_pkt_t * pkt)2043 NETIO_PKT_FLUSH_MINIMAL_METADATA(netio_pkt_t* pkt)
2044 {
2045 }
2046 
2047 
2048 /** Invalidate a packet's minimal metadata from the cache.
2049  * @ingroup egress
2050  *
2051  * @param[in] pkt Packet on which to operate.
2052  */
2053 static __inline void
NETIO_PKT_INV_MINIMAL_METADATA(netio_pkt_t * pkt)2054 NETIO_PKT_INV_MINIMAL_METADATA(netio_pkt_t* pkt)
2055 {
2056 }
2057 
2058 
2059 /** Flush and then invalidate a packet's minimal metadata from the cache.
2060  * @ingroup egress
2061  *
2062  * @param[in] pkt Packet on which to operate.
2063  */
2064 static __inline void
NETIO_PKT_FLUSH_INV_MINIMAL_METADATA(netio_pkt_t * pkt)2065 NETIO_PKT_FLUSH_INV_MINIMAL_METADATA(netio_pkt_t* pkt)
2066 {
2067 }
2068 
2069 
2070 /** Flush a packet's metadata from the cache.
2071  * @ingroup ingress
2072  *
2073  * @param[in] pkt Packet on which to operate.
2074  */
2075 static __inline void
NETIO_PKT_FLUSH_METADATA(netio_pkt_t * pkt)2076 NETIO_PKT_FLUSH_METADATA(netio_pkt_t* pkt)
2077 {
2078 }
2079 
2080 
2081 /** Invalidate a packet's metadata from the cache.
2082  * @ingroup ingress
2083  *
2084  * @param[in] pkt Packet on which to operate.
2085  */
2086 static __inline void
NETIO_PKT_INV_METADATA(netio_pkt_t * pkt)2087 NETIO_PKT_INV_METADATA(netio_pkt_t* pkt)
2088 {
2089 }
2090 
2091 
2092 /** Flush and then invalidate a packet's metadata from the cache.
2093  * @ingroup ingress
2094  *
2095  * @param[in] pkt Packet on which to operate.
2096  */
2097 static __inline void
NETIO_PKT_FLUSH_INV_METADATA(netio_pkt_t * pkt)2098 NETIO_PKT_FLUSH_INV_METADATA(netio_pkt_t* pkt)
2099 {
2100 }
2101 
2102 /** Number of NUMA nodes we can distribute buffers to.
2103  * @ingroup setup */
2104 #define NETIO_NUM_NODE_WEIGHTS  16
2105 
2106 /**
2107  * @brief An object for specifying the characteristics of NetIO communication
2108  * endpoint.
2109  *
2110  * @ingroup setup
2111  *
2112  * The @ref netio_input_register() function uses this structure to define
2113  * how an application tile will communicate with an IPP.
2114  *
2115  *
2116  * Future updates to NetIO may add new members to this structure,
2117  * which can affect the success of the registration operation.  Thus,
2118  * if dynamically initializing the structure, applications are urged to
2119  * zero it out first, for example:
2120  *
2121  * @code
2122  * netio_input_config_t config;
2123  * memset(&config, 0, sizeof (config));
2124  * config.flags = NETIO_RECV | NETIO_XMIT_CSUM | NETIO_TAG_NONE;
2125  * config.num_receive_packets = NETIO_MAX_RECEIVE_PKTS;
2126  * config.queue_id = 0;
2127  *     .
2128  *     .
2129  *     .
2130  * @endcode
2131  *
2132  * since that guarantees that any unused structure members, including
2133  * members which did not exist when the application was first developed,
2134  * will not have unexpected values.
2135  *
2136  * If statically initializing the structure, we strongly recommend use of
2137  * C99-style named initializers, for example:
2138  *
2139  * @code
2140  * netio_input_config_t config = {
2141  *    .flags = NETIO_RECV | NETIO_XMIT_CSUM | NETIO_TAG_NONE,
2142  *    .num_receive_packets = NETIO_MAX_RECEIVE_PKTS,
2143  *    .queue_id = 0,
2144  * },
2145  * @endcode
2146  *
2147  * instead of the old-style structure initialization:
2148  *
2149  * @code
2150  * // Bad example! Currently equivalent to the above, but don't do this.
2151  * netio_input_config_t config = {
2152  *    NETIO_RECV | NETIO_XMIT_CSUM | NETIO_TAG_NONE, NETIO_MAX_RECEIVE_PKTS, 0
2153  * },
2154  * @endcode
2155  *
2156  * since the C99 style requires no changes to the code if elements of the
2157  * config structure are rearranged.  (It also makes the initialization much
2158  * easier to understand.)
2159  *
2160  * Except for items which address a particular tile's transmit or receive
2161  * characteristics, such as the ::NETIO_RECV flag, applications are advised
2162  * to specify the same set of configuration data on all registrations.
2163  * This prevents differing results if multiple tiles happen to do their
2164  * registration operations in a different order on different invocations of
2165  * the application.  This is particularly important for things like link
2166  * management flags, and buffer size and homing specifications.
2167  *
2168  * Unless the ::NETIO_FIXED_BUFFER_VA flag is specified in flags, the NetIO
2169  * buffer pool is automatically created and mapped into the application's
2170  * virtual address space at an address chosen by the operating system,
2171  * using the common memory (cmem) facility in the Tilera Multicore
2172  * Components library.  The cmem facility allows multiple processes to gain
2173  * access to shared memory which is mapped into each process at an
2174  * identical virtual address.  In order for this to work, the processes
2175  * must have a common ancestor, which must create the common memory using
2176  * tmc_cmem_init().
2177  *
2178  * In programs using the iLib process creation API, or in programs which use
2179  * only one process (which include programs using the pthreads library),
2180  * tmc_cmem_init() is called automatically.  All other applications
2181  * must call it explicitly, before any child processes which might call
2182  * netio_input_register() are created.
2183  */
2184 typedef struct
2185 {
2186   /** Registration characteristics.
2187 
2188       This value determines several characteristics of the registration;
2189       flags for different types of behavior are ORed together to make the
2190       final flag value.  Generally applications should specify exactly
2191       one flag from each of the following categories:
2192 
2193       - Whether the application will be receiving packets on this queue
2194         (::NETIO_RECV or ::NETIO_NO_RECV).
2195 
2196       - Whether the application will be transmitting packets on this queue,
2197         and if so, whether it will request egress checksum calculation
2198         (::NETIO_XMIT, ::NETIO_XMIT_CSUM, or ::NETIO_NO_XMIT).  It is
2199         legal to call netio_get_buffer() without one of the XMIT flags,
2200         as long as ::NETIO_RECV is specified; in this case, the retrieved
2201         buffers must be passed to another tile for transmission.
2202 
2203       - Whether the application expects any vendor-specific tags in
2204         its packets' L2 headers (::NETIO_TAG_NONE, ::NETIO_TAG_BRCM,
2205         or ::NETIO_TAG_MRVL).  This must match the configuration of the
2206         target IPP.
2207 
2208       To accommodate applications written to previous versions of the NetIO
2209       interface, none of the flags above are currently required; if omitted,
2210       NetIO behaves more or less as if ::NETIO_RECV | ::NETIO_XMIT_CSUM |
2211       ::NETIO_TAG_NONE were used.  However, explicit specification of
2212       the relevant flags allows NetIO to do a better job of resource
2213       allocation, allows earlier detection of certain configuration errors,
2214       and may enable advanced features or higher performance in the future,
2215       so their use is strongly recommended.
2216 
2217       Note that specifying ::NETIO_NO_RECV along with ::NETIO_NO_XMIT
2218       is a special case, intended primarily for use by programs which
2219       retrieve network statistics or do link management operations.
2220       When these flags are both specified, the resulting queue may not
2221       be used with NetIO routines other than netio_get(), netio_set(),
2222       and netio_input_unregister().  See @ref link for more information
2223       on link management.
2224 
2225       Other flags are optional; their use is described below.
2226   */
2227   int flags;
2228 
2229   /** Interface name.  This is a string which identifies the specific
2230       Ethernet controller hardware to be used.  The format of the string
2231       is a device type and a device index, separated by a slash; so,
2232       the first 10 Gigabit Ethernet controller is named "xgbe/0", while
2233       the second 10/100/1000 Megabit Ethernet controller is named "gbe/1".
2234    */
2235   const char* interface;
2236 
2237   /** Receive packet queue size.  This specifies the maximum number
2238       of ingress packets that can be received on this queue without
2239       being retrieved by @ref netio_get_packet().  If the IPP's distribution
2240       algorithm calls for a packet to be sent to this queue, and this
2241       number of packets are already pending there, the new packet
2242       will either be discarded, or sent to another tile registered
2243       for the same queue_id (see @ref drops).  This value must
2244       be at least ::NETIO_MIN_RECEIVE_PKTS, can always be at least
2245       ::NETIO_MAX_RECEIVE_PKTS, and may be larger than that on certain
2246       interfaces.
2247    */
2248   int num_receive_packets;
2249 
2250   /** The queue ID being requested.  Legal values for this range from 0
2251       to ::NETIO_MAX_QUEUE_ID, inclusive.  ::NETIO_MAX_QUEUE_ID is always
2252       greater than or equal to the number of tiles; this allows one queue
2253       for each tile, plus at least one additional queue.  Some applications
2254       may wish to use the additional queue as a destination for unwanted
2255       packets, since packets delivered to queues for which no tiles have
2256       registered are discarded.
2257    */
2258   unsigned int queue_id;
2259 
2260   /** Maximum number of small send buffers to be held in the local empty
2261       buffer cache.  This specifies the size of the area which holds
2262       empty small egress buffers requested from the IPP but not yet
2263       retrieved via @ref netio_get_buffer().  This value must be greater
2264       than zero if the application will ever use @ref netio_get_buffer()
2265       to allocate empty small egress buffers; it may be no larger than
2266       ::NETIO_MAX_SEND_BUFFERS.  See @ref epp for more details on empty
2267       buffer caching.
2268    */
2269   int num_send_buffers_small_total;
2270 
2271   /** Number of small send buffers to be preallocated at registration.
2272       If this value is nonzero, the specified number of empty small egress
2273       buffers will be requested from the IPP during the netio_input_register
2274       operation; this may speed the execution of @ref netio_get_buffer().
2275       This may be no larger than @ref num_send_buffers_small_total.  See @ref
2276       epp for more details on empty buffer caching.
2277    */
2278   int num_send_buffers_small_prealloc;
2279 
2280   /** Maximum number of large send buffers to be held in the local empty
2281       buffer cache.  This specifies the size of the area which holds empty
2282       large egress buffers requested from the IPP but not yet retrieved via
2283       @ref netio_get_buffer().  This value must be greater than zero if the
2284       application will ever use @ref netio_get_buffer() to allocate empty
2285       large egress buffers; it may be no larger than ::NETIO_MAX_SEND_BUFFERS.
2286       See @ref epp for more details on empty buffer caching.
2287    */
2288   int num_send_buffers_large_total;
2289 
2290   /** Number of large send buffers to be preallocated at registration.
2291       If this value is nonzero, the specified number of empty large egress
2292       buffers will be requested from the IPP during the netio_input_register
2293       operation; this may speed the execution of @ref netio_get_buffer().
2294       This may be no larger than @ref num_send_buffers_large_total.  See @ref
2295       epp for more details on empty buffer caching.
2296    */
2297   int num_send_buffers_large_prealloc;
2298 
2299   /** Maximum number of jumbo send buffers to be held in the local empty
2300       buffer cache.  This specifies the size of the area which holds empty
2301       jumbo egress buffers requested from the IPP but not yet retrieved via
2302       @ref netio_get_buffer().  This value must be greater than zero if the
2303       application will ever use @ref netio_get_buffer() to allocate empty
2304       jumbo egress buffers; it may be no larger than ::NETIO_MAX_SEND_BUFFERS.
2305       See @ref epp for more details on empty buffer caching.
2306    */
2307   int num_send_buffers_jumbo_total;
2308 
2309   /** Number of jumbo send buffers to be preallocated at registration.
2310       If this value is nonzero, the specified number of empty jumbo egress
2311       buffers will be requested from the IPP during the netio_input_register
2312       operation; this may speed the execution of @ref netio_get_buffer().
2313       This may be no larger than @ref num_send_buffers_jumbo_total.  See @ref
2314       epp for more details on empty buffer caching.
2315    */
2316   int num_send_buffers_jumbo_prealloc;
2317 
2318   /** Total packet buffer size.  This determines the total size, in bytes,
2319       of the NetIO buffer pool.  Note that the maximum number of available
2320       buffers of each size is determined during hypervisor configuration
2321       (see the <em>System Programmer's Guide</em> for details); this just
2322       influences how much host memory is allocated for those buffers.
2323 
2324       The buffer pool is allocated from common memory, which will be
2325       automatically initialized if needed.  If your buffer pool is larger
2326       than 240 MB, you might need to explicitly call @c tmc_cmem_init(),
2327       as described in the Application Libraries Reference Manual (UG227).
2328 
2329       Packet buffers are currently allocated in chunks of 16 MB; this
2330       value will be rounded up to the next larger multiple of 16 MB.
2331       If this value is zero, a default of 32 MB will be used; this was
2332       the value used by previous versions of NetIO.  Note that taking this
2333       default also affects the placement of buffers on Linux NUMA nodes.
2334       See @ref buffer_node_weights for an explanation of buffer placement.
2335 
2336       In order to successfully allocate packet buffers, Linux must have
2337       available huge pages on the relevant Linux NUMA nodes.  See the
2338       <em>System Programmer's Guide</em> for information on configuring
2339       huge page support in Linux.
2340    */
2341   uint64_t total_buffer_size;
2342 
2343   /** Buffer placement weighting factors.
2344 
2345       This array specifies the relative amount of buffering to place
2346       on each of the available Linux NUMA nodes.  This array is
2347       indexed by the NUMA node, and the values in the array are
2348       proportional to the amount of buffer space to allocate on that
2349       node.
2350 
2351       If memory striping is enabled in the Hypervisor, then there is
2352       only one logical NUMA node (node 0). In that case, NetIO will by
2353       default ignore the suggested buffer node weights, and buffers
2354       will be striped across the physical memory controllers. See
2355       UG209 System Programmer's Guide for a description of the
2356       hypervisor option that controls memory striping.
2357 
2358       If memory striping is disabled, then there are up to four NUMA
2359       nodes, corresponding to the four DDRAM controllers in the TILE
2360       processor architecture.  See UG100 Tile Processor Architecture
2361       Overview for a diagram showing the location of each of the DDRAM
2362       controllers relative to the tile array.
2363 
2364       For instance, if memory striping is disabled, the following
2365       configuration strucure:
2366 
2367       @code
2368       netio_input_config_t config = {
2369             .
2370             .
2371             .
2372         .total_buffer_size = 4 * 16 * 1024 * 1024;
2373         .buffer_node_weights = { 1, 0, 1, 0 },
2374       },
2375       @endcode
2376 
2377       would result in 32 MB of buffers being placed on controller 0, and
2378       32 MB on controller 2.  (Since buffers are allocated in units of
2379       16 MB, some sets of weights will not be able to be matched exactly.)
2380 
2381       For the weights to be effective, @ref total_buffer_size must be
2382       nonzero.  If @ref total_buffer_size is zero, causing the default
2383       32 MB of buffer space to be used, then any specified weights will
2384       be ignored, and buffers will positioned as they were in previous
2385       versions of NetIO:
2386 
2387       - For xgbe/0 and gbe/0, 16 MB of buffers will be placed on controller 1,
2388         and the other 16 MB will be placed on controller 2.
2389 
2390       - For xgbe/1 and gbe/1, 16 MB of buffers will be placed on controller 2,
2391         and the other 16 MB will be placed on controller 3.
2392 
2393       If @ref total_buffer_size is nonzero, but all weights are zero,
2394       then all buffer space will be allocated on Linux NUMA node zero.
2395 
2396       By default, the specified buffer placement is treated as a hint;
2397       if sufficient free memory is not available on the specified
2398       controllers, the buffers will be allocated elsewhere.  However,
2399       if the ::NETIO_STRICT_HOMING flag is specified in @ref flags, then a
2400       failure to allocate buffer space exactly as requested will cause the
2401       registration operation to fail with an error of ::NETIO_CANNOT_HOME.
2402 
2403       Note that maximal network performance cannot be achieved with
2404       only one memory controller.
2405    */
2406   uint8_t buffer_node_weights[NETIO_NUM_NODE_WEIGHTS];
2407 
2408   /** Fixed virtual address for packet buffers.  Only valid when
2409       ::NETIO_FIXED_BUFFER_VA is specified in @ref flags; see the
2410       description of that flag for details.
2411    */
2412   void* fixed_buffer_va;
2413 
2414   /**
2415       Maximum number of outstanding send packet requests.  This value is
2416       only relevant when an EPP is in use; it determines the number of
2417       slots in the EPP's outgoing packet queue which this tile is allowed
2418       to consume, and thus the number of packets which may be sent before
2419       the sending tile must wait for an acknowledgment from the EPP.
2420       Modifying this value is generally only helpful when using @ref
2421       netio_send_packet_vector(), where it can help improve performance by
2422       allowing a single vector send operation to process more packets.
2423       Typically it is not specified, and the default, which divides the
2424       outgoing packet slots evenly between all tiles on the chip, is used.
2425 
2426       If a registration asks for more outgoing packet queue slots than are
2427       available, ::NETIO_TOOMANY_XMIT will be returned.  The total number
2428       of packet queue slots which are available for all tiles for each EPP
2429       is subject to change, but is currently ::NETIO_TOTAL_SENDS_OUTSTANDING.
2430 
2431 
2432       This value is ignored if ::NETIO_XMIT is not specified in flags.
2433       If you want to specify a large value here for a specific tile, you are
2434       advised to specify NETIO_NO_XMIT on other, non-transmitting tiles so
2435       that they do not consume a default number of packet slots.  Any tile
2436       transmitting is required to have at least ::NETIO_MIN_SENDS_OUTSTANDING
2437       slots allocated to it; values less than that will be silently
2438       increased by the NetIO library.
2439    */
2440   int num_sends_outstanding;
2441 }
2442 netio_input_config_t;
2443 
2444 
2445 /** Registration flags; used in the @ref netio_input_config_t structure.
2446  * @addtogroup setup
2447  */
2448 /** @{ */
2449 
2450 /** Fail a registration request if we can't put packet buffers
2451     on the specified memory controllers. */
2452 #define NETIO_STRICT_HOMING   0x00000002
2453 
2454 /** This application expects no tags on its L2 headers. */
2455 #define NETIO_TAG_NONE        0x00000004
2456 
2457 /** This application expects Marvell extended tags on its L2 headers. */
2458 #define NETIO_TAG_MRVL        0x00000008
2459 
2460 /** This application expects Broadcom tags on its L2 headers. */
2461 #define NETIO_TAG_BRCM        0x00000010
2462 
2463 /** This registration may call routines which receive packets. */
2464 #define NETIO_RECV            0x00000020
2465 
2466 /** This registration may not call routines which receive packets. */
2467 #define NETIO_NO_RECV         0x00000040
2468 
2469 /** This registration may call routines which transmit packets. */
2470 #define NETIO_XMIT            0x00000080
2471 
2472 /** This registration may call routines which transmit packets with
2473     checksum acceleration. */
2474 #define NETIO_XMIT_CSUM       0x00000100
2475 
2476 /** This registration may not call routines which transmit packets. */
2477 #define NETIO_NO_XMIT         0x00000200
2478 
2479 /** This registration wants NetIO buffers mapped at an application-specified
2480     virtual address.
2481 
2482     NetIO buffers are by default created by the TMC common memory facility,
2483     which must be configured by a common ancestor of all processes sharing
2484     a network interface.  When this flag is specified, NetIO buffers are
2485     instead mapped at an address chosen by the application (and specified
2486     in @ref netio_input_config_t::fixed_buffer_va).  This allows multiple
2487     unrelated but cooperating processes to share a NetIO interface.
2488     All processes sharing the same interface must specify this flag,
2489     and all must specify the same fixed virtual address.
2490 
2491     @ref netio_input_config_t::fixed_buffer_va must be a
2492     multiple of 16 MB, and the packet buffers will occupy @ref
2493     netio_input_config_t::total_buffer_size bytes of virtual address
2494     space, beginning at that address.  If any of those virtual addresses
2495     are currently occupied by other memory objects, like application or
2496     shared library code or data, @ref netio_input_register() will return
2497     ::NETIO_FAULT.  While it is impossible to provide a fixed_buffer_va
2498     which will work for all applications, a good first guess might be to
2499     use 0xb0000000 minus @ref netio_input_config_t::total_buffer_size.
2500     If that fails, it might be helpful to consult the running application's
2501     virtual address description file (/proc/<em>pid</em>/maps) to see
2502     which regions of virtual address space are available.
2503  */
2504 #define NETIO_FIXED_BUFFER_VA 0x00000400
2505 
2506 /** This registration call will not complete unless the network link
2507     is up.  The process will wait several seconds for this to happen (the
2508     precise interval is link-dependent), but if the link does not come up,
2509     ::NETIO_LINK_DOWN will be returned.  This flag is the default if
2510     ::NETIO_NOREQUIRE_LINK_UP is not specified.  Note that this flag by
2511     itself does not request that the link be brought up; that can be done
2512     with the ::NETIO_AUTO_LINK_UPDN or ::NETIO_AUTO_LINK_UP flags (the
2513     latter is the default if no NETIO_AUTO_LINK_xxx flags are specified),
2514     or by explicitly setting the link's desired state via netio_set().
2515     If the link is not brought up by one of those methods, and this flag
2516     is specified, the registration operation will return ::NETIO_LINK_DOWN.
2517     This flag is ignored if it is specified along with ::NETIO_NO_XMIT and
2518     ::NETIO_NO_RECV.  See @ref link for more information on link
2519     management.
2520  */
2521 #define NETIO_REQUIRE_LINK_UP    0x00000800
2522 
2523 /** This registration call will complete even if the network link is not up.
2524     Whenever the link is not up, packets will not be sent or received:
2525     netio_get_packet() will return ::NETIO_NOPKT once all queued packets
2526     have been drained, and netio_send_packet() and similar routines will
2527     return NETIO_QUEUE_FULL once the outgoing packet queue in the EPP
2528     or the I/O shim is full.  See @ref link for more information on link
2529     management.
2530  */
2531 #define NETIO_NOREQUIRE_LINK_UP  0x00001000
2532 
2533 #ifndef __DOXYGEN__
2534 /*
2535  * These are part of the implementation of the NETIO_AUTO_LINK_xxx flags,
2536  * but should not be used directly by applications, and are thus not
2537  * documented.
2538  */
2539 #define _NETIO_AUTO_UP        0x00002000
2540 #define _NETIO_AUTO_DN        0x00004000
2541 #define _NETIO_AUTO_PRESENT   0x00008000
2542 #endif
2543 
2544 /** Set the desired state of the link to up, allowing any speeds which are
2545     supported by the link hardware, as part of this registration operation.
2546     Do not take down the link automatically.  This is the default if
2547     no other NETIO_AUTO_LINK_xxx flags are specified.  This flag is ignored
2548     if it is specified along with ::NETIO_NO_XMIT and ::NETIO_NO_RECV.
2549     See @ref link for more information on link management.
2550  */
2551 #define NETIO_AUTO_LINK_UP     (_NETIO_AUTO_PRESENT | _NETIO_AUTO_UP)
2552 
2553 /** Set the desired state of the link to up, allowing any speeds which are
2554     supported by the link hardware, as part of this registration operation.
2555     Set the desired state of the link to down the next time no tiles are
2556     registered for packet reception or transmission.  This flag is ignored
2557     if it is specified along with ::NETIO_NO_XMIT and ::NETIO_NO_RECV.
2558     See @ref link for more information on link management.
2559  */
2560 #define NETIO_AUTO_LINK_UPDN   (_NETIO_AUTO_PRESENT | _NETIO_AUTO_UP | \
2561                                 _NETIO_AUTO_DN)
2562 
2563 /** Set the desired state of the link to down the next time no tiles are
2564     registered for packet reception or transmission.  This flag is ignored
2565     if it is specified along with ::NETIO_NO_XMIT and ::NETIO_NO_RECV.
2566     See @ref link for more information on link management.
2567  */
2568 #define NETIO_AUTO_LINK_DN     (_NETIO_AUTO_PRESENT | _NETIO_AUTO_DN)
2569 
2570 /** Do not bring up the link automatically as part of this registration
2571     operation.  Do not take down the link automatically.  This flag
2572     is ignored if it is specified along with ::NETIO_NO_XMIT and
2573     ::NETIO_NO_RECV.  See @ref link for more information on link management.
2574   */
2575 #define NETIO_AUTO_LINK_NONE   _NETIO_AUTO_PRESENT
2576 
2577 
2578 /** Minimum number of receive packets. */
2579 #define NETIO_MIN_RECEIVE_PKTS            16
2580 
2581 /** Lower bound on the maximum number of receive packets; may be higher
2582     than this on some interfaces. */
2583 #define NETIO_MAX_RECEIVE_PKTS           128
2584 
2585 /** Maximum number of send buffers, per packet size. */
2586 #define NETIO_MAX_SEND_BUFFERS            16
2587 
2588 /** Number of EPP queue slots, and thus outstanding sends, per EPP. */
2589 #define NETIO_TOTAL_SENDS_OUTSTANDING   2015
2590 
2591 /** Minimum number of EPP queue slots, and thus outstanding sends, per
2592  *  transmitting tile. */
2593 #define NETIO_MIN_SENDS_OUTSTANDING       16
2594 
2595 
2596 /**@}*/
2597 
2598 #ifndef __DOXYGEN__
2599 
2600 /**
2601  * An object for providing Ethernet packets to a process.
2602  */
2603 struct __netio_queue_impl_t;
2604 
2605 /**
2606  * An object for managing the user end of a NetIO queue.
2607  */
2608 struct __netio_queue_user_impl_t;
2609 
2610 #endif /* !__DOXYGEN__ */
2611 
2612 
2613 /** A netio_queue_t describes a NetIO communications endpoint.
2614  * @ingroup setup
2615  */
2616 typedef struct
2617 {
2618 #ifdef __DOXYGEN__
2619   uint8_t opaque[8];                 /**< This is an opaque structure. */
2620 #else
2621   struct __netio_queue_impl_t* __system_part;    /**< The system part. */
2622   struct __netio_queue_user_impl_t* __user_part; /**< The user part. */
2623 #ifdef _NETIO_PTHREAD
2624   _netio_percpu_mutex_t lock;                    /**< Queue lock. */
2625 #endif
2626 #endif
2627 }
2628 netio_queue_t;
2629 
2630 
2631 /**
2632  * @brief Packet send context.
2633  *
2634  * @ingroup egress
2635  *
2636  * Packet send context for use with netio_send_packet_prepare and _commit.
2637  */
2638 typedef struct
2639 {
2640 #ifdef __DOXYGEN__
2641   uint8_t opaque[44];   /**< This is an opaque structure. */
2642 #else
2643   uint8_t flags;        /**< Defined below */
2644   uint8_t datalen;      /**< Number of valid words pointed to by data. */
2645   uint32_t request[9];  /**< Request to be sent to the EPP or shim.  Note
2646                              that this is smaller than the 11-word maximum
2647                              request size, since some constant values are
2648                              not saved in the context. */
2649   uint32_t *data;       /**< Data to be sent to the EPP or shim via IDN. */
2650 #endif
2651 }
2652 netio_send_pkt_context_t;
2653 
2654 
2655 #ifndef __DOXYGEN__
2656 #define SEND_PKT_CTX_USE_EPP   1  /**< We're sending to an EPP. */
2657 #define SEND_PKT_CTX_SEND_CSUM 2  /**< Request includes a checksum. */
2658 #endif
2659 
2660 /**
2661  * @brief Packet vector entry.
2662  *
2663  * @ingroup egress
2664  *
2665  * This data structure is used with netio_send_packet_vector() to send multiple
2666  * packets with one NetIO call.  The structure should be initialized by
2667  * calling netio_pkt_vector_set(), rather than by setting the fields
2668  * directly.
2669  *
2670  * This structure is guaranteed to be a power of two in size, no
2671  * bigger than one L2 cache line, and to be aligned modulo its size.
2672  */
2673 typedef struct
2674 #ifndef __DOXYGEN__
2675 __attribute__((aligned(8)))
2676 #endif
2677 {
2678   /** Reserved for use by the user application.  When initialized with
2679    *  the netio_set_pkt_vector_entry() function, this field is guaranteed
2680    *  to be visible to readers only after all other fields are already
2681    *  visible.  This way it can be used as a valid flag or generation
2682    *  counter. */
2683   uint8_t user_data;
2684 
2685   /* Structure members below this point should not be accessed directly by
2686    * applications, as they may change in the future. */
2687 
2688   /** Low 8 bits of the packet address to send.  The high bits are
2689    *  acquired from the 'handle' field. */
2690   uint8_t buffer_address_low;
2691 
2692   /** Number of bytes to transmit. */
2693   uint16_t size;
2694 
2695   /** The raw handle from a netio_pkt_t.  If this is NETIO_PKT_HANDLE_NONE,
2696    *  this vector entry will be skipped and no packet will be transmitted. */
2697   netio_pkt_handle_t handle;
2698 }
2699 netio_pkt_vector_entry_t;
2700 
2701 
2702 /**
2703  * @brief Initialize fields in a packet vector entry.
2704  *
2705  * @ingroup egress
2706  *
2707  * @param[out] v Pointer to the vector entry to be initialized.
2708  * @param[in] pkt Packet to be transmitted when the vector entry is passed to
2709  *        netio_send_packet_vector().  Note that the packet's attributes
2710  *        (e.g., its L2 offset and length) are captured at the time this
2711  *        routine is called; subsequent changes in those attributes will not
2712  *        be reflected in the packet which is actually transmitted.
2713  *        Changes in the packet's contents, however, will be so reflected.
2714  *        If this is NULL, no packet will be transmitted.
2715  * @param[in] user_data User data to be set in the vector entry.
2716  *        This function guarantees that the "user_data" field will become
2717  *        visible to a reader only after all other fields have become visible.
2718  *        This allows a structure in a ring buffer to be written and read
2719  *        by a polling reader without any locks or other synchronization.
2720  */
2721 static __inline void
netio_pkt_vector_set(volatile netio_pkt_vector_entry_t * v,netio_pkt_t * pkt,uint8_t user_data)2722 netio_pkt_vector_set(volatile netio_pkt_vector_entry_t* v, netio_pkt_t* pkt,
2723                      uint8_t user_data)
2724 {
2725   if (pkt)
2726   {
2727     if (NETIO_PKT_IS_MINIMAL(pkt))
2728     {
2729       netio_pkt_minimal_metadata_t* mmd =
2730         (netio_pkt_minimal_metadata_t*) &pkt->__metadata;
2731       v->buffer_address_low = (uintptr_t) NETIO_PKT_L2_DATA_MM(mmd, pkt) & 0xFF;
2732       v->size = NETIO_PKT_L2_LENGTH_MM(mmd, pkt);
2733     }
2734     else
2735     {
2736       netio_pkt_metadata_t* mda = &pkt->__metadata;
2737       v->buffer_address_low = (uintptr_t) NETIO_PKT_L2_DATA_M(mda, pkt) & 0xFF;
2738       v->size = NETIO_PKT_L2_LENGTH_M(mda, pkt);
2739     }
2740     v->handle.word = pkt->__packet.word;
2741   }
2742   else
2743   {
2744     v->handle.word = 0;   /* Set handle to NETIO_PKT_HANDLE_NONE. */
2745   }
2746 
2747   __asm__("" : : : "memory");
2748 
2749   v->user_data = user_data;
2750 }
2751 
2752 
2753 /**
2754  * Flags and structures for @ref netio_get() and @ref netio_set().
2755  * @ingroup config
2756  */
2757 
2758 /** @{ */
2759 /** Parameter class; addr is a NETIO_PARAM_xxx value. */
2760 #define NETIO_PARAM       0
2761 /** Interface MAC address. This address is only valid with @ref netio_get().
2762  *  The value is a 6-byte MAC address.  Depending upon the overall system
2763  *  design, a MAC address may or may not be available for each interface. */
2764 #define NETIO_PARAM_MAC        0
2765 
2766 /** Determine whether to suspend output on the receipt of pause frames.
2767  *  If the value is nonzero, the I/O shim will suspend output when a pause
2768  *  frame is received.  If the value is zero, pause frames will be ignored. */
2769 #define NETIO_PARAM_PAUSE_IN   1
2770 
2771 /** Determine whether to send pause frames if the I/O shim packet FIFOs are
2772  *  nearly full.  If the value is zero, pause frames are not sent.  If
2773  *  the value is nonzero, it is the delay value which will be sent in any
2774  *  pause frames which are output, in units of 512 bit times. */
2775 #define NETIO_PARAM_PAUSE_OUT  2
2776 
2777 /** Jumbo frame support.  The value is a 4-byte integer.  If the value is
2778  *  nonzero, the MAC will accept frames of up to 10240 bytes.  If the value
2779  *  is zero, the MAC will only accept frames of up to 1544 bytes. */
2780 #define NETIO_PARAM_JUMBO      3
2781 
2782 /** I/O shim's overflow statistics register.  The value is two 16-bit integers.
2783  *  The first 16-bit value (or the low 16 bits, if the value is treated as a
2784  *  32-bit number) is the count of packets which were completely dropped and
2785  *  not delivered by the shim.  The second 16-bit value (or the high 16 bits,
2786  *  if the value is treated as a 32-bit number) is the count of packets
2787  *  which were truncated and thus only partially delivered by the shim.  This
2788  *  register is automatically reset to zero after it has been read.
2789  */
2790 #define NETIO_PARAM_OVERFLOW   4
2791 
2792 /** IPP statistics.  This address is only valid with @ref netio_get().  The
2793  *  value is a netio_stat_t structure.  Unlike the I/O shim statistics, the
2794  *  IPP statistics are not all reset to zero on read; see the description
2795  *  of the netio_stat_t for details. */
2796 #define NETIO_PARAM_STAT 5
2797 
2798 /** Possible link state.  The value is a combination of "NETIO_LINK_xxx"
2799  *  flags.  With @ref netio_get(), this will indicate which flags are
2800  *  actually supported by the hardware.
2801  *
2802  *  For historical reasons, specifying this value to netio_set() will have
2803  *  the same behavior as using ::NETIO_PARAM_LINK_CONFIG, but this usage is
2804  *  discouraged.
2805  */
2806 #define NETIO_PARAM_LINK_POSSIBLE_STATE 6
2807 
2808 /** Link configuration. The value is a combination of "NETIO_LINK_xxx" flags.
2809  *  With @ref netio_set(), this will attempt to immediately bring up the
2810  *  link using whichever of the requested flags are supported by the
2811  *  hardware, or take down the link if the flags are zero; if this is
2812  *  not possible, an error will be returned.  Many programs will want
2813  *  to use ::NETIO_PARAM_LINK_DESIRED_STATE instead.
2814  *
2815  *  For historical reasons, specifying this value to netio_get() will
2816  *  have the same behavior as using ::NETIO_PARAM_LINK_POSSIBLE_STATE,
2817  *  but this usage is discouraged.
2818  */
2819 #define NETIO_PARAM_LINK_CONFIG NETIO_PARAM_LINK_POSSIBLE_STATE
2820 
2821 /** Current link state. This address is only valid with @ref netio_get().
2822  *  The value is zero or more of the "NETIO_LINK_xxx" flags, ORed together.
2823  *  If the link is down, the value ANDed with NETIO_LINK_SPEED will be
2824  *  zero; if the link is up, the value ANDed with NETIO_LINK_SPEED will
2825  *  result in exactly one of the NETIO_LINK_xxx values, indicating the
2826  *  current speed. */
2827 #define NETIO_PARAM_LINK_CURRENT_STATE 7
2828 
2829 /** Variant symbol for current state, retained for compatibility with
2830  *  pre-MDE-2.1 programs. */
2831 #define NETIO_PARAM_LINK_STATUS NETIO_PARAM_LINK_CURRENT_STATE
2832 
2833 /** Packet Coherence protocol. This address is only valid with @ref netio_get().
2834  *  The value is nonzero if the interface is configured for cache-coherent DMA.
2835  */
2836 #define NETIO_PARAM_COHERENT 8
2837 
2838 /** Desired link state. The value is a conbination of "NETIO_LINK_xxx"
2839  *  flags, which specify the desired state for the link.  With @ref
2840  *  netio_set(), this will, in the background, attempt to bring up the link
2841  *  using whichever of the requested flags are reasonable, or take down the
2842  *  link if the flags are zero.  The actual link up or down operation may
2843  *  happen after this call completes.  If the link state changes in the
2844  *  future, the system will continue to try to get back to the desired link
2845  *  state; for instance, if the link is brought up successfully, and then
2846  *  the network cable is disconnected, the link will go down.  However, the
2847  *  desired state of the link is still up, so if the cable is reconnected,
2848  *  the link will be brought up again.
2849  *
2850  *  With @ref netio_get(), this will indicate the desired state for the
2851  *  link, as set with a previous netio_set() call, or implicitly by a
2852  *  netio_input_register() or netio_input_unregister() operation.  This may
2853  *  not reflect the current state of the link; to get that, use
2854  *  ::NETIO_PARAM_LINK_CURRENT_STATE. */
2855 #define NETIO_PARAM_LINK_DESIRED_STATE 9
2856 
2857 /** NetIO statistics structure.  Retrieved using the ::NETIO_PARAM_STAT
2858  *  address passed to @ref netio_get(). */
2859 typedef struct
2860 {
2861   /** Number of packets which have been received by the IPP and forwarded
2862    *  to a tile's receive queue for processing.  This value wraps at its
2863    *  maximum, and is not cleared upon read. */
2864   uint32_t packets_received;
2865 
2866   /** Number of packets which have been dropped by the IPP, because they could
2867    *  not be received, or could not be forwarded to a tile.  The former happens
2868    *  when the IPP does not have a free packet buffer of suitable size for an
2869    *  incoming frame.  The latter happens when all potential destination tiles
2870    *  for a packet, as defined by the group, bucket, and queue configuration,
2871    *  have full receive queues.   This value wraps at its maximum, and is not
2872    *  cleared upon read. */
2873   uint32_t packets_dropped;
2874 
2875   /*
2876    * Note: the #defines after each of the following four one-byte values
2877    * denote their location within the third word of the netio_stat_t.  They
2878    * are intended for use only by the IPP implementation and are thus omitted
2879    * from the Doxygen output.
2880    */
2881 
2882   /** Number of packets dropped because no worker was able to accept a new
2883    *  packet.  This value saturates at its maximum, and is cleared upon
2884    *  read. */
2885   uint8_t drops_no_worker;
2886 #ifndef __DOXYGEN__
2887 #define NETIO_STAT_DROPS_NO_WORKER   0
2888 #endif
2889 
2890   /** Number of packets dropped because no small buffers were available.
2891    *  This value saturates at its maximum, and is cleared upon read. */
2892   uint8_t drops_no_smallbuf;
2893 #ifndef __DOXYGEN__
2894 #define NETIO_STAT_DROPS_NO_SMALLBUF 1
2895 #endif
2896 
2897   /** Number of packets dropped because no large buffers were available.
2898    *  This value saturates at its maximum, and is cleared upon read. */
2899   uint8_t drops_no_largebuf;
2900 #ifndef __DOXYGEN__
2901 #define NETIO_STAT_DROPS_NO_LARGEBUF 2
2902 #endif
2903 
2904   /** Number of packets dropped because no jumbo buffers were available.
2905    *  This value saturates at its maximum, and is cleared upon read. */
2906   uint8_t drops_no_jumbobuf;
2907 #ifndef __DOXYGEN__
2908 #define NETIO_STAT_DROPS_NO_JUMBOBUF 3
2909 #endif
2910 }
2911 netio_stat_t;
2912 
2913 
2914 /** Link can run, should run, or is running at 10 Mbps. */
2915 #define NETIO_LINK_10M         0x01
2916 
2917 /** Link can run, should run, or is running at 100 Mbps. */
2918 #define NETIO_LINK_100M        0x02
2919 
2920 /** Link can run, should run, or is running at 1 Gbps. */
2921 #define NETIO_LINK_1G          0x04
2922 
2923 /** Link can run, should run, or is running at 10 Gbps. */
2924 #define NETIO_LINK_10G         0x08
2925 
2926 /** Link should run at the highest speed supported by the link and by
2927  *  the device connected to the link.  Only usable as a value for
2928  *  the link's desired state; never returned as a value for the current
2929  *  or possible states. */
2930 #define NETIO_LINK_ANYSPEED    0x10
2931 
2932 /** All legal link speeds. */
2933 #define NETIO_LINK_SPEED  (NETIO_LINK_10M  | \
2934                            NETIO_LINK_100M | \
2935                            NETIO_LINK_1G   | \
2936                            NETIO_LINK_10G  | \
2937                            NETIO_LINK_ANYSPEED)
2938 
2939 
2940 /** MAC register class.  Addr is a register offset within the MAC.
2941  *  Registers within the XGbE and GbE MACs are documented in the Tile
2942  *  Processor I/O Device Guide (UG104). MAC registers start at address
2943  *  0x4000, and do not include the MAC_INTERFACE registers. */
2944 #define NETIO_MAC             1
2945 
2946 /** MDIO register class (IEEE 802.3 clause 22 format).  Addr is the "addr"
2947  *  member of a netio_mdio_addr_t structure. */
2948 #define NETIO_MDIO            2
2949 
2950 /** MDIO register class (IEEE 802.3 clause 45 format).  Addr is the "addr"
2951  *  member of a netio_mdio_addr_t structure. */
2952 #define NETIO_MDIO_CLAUSE45   3
2953 
2954 /** NetIO MDIO address type.  Retrieved or provided using the ::NETIO_MDIO
2955  *  address passed to @ref netio_get() or @ref netio_set(). */
2956 typedef union
2957 {
2958   struct
2959   {
2960     unsigned int reg:16;  /**< MDIO register offset.  For clause 22 access,
2961                                must be less than 32. */
2962     unsigned int phy:5;   /**< Which MDIO PHY to access. */
2963     unsigned int dev:5;   /**< Which MDIO device to access within that PHY.
2964                                Applicable for clause 45 access only; ignored
2965                                for clause 22 access. */
2966   }
2967   bits;                   /**< Container for bitfields. */
2968   uint64_t addr;          /**< Value to pass to @ref netio_get() or
2969                            *   @ref netio_set(). */
2970 }
2971 netio_mdio_addr_t;
2972 
2973 /** @} */
2974 
2975 #endif /* __NETIO_INTF_H__ */
2976