cma.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452
  1. /*
  2. * Copyright (c) 2005 Voltaire Inc. All rights reserved.
  3. * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
  4. * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
  5. * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/completion.h>
  36. #include <linux/in.h>
  37. #include <linux/in6.h>
  38. #include <linux/mutex.h>
  39. #include <linux/random.h>
  40. #include <linux/igmp.h>
  41. #include <linux/idr.h>
  42. #include <linux/inetdevice.h>
  43. #include <linux/slab.h>
  44. #include <linux/module.h>
  45. #include <net/route.h>
  46. #include <net/net_namespace.h>
  47. #include <net/netns/generic.h>
  48. #include <net/tcp.h>
  49. #include <net/ipv6.h>
  50. #include <net/ip_fib.h>
  51. #include <net/ip6_route.h>
  52. #include <rdma/rdma_cm.h>
  53. #include <rdma/rdma_cm_ib.h>
  54. #include <rdma/rdma_netlink.h>
  55. #include <rdma/ib.h>
  56. #include <rdma/ib_cache.h>
  57. #include <rdma/ib_cm.h>
  58. #include <rdma/ib_sa.h>
  59. #include <rdma/iw_cm.h>
  60. #include "core_priv.h"
  61. MODULE_AUTHOR("Sean Hefty");
  62. MODULE_DESCRIPTION("Generic RDMA CM Agent");
  63. MODULE_LICENSE("Dual BSD/GPL");
  64. #define CMA_CM_RESPONSE_TIMEOUT 20
  65. #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
  66. #define CMA_MAX_CM_RETRIES 15
  67. #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
  68. #define CMA_IBOE_PACKET_LIFETIME 18
  69. static const char * const cma_events[] = {
  70. [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
  71. [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
  72. [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
  73. [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
  74. [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
  75. [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
  76. [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
  77. [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
  78. [RDMA_CM_EVENT_REJECTED] = "rejected",
  79. [RDMA_CM_EVENT_ESTABLISHED] = "established",
  80. [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
  81. [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
  82. [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
  83. [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
  84. [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
  85. [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
  86. };
  87. const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
  88. {
  89. size_t index = event;
  90. return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
  91. cma_events[index] : "unrecognized event";
  92. }
  93. EXPORT_SYMBOL(rdma_event_msg);
  94. static void cma_add_one(struct ib_device *device);
  95. static void cma_remove_one(struct ib_device *device, void *client_data);
  96. static struct ib_client cma_client = {
  97. .name = "cma",
  98. .add = cma_add_one,
  99. .remove = cma_remove_one
  100. };
  101. static struct ib_sa_client sa_client;
  102. static struct rdma_addr_client addr_client;
  103. static LIST_HEAD(dev_list);
  104. static LIST_HEAD(listen_any_list);
  105. static DEFINE_MUTEX(lock);
  106. static struct workqueue_struct *cma_wq;
  107. static int cma_pernet_id;
  108. struct cma_pernet {
  109. struct idr tcp_ps;
  110. struct idr udp_ps;
  111. struct idr ipoib_ps;
  112. struct idr ib_ps;
  113. };
  114. static struct cma_pernet *cma_pernet(struct net *net)
  115. {
  116. return net_generic(net, cma_pernet_id);
  117. }
  118. static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps)
  119. {
  120. struct cma_pernet *pernet = cma_pernet(net);
  121. switch (ps) {
  122. case RDMA_PS_TCP:
  123. return &pernet->tcp_ps;
  124. case RDMA_PS_UDP:
  125. return &pernet->udp_ps;
  126. case RDMA_PS_IPOIB:
  127. return &pernet->ipoib_ps;
  128. case RDMA_PS_IB:
  129. return &pernet->ib_ps;
  130. default:
  131. return NULL;
  132. }
  133. }
  134. struct cma_device {
  135. struct list_head list;
  136. struct ib_device *device;
  137. struct completion comp;
  138. atomic_t refcount;
  139. struct list_head id_list;
  140. enum ib_gid_type *default_gid_type;
  141. };
  142. struct rdma_bind_list {
  143. enum rdma_port_space ps;
  144. struct hlist_head owners;
  145. unsigned short port;
  146. };
  147. struct class_port_info_context {
  148. struct ib_class_port_info *class_port_info;
  149. struct ib_device *device;
  150. struct completion done;
  151. struct ib_sa_query *sa_query;
  152. u8 port_num;
  153. };
  154. static int cma_ps_alloc(struct net *net, enum rdma_port_space ps,
  155. struct rdma_bind_list *bind_list, int snum)
  156. {
  157. struct idr *idr = cma_pernet_idr(net, ps);
  158. return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
  159. }
  160. static struct rdma_bind_list *cma_ps_find(struct net *net,
  161. enum rdma_port_space ps, int snum)
  162. {
  163. struct idr *idr = cma_pernet_idr(net, ps);
  164. return idr_find(idr, snum);
  165. }
  166. static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum)
  167. {
  168. struct idr *idr = cma_pernet_idr(net, ps);
  169. idr_remove(idr, snum);
  170. }
  171. enum {
  172. CMA_OPTION_AFONLY,
  173. };
  174. void cma_ref_dev(struct cma_device *cma_dev)
  175. {
  176. atomic_inc(&cma_dev->refcount);
  177. }
  178. struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
  179. void *cookie)
  180. {
  181. struct cma_device *cma_dev;
  182. struct cma_device *found_cma_dev = NULL;
  183. mutex_lock(&lock);
  184. list_for_each_entry(cma_dev, &dev_list, list)
  185. if (filter(cma_dev->device, cookie)) {
  186. found_cma_dev = cma_dev;
  187. break;
  188. }
  189. if (found_cma_dev)
  190. cma_ref_dev(found_cma_dev);
  191. mutex_unlock(&lock);
  192. return found_cma_dev;
  193. }
  194. int cma_get_default_gid_type(struct cma_device *cma_dev,
  195. unsigned int port)
  196. {
  197. if (port < rdma_start_port(cma_dev->device) ||
  198. port > rdma_end_port(cma_dev->device))
  199. return -EINVAL;
  200. return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
  201. }
  202. int cma_set_default_gid_type(struct cma_device *cma_dev,
  203. unsigned int port,
  204. enum ib_gid_type default_gid_type)
  205. {
  206. unsigned long supported_gids;
  207. if (port < rdma_start_port(cma_dev->device) ||
  208. port > rdma_end_port(cma_dev->device))
  209. return -EINVAL;
  210. supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
  211. if (!(supported_gids & 1 << default_gid_type))
  212. return -EINVAL;
  213. cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
  214. default_gid_type;
  215. return 0;
  216. }
  217. struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
  218. {
  219. return cma_dev->device;
  220. }
  221. /*
  222. * Device removal can occur at anytime, so we need extra handling to
  223. * serialize notifying the user of device removal with other callbacks.
  224. * We do this by disabling removal notification while a callback is in process,
  225. * and reporting it after the callback completes.
  226. */
  227. struct rdma_id_private {
  228. struct rdma_cm_id id;
  229. struct rdma_bind_list *bind_list;
  230. struct hlist_node node;
  231. struct list_head list; /* listen_any_list or cma_device.list */
  232. struct list_head listen_list; /* per device listens */
  233. struct cma_device *cma_dev;
  234. struct list_head mc_list;
  235. int internal_id;
  236. enum rdma_cm_state state;
  237. spinlock_t lock;
  238. struct mutex qp_mutex;
  239. struct completion comp;
  240. atomic_t refcount;
  241. struct mutex handler_mutex;
  242. int backlog;
  243. int timeout_ms;
  244. struct ib_sa_query *query;
  245. int query_id;
  246. union {
  247. struct ib_cm_id *ib;
  248. struct iw_cm_id *iw;
  249. } cm_id;
  250. u32 seq_num;
  251. u32 qkey;
  252. u32 qp_num;
  253. pid_t owner;
  254. u32 options;
  255. u8 srq;
  256. u8 tos;
  257. u8 reuseaddr;
  258. u8 afonly;
  259. enum ib_gid_type gid_type;
  260. };
  261. struct cma_multicast {
  262. struct rdma_id_private *id_priv;
  263. union {
  264. struct ib_sa_multicast *ib;
  265. } multicast;
  266. struct list_head list;
  267. void *context;
  268. struct sockaddr_storage addr;
  269. struct kref mcref;
  270. bool igmp_joined;
  271. u8 join_state;
  272. };
  273. struct cma_work {
  274. struct work_struct work;
  275. struct rdma_id_private *id;
  276. enum rdma_cm_state old_state;
  277. enum rdma_cm_state new_state;
  278. struct rdma_cm_event event;
  279. };
  280. struct cma_ndev_work {
  281. struct work_struct work;
  282. struct rdma_id_private *id;
  283. struct rdma_cm_event event;
  284. };
  285. struct iboe_mcast_work {
  286. struct work_struct work;
  287. struct rdma_id_private *id;
  288. struct cma_multicast *mc;
  289. };
  290. union cma_ip_addr {
  291. struct in6_addr ip6;
  292. struct {
  293. __be32 pad[3];
  294. __be32 addr;
  295. } ip4;
  296. };
  297. struct cma_hdr {
  298. u8 cma_version;
  299. u8 ip_version; /* IP version: 7:4 */
  300. __be16 port;
  301. union cma_ip_addr src_addr;
  302. union cma_ip_addr dst_addr;
  303. };
  304. #define CMA_VERSION 0x00
  305. struct cma_req_info {
  306. struct ib_device *device;
  307. int port;
  308. union ib_gid local_gid;
  309. __be64 service_id;
  310. u16 pkey;
  311. bool has_gid:1;
  312. };
  313. static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
  314. {
  315. unsigned long flags;
  316. int ret;
  317. spin_lock_irqsave(&id_priv->lock, flags);
  318. ret = (id_priv->state == comp);
  319. spin_unlock_irqrestore(&id_priv->lock, flags);
  320. return ret;
  321. }
  322. static int cma_comp_exch(struct rdma_id_private *id_priv,
  323. enum rdma_cm_state comp, enum rdma_cm_state exch)
  324. {
  325. unsigned long flags;
  326. int ret;
  327. spin_lock_irqsave(&id_priv->lock, flags);
  328. if ((ret = (id_priv->state == comp)))
  329. id_priv->state = exch;
  330. spin_unlock_irqrestore(&id_priv->lock, flags);
  331. return ret;
  332. }
  333. static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
  334. enum rdma_cm_state exch)
  335. {
  336. unsigned long flags;
  337. enum rdma_cm_state old;
  338. spin_lock_irqsave(&id_priv->lock, flags);
  339. old = id_priv->state;
  340. id_priv->state = exch;
  341. spin_unlock_irqrestore(&id_priv->lock, flags);
  342. return old;
  343. }
  344. static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
  345. {
  346. return hdr->ip_version >> 4;
  347. }
  348. static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
  349. {
  350. hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
  351. }
  352. static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
  353. {
  354. struct in_device *in_dev = NULL;
  355. if (ndev) {
  356. rtnl_lock();
  357. in_dev = __in_dev_get_rtnl(ndev);
  358. if (in_dev) {
  359. if (join)
  360. ip_mc_inc_group(in_dev,
  361. *(__be32 *)(mgid->raw + 12));
  362. else
  363. ip_mc_dec_group(in_dev,
  364. *(__be32 *)(mgid->raw + 12));
  365. }
  366. rtnl_unlock();
  367. }
  368. return (in_dev) ? 0 : -ENODEV;
  369. }
  370. static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
  371. struct cma_device *cma_dev)
  372. {
  373. cma_ref_dev(cma_dev);
  374. id_priv->cma_dev = cma_dev;
  375. id_priv->gid_type = 0;
  376. id_priv->id.device = cma_dev->device;
  377. id_priv->id.route.addr.dev_addr.transport =
  378. rdma_node_get_transport(cma_dev->device->node_type);
  379. list_add_tail(&id_priv->list, &cma_dev->id_list);
  380. }
  381. static void cma_attach_to_dev(struct rdma_id_private *id_priv,
  382. struct cma_device *cma_dev)
  383. {
  384. _cma_attach_to_dev(id_priv, cma_dev);
  385. id_priv->gid_type =
  386. cma_dev->default_gid_type[id_priv->id.port_num -
  387. rdma_start_port(cma_dev->device)];
  388. }
  389. void cma_deref_dev(struct cma_device *cma_dev)
  390. {
  391. if (atomic_dec_and_test(&cma_dev->refcount))
  392. complete(&cma_dev->comp);
  393. }
  394. static inline void release_mc(struct kref *kref)
  395. {
  396. struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
  397. kfree(mc->multicast.ib);
  398. kfree(mc);
  399. }
  400. static void cma_release_dev(struct rdma_id_private *id_priv)
  401. {
  402. mutex_lock(&lock);
  403. list_del(&id_priv->list);
  404. cma_deref_dev(id_priv->cma_dev);
  405. id_priv->cma_dev = NULL;
  406. mutex_unlock(&lock);
  407. }
  408. static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
  409. {
  410. return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
  411. }
  412. static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
  413. {
  414. return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
  415. }
  416. static inline unsigned short cma_family(struct rdma_id_private *id_priv)
  417. {
  418. return id_priv->id.route.addr.src_addr.ss_family;
  419. }
  420. static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
  421. {
  422. struct ib_sa_mcmember_rec rec;
  423. int ret = 0;
  424. if (id_priv->qkey) {
  425. if (qkey && id_priv->qkey != qkey)
  426. return -EINVAL;
  427. return 0;
  428. }
  429. if (qkey) {
  430. id_priv->qkey = qkey;
  431. return 0;
  432. }
  433. switch (id_priv->id.ps) {
  434. case RDMA_PS_UDP:
  435. case RDMA_PS_IB:
  436. id_priv->qkey = RDMA_UDP_QKEY;
  437. break;
  438. case RDMA_PS_IPOIB:
  439. ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
  440. ret = ib_sa_get_mcmember_rec(id_priv->id.device,
  441. id_priv->id.port_num, &rec.mgid,
  442. &rec);
  443. if (!ret)
  444. id_priv->qkey = be32_to_cpu(rec.qkey);
  445. break;
  446. default:
  447. break;
  448. }
  449. return ret;
  450. }
  451. static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
  452. {
  453. dev_addr->dev_type = ARPHRD_INFINIBAND;
  454. rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
  455. ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
  456. }
  457. static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
  458. {
  459. int ret;
  460. if (addr->sa_family != AF_IB) {
  461. ret = rdma_translate_ip(addr, dev_addr, NULL);
  462. } else {
  463. cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
  464. ret = 0;
  465. }
  466. return ret;
  467. }
  468. static inline int cma_validate_port(struct ib_device *device, u8 port,
  469. enum ib_gid_type gid_type,
  470. union ib_gid *gid, int dev_type,
  471. int bound_if_index)
  472. {
  473. int ret = -ENODEV;
  474. struct net_device *ndev = NULL;
  475. if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
  476. return ret;
  477. if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
  478. return ret;
  479. if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
  480. ndev = dev_get_by_index(&init_net, bound_if_index);
  481. if (ndev && ndev->flags & IFF_LOOPBACK) {
  482. pr_info("detected loopback device\n");
  483. dev_put(ndev);
  484. if (!device->get_netdev)
  485. return -EOPNOTSUPP;
  486. ndev = device->get_netdev(device, port);
  487. if (!ndev)
  488. return -ENODEV;
  489. }
  490. } else {
  491. gid_type = IB_GID_TYPE_IB;
  492. }
  493. ret = ib_find_cached_gid_by_port(device, gid, gid_type, port,
  494. ndev, NULL);
  495. if (ndev)
  496. dev_put(ndev);
  497. return ret;
  498. }
  499. static int cma_acquire_dev(struct rdma_id_private *id_priv,
  500. struct rdma_id_private *listen_id_priv)
  501. {
  502. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  503. struct cma_device *cma_dev;
  504. union ib_gid gid, iboe_gid, *gidp;
  505. int ret = -ENODEV;
  506. u8 port;
  507. if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
  508. id_priv->id.ps == RDMA_PS_IPOIB)
  509. return -EINVAL;
  510. mutex_lock(&lock);
  511. rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
  512. &iboe_gid);
  513. memcpy(&gid, dev_addr->src_dev_addr +
  514. rdma_addr_gid_offset(dev_addr), sizeof gid);
  515. if (listen_id_priv) {
  516. cma_dev = listen_id_priv->cma_dev;
  517. port = listen_id_priv->id.port_num;
  518. gidp = rdma_protocol_roce(cma_dev->device, port) ?
  519. &iboe_gid : &gid;
  520. ret = cma_validate_port(cma_dev->device, port,
  521. rdma_protocol_ib(cma_dev->device, port) ?
  522. IB_GID_TYPE_IB :
  523. listen_id_priv->gid_type, gidp,
  524. dev_addr->dev_type,
  525. dev_addr->bound_dev_if);
  526. if (!ret) {
  527. id_priv->id.port_num = port;
  528. goto out;
  529. }
  530. }
  531. list_for_each_entry(cma_dev, &dev_list, list) {
  532. for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
  533. if (listen_id_priv &&
  534. listen_id_priv->cma_dev == cma_dev &&
  535. listen_id_priv->id.port_num == port)
  536. continue;
  537. gidp = rdma_protocol_roce(cma_dev->device, port) ?
  538. &iboe_gid : &gid;
  539. ret = cma_validate_port(cma_dev->device, port,
  540. rdma_protocol_ib(cma_dev->device, port) ?
  541. IB_GID_TYPE_IB :
  542. cma_dev->default_gid_type[port - 1],
  543. gidp, dev_addr->dev_type,
  544. dev_addr->bound_dev_if);
  545. if (!ret) {
  546. id_priv->id.port_num = port;
  547. goto out;
  548. }
  549. }
  550. }
  551. out:
  552. if (!ret)
  553. cma_attach_to_dev(id_priv, cma_dev);
  554. mutex_unlock(&lock);
  555. return ret;
  556. }
  557. /*
  558. * Select the source IB device and address to reach the destination IB address.
  559. */
  560. static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
  561. {
  562. struct cma_device *cma_dev, *cur_dev;
  563. struct sockaddr_ib *addr;
  564. union ib_gid gid, sgid, *dgid;
  565. u16 pkey, index;
  566. u8 p;
  567. int i;
  568. cma_dev = NULL;
  569. addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
  570. dgid = (union ib_gid *) &addr->sib_addr;
  571. pkey = ntohs(addr->sib_pkey);
  572. list_for_each_entry(cur_dev, &dev_list, list) {
  573. for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
  574. if (!rdma_cap_af_ib(cur_dev->device, p))
  575. continue;
  576. if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
  577. continue;
  578. for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i,
  579. &gid, NULL);
  580. i++) {
  581. if (!memcmp(&gid, dgid, sizeof(gid))) {
  582. cma_dev = cur_dev;
  583. sgid = gid;
  584. id_priv->id.port_num = p;
  585. goto found;
  586. }
  587. if (!cma_dev && (gid.global.subnet_prefix ==
  588. dgid->global.subnet_prefix)) {
  589. cma_dev = cur_dev;
  590. sgid = gid;
  591. id_priv->id.port_num = p;
  592. }
  593. }
  594. }
  595. }
  596. if (!cma_dev)
  597. return -ENODEV;
  598. found:
  599. cma_attach_to_dev(id_priv, cma_dev);
  600. addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
  601. memcpy(&addr->sib_addr, &sgid, sizeof sgid);
  602. cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
  603. return 0;
  604. }
  605. static void cma_deref_id(struct rdma_id_private *id_priv)
  606. {
  607. if (atomic_dec_and_test(&id_priv->refcount))
  608. complete(&id_priv->comp);
  609. }
  610. struct rdma_cm_id *rdma_create_id(struct net *net,
  611. rdma_cm_event_handler event_handler,
  612. void *context, enum rdma_port_space ps,
  613. enum ib_qp_type qp_type)
  614. {
  615. struct rdma_id_private *id_priv;
  616. id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
  617. if (!id_priv)
  618. return ERR_PTR(-ENOMEM);
  619. id_priv->owner = task_pid_nr(current);
  620. id_priv->state = RDMA_CM_IDLE;
  621. id_priv->id.context = context;
  622. id_priv->id.event_handler = event_handler;
  623. id_priv->id.ps = ps;
  624. id_priv->id.qp_type = qp_type;
  625. spin_lock_init(&id_priv->lock);
  626. mutex_init(&id_priv->qp_mutex);
  627. init_completion(&id_priv->comp);
  628. atomic_set(&id_priv->refcount, 1);
  629. mutex_init(&id_priv->handler_mutex);
  630. INIT_LIST_HEAD(&id_priv->listen_list);
  631. INIT_LIST_HEAD(&id_priv->mc_list);
  632. get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
  633. id_priv->id.route.addr.dev_addr.net = get_net(net);
  634. id_priv->seq_num &= 0x00ffffff;
  635. return &id_priv->id;
  636. }
  637. EXPORT_SYMBOL(rdma_create_id);
  638. static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
  639. {
  640. struct ib_qp_attr qp_attr;
  641. int qp_attr_mask, ret;
  642. qp_attr.qp_state = IB_QPS_INIT;
  643. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  644. if (ret)
  645. return ret;
  646. ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
  647. if (ret)
  648. return ret;
  649. qp_attr.qp_state = IB_QPS_RTR;
  650. ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
  651. if (ret)
  652. return ret;
  653. qp_attr.qp_state = IB_QPS_RTS;
  654. qp_attr.sq_psn = 0;
  655. ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
  656. return ret;
  657. }
  658. static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
  659. {
  660. struct ib_qp_attr qp_attr;
  661. int qp_attr_mask, ret;
  662. qp_attr.qp_state = IB_QPS_INIT;
  663. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  664. if (ret)
  665. return ret;
  666. return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
  667. }
  668. int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
  669. struct ib_qp_init_attr *qp_init_attr)
  670. {
  671. struct rdma_id_private *id_priv;
  672. struct ib_qp *qp;
  673. int ret;
  674. id_priv = container_of(id, struct rdma_id_private, id);
  675. if (id->device != pd->device)
  676. return -EINVAL;
  677. qp_init_attr->port_num = id->port_num;
  678. qp = ib_create_qp(pd, qp_init_attr);
  679. if (IS_ERR(qp))
  680. return PTR_ERR(qp);
  681. if (id->qp_type == IB_QPT_UD)
  682. ret = cma_init_ud_qp(id_priv, qp);
  683. else
  684. ret = cma_init_conn_qp(id_priv, qp);
  685. if (ret)
  686. goto err;
  687. id->qp = qp;
  688. id_priv->qp_num = qp->qp_num;
  689. id_priv->srq = (qp->srq != NULL);
  690. return 0;
  691. err:
  692. ib_destroy_qp(qp);
  693. return ret;
  694. }
  695. EXPORT_SYMBOL(rdma_create_qp);
  696. void rdma_destroy_qp(struct rdma_cm_id *id)
  697. {
  698. struct rdma_id_private *id_priv;
  699. id_priv = container_of(id, struct rdma_id_private, id);
  700. mutex_lock(&id_priv->qp_mutex);
  701. ib_destroy_qp(id_priv->id.qp);
  702. id_priv->id.qp = NULL;
  703. mutex_unlock(&id_priv->qp_mutex);
  704. }
  705. EXPORT_SYMBOL(rdma_destroy_qp);
  706. static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
  707. struct rdma_conn_param *conn_param)
  708. {
  709. struct ib_qp_attr qp_attr;
  710. int qp_attr_mask, ret;
  711. union ib_gid sgid;
  712. mutex_lock(&id_priv->qp_mutex);
  713. if (!id_priv->id.qp) {
  714. ret = 0;
  715. goto out;
  716. }
  717. /* Need to update QP attributes from default values. */
  718. qp_attr.qp_state = IB_QPS_INIT;
  719. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  720. if (ret)
  721. goto out;
  722. ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
  723. if (ret)
  724. goto out;
  725. qp_attr.qp_state = IB_QPS_RTR;
  726. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  727. if (ret)
  728. goto out;
  729. ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
  730. qp_attr.ah_attr.grh.sgid_index, &sgid, NULL);
  731. if (ret)
  732. goto out;
  733. BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
  734. if (conn_param)
  735. qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
  736. ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
  737. out:
  738. mutex_unlock(&id_priv->qp_mutex);
  739. return ret;
  740. }
  741. static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
  742. struct rdma_conn_param *conn_param)
  743. {
  744. struct ib_qp_attr qp_attr;
  745. int qp_attr_mask, ret;
  746. mutex_lock(&id_priv->qp_mutex);
  747. if (!id_priv->id.qp) {
  748. ret = 0;
  749. goto out;
  750. }
  751. qp_attr.qp_state = IB_QPS_RTS;
  752. ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
  753. if (ret)
  754. goto out;
  755. if (conn_param)
  756. qp_attr.max_rd_atomic = conn_param->initiator_depth;
  757. ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
  758. out:
  759. mutex_unlock(&id_priv->qp_mutex);
  760. return ret;
  761. }
  762. static int cma_modify_qp_err(struct rdma_id_private *id_priv)
  763. {
  764. struct ib_qp_attr qp_attr;
  765. int ret;
  766. mutex_lock(&id_priv->qp_mutex);
  767. if (!id_priv->id.qp) {
  768. ret = 0;
  769. goto out;
  770. }
  771. qp_attr.qp_state = IB_QPS_ERR;
  772. ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
  773. out:
  774. mutex_unlock(&id_priv->qp_mutex);
  775. return ret;
  776. }
  777. static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
  778. struct ib_qp_attr *qp_attr, int *qp_attr_mask)
  779. {
  780. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  781. int ret;
  782. u16 pkey;
  783. if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
  784. pkey = 0xffff;
  785. else
  786. pkey = ib_addr_get_pkey(dev_addr);
  787. ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
  788. pkey, &qp_attr->pkey_index);
  789. if (ret)
  790. return ret;
  791. qp_attr->port_num = id_priv->id.port_num;
  792. *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
  793. if (id_priv->id.qp_type == IB_QPT_UD) {
  794. ret = cma_set_qkey(id_priv, 0);
  795. if (ret)
  796. return ret;
  797. qp_attr->qkey = id_priv->qkey;
  798. *qp_attr_mask |= IB_QP_QKEY;
  799. } else {
  800. qp_attr->qp_access_flags = 0;
  801. *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
  802. }
  803. return 0;
  804. }
  805. int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
  806. int *qp_attr_mask)
  807. {
  808. struct rdma_id_private *id_priv;
  809. int ret = 0;
  810. id_priv = container_of(id, struct rdma_id_private, id);
  811. if (rdma_cap_ib_cm(id->device, id->port_num)) {
  812. if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
  813. ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
  814. else
  815. ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
  816. qp_attr_mask);
  817. if (qp_attr->qp_state == IB_QPS_RTR)
  818. qp_attr->rq_psn = id_priv->seq_num;
  819. } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
  820. if (!id_priv->cm_id.iw) {
  821. qp_attr->qp_access_flags = 0;
  822. *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
  823. } else
  824. ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
  825. qp_attr_mask);
  826. qp_attr->port_num = id_priv->id.port_num;
  827. *qp_attr_mask |= IB_QP_PORT;
  828. } else
  829. ret = -ENOSYS;
  830. return ret;
  831. }
  832. EXPORT_SYMBOL(rdma_init_qp_attr);
  833. static inline int cma_zero_addr(struct sockaddr *addr)
  834. {
  835. switch (addr->sa_family) {
  836. case AF_INET:
  837. return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
  838. case AF_INET6:
  839. return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
  840. case AF_IB:
  841. return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
  842. default:
  843. return 0;
  844. }
  845. }
  846. static inline int cma_loopback_addr(struct sockaddr *addr)
  847. {
  848. switch (addr->sa_family) {
  849. case AF_INET:
  850. return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
  851. case AF_INET6:
  852. return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
  853. case AF_IB:
  854. return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
  855. default:
  856. return 0;
  857. }
  858. }
  859. static inline int cma_any_addr(struct sockaddr *addr)
  860. {
  861. return cma_zero_addr(addr) || cma_loopback_addr(addr);
  862. }
  863. static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
  864. {
  865. if (src->sa_family != dst->sa_family)
  866. return -1;
  867. switch (src->sa_family) {
  868. case AF_INET:
  869. return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
  870. ((struct sockaddr_in *) dst)->sin_addr.s_addr;
  871. case AF_INET6:
  872. return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
  873. &((struct sockaddr_in6 *) dst)->sin6_addr);
  874. default:
  875. return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
  876. &((struct sockaddr_ib *) dst)->sib_addr);
  877. }
  878. }
  879. static __be16 cma_port(struct sockaddr *addr)
  880. {
  881. struct sockaddr_ib *sib;
  882. switch (addr->sa_family) {
  883. case AF_INET:
  884. return ((struct sockaddr_in *) addr)->sin_port;
  885. case AF_INET6:
  886. return ((struct sockaddr_in6 *) addr)->sin6_port;
  887. case AF_IB:
  888. sib = (struct sockaddr_ib *) addr;
  889. return htons((u16) (be64_to_cpu(sib->sib_sid) &
  890. be64_to_cpu(sib->sib_sid_mask)));
  891. default:
  892. return 0;
  893. }
  894. }
  895. static inline int cma_any_port(struct sockaddr *addr)
  896. {
  897. return !cma_port(addr);
  898. }
  899. static void cma_save_ib_info(struct sockaddr *src_addr,
  900. struct sockaddr *dst_addr,
  901. struct rdma_cm_id *listen_id,
  902. struct ib_sa_path_rec *path)
  903. {
  904. struct sockaddr_ib *listen_ib, *ib;
  905. listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
  906. if (src_addr) {
  907. ib = (struct sockaddr_ib *)src_addr;
  908. ib->sib_family = AF_IB;
  909. if (path) {
  910. ib->sib_pkey = path->pkey;
  911. ib->sib_flowinfo = path->flow_label;
  912. memcpy(&ib->sib_addr, &path->sgid, 16);
  913. ib->sib_sid = path->service_id;
  914. ib->sib_scope_id = 0;
  915. } else {
  916. ib->sib_pkey = listen_ib->sib_pkey;
  917. ib->sib_flowinfo = listen_ib->sib_flowinfo;
  918. ib->sib_addr = listen_ib->sib_addr;
  919. ib->sib_sid = listen_ib->sib_sid;
  920. ib->sib_scope_id = listen_ib->sib_scope_id;
  921. }
  922. ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
  923. }
  924. if (dst_addr) {
  925. ib = (struct sockaddr_ib *)dst_addr;
  926. ib->sib_family = AF_IB;
  927. if (path) {
  928. ib->sib_pkey = path->pkey;
  929. ib->sib_flowinfo = path->flow_label;
  930. memcpy(&ib->sib_addr, &path->dgid, 16);
  931. }
  932. }
  933. }
  934. static void cma_save_ip4_info(struct sockaddr_in *src_addr,
  935. struct sockaddr_in *dst_addr,
  936. struct cma_hdr *hdr,
  937. __be16 local_port)
  938. {
  939. if (src_addr) {
  940. *src_addr = (struct sockaddr_in) {
  941. .sin_family = AF_INET,
  942. .sin_addr.s_addr = hdr->dst_addr.ip4.addr,
  943. .sin_port = local_port,
  944. };
  945. }
  946. if (dst_addr) {
  947. *dst_addr = (struct sockaddr_in) {
  948. .sin_family = AF_INET,
  949. .sin_addr.s_addr = hdr->src_addr.ip4.addr,
  950. .sin_port = hdr->port,
  951. };
  952. }
  953. }
  954. static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
  955. struct sockaddr_in6 *dst_addr,
  956. struct cma_hdr *hdr,
  957. __be16 local_port)
  958. {
  959. if (src_addr) {
  960. *src_addr = (struct sockaddr_in6) {
  961. .sin6_family = AF_INET6,
  962. .sin6_addr = hdr->dst_addr.ip6,
  963. .sin6_port = local_port,
  964. };
  965. }
  966. if (dst_addr) {
  967. *dst_addr = (struct sockaddr_in6) {
  968. .sin6_family = AF_INET6,
  969. .sin6_addr = hdr->src_addr.ip6,
  970. .sin6_port = hdr->port,
  971. };
  972. }
  973. }
  974. static u16 cma_port_from_service_id(__be64 service_id)
  975. {
  976. return (u16)be64_to_cpu(service_id);
  977. }
  978. static int cma_save_ip_info(struct sockaddr *src_addr,
  979. struct sockaddr *dst_addr,
  980. struct ib_cm_event *ib_event,
  981. __be64 service_id)
  982. {
  983. struct cma_hdr *hdr;
  984. __be16 port;
  985. hdr = ib_event->private_data;
  986. if (hdr->cma_version != CMA_VERSION)
  987. return -EINVAL;
  988. port = htons(cma_port_from_service_id(service_id));
  989. switch (cma_get_ip_ver(hdr)) {
  990. case 4:
  991. cma_save_ip4_info((struct sockaddr_in *)src_addr,
  992. (struct sockaddr_in *)dst_addr, hdr, port);
  993. break;
  994. case 6:
  995. cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
  996. (struct sockaddr_in6 *)dst_addr, hdr, port);
  997. break;
  998. default:
  999. return -EAFNOSUPPORT;
  1000. }
  1001. return 0;
  1002. }
  1003. static int cma_save_net_info(struct sockaddr *src_addr,
  1004. struct sockaddr *dst_addr,
  1005. struct rdma_cm_id *listen_id,
  1006. struct ib_cm_event *ib_event,
  1007. sa_family_t sa_family, __be64 service_id)
  1008. {
  1009. if (sa_family == AF_IB) {
  1010. if (ib_event->event == IB_CM_REQ_RECEIVED)
  1011. cma_save_ib_info(src_addr, dst_addr, listen_id,
  1012. ib_event->param.req_rcvd.primary_path);
  1013. else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
  1014. cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
  1015. return 0;
  1016. }
  1017. return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
  1018. }
  1019. static int cma_save_req_info(const struct ib_cm_event *ib_event,
  1020. struct cma_req_info *req)
  1021. {
  1022. const struct ib_cm_req_event_param *req_param =
  1023. &ib_event->param.req_rcvd;
  1024. const struct ib_cm_sidr_req_event_param *sidr_param =
  1025. &ib_event->param.sidr_req_rcvd;
  1026. switch (ib_event->event) {
  1027. case IB_CM_REQ_RECEIVED:
  1028. req->device = req_param->listen_id->device;
  1029. req->port = req_param->port;
  1030. memcpy(&req->local_gid, &req_param->primary_path->sgid,
  1031. sizeof(req->local_gid));
  1032. req->has_gid = true;
  1033. req->service_id = req_param->primary_path->service_id;
  1034. req->pkey = be16_to_cpu(req_param->primary_path->pkey);
  1035. if (req->pkey != req_param->bth_pkey)
  1036. pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
  1037. "RDMA CMA: in the future this may cause the request to be dropped\n",
  1038. req_param->bth_pkey, req->pkey);
  1039. break;
  1040. case IB_CM_SIDR_REQ_RECEIVED:
  1041. req->device = sidr_param->listen_id->device;
  1042. req->port = sidr_param->port;
  1043. req->has_gid = false;
  1044. req->service_id = sidr_param->service_id;
  1045. req->pkey = sidr_param->pkey;
  1046. if (req->pkey != sidr_param->bth_pkey)
  1047. pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
  1048. "RDMA CMA: in the future this may cause the request to be dropped\n",
  1049. sidr_param->bth_pkey, req->pkey);
  1050. break;
  1051. default:
  1052. return -EINVAL;
  1053. }
  1054. return 0;
  1055. }
  1056. static bool validate_ipv4_net_dev(struct net_device *net_dev,
  1057. const struct sockaddr_in *dst_addr,
  1058. const struct sockaddr_in *src_addr)
  1059. {
  1060. __be32 daddr = dst_addr->sin_addr.s_addr,
  1061. saddr = src_addr->sin_addr.s_addr;
  1062. struct fib_result res;
  1063. struct flowi4 fl4;
  1064. int err;
  1065. bool ret;
  1066. if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
  1067. ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
  1068. ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
  1069. ipv4_is_loopback(saddr))
  1070. return false;
  1071. memset(&fl4, 0, sizeof(fl4));
  1072. fl4.flowi4_iif = net_dev->ifindex;
  1073. fl4.daddr = daddr;
  1074. fl4.saddr = saddr;
  1075. rcu_read_lock();
  1076. err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
  1077. ret = err == 0 && FIB_RES_DEV(res) == net_dev;
  1078. rcu_read_unlock();
  1079. return ret;
  1080. }
  1081. static bool validate_ipv6_net_dev(struct net_device *net_dev,
  1082. const struct sockaddr_in6 *dst_addr,
  1083. const struct sockaddr_in6 *src_addr)
  1084. {
  1085. #if IS_ENABLED(CONFIG_IPV6)
  1086. const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
  1087. IPV6_ADDR_LINKLOCAL;
  1088. struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
  1089. &src_addr->sin6_addr, net_dev->ifindex,
  1090. strict);
  1091. bool ret;
  1092. if (!rt)
  1093. return false;
  1094. ret = rt->rt6i_idev->dev == net_dev;
  1095. ip6_rt_put(rt);
  1096. return ret;
  1097. #else
  1098. return false;
  1099. #endif
  1100. }
  1101. static bool validate_net_dev(struct net_device *net_dev,
  1102. const struct sockaddr *daddr,
  1103. const struct sockaddr *saddr)
  1104. {
  1105. const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
  1106. const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
  1107. const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
  1108. const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
  1109. switch (daddr->sa_family) {
  1110. case AF_INET:
  1111. return saddr->sa_family == AF_INET &&
  1112. validate_ipv4_net_dev(net_dev, daddr4, saddr4);
  1113. case AF_INET6:
  1114. return saddr->sa_family == AF_INET6 &&
  1115. validate_ipv6_net_dev(net_dev, daddr6, saddr6);
  1116. default:
  1117. return false;
  1118. }
  1119. }
  1120. static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
  1121. const struct cma_req_info *req)
  1122. {
  1123. struct sockaddr_storage listen_addr_storage, src_addr_storage;
  1124. struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
  1125. *src_addr = (struct sockaddr *)&src_addr_storage;
  1126. struct net_device *net_dev;
  1127. const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
  1128. int err;
  1129. err = cma_save_ip_info(listen_addr, src_addr, ib_event,
  1130. req->service_id);
  1131. if (err)
  1132. return ERR_PTR(err);
  1133. net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
  1134. gid, listen_addr);
  1135. if (!net_dev)
  1136. return ERR_PTR(-ENODEV);
  1137. if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
  1138. dev_put(net_dev);
  1139. return ERR_PTR(-EHOSTUNREACH);
  1140. }
  1141. return net_dev;
  1142. }
  1143. static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
  1144. {
  1145. return (be64_to_cpu(service_id) >> 16) & 0xffff;
  1146. }
  1147. static bool cma_match_private_data(struct rdma_id_private *id_priv,
  1148. const struct cma_hdr *hdr)
  1149. {
  1150. struct sockaddr *addr = cma_src_addr(id_priv);
  1151. __be32 ip4_addr;
  1152. struct in6_addr ip6_addr;
  1153. if (cma_any_addr(addr) && !id_priv->afonly)
  1154. return true;
  1155. switch (addr->sa_family) {
  1156. case AF_INET:
  1157. ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  1158. if (cma_get_ip_ver(hdr) != 4)
  1159. return false;
  1160. if (!cma_any_addr(addr) &&
  1161. hdr->dst_addr.ip4.addr != ip4_addr)
  1162. return false;
  1163. break;
  1164. case AF_INET6:
  1165. ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
  1166. if (cma_get_ip_ver(hdr) != 6)
  1167. return false;
  1168. if (!cma_any_addr(addr) &&
  1169. memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
  1170. return false;
  1171. break;
  1172. case AF_IB:
  1173. return true;
  1174. default:
  1175. return false;
  1176. }
  1177. return true;
  1178. }
  1179. static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
  1180. {
  1181. enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
  1182. enum rdma_transport_type transport =
  1183. rdma_node_get_transport(device->node_type);
  1184. return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
  1185. }
  1186. static bool cma_protocol_roce(const struct rdma_cm_id *id)
  1187. {
  1188. struct ib_device *device = id->device;
  1189. const int port_num = id->port_num ?: rdma_start_port(device);
  1190. return cma_protocol_roce_dev_port(device, port_num);
  1191. }
  1192. static bool cma_match_net_dev(const struct rdma_cm_id *id,
  1193. const struct net_device *net_dev,
  1194. u8 port_num)
  1195. {
  1196. const struct rdma_addr *addr = &id->route.addr;
  1197. if (!net_dev)
  1198. /* This request is an AF_IB request or a RoCE request */
  1199. return (!id->port_num || id->port_num == port_num) &&
  1200. (addr->src_addr.ss_family == AF_IB ||
  1201. cma_protocol_roce_dev_port(id->device, port_num));
  1202. return !addr->dev_addr.bound_dev_if ||
  1203. (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
  1204. addr->dev_addr.bound_dev_if == net_dev->ifindex);
  1205. }
  1206. static struct rdma_id_private *cma_find_listener(
  1207. const struct rdma_bind_list *bind_list,
  1208. const struct ib_cm_id *cm_id,
  1209. const struct ib_cm_event *ib_event,
  1210. const struct cma_req_info *req,
  1211. const struct net_device *net_dev)
  1212. {
  1213. struct rdma_id_private *id_priv, *id_priv_dev;
  1214. if (!bind_list)
  1215. return ERR_PTR(-EINVAL);
  1216. hlist_for_each_entry(id_priv, &bind_list->owners, node) {
  1217. if (cma_match_private_data(id_priv, ib_event->private_data)) {
  1218. if (id_priv->id.device == cm_id->device &&
  1219. cma_match_net_dev(&id_priv->id, net_dev, req->port))
  1220. return id_priv;
  1221. list_for_each_entry(id_priv_dev,
  1222. &id_priv->listen_list,
  1223. listen_list) {
  1224. if (id_priv_dev->id.device == cm_id->device &&
  1225. cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
  1226. return id_priv_dev;
  1227. }
  1228. }
  1229. }
  1230. return ERR_PTR(-EINVAL);
  1231. }
  1232. static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
  1233. struct ib_cm_event *ib_event,
  1234. struct net_device **net_dev)
  1235. {
  1236. struct cma_req_info req;
  1237. struct rdma_bind_list *bind_list;
  1238. struct rdma_id_private *id_priv;
  1239. int err;
  1240. err = cma_save_req_info(ib_event, &req);
  1241. if (err)
  1242. return ERR_PTR(err);
  1243. *net_dev = cma_get_net_dev(ib_event, &req);
  1244. if (IS_ERR(*net_dev)) {
  1245. if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
  1246. /* Assuming the protocol is AF_IB */
  1247. *net_dev = NULL;
  1248. } else if (cma_protocol_roce_dev_port(req.device, req.port)) {
  1249. /* TODO find the net dev matching the request parameters
  1250. * through the RoCE GID table */
  1251. *net_dev = NULL;
  1252. } else {
  1253. return ERR_CAST(*net_dev);
  1254. }
  1255. }
  1256. bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
  1257. rdma_ps_from_service_id(req.service_id),
  1258. cma_port_from_service_id(req.service_id));
  1259. id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
  1260. if (IS_ERR(id_priv) && *net_dev) {
  1261. dev_put(*net_dev);
  1262. *net_dev = NULL;
  1263. }
  1264. return id_priv;
  1265. }
  1266. static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
  1267. {
  1268. return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
  1269. }
  1270. static void cma_cancel_route(struct rdma_id_private *id_priv)
  1271. {
  1272. if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
  1273. if (id_priv->query)
  1274. ib_sa_cancel_query(id_priv->query_id, id_priv->query);
  1275. }
  1276. }
  1277. static void cma_cancel_listens(struct rdma_id_private *id_priv)
  1278. {
  1279. struct rdma_id_private *dev_id_priv;
  1280. /*
  1281. * Remove from listen_any_list to prevent added devices from spawning
  1282. * additional listen requests.
  1283. */
  1284. mutex_lock(&lock);
  1285. list_del(&id_priv->list);
  1286. while (!list_empty(&id_priv->listen_list)) {
  1287. dev_id_priv = list_entry(id_priv->listen_list.next,
  1288. struct rdma_id_private, listen_list);
  1289. /* sync with device removal to avoid duplicate destruction */
  1290. list_del_init(&dev_id_priv->list);
  1291. list_del(&dev_id_priv->listen_list);
  1292. mutex_unlock(&lock);
  1293. rdma_destroy_id(&dev_id_priv->id);
  1294. mutex_lock(&lock);
  1295. }
  1296. mutex_unlock(&lock);
  1297. }
  1298. static void cma_cancel_operation(struct rdma_id_private *id_priv,
  1299. enum rdma_cm_state state)
  1300. {
  1301. switch (state) {
  1302. case RDMA_CM_ADDR_QUERY:
  1303. rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
  1304. break;
  1305. case RDMA_CM_ROUTE_QUERY:
  1306. cma_cancel_route(id_priv);
  1307. break;
  1308. case RDMA_CM_LISTEN:
  1309. if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
  1310. cma_cancel_listens(id_priv);
  1311. break;
  1312. default:
  1313. break;
  1314. }
  1315. }
  1316. static void cma_release_port(struct rdma_id_private *id_priv)
  1317. {
  1318. struct rdma_bind_list *bind_list = id_priv->bind_list;
  1319. struct net *net = id_priv->id.route.addr.dev_addr.net;
  1320. if (!bind_list)
  1321. return;
  1322. mutex_lock(&lock);
  1323. hlist_del(&id_priv->node);
  1324. if (hlist_empty(&bind_list->owners)) {
  1325. cma_ps_remove(net, bind_list->ps, bind_list->port);
  1326. kfree(bind_list);
  1327. }
  1328. mutex_unlock(&lock);
  1329. }
  1330. static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
  1331. {
  1332. struct cma_multicast *mc;
  1333. while (!list_empty(&id_priv->mc_list)) {
  1334. mc = container_of(id_priv->mc_list.next,
  1335. struct cma_multicast, list);
  1336. list_del(&mc->list);
  1337. if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
  1338. id_priv->id.port_num)) {
  1339. ib_sa_free_multicast(mc->multicast.ib);
  1340. kfree(mc);
  1341. } else {
  1342. if (mc->igmp_joined) {
  1343. struct rdma_dev_addr *dev_addr =
  1344. &id_priv->id.route.addr.dev_addr;
  1345. struct net_device *ndev = NULL;
  1346. if (dev_addr->bound_dev_if)
  1347. ndev = dev_get_by_index(&init_net,
  1348. dev_addr->bound_dev_if);
  1349. if (ndev) {
  1350. cma_igmp_send(ndev,
  1351. &mc->multicast.ib->rec.mgid,
  1352. false);
  1353. dev_put(ndev);
  1354. }
  1355. }
  1356. kref_put(&mc->mcref, release_mc);
  1357. }
  1358. }
  1359. }
  1360. void rdma_destroy_id(struct rdma_cm_id *id)
  1361. {
  1362. struct rdma_id_private *id_priv;
  1363. enum rdma_cm_state state;
  1364. id_priv = container_of(id, struct rdma_id_private, id);
  1365. state = cma_exch(id_priv, RDMA_CM_DESTROYING);
  1366. cma_cancel_operation(id_priv, state);
  1367. /*
  1368. * Wait for any active callback to finish. New callbacks will find
  1369. * the id_priv state set to destroying and abort.
  1370. */
  1371. mutex_lock(&id_priv->handler_mutex);
  1372. mutex_unlock(&id_priv->handler_mutex);
  1373. if (id_priv->cma_dev) {
  1374. if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
  1375. if (id_priv->cm_id.ib)
  1376. ib_destroy_cm_id(id_priv->cm_id.ib);
  1377. } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
  1378. if (id_priv->cm_id.iw)
  1379. iw_destroy_cm_id(id_priv->cm_id.iw);
  1380. }
  1381. cma_leave_mc_groups(id_priv);
  1382. cma_release_dev(id_priv);
  1383. }
  1384. cma_release_port(id_priv);
  1385. cma_deref_id(id_priv);
  1386. wait_for_completion(&id_priv->comp);
  1387. if (id_priv->internal_id)
  1388. cma_deref_id(id_priv->id.context);
  1389. kfree(id_priv->id.route.path_rec);
  1390. put_net(id_priv->id.route.addr.dev_addr.net);
  1391. kfree(id_priv);
  1392. }
  1393. EXPORT_SYMBOL(rdma_destroy_id);
  1394. static int cma_rep_recv(struct rdma_id_private *id_priv)
  1395. {
  1396. int ret;
  1397. ret = cma_modify_qp_rtr(id_priv, NULL);
  1398. if (ret)
  1399. goto reject;
  1400. ret = cma_modify_qp_rts(id_priv, NULL);
  1401. if (ret)
  1402. goto reject;
  1403. ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
  1404. if (ret)
  1405. goto reject;
  1406. return 0;
  1407. reject:
  1408. cma_modify_qp_err(id_priv);
  1409. ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
  1410. NULL, 0, NULL, 0);
  1411. return ret;
  1412. }
  1413. static void cma_set_rep_event_data(struct rdma_cm_event *event,
  1414. struct ib_cm_rep_event_param *rep_data,
  1415. void *private_data)
  1416. {
  1417. event->param.conn.private_data = private_data;
  1418. event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
  1419. event->param.conn.responder_resources = rep_data->responder_resources;
  1420. event->param.conn.initiator_depth = rep_data->initiator_depth;
  1421. event->param.conn.flow_control = rep_data->flow_control;
  1422. event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
  1423. event->param.conn.srq = rep_data->srq;
  1424. event->param.conn.qp_num = rep_data->remote_qpn;
  1425. }
  1426. static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
  1427. {
  1428. struct rdma_id_private *id_priv = cm_id->context;
  1429. struct rdma_cm_event event;
  1430. int ret = 0;
  1431. mutex_lock(&id_priv->handler_mutex);
  1432. if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
  1433. id_priv->state != RDMA_CM_CONNECT) ||
  1434. (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
  1435. id_priv->state != RDMA_CM_DISCONNECT))
  1436. goto out;
  1437. memset(&event, 0, sizeof event);
  1438. switch (ib_event->event) {
  1439. case IB_CM_REQ_ERROR:
  1440. case IB_CM_REP_ERROR:
  1441. event.event = RDMA_CM_EVENT_UNREACHABLE;
  1442. event.status = -ETIMEDOUT;
  1443. break;
  1444. case IB_CM_REP_RECEIVED:
  1445. if (id_priv->id.qp) {
  1446. event.status = cma_rep_recv(id_priv);
  1447. event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
  1448. RDMA_CM_EVENT_ESTABLISHED;
  1449. } else {
  1450. event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
  1451. }
  1452. cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
  1453. ib_event->private_data);
  1454. break;
  1455. case IB_CM_RTU_RECEIVED:
  1456. case IB_CM_USER_ESTABLISHED:
  1457. event.event = RDMA_CM_EVENT_ESTABLISHED;
  1458. break;
  1459. case IB_CM_DREQ_ERROR:
  1460. event.status = -ETIMEDOUT; /* fall through */
  1461. case IB_CM_DREQ_RECEIVED:
  1462. case IB_CM_DREP_RECEIVED:
  1463. if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
  1464. RDMA_CM_DISCONNECT))
  1465. goto out;
  1466. event.event = RDMA_CM_EVENT_DISCONNECTED;
  1467. break;
  1468. case IB_CM_TIMEWAIT_EXIT:
  1469. event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
  1470. break;
  1471. case IB_CM_MRA_RECEIVED:
  1472. /* ignore event */
  1473. goto out;
  1474. case IB_CM_REJ_RECEIVED:
  1475. cma_modify_qp_err(id_priv);
  1476. event.status = ib_event->param.rej_rcvd.reason;
  1477. event.event = RDMA_CM_EVENT_REJECTED;
  1478. event.param.conn.private_data = ib_event->private_data;
  1479. event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
  1480. break;
  1481. default:
  1482. pr_err("RDMA CMA: unexpected IB CM event: %d\n",
  1483. ib_event->event);
  1484. goto out;
  1485. }
  1486. ret = id_priv->id.event_handler(&id_priv->id, &event);
  1487. if (ret) {
  1488. /* Destroy the CM ID by returning a non-zero value. */
  1489. id_priv->cm_id.ib = NULL;
  1490. cma_exch(id_priv, RDMA_CM_DESTROYING);
  1491. mutex_unlock(&id_priv->handler_mutex);
  1492. rdma_destroy_id(&id_priv->id);
  1493. return ret;
  1494. }
  1495. out:
  1496. mutex_unlock(&id_priv->handler_mutex);
  1497. return ret;
  1498. }
  1499. static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
  1500. struct ib_cm_event *ib_event,
  1501. struct net_device *net_dev)
  1502. {
  1503. struct rdma_id_private *id_priv;
  1504. struct rdma_cm_id *id;
  1505. struct rdma_route *rt;
  1506. const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
  1507. const __be64 service_id =
  1508. ib_event->param.req_rcvd.primary_path->service_id;
  1509. int ret;
  1510. id = rdma_create_id(listen_id->route.addr.dev_addr.net,
  1511. listen_id->event_handler, listen_id->context,
  1512. listen_id->ps, ib_event->param.req_rcvd.qp_type);
  1513. if (IS_ERR(id))
  1514. return NULL;
  1515. id_priv = container_of(id, struct rdma_id_private, id);
  1516. if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
  1517. (struct sockaddr *)&id->route.addr.dst_addr,
  1518. listen_id, ib_event, ss_family, service_id))
  1519. goto err;
  1520. rt = &id->route;
  1521. rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
  1522. rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
  1523. GFP_KERNEL);
  1524. if (!rt->path_rec)
  1525. goto err;
  1526. rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
  1527. if (rt->num_paths == 2)
  1528. rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
  1529. if (net_dev) {
  1530. ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
  1531. if (ret)
  1532. goto err;
  1533. } else {
  1534. if (!cma_protocol_roce(listen_id) &&
  1535. cma_any_addr(cma_src_addr(id_priv))) {
  1536. rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
  1537. rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
  1538. ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
  1539. } else if (!cma_any_addr(cma_src_addr(id_priv))) {
  1540. ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
  1541. if (ret)
  1542. goto err;
  1543. }
  1544. }
  1545. rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
  1546. id_priv->state = RDMA_CM_CONNECT;
  1547. return id_priv;
  1548. err:
  1549. rdma_destroy_id(id);
  1550. return NULL;
  1551. }
  1552. static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
  1553. struct ib_cm_event *ib_event,
  1554. struct net_device *net_dev)
  1555. {
  1556. struct rdma_id_private *id_priv;
  1557. struct rdma_cm_id *id;
  1558. const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
  1559. struct net *net = listen_id->route.addr.dev_addr.net;
  1560. int ret;
  1561. id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
  1562. listen_id->ps, IB_QPT_UD);
  1563. if (IS_ERR(id))
  1564. return NULL;
  1565. id_priv = container_of(id, struct rdma_id_private, id);
  1566. if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
  1567. (struct sockaddr *)&id->route.addr.dst_addr,
  1568. listen_id, ib_event, ss_family,
  1569. ib_event->param.sidr_req_rcvd.service_id))
  1570. goto err;
  1571. if (net_dev) {
  1572. ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
  1573. if (ret)
  1574. goto err;
  1575. } else {
  1576. if (!cma_any_addr(cma_src_addr(id_priv))) {
  1577. ret = cma_translate_addr(cma_src_addr(id_priv),
  1578. &id->route.addr.dev_addr);
  1579. if (ret)
  1580. goto err;
  1581. }
  1582. }
  1583. id_priv->state = RDMA_CM_CONNECT;
  1584. return id_priv;
  1585. err:
  1586. rdma_destroy_id(id);
  1587. return NULL;
  1588. }
  1589. static void cma_set_req_event_data(struct rdma_cm_event *event,
  1590. struct ib_cm_req_event_param *req_data,
  1591. void *private_data, int offset)
  1592. {
  1593. event->param.conn.private_data = private_data + offset;
  1594. event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
  1595. event->param.conn.responder_resources = req_data->responder_resources;
  1596. event->param.conn.initiator_depth = req_data->initiator_depth;
  1597. event->param.conn.flow_control = req_data->flow_control;
  1598. event->param.conn.retry_count = req_data->retry_count;
  1599. event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
  1600. event->param.conn.srq = req_data->srq;
  1601. event->param.conn.qp_num = req_data->remote_qpn;
  1602. }
  1603. static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
  1604. {
  1605. return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
  1606. (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
  1607. ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
  1608. (id->qp_type == IB_QPT_UD)) ||
  1609. (!id->qp_type));
  1610. }
  1611. static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
  1612. {
  1613. struct rdma_id_private *listen_id, *conn_id = NULL;
  1614. struct rdma_cm_event event;
  1615. struct net_device *net_dev;
  1616. u8 offset;
  1617. int ret;
  1618. listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
  1619. if (IS_ERR(listen_id))
  1620. return PTR_ERR(listen_id);
  1621. if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
  1622. ret = -EINVAL;
  1623. goto net_dev_put;
  1624. }
  1625. mutex_lock(&listen_id->handler_mutex);
  1626. if (listen_id->state != RDMA_CM_LISTEN) {
  1627. ret = -ECONNABORTED;
  1628. goto err1;
  1629. }
  1630. memset(&event, 0, sizeof event);
  1631. offset = cma_user_data_offset(listen_id);
  1632. event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
  1633. if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
  1634. conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
  1635. event.param.ud.private_data = ib_event->private_data + offset;
  1636. event.param.ud.private_data_len =
  1637. IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
  1638. } else {
  1639. conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
  1640. cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
  1641. ib_event->private_data, offset);
  1642. }
  1643. if (!conn_id) {
  1644. ret = -ENOMEM;
  1645. goto err1;
  1646. }
  1647. mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
  1648. ret = cma_acquire_dev(conn_id, listen_id);
  1649. if (ret)
  1650. goto err2;
  1651. conn_id->cm_id.ib = cm_id;
  1652. cm_id->context = conn_id;
  1653. cm_id->cm_handler = cma_ib_handler;
  1654. /*
  1655. * Protect against the user destroying conn_id from another thread
  1656. * until we're done accessing it.
  1657. */
  1658. atomic_inc(&conn_id->refcount);
  1659. ret = conn_id->id.event_handler(&conn_id->id, &event);
  1660. if (ret)
  1661. goto err3;
  1662. /*
  1663. * Acquire mutex to prevent user executing rdma_destroy_id()
  1664. * while we're accessing the cm_id.
  1665. */
  1666. mutex_lock(&lock);
  1667. if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
  1668. (conn_id->id.qp_type != IB_QPT_UD))
  1669. ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
  1670. mutex_unlock(&lock);
  1671. mutex_unlock(&conn_id->handler_mutex);
  1672. mutex_unlock(&listen_id->handler_mutex);
  1673. cma_deref_id(conn_id);
  1674. if (net_dev)
  1675. dev_put(net_dev);
  1676. return 0;
  1677. err3:
  1678. cma_deref_id(conn_id);
  1679. /* Destroy the CM ID by returning a non-zero value. */
  1680. conn_id->cm_id.ib = NULL;
  1681. err2:
  1682. cma_exch(conn_id, RDMA_CM_DESTROYING);
  1683. mutex_unlock(&conn_id->handler_mutex);
  1684. err1:
  1685. mutex_unlock(&listen_id->handler_mutex);
  1686. if (conn_id)
  1687. rdma_destroy_id(&conn_id->id);
  1688. net_dev_put:
  1689. if (net_dev)
  1690. dev_put(net_dev);
  1691. return ret;
  1692. }
  1693. __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
  1694. {
  1695. if (addr->sa_family == AF_IB)
  1696. return ((struct sockaddr_ib *) addr)->sib_sid;
  1697. return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
  1698. }
  1699. EXPORT_SYMBOL(rdma_get_service_id);
  1700. static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
  1701. {
  1702. struct rdma_id_private *id_priv = iw_id->context;
  1703. struct rdma_cm_event event;
  1704. int ret = 0;
  1705. struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
  1706. struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
  1707. mutex_lock(&id_priv->handler_mutex);
  1708. if (id_priv->state != RDMA_CM_CONNECT)
  1709. goto out;
  1710. memset(&event, 0, sizeof event);
  1711. switch (iw_event->event) {
  1712. case IW_CM_EVENT_CLOSE:
  1713. event.event = RDMA_CM_EVENT_DISCONNECTED;
  1714. break;
  1715. case IW_CM_EVENT_CONNECT_REPLY:
  1716. memcpy(cma_src_addr(id_priv), laddr,
  1717. rdma_addr_size(laddr));
  1718. memcpy(cma_dst_addr(id_priv), raddr,
  1719. rdma_addr_size(raddr));
  1720. switch (iw_event->status) {
  1721. case 0:
  1722. event.event = RDMA_CM_EVENT_ESTABLISHED;
  1723. event.param.conn.initiator_depth = iw_event->ird;
  1724. event.param.conn.responder_resources = iw_event->ord;
  1725. break;
  1726. case -ECONNRESET:
  1727. case -ECONNREFUSED:
  1728. event.event = RDMA_CM_EVENT_REJECTED;
  1729. break;
  1730. case -ETIMEDOUT:
  1731. event.event = RDMA_CM_EVENT_UNREACHABLE;
  1732. break;
  1733. default:
  1734. event.event = RDMA_CM_EVENT_CONNECT_ERROR;
  1735. break;
  1736. }
  1737. break;
  1738. case IW_CM_EVENT_ESTABLISHED:
  1739. event.event = RDMA_CM_EVENT_ESTABLISHED;
  1740. event.param.conn.initiator_depth = iw_event->ird;
  1741. event.param.conn.responder_resources = iw_event->ord;
  1742. break;
  1743. default:
  1744. BUG_ON(1);
  1745. }
  1746. event.status = iw_event->status;
  1747. event.param.conn.private_data = iw_event->private_data;
  1748. event.param.conn.private_data_len = iw_event->private_data_len;
  1749. ret = id_priv->id.event_handler(&id_priv->id, &event);
  1750. if (ret) {
  1751. /* Destroy the CM ID by returning a non-zero value. */
  1752. id_priv->cm_id.iw = NULL;
  1753. cma_exch(id_priv, RDMA_CM_DESTROYING);
  1754. mutex_unlock(&id_priv->handler_mutex);
  1755. rdma_destroy_id(&id_priv->id);
  1756. return ret;
  1757. }
  1758. out:
  1759. mutex_unlock(&id_priv->handler_mutex);
  1760. return ret;
  1761. }
  1762. static int iw_conn_req_handler(struct iw_cm_id *cm_id,
  1763. struct iw_cm_event *iw_event)
  1764. {
  1765. struct rdma_cm_id *new_cm_id;
  1766. struct rdma_id_private *listen_id, *conn_id;
  1767. struct rdma_cm_event event;
  1768. int ret = -ECONNABORTED;
  1769. struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
  1770. struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
  1771. listen_id = cm_id->context;
  1772. mutex_lock(&listen_id->handler_mutex);
  1773. if (listen_id->state != RDMA_CM_LISTEN)
  1774. goto out;
  1775. /* Create a new RDMA id for the new IW CM ID */
  1776. new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
  1777. listen_id->id.event_handler,
  1778. listen_id->id.context,
  1779. RDMA_PS_TCP, IB_QPT_RC);
  1780. if (IS_ERR(new_cm_id)) {
  1781. ret = -ENOMEM;
  1782. goto out;
  1783. }
  1784. conn_id = container_of(new_cm_id, struct rdma_id_private, id);
  1785. mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
  1786. conn_id->state = RDMA_CM_CONNECT;
  1787. ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
  1788. if (ret) {
  1789. mutex_unlock(&conn_id->handler_mutex);
  1790. rdma_destroy_id(new_cm_id);
  1791. goto out;
  1792. }
  1793. ret = cma_acquire_dev(conn_id, listen_id);
  1794. if (ret) {
  1795. mutex_unlock(&conn_id->handler_mutex);
  1796. rdma_destroy_id(new_cm_id);
  1797. goto out;
  1798. }
  1799. conn_id->cm_id.iw = cm_id;
  1800. cm_id->context = conn_id;
  1801. cm_id->cm_handler = cma_iw_handler;
  1802. memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
  1803. memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
  1804. memset(&event, 0, sizeof event);
  1805. event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
  1806. event.param.conn.private_data = iw_event->private_data;
  1807. event.param.conn.private_data_len = iw_event->private_data_len;
  1808. event.param.conn.initiator_depth = iw_event->ird;
  1809. event.param.conn.responder_resources = iw_event->ord;
  1810. /*
  1811. * Protect against the user destroying conn_id from another thread
  1812. * until we're done accessing it.
  1813. */
  1814. atomic_inc(&conn_id->refcount);
  1815. ret = conn_id->id.event_handler(&conn_id->id, &event);
  1816. if (ret) {
  1817. /* User wants to destroy the CM ID */
  1818. conn_id->cm_id.iw = NULL;
  1819. cma_exch(conn_id, RDMA_CM_DESTROYING);
  1820. mutex_unlock(&conn_id->handler_mutex);
  1821. cma_deref_id(conn_id);
  1822. rdma_destroy_id(&conn_id->id);
  1823. goto out;
  1824. }
  1825. mutex_unlock(&conn_id->handler_mutex);
  1826. cma_deref_id(conn_id);
  1827. out:
  1828. mutex_unlock(&listen_id->handler_mutex);
  1829. return ret;
  1830. }
  1831. static int cma_ib_listen(struct rdma_id_private *id_priv)
  1832. {
  1833. struct sockaddr *addr;
  1834. struct ib_cm_id *id;
  1835. __be64 svc_id;
  1836. addr = cma_src_addr(id_priv);
  1837. svc_id = rdma_get_service_id(&id_priv->id, addr);
  1838. id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
  1839. if (IS_ERR(id))
  1840. return PTR_ERR(id);
  1841. id_priv->cm_id.ib = id;
  1842. return 0;
  1843. }
  1844. static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
  1845. {
  1846. int ret;
  1847. struct iw_cm_id *id;
  1848. id = iw_create_cm_id(id_priv->id.device,
  1849. iw_conn_req_handler,
  1850. id_priv);
  1851. if (IS_ERR(id))
  1852. return PTR_ERR(id);
  1853. id->tos = id_priv->tos;
  1854. id_priv->cm_id.iw = id;
  1855. memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
  1856. rdma_addr_size(cma_src_addr(id_priv)));
  1857. ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
  1858. if (ret) {
  1859. iw_destroy_cm_id(id_priv->cm_id.iw);
  1860. id_priv->cm_id.iw = NULL;
  1861. }
  1862. return ret;
  1863. }
  1864. static int cma_listen_handler(struct rdma_cm_id *id,
  1865. struct rdma_cm_event *event)
  1866. {
  1867. struct rdma_id_private *id_priv = id->context;
  1868. id->context = id_priv->id.context;
  1869. id->event_handler = id_priv->id.event_handler;
  1870. return id_priv->id.event_handler(id, event);
  1871. }
  1872. static void cma_listen_on_dev(struct rdma_id_private *id_priv,
  1873. struct cma_device *cma_dev)
  1874. {
  1875. struct rdma_id_private *dev_id_priv;
  1876. struct rdma_cm_id *id;
  1877. struct net *net = id_priv->id.route.addr.dev_addr.net;
  1878. int ret;
  1879. if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
  1880. return;
  1881. id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
  1882. id_priv->id.qp_type);
  1883. if (IS_ERR(id))
  1884. return;
  1885. dev_id_priv = container_of(id, struct rdma_id_private, id);
  1886. dev_id_priv->state = RDMA_CM_ADDR_BOUND;
  1887. memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
  1888. rdma_addr_size(cma_src_addr(id_priv)));
  1889. _cma_attach_to_dev(dev_id_priv, cma_dev);
  1890. list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
  1891. atomic_inc(&id_priv->refcount);
  1892. dev_id_priv->internal_id = 1;
  1893. dev_id_priv->afonly = id_priv->afonly;
  1894. ret = rdma_listen(id, id_priv->backlog);
  1895. if (ret)
  1896. pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
  1897. ret, cma_dev->device->name);
  1898. }
  1899. static void cma_listen_on_all(struct rdma_id_private *id_priv)
  1900. {
  1901. struct cma_device *cma_dev;
  1902. mutex_lock(&lock);
  1903. list_add_tail(&id_priv->list, &listen_any_list);
  1904. list_for_each_entry(cma_dev, &dev_list, list)
  1905. cma_listen_on_dev(id_priv, cma_dev);
  1906. mutex_unlock(&lock);
  1907. }
  1908. void rdma_set_service_type(struct rdma_cm_id *id, int tos)
  1909. {
  1910. struct rdma_id_private *id_priv;
  1911. id_priv = container_of(id, struct rdma_id_private, id);
  1912. id_priv->tos = (u8) tos;
  1913. }
  1914. EXPORT_SYMBOL(rdma_set_service_type);
  1915. static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
  1916. void *context)
  1917. {
  1918. struct cma_work *work = context;
  1919. struct rdma_route *route;
  1920. route = &work->id->id.route;
  1921. if (!status) {
  1922. route->num_paths = 1;
  1923. *route->path_rec = *path_rec;
  1924. } else {
  1925. work->old_state = RDMA_CM_ROUTE_QUERY;
  1926. work->new_state = RDMA_CM_ADDR_RESOLVED;
  1927. work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
  1928. work->event.status = status;
  1929. }
  1930. queue_work(cma_wq, &work->work);
  1931. }
  1932. static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
  1933. struct cma_work *work)
  1934. {
  1935. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  1936. struct ib_sa_path_rec path_rec;
  1937. ib_sa_comp_mask comp_mask;
  1938. struct sockaddr_in6 *sin6;
  1939. struct sockaddr_ib *sib;
  1940. memset(&path_rec, 0, sizeof path_rec);
  1941. rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
  1942. rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
  1943. path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  1944. path_rec.numb_path = 1;
  1945. path_rec.reversible = 1;
  1946. path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
  1947. comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
  1948. IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
  1949. IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
  1950. switch (cma_family(id_priv)) {
  1951. case AF_INET:
  1952. path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
  1953. comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
  1954. break;
  1955. case AF_INET6:
  1956. sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
  1957. path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
  1958. comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
  1959. break;
  1960. case AF_IB:
  1961. sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
  1962. path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
  1963. comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
  1964. break;
  1965. }
  1966. id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
  1967. id_priv->id.port_num, &path_rec,
  1968. comp_mask, timeout_ms,
  1969. GFP_KERNEL, cma_query_handler,
  1970. work, &id_priv->query);
  1971. return (id_priv->query_id < 0) ? id_priv->query_id : 0;
  1972. }
  1973. static void cma_work_handler(struct work_struct *_work)
  1974. {
  1975. struct cma_work *work = container_of(_work, struct cma_work, work);
  1976. struct rdma_id_private *id_priv = work->id;
  1977. int destroy = 0;
  1978. mutex_lock(&id_priv->handler_mutex);
  1979. if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
  1980. goto out;
  1981. if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
  1982. cma_exch(id_priv, RDMA_CM_DESTROYING);
  1983. destroy = 1;
  1984. }
  1985. out:
  1986. mutex_unlock(&id_priv->handler_mutex);
  1987. cma_deref_id(id_priv);
  1988. if (destroy)
  1989. rdma_destroy_id(&id_priv->id);
  1990. kfree(work);
  1991. }
  1992. static void cma_ndev_work_handler(struct work_struct *_work)
  1993. {
  1994. struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
  1995. struct rdma_id_private *id_priv = work->id;
  1996. int destroy = 0;
  1997. mutex_lock(&id_priv->handler_mutex);
  1998. if (id_priv->state == RDMA_CM_DESTROYING ||
  1999. id_priv->state == RDMA_CM_DEVICE_REMOVAL)
  2000. goto out;
  2001. if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
  2002. cma_exch(id_priv, RDMA_CM_DESTROYING);
  2003. destroy = 1;
  2004. }
  2005. out:
  2006. mutex_unlock(&id_priv->handler_mutex);
  2007. cma_deref_id(id_priv);
  2008. if (destroy)
  2009. rdma_destroy_id(&id_priv->id);
  2010. kfree(work);
  2011. }
  2012. static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
  2013. {
  2014. struct rdma_route *route = &id_priv->id.route;
  2015. struct cma_work *work;
  2016. int ret;
  2017. work = kzalloc(sizeof *work, GFP_KERNEL);
  2018. if (!work)
  2019. return -ENOMEM;
  2020. work->id = id_priv;
  2021. INIT_WORK(&work->work, cma_work_handler);
  2022. work->old_state = RDMA_CM_ROUTE_QUERY;
  2023. work->new_state = RDMA_CM_ROUTE_RESOLVED;
  2024. work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  2025. route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
  2026. if (!route->path_rec) {
  2027. ret = -ENOMEM;
  2028. goto err1;
  2029. }
  2030. ret = cma_query_ib_route(id_priv, timeout_ms, work);
  2031. if (ret)
  2032. goto err2;
  2033. return 0;
  2034. err2:
  2035. kfree(route->path_rec);
  2036. route->path_rec = NULL;
  2037. err1:
  2038. kfree(work);
  2039. return ret;
  2040. }
  2041. int rdma_set_ib_paths(struct rdma_cm_id *id,
  2042. struct ib_sa_path_rec *path_rec, int num_paths)
  2043. {
  2044. struct rdma_id_private *id_priv;
  2045. int ret;
  2046. id_priv = container_of(id, struct rdma_id_private, id);
  2047. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
  2048. RDMA_CM_ROUTE_RESOLVED))
  2049. return -EINVAL;
  2050. id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
  2051. GFP_KERNEL);
  2052. if (!id->route.path_rec) {
  2053. ret = -ENOMEM;
  2054. goto err;
  2055. }
  2056. id->route.num_paths = num_paths;
  2057. return 0;
  2058. err:
  2059. cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
  2060. return ret;
  2061. }
  2062. EXPORT_SYMBOL(rdma_set_ib_paths);
  2063. static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
  2064. {
  2065. struct cma_work *work;
  2066. work = kzalloc(sizeof *work, GFP_KERNEL);
  2067. if (!work)
  2068. return -ENOMEM;
  2069. work->id = id_priv;
  2070. INIT_WORK(&work->work, cma_work_handler);
  2071. work->old_state = RDMA_CM_ROUTE_QUERY;
  2072. work->new_state = RDMA_CM_ROUTE_RESOLVED;
  2073. work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  2074. queue_work(cma_wq, &work->work);
  2075. return 0;
  2076. }
  2077. static int iboe_tos_to_sl(struct net_device *ndev, int tos)
  2078. {
  2079. int prio;
  2080. struct net_device *dev;
  2081. prio = rt_tos2priority(tos);
  2082. dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
  2083. vlan_dev_real_dev(ndev) : ndev;
  2084. if (dev->num_tc)
  2085. return netdev_get_prio_tc_map(dev, prio);
  2086. #if IS_ENABLED(CONFIG_VLAN_8021Q)
  2087. if (ndev->priv_flags & IFF_802_1Q_VLAN)
  2088. return (vlan_dev_get_egress_qos_mask(ndev, prio) &
  2089. VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
  2090. #endif
  2091. return 0;
  2092. }
  2093. static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
  2094. unsigned long supported_gids,
  2095. enum ib_gid_type default_gid)
  2096. {
  2097. if ((network_type == RDMA_NETWORK_IPV4 ||
  2098. network_type == RDMA_NETWORK_IPV6) &&
  2099. test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
  2100. return IB_GID_TYPE_ROCE_UDP_ENCAP;
  2101. return default_gid;
  2102. }
  2103. static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
  2104. {
  2105. struct rdma_route *route = &id_priv->id.route;
  2106. struct rdma_addr *addr = &route->addr;
  2107. struct cma_work *work;
  2108. int ret;
  2109. struct net_device *ndev = NULL;
  2110. work = kzalloc(sizeof *work, GFP_KERNEL);
  2111. if (!work)
  2112. return -ENOMEM;
  2113. work->id = id_priv;
  2114. INIT_WORK(&work->work, cma_work_handler);
  2115. route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
  2116. if (!route->path_rec) {
  2117. ret = -ENOMEM;
  2118. goto err1;
  2119. }
  2120. route->num_paths = 1;
  2121. if (addr->dev_addr.bound_dev_if) {
  2122. unsigned long supported_gids;
  2123. ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
  2124. if (!ndev) {
  2125. ret = -ENODEV;
  2126. goto err2;
  2127. }
  2128. if (ndev->flags & IFF_LOOPBACK) {
  2129. dev_put(ndev);
  2130. if (!id_priv->id.device->get_netdev) {
  2131. ret = -EOPNOTSUPP;
  2132. goto err2;
  2133. }
  2134. ndev = id_priv->id.device->get_netdev(id_priv->id.device,
  2135. id_priv->id.port_num);
  2136. if (!ndev) {
  2137. ret = -ENODEV;
  2138. goto err2;
  2139. }
  2140. }
  2141. route->path_rec->net = &init_net;
  2142. route->path_rec->ifindex = ndev->ifindex;
  2143. supported_gids = roce_gid_type_mask_support(id_priv->id.device,
  2144. id_priv->id.port_num);
  2145. route->path_rec->gid_type =
  2146. cma_route_gid_type(addr->dev_addr.network,
  2147. supported_gids,
  2148. id_priv->gid_type);
  2149. }
  2150. if (!ndev) {
  2151. ret = -ENODEV;
  2152. goto err2;
  2153. }
  2154. memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
  2155. rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
  2156. &route->path_rec->sgid);
  2157. rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
  2158. &route->path_rec->dgid);
  2159. /* Use the hint from IP Stack to select GID Type */
  2160. if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network))
  2161. route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network);
  2162. if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
  2163. /* TODO: get the hoplimit from the inet/inet6 device */
  2164. route->path_rec->hop_limit = addr->dev_addr.hoplimit;
  2165. else
  2166. route->path_rec->hop_limit = 1;
  2167. route->path_rec->reversible = 1;
  2168. route->path_rec->pkey = cpu_to_be16(0xffff);
  2169. route->path_rec->mtu_selector = IB_SA_EQ;
  2170. route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
  2171. route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
  2172. route->path_rec->rate_selector = IB_SA_EQ;
  2173. route->path_rec->rate = iboe_get_rate(ndev);
  2174. dev_put(ndev);
  2175. route->path_rec->packet_life_time_selector = IB_SA_EQ;
  2176. route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
  2177. if (!route->path_rec->mtu) {
  2178. ret = -EINVAL;
  2179. goto err2;
  2180. }
  2181. work->old_state = RDMA_CM_ROUTE_QUERY;
  2182. work->new_state = RDMA_CM_ROUTE_RESOLVED;
  2183. work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  2184. work->event.status = 0;
  2185. queue_work(cma_wq, &work->work);
  2186. return 0;
  2187. err2:
  2188. kfree(route->path_rec);
  2189. route->path_rec = NULL;
  2190. err1:
  2191. kfree(work);
  2192. return ret;
  2193. }
  2194. int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
  2195. {
  2196. struct rdma_id_private *id_priv;
  2197. int ret;
  2198. id_priv = container_of(id, struct rdma_id_private, id);
  2199. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
  2200. return -EINVAL;
  2201. atomic_inc(&id_priv->refcount);
  2202. if (rdma_cap_ib_sa(id->device, id->port_num))
  2203. ret = cma_resolve_ib_route(id_priv, timeout_ms);
  2204. else if (rdma_protocol_roce(id->device, id->port_num))
  2205. ret = cma_resolve_iboe_route(id_priv);
  2206. else if (rdma_protocol_iwarp(id->device, id->port_num))
  2207. ret = cma_resolve_iw_route(id_priv, timeout_ms);
  2208. else
  2209. ret = -ENOSYS;
  2210. if (ret)
  2211. goto err;
  2212. return 0;
  2213. err:
  2214. cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
  2215. cma_deref_id(id_priv);
  2216. return ret;
  2217. }
  2218. EXPORT_SYMBOL(rdma_resolve_route);
  2219. static void cma_set_loopback(struct sockaddr *addr)
  2220. {
  2221. switch (addr->sa_family) {
  2222. case AF_INET:
  2223. ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  2224. break;
  2225. case AF_INET6:
  2226. ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
  2227. 0, 0, 0, htonl(1));
  2228. break;
  2229. default:
  2230. ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
  2231. 0, 0, 0, htonl(1));
  2232. break;
  2233. }
  2234. }
  2235. static int cma_bind_loopback(struct rdma_id_private *id_priv)
  2236. {
  2237. struct cma_device *cma_dev, *cur_dev;
  2238. struct ib_port_attr port_attr;
  2239. union ib_gid gid;
  2240. u16 pkey;
  2241. int ret;
  2242. u8 p;
  2243. cma_dev = NULL;
  2244. mutex_lock(&lock);
  2245. list_for_each_entry(cur_dev, &dev_list, list) {
  2246. if (cma_family(id_priv) == AF_IB &&
  2247. !rdma_cap_ib_cm(cur_dev->device, 1))
  2248. continue;
  2249. if (!cma_dev)
  2250. cma_dev = cur_dev;
  2251. for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
  2252. if (!ib_query_port(cur_dev->device, p, &port_attr) &&
  2253. port_attr.state == IB_PORT_ACTIVE) {
  2254. cma_dev = cur_dev;
  2255. goto port_found;
  2256. }
  2257. }
  2258. }
  2259. if (!cma_dev) {
  2260. ret = -ENODEV;
  2261. goto out;
  2262. }
  2263. p = 1;
  2264. port_found:
  2265. ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
  2266. if (ret)
  2267. goto out;
  2268. ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
  2269. if (ret)
  2270. goto out;
  2271. id_priv->id.route.addr.dev_addr.dev_type =
  2272. (rdma_protocol_ib(cma_dev->device, p)) ?
  2273. ARPHRD_INFINIBAND : ARPHRD_ETHER;
  2274. rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
  2275. ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
  2276. id_priv->id.port_num = p;
  2277. cma_attach_to_dev(id_priv, cma_dev);
  2278. cma_set_loopback(cma_src_addr(id_priv));
  2279. out:
  2280. mutex_unlock(&lock);
  2281. return ret;
  2282. }
  2283. static void addr_handler(int status, struct sockaddr *src_addr,
  2284. struct rdma_dev_addr *dev_addr, void *context)
  2285. {
  2286. struct rdma_id_private *id_priv = context;
  2287. struct rdma_cm_event event;
  2288. memset(&event, 0, sizeof event);
  2289. mutex_lock(&id_priv->handler_mutex);
  2290. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
  2291. RDMA_CM_ADDR_RESOLVED))
  2292. goto out;
  2293. memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
  2294. if (!status && !id_priv->cma_dev)
  2295. status = cma_acquire_dev(id_priv, NULL);
  2296. if (status) {
  2297. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
  2298. RDMA_CM_ADDR_BOUND))
  2299. goto out;
  2300. event.event = RDMA_CM_EVENT_ADDR_ERROR;
  2301. event.status = status;
  2302. } else
  2303. event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
  2304. if (id_priv->id.event_handler(&id_priv->id, &event)) {
  2305. cma_exch(id_priv, RDMA_CM_DESTROYING);
  2306. mutex_unlock(&id_priv->handler_mutex);
  2307. cma_deref_id(id_priv);
  2308. rdma_destroy_id(&id_priv->id);
  2309. return;
  2310. }
  2311. out:
  2312. mutex_unlock(&id_priv->handler_mutex);
  2313. cma_deref_id(id_priv);
  2314. }
  2315. static int cma_resolve_loopback(struct rdma_id_private *id_priv)
  2316. {
  2317. struct cma_work *work;
  2318. union ib_gid gid;
  2319. int ret;
  2320. work = kzalloc(sizeof *work, GFP_KERNEL);
  2321. if (!work)
  2322. return -ENOMEM;
  2323. if (!id_priv->cma_dev) {
  2324. ret = cma_bind_loopback(id_priv);
  2325. if (ret)
  2326. goto err;
  2327. }
  2328. rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
  2329. rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
  2330. work->id = id_priv;
  2331. INIT_WORK(&work->work, cma_work_handler);
  2332. work->old_state = RDMA_CM_ADDR_QUERY;
  2333. work->new_state = RDMA_CM_ADDR_RESOLVED;
  2334. work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
  2335. queue_work(cma_wq, &work->work);
  2336. return 0;
  2337. err:
  2338. kfree(work);
  2339. return ret;
  2340. }
  2341. static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
  2342. {
  2343. struct cma_work *work;
  2344. int ret;
  2345. work = kzalloc(sizeof *work, GFP_KERNEL);
  2346. if (!work)
  2347. return -ENOMEM;
  2348. if (!id_priv->cma_dev) {
  2349. ret = cma_resolve_ib_dev(id_priv);
  2350. if (ret)
  2351. goto err;
  2352. }
  2353. rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
  2354. &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
  2355. work->id = id_priv;
  2356. INIT_WORK(&work->work, cma_work_handler);
  2357. work->old_state = RDMA_CM_ADDR_QUERY;
  2358. work->new_state = RDMA_CM_ADDR_RESOLVED;
  2359. work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
  2360. queue_work(cma_wq, &work->work);
  2361. return 0;
  2362. err:
  2363. kfree(work);
  2364. return ret;
  2365. }
  2366. static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
  2367. struct sockaddr *dst_addr)
  2368. {
  2369. if (!src_addr || !src_addr->sa_family) {
  2370. src_addr = (struct sockaddr *) &id->route.addr.src_addr;
  2371. src_addr->sa_family = dst_addr->sa_family;
  2372. if (IS_ENABLED(CONFIG_IPV6) &&
  2373. dst_addr->sa_family == AF_INET6) {
  2374. struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
  2375. struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
  2376. src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
  2377. if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  2378. id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
  2379. } else if (dst_addr->sa_family == AF_IB) {
  2380. ((struct sockaddr_ib *) src_addr)->sib_pkey =
  2381. ((struct sockaddr_ib *) dst_addr)->sib_pkey;
  2382. }
  2383. }
  2384. return rdma_bind_addr(id, src_addr);
  2385. }
  2386. int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
  2387. struct sockaddr *dst_addr, int timeout_ms)
  2388. {
  2389. struct rdma_id_private *id_priv;
  2390. int ret;
  2391. id_priv = container_of(id, struct rdma_id_private, id);
  2392. if (id_priv->state == RDMA_CM_IDLE) {
  2393. ret = cma_bind_addr(id, src_addr, dst_addr);
  2394. if (ret)
  2395. return ret;
  2396. }
  2397. if (cma_family(id_priv) != dst_addr->sa_family)
  2398. return -EINVAL;
  2399. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
  2400. return -EINVAL;
  2401. atomic_inc(&id_priv->refcount);
  2402. memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
  2403. if (cma_any_addr(dst_addr)) {
  2404. ret = cma_resolve_loopback(id_priv);
  2405. } else {
  2406. if (dst_addr->sa_family == AF_IB) {
  2407. ret = cma_resolve_ib_addr(id_priv);
  2408. } else {
  2409. ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
  2410. dst_addr, &id->route.addr.dev_addr,
  2411. timeout_ms, addr_handler, id_priv);
  2412. }
  2413. }
  2414. if (ret)
  2415. goto err;
  2416. return 0;
  2417. err:
  2418. cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
  2419. cma_deref_id(id_priv);
  2420. return ret;
  2421. }
  2422. EXPORT_SYMBOL(rdma_resolve_addr);
  2423. int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
  2424. {
  2425. struct rdma_id_private *id_priv;
  2426. unsigned long flags;
  2427. int ret;
  2428. id_priv = container_of(id, struct rdma_id_private, id);
  2429. spin_lock_irqsave(&id_priv->lock, flags);
  2430. if (reuse || id_priv->state == RDMA_CM_IDLE) {
  2431. id_priv->reuseaddr = reuse;
  2432. ret = 0;
  2433. } else {
  2434. ret = -EINVAL;
  2435. }
  2436. spin_unlock_irqrestore(&id_priv->lock, flags);
  2437. return ret;
  2438. }
  2439. EXPORT_SYMBOL(rdma_set_reuseaddr);
  2440. int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
  2441. {
  2442. struct rdma_id_private *id_priv;
  2443. unsigned long flags;
  2444. int ret;
  2445. id_priv = container_of(id, struct rdma_id_private, id);
  2446. spin_lock_irqsave(&id_priv->lock, flags);
  2447. if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
  2448. id_priv->options |= (1 << CMA_OPTION_AFONLY);
  2449. id_priv->afonly = afonly;
  2450. ret = 0;
  2451. } else {
  2452. ret = -EINVAL;
  2453. }
  2454. spin_unlock_irqrestore(&id_priv->lock, flags);
  2455. return ret;
  2456. }
  2457. EXPORT_SYMBOL(rdma_set_afonly);
  2458. static void cma_bind_port(struct rdma_bind_list *bind_list,
  2459. struct rdma_id_private *id_priv)
  2460. {
  2461. struct sockaddr *addr;
  2462. struct sockaddr_ib *sib;
  2463. u64 sid, mask;
  2464. __be16 port;
  2465. addr = cma_src_addr(id_priv);
  2466. port = htons(bind_list->port);
  2467. switch (addr->sa_family) {
  2468. case AF_INET:
  2469. ((struct sockaddr_in *) addr)->sin_port = port;
  2470. break;
  2471. case AF_INET6:
  2472. ((struct sockaddr_in6 *) addr)->sin6_port = port;
  2473. break;
  2474. case AF_IB:
  2475. sib = (struct sockaddr_ib *) addr;
  2476. sid = be64_to_cpu(sib->sib_sid);
  2477. mask = be64_to_cpu(sib->sib_sid_mask);
  2478. sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
  2479. sib->sib_sid_mask = cpu_to_be64(~0ULL);
  2480. break;
  2481. }
  2482. id_priv->bind_list = bind_list;
  2483. hlist_add_head(&id_priv->node, &bind_list->owners);
  2484. }
  2485. static int cma_alloc_port(enum rdma_port_space ps,
  2486. struct rdma_id_private *id_priv, unsigned short snum)
  2487. {
  2488. struct rdma_bind_list *bind_list;
  2489. int ret;
  2490. bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
  2491. if (!bind_list)
  2492. return -ENOMEM;
  2493. ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
  2494. snum);
  2495. if (ret < 0)
  2496. goto err;
  2497. bind_list->ps = ps;
  2498. bind_list->port = (unsigned short)ret;
  2499. cma_bind_port(bind_list, id_priv);
  2500. return 0;
  2501. err:
  2502. kfree(bind_list);
  2503. return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
  2504. }
  2505. static int cma_alloc_any_port(enum rdma_port_space ps,
  2506. struct rdma_id_private *id_priv)
  2507. {
  2508. static unsigned int last_used_port;
  2509. int low, high, remaining;
  2510. unsigned int rover;
  2511. struct net *net = id_priv->id.route.addr.dev_addr.net;
  2512. inet_get_local_port_range(net, &low, &high);
  2513. remaining = (high - low) + 1;
  2514. rover = prandom_u32() % remaining + low;
  2515. retry:
  2516. if (last_used_port != rover &&
  2517. !cma_ps_find(net, ps, (unsigned short)rover)) {
  2518. int ret = cma_alloc_port(ps, id_priv, rover);
  2519. /*
  2520. * Remember previously used port number in order to avoid
  2521. * re-using same port immediately after it is closed.
  2522. */
  2523. if (!ret)
  2524. last_used_port = rover;
  2525. if (ret != -EADDRNOTAVAIL)
  2526. return ret;
  2527. }
  2528. if (--remaining) {
  2529. rover++;
  2530. if ((rover < low) || (rover > high))
  2531. rover = low;
  2532. goto retry;
  2533. }
  2534. return -EADDRNOTAVAIL;
  2535. }
  2536. /*
  2537. * Check that the requested port is available. This is called when trying to
  2538. * bind to a specific port, or when trying to listen on a bound port. In
  2539. * the latter case, the provided id_priv may already be on the bind_list, but
  2540. * we still need to check that it's okay to start listening.
  2541. */
  2542. static int cma_check_port(struct rdma_bind_list *bind_list,
  2543. struct rdma_id_private *id_priv, uint8_t reuseaddr)
  2544. {
  2545. struct rdma_id_private *cur_id;
  2546. struct sockaddr *addr, *cur_addr;
  2547. addr = cma_src_addr(id_priv);
  2548. hlist_for_each_entry(cur_id, &bind_list->owners, node) {
  2549. if (id_priv == cur_id)
  2550. continue;
  2551. if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
  2552. cur_id->reuseaddr)
  2553. continue;
  2554. cur_addr = cma_src_addr(cur_id);
  2555. if (id_priv->afonly && cur_id->afonly &&
  2556. (addr->sa_family != cur_addr->sa_family))
  2557. continue;
  2558. if (cma_any_addr(addr) || cma_any_addr(cur_addr))
  2559. return -EADDRNOTAVAIL;
  2560. if (!cma_addr_cmp(addr, cur_addr))
  2561. return -EADDRINUSE;
  2562. }
  2563. return 0;
  2564. }
  2565. static int cma_use_port(enum rdma_port_space ps,
  2566. struct rdma_id_private *id_priv)
  2567. {
  2568. struct rdma_bind_list *bind_list;
  2569. unsigned short snum;
  2570. int ret;
  2571. snum = ntohs(cma_port(cma_src_addr(id_priv)));
  2572. if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  2573. return -EACCES;
  2574. bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
  2575. if (!bind_list) {
  2576. ret = cma_alloc_port(ps, id_priv, snum);
  2577. } else {
  2578. ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
  2579. if (!ret)
  2580. cma_bind_port(bind_list, id_priv);
  2581. }
  2582. return ret;
  2583. }
  2584. static int cma_bind_listen(struct rdma_id_private *id_priv)
  2585. {
  2586. struct rdma_bind_list *bind_list = id_priv->bind_list;
  2587. int ret = 0;
  2588. mutex_lock(&lock);
  2589. if (bind_list->owners.first->next)
  2590. ret = cma_check_port(bind_list, id_priv, 0);
  2591. mutex_unlock(&lock);
  2592. return ret;
  2593. }
  2594. static enum rdma_port_space cma_select_inet_ps(
  2595. struct rdma_id_private *id_priv)
  2596. {
  2597. switch (id_priv->id.ps) {
  2598. case RDMA_PS_TCP:
  2599. case RDMA_PS_UDP:
  2600. case RDMA_PS_IPOIB:
  2601. case RDMA_PS_IB:
  2602. return id_priv->id.ps;
  2603. default:
  2604. return 0;
  2605. }
  2606. }
  2607. static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
  2608. {
  2609. enum rdma_port_space ps = 0;
  2610. struct sockaddr_ib *sib;
  2611. u64 sid_ps, mask, sid;
  2612. sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
  2613. mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
  2614. sid = be64_to_cpu(sib->sib_sid) & mask;
  2615. if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
  2616. sid_ps = RDMA_IB_IP_PS_IB;
  2617. ps = RDMA_PS_IB;
  2618. } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
  2619. (sid == (RDMA_IB_IP_PS_TCP & mask))) {
  2620. sid_ps = RDMA_IB_IP_PS_TCP;
  2621. ps = RDMA_PS_TCP;
  2622. } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
  2623. (sid == (RDMA_IB_IP_PS_UDP & mask))) {
  2624. sid_ps = RDMA_IB_IP_PS_UDP;
  2625. ps = RDMA_PS_UDP;
  2626. }
  2627. if (ps) {
  2628. sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
  2629. sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
  2630. be64_to_cpu(sib->sib_sid_mask));
  2631. }
  2632. return ps;
  2633. }
  2634. static int cma_get_port(struct rdma_id_private *id_priv)
  2635. {
  2636. enum rdma_port_space ps;
  2637. int ret;
  2638. if (cma_family(id_priv) != AF_IB)
  2639. ps = cma_select_inet_ps(id_priv);
  2640. else
  2641. ps = cma_select_ib_ps(id_priv);
  2642. if (!ps)
  2643. return -EPROTONOSUPPORT;
  2644. mutex_lock(&lock);
  2645. if (cma_any_port(cma_src_addr(id_priv)))
  2646. ret = cma_alloc_any_port(ps, id_priv);
  2647. else
  2648. ret = cma_use_port(ps, id_priv);
  2649. mutex_unlock(&lock);
  2650. return ret;
  2651. }
  2652. static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
  2653. struct sockaddr *addr)
  2654. {
  2655. #if IS_ENABLED(CONFIG_IPV6)
  2656. struct sockaddr_in6 *sin6;
  2657. if (addr->sa_family != AF_INET6)
  2658. return 0;
  2659. sin6 = (struct sockaddr_in6 *) addr;
  2660. if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
  2661. return 0;
  2662. if (!sin6->sin6_scope_id)
  2663. return -EINVAL;
  2664. dev_addr->bound_dev_if = sin6->sin6_scope_id;
  2665. #endif
  2666. return 0;
  2667. }
  2668. int rdma_listen(struct rdma_cm_id *id, int backlog)
  2669. {
  2670. struct rdma_id_private *id_priv;
  2671. int ret;
  2672. id_priv = container_of(id, struct rdma_id_private, id);
  2673. if (id_priv->state == RDMA_CM_IDLE) {
  2674. id->route.addr.src_addr.ss_family = AF_INET;
  2675. ret = rdma_bind_addr(id, cma_src_addr(id_priv));
  2676. if (ret)
  2677. return ret;
  2678. }
  2679. if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
  2680. return -EINVAL;
  2681. if (id_priv->reuseaddr) {
  2682. ret = cma_bind_listen(id_priv);
  2683. if (ret)
  2684. goto err;
  2685. }
  2686. id_priv->backlog = backlog;
  2687. if (id->device) {
  2688. if (rdma_cap_ib_cm(id->device, 1)) {
  2689. ret = cma_ib_listen(id_priv);
  2690. if (ret)
  2691. goto err;
  2692. } else if (rdma_cap_iw_cm(id->device, 1)) {
  2693. ret = cma_iw_listen(id_priv, backlog);
  2694. if (ret)
  2695. goto err;
  2696. } else {
  2697. ret = -ENOSYS;
  2698. goto err;
  2699. }
  2700. } else
  2701. cma_listen_on_all(id_priv);
  2702. return 0;
  2703. err:
  2704. id_priv->backlog = 0;
  2705. cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
  2706. return ret;
  2707. }
  2708. EXPORT_SYMBOL(rdma_listen);
  2709. int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
  2710. {
  2711. struct rdma_id_private *id_priv;
  2712. int ret;
  2713. if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
  2714. addr->sa_family != AF_IB)
  2715. return -EAFNOSUPPORT;
  2716. id_priv = container_of(id, struct rdma_id_private, id);
  2717. if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
  2718. return -EINVAL;
  2719. ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
  2720. if (ret)
  2721. goto err1;
  2722. memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
  2723. if (!cma_any_addr(addr)) {
  2724. ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
  2725. if (ret)
  2726. goto err1;
  2727. ret = cma_acquire_dev(id_priv, NULL);
  2728. if (ret)
  2729. goto err1;
  2730. }
  2731. if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
  2732. if (addr->sa_family == AF_INET)
  2733. id_priv->afonly = 1;
  2734. #if IS_ENABLED(CONFIG_IPV6)
  2735. else if (addr->sa_family == AF_INET6) {
  2736. struct net *net = id_priv->id.route.addr.dev_addr.net;
  2737. id_priv->afonly = net->ipv6.sysctl.bindv6only;
  2738. }
  2739. #endif
  2740. }
  2741. ret = cma_get_port(id_priv);
  2742. if (ret)
  2743. goto err2;
  2744. return 0;
  2745. err2:
  2746. if (id_priv->cma_dev)
  2747. cma_release_dev(id_priv);
  2748. err1:
  2749. cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
  2750. return ret;
  2751. }
  2752. EXPORT_SYMBOL(rdma_bind_addr);
  2753. static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
  2754. {
  2755. struct cma_hdr *cma_hdr;
  2756. cma_hdr = hdr;
  2757. cma_hdr->cma_version = CMA_VERSION;
  2758. if (cma_family(id_priv) == AF_INET) {
  2759. struct sockaddr_in *src4, *dst4;
  2760. src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
  2761. dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
  2762. cma_set_ip_ver(cma_hdr, 4);
  2763. cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
  2764. cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
  2765. cma_hdr->port = src4->sin_port;
  2766. } else if (cma_family(id_priv) == AF_INET6) {
  2767. struct sockaddr_in6 *src6, *dst6;
  2768. src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
  2769. dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
  2770. cma_set_ip_ver(cma_hdr, 6);
  2771. cma_hdr->src_addr.ip6 = src6->sin6_addr;
  2772. cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
  2773. cma_hdr->port = src6->sin6_port;
  2774. }
  2775. return 0;
  2776. }
  2777. static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
  2778. struct ib_cm_event *ib_event)
  2779. {
  2780. struct rdma_id_private *id_priv = cm_id->context;
  2781. struct rdma_cm_event event;
  2782. struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
  2783. int ret = 0;
  2784. mutex_lock(&id_priv->handler_mutex);
  2785. if (id_priv->state != RDMA_CM_CONNECT)
  2786. goto out;
  2787. memset(&event, 0, sizeof event);
  2788. switch (ib_event->event) {
  2789. case IB_CM_SIDR_REQ_ERROR:
  2790. event.event = RDMA_CM_EVENT_UNREACHABLE;
  2791. event.status = -ETIMEDOUT;
  2792. break;
  2793. case IB_CM_SIDR_REP_RECEIVED:
  2794. event.param.ud.private_data = ib_event->private_data;
  2795. event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
  2796. if (rep->status != IB_SIDR_SUCCESS) {
  2797. event.event = RDMA_CM_EVENT_UNREACHABLE;
  2798. event.status = ib_event->param.sidr_rep_rcvd.status;
  2799. break;
  2800. }
  2801. ret = cma_set_qkey(id_priv, rep->qkey);
  2802. if (ret) {
  2803. event.event = RDMA_CM_EVENT_ADDR_ERROR;
  2804. event.status = ret;
  2805. break;
  2806. }
  2807. ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
  2808. id_priv->id.route.path_rec,
  2809. &event.param.ud.ah_attr);
  2810. event.param.ud.qp_num = rep->qpn;
  2811. event.param.ud.qkey = rep->qkey;
  2812. event.event = RDMA_CM_EVENT_ESTABLISHED;
  2813. event.status = 0;
  2814. break;
  2815. default:
  2816. pr_err("RDMA CMA: unexpected IB CM event: %d\n",
  2817. ib_event->event);
  2818. goto out;
  2819. }
  2820. ret = id_priv->id.event_handler(&id_priv->id, &event);
  2821. if (ret) {
  2822. /* Destroy the CM ID by returning a non-zero value. */
  2823. id_priv->cm_id.ib = NULL;
  2824. cma_exch(id_priv, RDMA_CM_DESTROYING);
  2825. mutex_unlock(&id_priv->handler_mutex);
  2826. rdma_destroy_id(&id_priv->id);
  2827. return ret;
  2828. }
  2829. out:
  2830. mutex_unlock(&id_priv->handler_mutex);
  2831. return ret;
  2832. }
  2833. static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
  2834. struct rdma_conn_param *conn_param)
  2835. {
  2836. struct ib_cm_sidr_req_param req;
  2837. struct ib_cm_id *id;
  2838. void *private_data;
  2839. u8 offset;
  2840. int ret;
  2841. memset(&req, 0, sizeof req);
  2842. offset = cma_user_data_offset(id_priv);
  2843. req.private_data_len = offset + conn_param->private_data_len;
  2844. if (req.private_data_len < conn_param->private_data_len)
  2845. return -EINVAL;
  2846. if (req.private_data_len) {
  2847. private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
  2848. if (!private_data)
  2849. return -ENOMEM;
  2850. } else {
  2851. private_data = NULL;
  2852. }
  2853. if (conn_param->private_data && conn_param->private_data_len)
  2854. memcpy(private_data + offset, conn_param->private_data,
  2855. conn_param->private_data_len);
  2856. if (private_data) {
  2857. ret = cma_format_hdr(private_data, id_priv);
  2858. if (ret)
  2859. goto out;
  2860. req.private_data = private_data;
  2861. }
  2862. id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
  2863. id_priv);
  2864. if (IS_ERR(id)) {
  2865. ret = PTR_ERR(id);
  2866. goto out;
  2867. }
  2868. id_priv->cm_id.ib = id;
  2869. req.path = id_priv->id.route.path_rec;
  2870. req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
  2871. req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
  2872. req.max_cm_retries = CMA_MAX_CM_RETRIES;
  2873. ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
  2874. if (ret) {
  2875. ib_destroy_cm_id(id_priv->cm_id.ib);
  2876. id_priv->cm_id.ib = NULL;
  2877. }
  2878. out:
  2879. kfree(private_data);
  2880. return ret;
  2881. }
  2882. static int cma_connect_ib(struct rdma_id_private *id_priv,
  2883. struct rdma_conn_param *conn_param)
  2884. {
  2885. struct ib_cm_req_param req;
  2886. struct rdma_route *route;
  2887. void *private_data;
  2888. struct ib_cm_id *id;
  2889. u8 offset;
  2890. int ret;
  2891. memset(&req, 0, sizeof req);
  2892. offset = cma_user_data_offset(id_priv);
  2893. req.private_data_len = offset + conn_param->private_data_len;
  2894. if (req.private_data_len < conn_param->private_data_len)
  2895. return -EINVAL;
  2896. if (req.private_data_len) {
  2897. private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
  2898. if (!private_data)
  2899. return -ENOMEM;
  2900. } else {
  2901. private_data = NULL;
  2902. }
  2903. if (conn_param->private_data && conn_param->private_data_len)
  2904. memcpy(private_data + offset, conn_param->private_data,
  2905. conn_param->private_data_len);
  2906. id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
  2907. if (IS_ERR(id)) {
  2908. ret = PTR_ERR(id);
  2909. goto out;
  2910. }
  2911. id_priv->cm_id.ib = id;
  2912. route = &id_priv->id.route;
  2913. if (private_data) {
  2914. ret = cma_format_hdr(private_data, id_priv);
  2915. if (ret)
  2916. goto out;
  2917. req.private_data = private_data;
  2918. }
  2919. req.primary_path = &route->path_rec[0];
  2920. if (route->num_paths == 2)
  2921. req.alternate_path = &route->path_rec[1];
  2922. req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
  2923. req.qp_num = id_priv->qp_num;
  2924. req.qp_type = id_priv->id.qp_type;
  2925. req.starting_psn = id_priv->seq_num;
  2926. req.responder_resources = conn_param->responder_resources;
  2927. req.initiator_depth = conn_param->initiator_depth;
  2928. req.flow_control = conn_param->flow_control;
  2929. req.retry_count = min_t(u8, 7, conn_param->retry_count);
  2930. req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
  2931. req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
  2932. req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
  2933. req.max_cm_retries = CMA_MAX_CM_RETRIES;
  2934. req.srq = id_priv->srq ? 1 : 0;
  2935. ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
  2936. out:
  2937. if (ret && !IS_ERR(id)) {
  2938. ib_destroy_cm_id(id);
  2939. id_priv->cm_id.ib = NULL;
  2940. }
  2941. kfree(private_data);
  2942. return ret;
  2943. }
  2944. static int cma_connect_iw(struct rdma_id_private *id_priv,
  2945. struct rdma_conn_param *conn_param)
  2946. {
  2947. struct iw_cm_id *cm_id;
  2948. int ret;
  2949. struct iw_cm_conn_param iw_param;
  2950. cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
  2951. if (IS_ERR(cm_id))
  2952. return PTR_ERR(cm_id);
  2953. cm_id->tos = id_priv->tos;
  2954. id_priv->cm_id.iw = cm_id;
  2955. memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
  2956. rdma_addr_size(cma_src_addr(id_priv)));
  2957. memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
  2958. rdma_addr_size(cma_dst_addr(id_priv)));
  2959. ret = cma_modify_qp_rtr(id_priv, conn_param);
  2960. if (ret)
  2961. goto out;
  2962. if (conn_param) {
  2963. iw_param.ord = conn_param->initiator_depth;
  2964. iw_param.ird = conn_param->responder_resources;
  2965. iw_param.private_data = conn_param->private_data;
  2966. iw_param.private_data_len = conn_param->private_data_len;
  2967. iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
  2968. } else {
  2969. memset(&iw_param, 0, sizeof iw_param);
  2970. iw_param.qpn = id_priv->qp_num;
  2971. }
  2972. ret = iw_cm_connect(cm_id, &iw_param);
  2973. out:
  2974. if (ret) {
  2975. iw_destroy_cm_id(cm_id);
  2976. id_priv->cm_id.iw = NULL;
  2977. }
  2978. return ret;
  2979. }
  2980. int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
  2981. {
  2982. struct rdma_id_private *id_priv;
  2983. int ret;
  2984. id_priv = container_of(id, struct rdma_id_private, id);
  2985. if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
  2986. return -EINVAL;
  2987. if (!id->qp) {
  2988. id_priv->qp_num = conn_param->qp_num;
  2989. id_priv->srq = conn_param->srq;
  2990. }
  2991. if (rdma_cap_ib_cm(id->device, id->port_num)) {
  2992. if (id->qp_type == IB_QPT_UD)
  2993. ret = cma_resolve_ib_udp(id_priv, conn_param);
  2994. else
  2995. ret = cma_connect_ib(id_priv, conn_param);
  2996. } else if (rdma_cap_iw_cm(id->device, id->port_num))
  2997. ret = cma_connect_iw(id_priv, conn_param);
  2998. else
  2999. ret = -ENOSYS;
  3000. if (ret)
  3001. goto err;
  3002. return 0;
  3003. err:
  3004. cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
  3005. return ret;
  3006. }
  3007. EXPORT_SYMBOL(rdma_connect);
  3008. static int cma_accept_ib(struct rdma_id_private *id_priv,
  3009. struct rdma_conn_param *conn_param)
  3010. {
  3011. struct ib_cm_rep_param rep;
  3012. int ret;
  3013. ret = cma_modify_qp_rtr(id_priv, conn_param);
  3014. if (ret)
  3015. goto out;
  3016. ret = cma_modify_qp_rts(id_priv, conn_param);
  3017. if (ret)
  3018. goto out;
  3019. memset(&rep, 0, sizeof rep);
  3020. rep.qp_num = id_priv->qp_num;
  3021. rep.starting_psn = id_priv->seq_num;
  3022. rep.private_data = conn_param->private_data;
  3023. rep.private_data_len = conn_param->private_data_len;
  3024. rep.responder_resources = conn_param->responder_resources;
  3025. rep.initiator_depth = conn_param->initiator_depth;
  3026. rep.failover_accepted = 0;
  3027. rep.flow_control = conn_param->flow_control;
  3028. rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
  3029. rep.srq = id_priv->srq ? 1 : 0;
  3030. ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
  3031. out:
  3032. return ret;
  3033. }
  3034. static int cma_accept_iw(struct rdma_id_private *id_priv,
  3035. struct rdma_conn_param *conn_param)
  3036. {
  3037. struct iw_cm_conn_param iw_param;
  3038. int ret;
  3039. if (!conn_param)
  3040. return -EINVAL;
  3041. ret = cma_modify_qp_rtr(id_priv, conn_param);
  3042. if (ret)
  3043. return ret;
  3044. iw_param.ord = conn_param->initiator_depth;
  3045. iw_param.ird = conn_param->responder_resources;
  3046. iw_param.private_data = conn_param->private_data;
  3047. iw_param.private_data_len = conn_param->private_data_len;
  3048. if (id_priv->id.qp) {
  3049. iw_param.qpn = id_priv->qp_num;
  3050. } else
  3051. iw_param.qpn = conn_param->qp_num;
  3052. return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
  3053. }
  3054. static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
  3055. enum ib_cm_sidr_status status, u32 qkey,
  3056. const void *private_data, int private_data_len)
  3057. {
  3058. struct ib_cm_sidr_rep_param rep;
  3059. int ret;
  3060. memset(&rep, 0, sizeof rep);
  3061. rep.status = status;
  3062. if (status == IB_SIDR_SUCCESS) {
  3063. ret = cma_set_qkey(id_priv, qkey);
  3064. if (ret)
  3065. return ret;
  3066. rep.qp_num = id_priv->qp_num;
  3067. rep.qkey = id_priv->qkey;
  3068. }
  3069. rep.private_data = private_data;
  3070. rep.private_data_len = private_data_len;
  3071. return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
  3072. }
  3073. int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
  3074. {
  3075. struct rdma_id_private *id_priv;
  3076. int ret;
  3077. id_priv = container_of(id, struct rdma_id_private, id);
  3078. id_priv->owner = task_pid_nr(current);
  3079. if (!cma_comp(id_priv, RDMA_CM_CONNECT))
  3080. return -EINVAL;
  3081. if (!id->qp && conn_param) {
  3082. id_priv->qp_num = conn_param->qp_num;
  3083. id_priv->srq = conn_param->srq;
  3084. }
  3085. if (rdma_cap_ib_cm(id->device, id->port_num)) {
  3086. if (id->qp_type == IB_QPT_UD) {
  3087. if (conn_param)
  3088. ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
  3089. conn_param->qkey,
  3090. conn_param->private_data,
  3091. conn_param->private_data_len);
  3092. else
  3093. ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
  3094. 0, NULL, 0);
  3095. } else {
  3096. if (conn_param)
  3097. ret = cma_accept_ib(id_priv, conn_param);
  3098. else
  3099. ret = cma_rep_recv(id_priv);
  3100. }
  3101. } else if (rdma_cap_iw_cm(id->device, id->port_num))
  3102. ret = cma_accept_iw(id_priv, conn_param);
  3103. else
  3104. ret = -ENOSYS;
  3105. if (ret)
  3106. goto reject;
  3107. return 0;
  3108. reject:
  3109. cma_modify_qp_err(id_priv);
  3110. rdma_reject(id, NULL, 0);
  3111. return ret;
  3112. }
  3113. EXPORT_SYMBOL(rdma_accept);
  3114. int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
  3115. {
  3116. struct rdma_id_private *id_priv;
  3117. int ret;
  3118. id_priv = container_of(id, struct rdma_id_private, id);
  3119. if (!id_priv->cm_id.ib)
  3120. return -EINVAL;
  3121. switch (id->device->node_type) {
  3122. case RDMA_NODE_IB_CA:
  3123. ret = ib_cm_notify(id_priv->cm_id.ib, event);
  3124. break;
  3125. default:
  3126. ret = 0;
  3127. break;
  3128. }
  3129. return ret;
  3130. }
  3131. EXPORT_SYMBOL(rdma_notify);
  3132. int rdma_reject(struct rdma_cm_id *id, const void *private_data,
  3133. u8 private_data_len)
  3134. {
  3135. struct rdma_id_private *id_priv;
  3136. int ret;
  3137. id_priv = container_of(id, struct rdma_id_private, id);
  3138. if (!id_priv->cm_id.ib)
  3139. return -EINVAL;
  3140. if (rdma_cap_ib_cm(id->device, id->port_num)) {
  3141. if (id->qp_type == IB_QPT_UD)
  3142. ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
  3143. private_data, private_data_len);
  3144. else
  3145. ret = ib_send_cm_rej(id_priv->cm_id.ib,
  3146. IB_CM_REJ_CONSUMER_DEFINED, NULL,
  3147. 0, private_data, private_data_len);
  3148. } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
  3149. ret = iw_cm_reject(id_priv->cm_id.iw,
  3150. private_data, private_data_len);
  3151. } else
  3152. ret = -ENOSYS;
  3153. return ret;
  3154. }
  3155. EXPORT_SYMBOL(rdma_reject);
  3156. int rdma_disconnect(struct rdma_cm_id *id)
  3157. {
  3158. struct rdma_id_private *id_priv;
  3159. int ret;
  3160. id_priv = container_of(id, struct rdma_id_private, id);
  3161. if (!id_priv->cm_id.ib)
  3162. return -EINVAL;
  3163. if (rdma_cap_ib_cm(id->device, id->port_num)) {
  3164. ret = cma_modify_qp_err(id_priv);
  3165. if (ret)
  3166. goto out;
  3167. /* Initiate or respond to a disconnect. */
  3168. if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
  3169. ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
  3170. } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
  3171. ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
  3172. } else
  3173. ret = -EINVAL;
  3174. out:
  3175. return ret;
  3176. }
  3177. EXPORT_SYMBOL(rdma_disconnect);
  3178. static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
  3179. {
  3180. struct rdma_id_private *id_priv;
  3181. struct cma_multicast *mc = multicast->context;
  3182. struct rdma_cm_event event;
  3183. int ret = 0;
  3184. id_priv = mc->id_priv;
  3185. mutex_lock(&id_priv->handler_mutex);
  3186. if (id_priv->state != RDMA_CM_ADDR_BOUND &&
  3187. id_priv->state != RDMA_CM_ADDR_RESOLVED)
  3188. goto out;
  3189. if (!status)
  3190. status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
  3191. mutex_lock(&id_priv->qp_mutex);
  3192. if (!status && id_priv->id.qp)
  3193. status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
  3194. be16_to_cpu(multicast->rec.mlid));
  3195. mutex_unlock(&id_priv->qp_mutex);
  3196. memset(&event, 0, sizeof event);
  3197. event.status = status;
  3198. event.param.ud.private_data = mc->context;
  3199. if (!status) {
  3200. struct rdma_dev_addr *dev_addr =
  3201. &id_priv->id.route.addr.dev_addr;
  3202. struct net_device *ndev =
  3203. dev_get_by_index(&init_net, dev_addr->bound_dev_if);
  3204. enum ib_gid_type gid_type =
  3205. id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
  3206. rdma_start_port(id_priv->cma_dev->device)];
  3207. event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
  3208. ib_init_ah_from_mcmember(id_priv->id.device,
  3209. id_priv->id.port_num, &multicast->rec,
  3210. ndev, gid_type,
  3211. &event.param.ud.ah_attr);
  3212. event.param.ud.qp_num = 0xFFFFFF;
  3213. event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
  3214. if (ndev)
  3215. dev_put(ndev);
  3216. } else
  3217. event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
  3218. ret = id_priv->id.event_handler(&id_priv->id, &event);
  3219. if (ret) {
  3220. cma_exch(id_priv, RDMA_CM_DESTROYING);
  3221. mutex_unlock(&id_priv->handler_mutex);
  3222. rdma_destroy_id(&id_priv->id);
  3223. return 0;
  3224. }
  3225. out:
  3226. mutex_unlock(&id_priv->handler_mutex);
  3227. return 0;
  3228. }
  3229. static void cma_set_mgid(struct rdma_id_private *id_priv,
  3230. struct sockaddr *addr, union ib_gid *mgid)
  3231. {
  3232. unsigned char mc_map[MAX_ADDR_LEN];
  3233. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  3234. struct sockaddr_in *sin = (struct sockaddr_in *) addr;
  3235. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
  3236. if (cma_any_addr(addr)) {
  3237. memset(mgid, 0, sizeof *mgid);
  3238. } else if ((addr->sa_family == AF_INET6) &&
  3239. ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
  3240. 0xFF10A01B)) {
  3241. /* IPv6 address is an SA assigned MGID. */
  3242. memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
  3243. } else if (addr->sa_family == AF_IB) {
  3244. memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
  3245. } else if ((addr->sa_family == AF_INET6)) {
  3246. ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
  3247. if (id_priv->id.ps == RDMA_PS_UDP)
  3248. mc_map[7] = 0x01; /* Use RDMA CM signature */
  3249. *mgid = *(union ib_gid *) (mc_map + 4);
  3250. } else {
  3251. ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
  3252. if (id_priv->id.ps == RDMA_PS_UDP)
  3253. mc_map[7] = 0x01; /* Use RDMA CM signature */
  3254. *mgid = *(union ib_gid *) (mc_map + 4);
  3255. }
  3256. }
  3257. static void cma_query_sa_classport_info_cb(int status,
  3258. struct ib_class_port_info *rec,
  3259. void *context)
  3260. {
  3261. struct class_port_info_context *cb_ctx = context;
  3262. WARN_ON(!context);
  3263. if (status || !rec) {
  3264. pr_debug("RDMA CM: %s port %u failed query ClassPortInfo status: %d\n",
  3265. cb_ctx->device->name, cb_ctx->port_num, status);
  3266. goto out;
  3267. }
  3268. memcpy(cb_ctx->class_port_info, rec, sizeof(struct ib_class_port_info));
  3269. out:
  3270. complete(&cb_ctx->done);
  3271. }
  3272. static int cma_query_sa_classport_info(struct ib_device *device, u8 port_num,
  3273. struct ib_class_port_info *class_port_info)
  3274. {
  3275. struct class_port_info_context *cb_ctx;
  3276. int ret;
  3277. cb_ctx = kmalloc(sizeof(*cb_ctx), GFP_KERNEL);
  3278. if (!cb_ctx)
  3279. return -ENOMEM;
  3280. cb_ctx->device = device;
  3281. cb_ctx->class_port_info = class_port_info;
  3282. cb_ctx->port_num = port_num;
  3283. init_completion(&cb_ctx->done);
  3284. ret = ib_sa_classport_info_rec_query(&sa_client, device, port_num,
  3285. CMA_QUERY_CLASSPORT_INFO_TIMEOUT,
  3286. GFP_KERNEL, cma_query_sa_classport_info_cb,
  3287. cb_ctx, &cb_ctx->sa_query);
  3288. if (ret < 0) {
  3289. pr_err("RDMA CM: %s port %u failed to send ClassPortInfo query, ret: %d\n",
  3290. device->name, port_num, ret);
  3291. goto out;
  3292. }
  3293. wait_for_completion(&cb_ctx->done);
  3294. out:
  3295. kfree(cb_ctx);
  3296. return ret;
  3297. }
  3298. static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
  3299. struct cma_multicast *mc)
  3300. {
  3301. struct ib_sa_mcmember_rec rec;
  3302. struct ib_class_port_info class_port_info;
  3303. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  3304. ib_sa_comp_mask comp_mask;
  3305. int ret;
  3306. ib_addr_get_mgid(dev_addr, &rec.mgid);
  3307. ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
  3308. &rec.mgid, &rec);
  3309. if (ret)
  3310. return ret;
  3311. ret = cma_set_qkey(id_priv, 0);
  3312. if (ret)
  3313. return ret;
  3314. cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
  3315. rec.qkey = cpu_to_be32(id_priv->qkey);
  3316. rdma_addr_get_sgid(dev_addr, &rec.port_gid);
  3317. rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  3318. rec.join_state = mc->join_state;
  3319. if (rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) {
  3320. ret = cma_query_sa_classport_info(id_priv->id.device,
  3321. id_priv->id.port_num,
  3322. &class_port_info);
  3323. if (ret)
  3324. return ret;
  3325. if (!(ib_get_cpi_capmask2(&class_port_info) &
  3326. IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT)) {
  3327. pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
  3328. "RDMA CM: SM doesn't support Send Only Full Member option\n",
  3329. id_priv->id.device->name, id_priv->id.port_num);
  3330. return -EOPNOTSUPP;
  3331. }
  3332. }
  3333. comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
  3334. IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
  3335. IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
  3336. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  3337. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
  3338. if (id_priv->id.ps == RDMA_PS_IPOIB)
  3339. comp_mask |= IB_SA_MCMEMBER_REC_RATE |
  3340. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  3341. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  3342. IB_SA_MCMEMBER_REC_MTU |
  3343. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  3344. mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
  3345. id_priv->id.port_num, &rec,
  3346. comp_mask, GFP_KERNEL,
  3347. cma_ib_mc_handler, mc);
  3348. return PTR_ERR_OR_ZERO(mc->multicast.ib);
  3349. }
  3350. static void iboe_mcast_work_handler(struct work_struct *work)
  3351. {
  3352. struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
  3353. struct cma_multicast *mc = mw->mc;
  3354. struct ib_sa_multicast *m = mc->multicast.ib;
  3355. mc->multicast.ib->context = mc;
  3356. cma_ib_mc_handler(0, m);
  3357. kref_put(&mc->mcref, release_mc);
  3358. kfree(mw);
  3359. }
  3360. static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
  3361. {
  3362. struct sockaddr_in *sin = (struct sockaddr_in *)addr;
  3363. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
  3364. if (cma_any_addr(addr)) {
  3365. memset(mgid, 0, sizeof *mgid);
  3366. } else if (addr->sa_family == AF_INET6) {
  3367. memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
  3368. } else {
  3369. mgid->raw[0] = 0xff;
  3370. mgid->raw[1] = 0x0e;
  3371. mgid->raw[2] = 0;
  3372. mgid->raw[3] = 0;
  3373. mgid->raw[4] = 0;
  3374. mgid->raw[5] = 0;
  3375. mgid->raw[6] = 0;
  3376. mgid->raw[7] = 0;
  3377. mgid->raw[8] = 0;
  3378. mgid->raw[9] = 0;
  3379. mgid->raw[10] = 0xff;
  3380. mgid->raw[11] = 0xff;
  3381. *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
  3382. }
  3383. }
  3384. static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
  3385. struct cma_multicast *mc)
  3386. {
  3387. struct iboe_mcast_work *work;
  3388. struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
  3389. int err = 0;
  3390. struct sockaddr *addr = (struct sockaddr *)&mc->addr;
  3391. struct net_device *ndev = NULL;
  3392. enum ib_gid_type gid_type;
  3393. bool send_only;
  3394. send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
  3395. if (cma_zero_addr((struct sockaddr *)&mc->addr))
  3396. return -EINVAL;
  3397. work = kzalloc(sizeof *work, GFP_KERNEL);
  3398. if (!work)
  3399. return -ENOMEM;
  3400. mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
  3401. if (!mc->multicast.ib) {
  3402. err = -ENOMEM;
  3403. goto out1;
  3404. }
  3405. cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
  3406. mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
  3407. if (id_priv->id.ps == RDMA_PS_UDP)
  3408. mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
  3409. if (dev_addr->bound_dev_if)
  3410. ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
  3411. if (!ndev) {
  3412. err = -ENODEV;
  3413. goto out2;
  3414. }
  3415. mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
  3416. mc->multicast.ib->rec.hop_limit = 1;
  3417. mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
  3418. gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
  3419. rdma_start_port(id_priv->cma_dev->device)];
  3420. if (addr->sa_family == AF_INET) {
  3421. if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
  3422. mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
  3423. if (!send_only) {
  3424. err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
  3425. true);
  3426. if (!err)
  3427. mc->igmp_joined = true;
  3428. }
  3429. }
  3430. } else {
  3431. if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
  3432. err = -ENOTSUPP;
  3433. }
  3434. dev_put(ndev);
  3435. if (err || !mc->multicast.ib->rec.mtu) {
  3436. if (!err)
  3437. err = -EINVAL;
  3438. goto out2;
  3439. }
  3440. rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
  3441. &mc->multicast.ib->rec.port_gid);
  3442. work->id = id_priv;
  3443. work->mc = mc;
  3444. INIT_WORK(&work->work, iboe_mcast_work_handler);
  3445. kref_get(&mc->mcref);
  3446. queue_work(cma_wq, &work->work);
  3447. return 0;
  3448. out2:
  3449. kfree(mc->multicast.ib);
  3450. out1:
  3451. kfree(work);
  3452. return err;
  3453. }
  3454. int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
  3455. u8 join_state, void *context)
  3456. {
  3457. struct rdma_id_private *id_priv;
  3458. struct cma_multicast *mc;
  3459. int ret;
  3460. if (!id->device)
  3461. return -EINVAL;
  3462. id_priv = container_of(id, struct rdma_id_private, id);
  3463. if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
  3464. !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
  3465. return -EINVAL;
  3466. mc = kmalloc(sizeof *mc, GFP_KERNEL);
  3467. if (!mc)
  3468. return -ENOMEM;
  3469. memcpy(&mc->addr, addr, rdma_addr_size(addr));
  3470. mc->context = context;
  3471. mc->id_priv = id_priv;
  3472. mc->igmp_joined = false;
  3473. mc->join_state = join_state;
  3474. spin_lock(&id_priv->lock);
  3475. list_add(&mc->list, &id_priv->mc_list);
  3476. spin_unlock(&id_priv->lock);
  3477. if (rdma_protocol_roce(id->device, id->port_num)) {
  3478. kref_init(&mc->mcref);
  3479. ret = cma_iboe_join_multicast(id_priv, mc);
  3480. } else if (rdma_cap_ib_mcast(id->device, id->port_num))
  3481. ret = cma_join_ib_multicast(id_priv, mc);
  3482. else
  3483. ret = -ENOSYS;
  3484. if (ret) {
  3485. spin_lock_irq(&id_priv->lock);
  3486. list_del(&mc->list);
  3487. spin_unlock_irq(&id_priv->lock);
  3488. kfree(mc);
  3489. }
  3490. return ret;
  3491. }
  3492. EXPORT_SYMBOL(rdma_join_multicast);
  3493. void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
  3494. {
  3495. struct rdma_id_private *id_priv;
  3496. struct cma_multicast *mc;
  3497. id_priv = container_of(id, struct rdma_id_private, id);
  3498. spin_lock_irq(&id_priv->lock);
  3499. list_for_each_entry(mc, &id_priv->mc_list, list) {
  3500. if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
  3501. list_del(&mc->list);
  3502. spin_unlock_irq(&id_priv->lock);
  3503. if (id->qp)
  3504. ib_detach_mcast(id->qp,
  3505. &mc->multicast.ib->rec.mgid,
  3506. be16_to_cpu(mc->multicast.ib->rec.mlid));
  3507. BUG_ON(id_priv->cma_dev->device != id->device);
  3508. if (rdma_cap_ib_mcast(id->device, id->port_num)) {
  3509. ib_sa_free_multicast(mc->multicast.ib);
  3510. kfree(mc);
  3511. } else if (rdma_protocol_roce(id->device, id->port_num)) {
  3512. if (mc->igmp_joined) {
  3513. struct rdma_dev_addr *dev_addr =
  3514. &id->route.addr.dev_addr;
  3515. struct net_device *ndev = NULL;
  3516. if (dev_addr->bound_dev_if)
  3517. ndev = dev_get_by_index(&init_net,
  3518. dev_addr->bound_dev_if);
  3519. if (ndev) {
  3520. cma_igmp_send(ndev,
  3521. &mc->multicast.ib->rec.mgid,
  3522. false);
  3523. dev_put(ndev);
  3524. }
  3525. mc->igmp_joined = false;
  3526. }
  3527. kref_put(&mc->mcref, release_mc);
  3528. }
  3529. return;
  3530. }
  3531. }
  3532. spin_unlock_irq(&id_priv->lock);
  3533. }
  3534. EXPORT_SYMBOL(rdma_leave_multicast);
  3535. static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
  3536. {
  3537. struct rdma_dev_addr *dev_addr;
  3538. struct cma_ndev_work *work;
  3539. dev_addr = &id_priv->id.route.addr.dev_addr;
  3540. if ((dev_addr->bound_dev_if == ndev->ifindex) &&
  3541. (net_eq(dev_net(ndev), dev_addr->net)) &&
  3542. memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
  3543. pr_info("RDMA CM addr change for ndev %s used by id %p\n",
  3544. ndev->name, &id_priv->id);
  3545. work = kzalloc(sizeof *work, GFP_KERNEL);
  3546. if (!work)
  3547. return -ENOMEM;
  3548. INIT_WORK(&work->work, cma_ndev_work_handler);
  3549. work->id = id_priv;
  3550. work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
  3551. atomic_inc(&id_priv->refcount);
  3552. queue_work(cma_wq, &work->work);
  3553. }
  3554. return 0;
  3555. }
  3556. static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
  3557. void *ptr)
  3558. {
  3559. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  3560. struct cma_device *cma_dev;
  3561. struct rdma_id_private *id_priv;
  3562. int ret = NOTIFY_DONE;
  3563. if (event != NETDEV_BONDING_FAILOVER)
  3564. return NOTIFY_DONE;
  3565. if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
  3566. return NOTIFY_DONE;
  3567. mutex_lock(&lock);
  3568. list_for_each_entry(cma_dev, &dev_list, list)
  3569. list_for_each_entry(id_priv, &cma_dev->id_list, list) {
  3570. ret = cma_netdev_change(ndev, id_priv);
  3571. if (ret)
  3572. goto out;
  3573. }
  3574. out:
  3575. mutex_unlock(&lock);
  3576. return ret;
  3577. }
  3578. static struct notifier_block cma_nb = {
  3579. .notifier_call = cma_netdev_callback
  3580. };
  3581. static void cma_add_one(struct ib_device *device)
  3582. {
  3583. struct cma_device *cma_dev;
  3584. struct rdma_id_private *id_priv;
  3585. unsigned int i;
  3586. unsigned long supported_gids = 0;
  3587. cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
  3588. if (!cma_dev)
  3589. return;
  3590. cma_dev->device = device;
  3591. cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
  3592. sizeof(*cma_dev->default_gid_type),
  3593. GFP_KERNEL);
  3594. if (!cma_dev->default_gid_type) {
  3595. kfree(cma_dev);
  3596. return;
  3597. }
  3598. for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
  3599. supported_gids = roce_gid_type_mask_support(device, i);
  3600. WARN_ON(!supported_gids);
  3601. cma_dev->default_gid_type[i - rdma_start_port(device)] =
  3602. find_first_bit(&supported_gids, BITS_PER_LONG);
  3603. }
  3604. init_completion(&cma_dev->comp);
  3605. atomic_set(&cma_dev->refcount, 1);
  3606. INIT_LIST_HEAD(&cma_dev->id_list);
  3607. ib_set_client_data(device, &cma_client, cma_dev);
  3608. mutex_lock(&lock);
  3609. list_add_tail(&cma_dev->list, &dev_list);
  3610. list_for_each_entry(id_priv, &listen_any_list, list)
  3611. cma_listen_on_dev(id_priv, cma_dev);
  3612. mutex_unlock(&lock);
  3613. }
  3614. static int cma_remove_id_dev(struct rdma_id_private *id_priv)
  3615. {
  3616. struct rdma_cm_event event;
  3617. enum rdma_cm_state state;
  3618. int ret = 0;
  3619. /* Record that we want to remove the device */
  3620. state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
  3621. if (state == RDMA_CM_DESTROYING)
  3622. return 0;
  3623. cma_cancel_operation(id_priv, state);
  3624. mutex_lock(&id_priv->handler_mutex);
  3625. /* Check for destruction from another callback. */
  3626. if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
  3627. goto out;
  3628. memset(&event, 0, sizeof event);
  3629. event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
  3630. ret = id_priv->id.event_handler(&id_priv->id, &event);
  3631. out:
  3632. mutex_unlock(&id_priv->handler_mutex);
  3633. return ret;
  3634. }
  3635. static void cma_process_remove(struct cma_device *cma_dev)
  3636. {
  3637. struct rdma_id_private *id_priv;
  3638. int ret;
  3639. mutex_lock(&lock);
  3640. while (!list_empty(&cma_dev->id_list)) {
  3641. id_priv = list_entry(cma_dev->id_list.next,
  3642. struct rdma_id_private, list);
  3643. list_del(&id_priv->listen_list);
  3644. list_del_init(&id_priv->list);
  3645. atomic_inc(&id_priv->refcount);
  3646. mutex_unlock(&lock);
  3647. ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
  3648. cma_deref_id(id_priv);
  3649. if (ret)
  3650. rdma_destroy_id(&id_priv->id);
  3651. mutex_lock(&lock);
  3652. }
  3653. mutex_unlock(&lock);
  3654. cma_deref_dev(cma_dev);
  3655. wait_for_completion(&cma_dev->comp);
  3656. }
  3657. static void cma_remove_one(struct ib_device *device, void *client_data)
  3658. {
  3659. struct cma_device *cma_dev = client_data;
  3660. if (!cma_dev)
  3661. return;
  3662. mutex_lock(&lock);
  3663. list_del(&cma_dev->list);
  3664. mutex_unlock(&lock);
  3665. cma_process_remove(cma_dev);
  3666. kfree(cma_dev->default_gid_type);
  3667. kfree(cma_dev);
  3668. }
  3669. static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
  3670. {
  3671. struct nlmsghdr *nlh;
  3672. struct rdma_cm_id_stats *id_stats;
  3673. struct rdma_id_private *id_priv;
  3674. struct rdma_cm_id *id = NULL;
  3675. struct cma_device *cma_dev;
  3676. int i_dev = 0, i_id = 0;
  3677. /*
  3678. * We export all of the IDs as a sequence of messages. Each
  3679. * ID gets its own netlink message.
  3680. */
  3681. mutex_lock(&lock);
  3682. list_for_each_entry(cma_dev, &dev_list, list) {
  3683. if (i_dev < cb->args[0]) {
  3684. i_dev++;
  3685. continue;
  3686. }
  3687. i_id = 0;
  3688. list_for_each_entry(id_priv, &cma_dev->id_list, list) {
  3689. if (i_id < cb->args[1]) {
  3690. i_id++;
  3691. continue;
  3692. }
  3693. id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
  3694. sizeof *id_stats, RDMA_NL_RDMA_CM,
  3695. RDMA_NL_RDMA_CM_ID_STATS,
  3696. NLM_F_MULTI);
  3697. if (!id_stats)
  3698. goto out;
  3699. memset(id_stats, 0, sizeof *id_stats);
  3700. id = &id_priv->id;
  3701. id_stats->node_type = id->route.addr.dev_addr.dev_type;
  3702. id_stats->port_num = id->port_num;
  3703. id_stats->bound_dev_if =
  3704. id->route.addr.dev_addr.bound_dev_if;
  3705. if (ibnl_put_attr(skb, nlh,
  3706. rdma_addr_size(cma_src_addr(id_priv)),
  3707. cma_src_addr(id_priv),
  3708. RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
  3709. goto out;
  3710. if (ibnl_put_attr(skb, nlh,
  3711. rdma_addr_size(cma_dst_addr(id_priv)),
  3712. cma_dst_addr(id_priv),
  3713. RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
  3714. goto out;
  3715. id_stats->pid = id_priv->owner;
  3716. id_stats->port_space = id->ps;
  3717. id_stats->cm_state = id_priv->state;
  3718. id_stats->qp_num = id_priv->qp_num;
  3719. id_stats->qp_type = id->qp_type;
  3720. i_id++;
  3721. }
  3722. cb->args[1] = 0;
  3723. i_dev++;
  3724. }
  3725. out:
  3726. mutex_unlock(&lock);
  3727. cb->args[0] = i_dev;
  3728. cb->args[1] = i_id;
  3729. return skb->len;
  3730. }
  3731. static const struct ibnl_client_cbs cma_cb_table[] = {
  3732. [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
  3733. .module = THIS_MODULE },
  3734. };
  3735. static int cma_init_net(struct net *net)
  3736. {
  3737. struct cma_pernet *pernet = cma_pernet(net);
  3738. idr_init(&pernet->tcp_ps);
  3739. idr_init(&pernet->udp_ps);
  3740. idr_init(&pernet->ipoib_ps);
  3741. idr_init(&pernet->ib_ps);
  3742. return 0;
  3743. }
  3744. static void cma_exit_net(struct net *net)
  3745. {
  3746. struct cma_pernet *pernet = cma_pernet(net);
  3747. idr_destroy(&pernet->tcp_ps);
  3748. idr_destroy(&pernet->udp_ps);
  3749. idr_destroy(&pernet->ipoib_ps);
  3750. idr_destroy(&pernet->ib_ps);
  3751. }
  3752. static struct pernet_operations cma_pernet_operations = {
  3753. .init = cma_init_net,
  3754. .exit = cma_exit_net,
  3755. .id = &cma_pernet_id,
  3756. .size = sizeof(struct cma_pernet),
  3757. };
  3758. static int __init cma_init(void)
  3759. {
  3760. int ret;
  3761. cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
  3762. if (!cma_wq)
  3763. return -ENOMEM;
  3764. ret = register_pernet_subsys(&cma_pernet_operations);
  3765. if (ret)
  3766. goto err_wq;
  3767. ib_sa_register_client(&sa_client);
  3768. rdma_addr_register_client(&addr_client);
  3769. register_netdevice_notifier(&cma_nb);
  3770. ret = ib_register_client(&cma_client);
  3771. if (ret)
  3772. goto err;
  3773. if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table),
  3774. cma_cb_table))
  3775. pr_warn("RDMA CMA: failed to add netlink callback\n");
  3776. cma_configfs_init();
  3777. return 0;
  3778. err:
  3779. unregister_netdevice_notifier(&cma_nb);
  3780. rdma_addr_unregister_client(&addr_client);
  3781. ib_sa_unregister_client(&sa_client);
  3782. err_wq:
  3783. destroy_workqueue(cma_wq);
  3784. return ret;
  3785. }
  3786. static void __exit cma_cleanup(void)
  3787. {
  3788. cma_configfs_exit();
  3789. ibnl_remove_client(RDMA_NL_RDMA_CM);
  3790. ib_unregister_client(&cma_client);
  3791. unregister_netdevice_notifier(&cma_nb);
  3792. rdma_addr_unregister_client(&addr_client);
  3793. ib_sa_unregister_client(&sa_client);
  3794. unregister_pernet_subsys(&cma_pernet_operations);
  3795. destroy_workqueue(cma_wq);
  3796. }
  3797. module_init(cma_init);
  3798. module_exit(cma_cleanup);