netio_intf.h 102 KB

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