You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipoib.c 21KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /*
  2. This software is available to you under a choice of one of two
  3. licenses. You may choose to be licensed under the terms of the GNU
  4. General Public License (GPL) Version 2, available at
  5. <http://www.fsf.org/copyleft/gpl.html>, or the OpenIB.org BSD
  6. license, available in the LICENSE.TXT file accompanying this
  7. software. These details are also available at
  8. <http://openib.org/license.html>.
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. SOFTWARE.
  17. Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved.
  18. */
  19. #include "ipoib.h"
  20. #include "ib_driver.h"
  21. #include "ib_mad.h"
  22. static const __u8 arp_packet_template[] = {
  23. 0x00, 0x20, /* hardware type */
  24. 0x08, 0x00, /* protocol type */
  25. 20, /* hw size */
  26. 4, /* protocol size */
  27. 0x00, 0x00, /* opcode */
  28. 0, 0, 0, 0, 0,
  29. 0, 0, 0, 0, 0,
  30. 0, 0, 0, 0, 0,
  31. 0, 0, 0, 0, 0, /* sender's mac */
  32. 0, 0, 0, 0, /* sender's IP address */
  33. 0, 0, 0, 0, 0,
  34. 0, 0, 0, 0, 0,
  35. 0, 0, 0, 0, 0,
  36. 0, 0, 0, 0, 0, /* Target's mac */
  37. 0, 0, 0, 0 /* targets's IP address */
  38. };
  39. struct ipoib_data_st {
  40. __u32 ipoib_qpn;
  41. udqp_t ipoib_qph;
  42. ud_av_t bcast_av;
  43. cq_t snd_cqh;
  44. cq_t rcv_cqh;
  45. __u8 *port_gid_raw;
  46. } ipoib_data;
  47. #define NUM_MAC_ENTRIES (NUM_AVS+2)
  48. static struct mac_xlation_st mac_tbl[NUM_MAC_ENTRIES];
  49. static __u32 mac_counter = 1;
  50. static __u32 youth_counter = 0;
  51. #define EQUAL_GUIDS(g1, g2) ( \
  52. ((g1)[0]==(g2)[0]) && \
  53. ((g1)[1]==(g2)[1]) && \
  54. ((g1)[2]==(g2)[2]) && \
  55. ((g1)[3]==(g2)[3]) && \
  56. ((g1)[4]==(g2)[4]) && \
  57. ((g1)[5]==(g2)[5]) && \
  58. ((g1)[6]==(g2)[6]) && \
  59. ((g1)[7]==(g2)[7]) )
  60. #define MAC_IDX(i) (((mac_tbl[i].eth_mac_lsb[0])<<16) | \
  61. ((mac_tbl[i].eth_mac_lsb[0])<<8) | \
  62. (mac_tbl[i].eth_mac_lsb[0]))
  63. static inline const void *qpn2buf(__u32 qpn, const void *buf)
  64. {
  65. ((__u8 *) buf)[0] = qpn >> 16;
  66. ((__u8 *) buf)[1] = (qpn >> 8) & 0xff;
  67. ((__u8 *) buf)[2] = qpn & 0xff;
  68. return buf;
  69. }
  70. static inline __u32 buf2qpn(const void *buf)
  71. {
  72. __u32 qpn;
  73. qpn = ((((__u8 *) buf)[0]) << 16) +
  74. ((((__u8 *) buf)[1]) << 8) + (((__u8 *) buf)[2]);
  75. return qpn;
  76. }
  77. static int is_bcast_mac(const char *dest)
  78. {
  79. int i;
  80. __u8 mac = 0xff;
  81. for (i = 0; i < 6; ++i)
  82. mac &= dest[i];
  83. return mac == 0xff;
  84. }
  85. /* find a free entry. if not found kick
  86. * another entry.
  87. */
  88. static int find_free_entry(void)
  89. {
  90. __u32 youth = 0xffffffff;
  91. __u8 i, remove_idx = NUM_MAC_ENTRIES;
  92. /* find a free entry */
  93. for (i = 0; i < NUM_MAC_ENTRIES; ++i) {
  94. if (!mac_tbl[i].valid) {
  95. mac_tbl[i].valid = 1;
  96. mac_tbl[i].youth = youth_counter;
  97. youth_counter++;
  98. return i;
  99. }
  100. }
  101. for (i = 0; i < NUM_MAC_ENTRIES; ++i) {
  102. if ((mac_tbl[i].av == NULL) && (mac_tbl[i].youth < youth)) {
  103. youth = mac_tbl[i].youth;
  104. remove_idx = i;
  105. }
  106. }
  107. if (remove_idx < NUM_MAC_ENTRIES) {
  108. /* update the new youth value */
  109. mac_tbl[remove_idx].youth = youth_counter;
  110. youth_counter++;
  111. return remove_idx;
  112. } else {
  113. tprintf("did not find an entry to kick");
  114. return -1;
  115. }
  116. }
  117. static int find_qpn_gid(__u32 qpn, const __u8 * gid)
  118. {
  119. __u16 i;
  120. for (i = 0; i < NUM_MAC_ENTRIES; ++i) {
  121. if (mac_tbl[i].valid &&
  122. (mac_tbl[i].qpn == qpn) &&
  123. !memcmp(mac_tbl[i].gid.raw, gid, 16)) {
  124. return i;
  125. }
  126. }
  127. return -1;
  128. }
  129. static void allocate_new_mac6(__u8 * mac_lsb)
  130. {
  131. __u32 eth_counter;
  132. eth_counter = mac_counter;
  133. mac_counter = (mac_counter + 1) & 0xffffff;
  134. mac_lsb[0] = eth_counter >> 16;
  135. mac_lsb[1] = eth_counter >> 8;
  136. mac_lsb[2] = eth_counter & 0xff;
  137. tprintf("add mac: %x:%x:%x", mac_lsb[0], mac_lsb[1], mac_lsb[2]);
  138. }
  139. static void modify_arp_reply(__u8 * eth_mac_lsb, void *data)
  140. {
  141. __u8 *packet;
  142. /* skip 4 bytes */
  143. packet = ((__u8 *) data) + 4;
  144. /* modify hw type */
  145. packet[0] = 0;
  146. packet[1] = 1;
  147. /* modify hw size */
  148. packet[4] = 6;
  149. /* modify sender's mac */
  150. packet[8] = MLX_ETH_BYTE0;
  151. packet[9] = MLX_ETH_BYTE1;
  152. packet[10] = MLX_ETH_BYTE2;
  153. packet[11] = eth_mac_lsb[0];
  154. packet[12] = eth_mac_lsb[1];
  155. packet[13] = eth_mac_lsb[2];
  156. /* move sender's IP address */
  157. memcpy(packet + 14, packet + 28, 4);
  158. /* set target MAC - that's us */
  159. packet[18] = MLX_ETH_BYTE0;
  160. packet[19] = MLX_ETH_BYTE1;
  161. packet[20] = MLX_ETH_BYTE2;
  162. packet[21] = 0;
  163. packet[22] = 0;
  164. packet[23] = 0;
  165. /* move target's IP address */
  166. memcpy(packet + 24, packet + 52, 4);
  167. }
  168. static void modify_arp_request(__u8 * eth_mac_lsb, void *data)
  169. {
  170. __u8 *packet;
  171. /* skip 4 bytes */
  172. packet = ((__u8 *) data) + 4;
  173. /* modify hw type */
  174. packet[0] = 0;
  175. packet[1] = 1;
  176. /* modify hw size */
  177. packet[4] = 6;
  178. /* modify sender's mac */
  179. packet[8] = MLX_ETH_BYTE0;
  180. packet[9] = MLX_ETH_BYTE1;
  181. packet[10] = MLX_ETH_BYTE2;
  182. packet[11] = eth_mac_lsb[0];
  183. packet[12] = eth_mac_lsb[1];
  184. packet[13] = eth_mac_lsb[2];
  185. /* move sender's IP address */
  186. memcpy(packet + 14, packet + 28, 4);
  187. /* set target MAC - that's us */
  188. packet[18] = 0;
  189. packet[19] = 0;
  190. packet[20] = 0;
  191. packet[21] = 0;
  192. packet[22] = 0;
  193. packet[23] = 0;
  194. /* move target's IP address */
  195. memcpy(packet + 24, packet + 52, 4);
  196. }
  197. static int handle_arp_packet(void *buf, void **out_buf_p,
  198. unsigned int *new_size_p)
  199. {
  200. __u16 opcode;
  201. const void *p;
  202. const __u8 *gid;
  203. __u32 qpn;
  204. int idx;
  205. opcode = get_opcode(buf);
  206. switch (opcode) {
  207. case ARP_OP_REQUESET:
  208. case ARP_OP_REPLY:
  209. break;
  210. default:
  211. return -1;
  212. }
  213. p = arp_mac20_get_sender_qpn(buf);
  214. qpn = buf2qpn(p);
  215. gid = arp_mac20_get_sender_gid(buf);
  216. if (!memcmp(gid, get_port_gid(), 16)) {
  217. /* my own gid */
  218. *out_buf_p = NULL;
  219. return 0;
  220. }
  221. idx = find_qpn_gid(qpn, gid);
  222. if (idx == -1) {
  223. /* entry not in the table */
  224. idx = find_free_entry();
  225. if (idx == -1) {
  226. eprintf("we're in broch\n");
  227. return -1;
  228. }
  229. allocate_new_mac6(mac_tbl[idx].eth_mac_lsb);
  230. mac_tbl[idx].av = NULL; // free the av id it exists ?? !!
  231. mac_tbl[idx].qpn = qpn;
  232. memcpy(mac_tbl[idx].gid.raw, gid, 16);
  233. }
  234. if (opcode == ARP_OP_REQUESET) {
  235. modify_arp_request(mac_tbl[idx].eth_mac_lsb, buf);
  236. } else {
  237. /* we want to filter possible broadcast arp
  238. replies not directed to us */
  239. p = arp_mac20_get_target_qpn(buf);
  240. qpn = buf2qpn(p);
  241. gid = arp_mac20_get_target_gid(buf);
  242. if ((qpn != ipoib_data.ipoib_qpn) ||
  243. (memcmp(gid, get_port_gid(), 16))) {
  244. *out_buf_p = NULL;
  245. return 0;
  246. }
  247. modify_arp_reply(mac_tbl[idx].eth_mac_lsb, buf);
  248. {
  249. __u8 i;
  250. tprintf("arp reply dump:\n");
  251. for (i = 4; i < 32; ++i) {
  252. tprintf("%x: ", ((__u8 *) buf)[i]);
  253. }
  254. tprintf("\n");
  255. }
  256. }
  257. *out_buf_p = ((__u8 *) buf) + 4;
  258. *new_size_p = 28; /* size of eth arp packet */
  259. tprintf("");
  260. return 0;
  261. }
  262. static void modify_udp_csum(void *buf, __u16 size)
  263. {
  264. __u8 *ptr = (__u8 *) buf;
  265. __u32 csum = 0;
  266. __u16 chksum;
  267. __u16 buf_size;
  268. __u16 *tmp;
  269. int i;
  270. buf_size = (size & 1) ? size + 1 : size;
  271. tmp = (__u16 *) (ptr + 12); /* src and dst ip addresses */
  272. for (i = 0; i < 4; ++i) {
  273. csum += tmp[i];
  274. }
  275. csum += 0x1100; // udp protocol
  276. tmp = (__u16 *) (ptr + 26);
  277. tmp[0] = 0; /* zero the checksum */
  278. tmp = (__u16 *) (ptr + 24);
  279. csum += tmp[0];
  280. tmp = (__u16 *) (ptr + 20);
  281. for (i = 0; i < ((buf_size - 20) >> 1); ++i) {
  282. csum += tmp[i];
  283. }
  284. chksum = ~((__u16) ((csum & 0xffff) + (csum >> 16)));
  285. tmp = (__u16 *) (ptr + 26);
  286. tmp[0] = chksum; /* set the checksum */
  287. }
  288. static void modify_dhcp_resp(void *buf, __u16 size)
  289. {
  290. set_eth_hwtype(buf);
  291. set_eth_hwlen(buf);
  292. set_own_mac(buf);
  293. modify_udp_csum(buf, size);
  294. }
  295. static void get_my_client_id(__u8 * my_client_id)
  296. {
  297. my_client_id[0] = 0;
  298. qpn2buf(ipoib_data.ipoib_qpn, my_client_id + 1);
  299. memcpy(my_client_id + 4, ipoib_data.port_gid_raw, 16);
  300. }
  301. static const __u8 *get_client_id(const void *buf, int len)
  302. {
  303. const __u8 *ptr;
  304. int delta;
  305. if (len < 268)
  306. return NULL;
  307. /* pointer to just after magic cookie */
  308. ptr = (const __u8 *)buf + 268;
  309. /* find last client identifier option */
  310. do {
  311. if (ptr[0] == 255) {
  312. /* found end of options list */
  313. return NULL;
  314. }
  315. if (ptr[0] == 0x3d) {
  316. /* client identifer option */
  317. return ptr + 3;
  318. }
  319. delta = ptr[1] + 2;
  320. ptr += delta;
  321. len -= delta;
  322. } while (len > 0);
  323. return NULL;
  324. }
  325. static int handle_ipv4_packet(void *buf, void **out_buf_p,
  326. unsigned int *new_size_p, int *is_bcast_p)
  327. {
  328. void *new_buf;
  329. __u16 new_size;
  330. __u8 msg_type;
  331. __u8 my_client_id[20];
  332. new_buf = (void *)(((__u8 *) buf) + 4);
  333. new_size = (*new_size_p) - 4;
  334. *out_buf_p = new_buf;
  335. *new_size_p = new_size;
  336. if (get_ip_protocl(new_buf) == IP_PROT_UDP) {
  337. __u16 udp_dst_port;
  338. const __u8 *client_id;
  339. udp_dst_port = get_udp_dst_port(new_buf);
  340. if (udp_dst_port == 67) {
  341. /* filter dhcp requests */
  342. *out_buf_p = 0;
  343. return 0;
  344. }
  345. if (udp_dst_port == 68) {
  346. get_my_client_id(my_client_id);
  347. /* packet client id */
  348. client_id = get_client_id(new_buf, new_size);
  349. if (!client_id) {
  350. *out_buf_p = 0;
  351. return 0;
  352. }
  353. if (memcmp(client_id, my_client_id, 20)) {
  354. *out_buf_p = 0;
  355. return 0;
  356. }
  357. }
  358. }
  359. msg_type = get_dhcp_msg_type(new_buf);
  360. if ((get_ip_protocl(new_buf) == IP_PROT_UDP) &&
  361. (get_udp_dst_port(new_buf) == 68) &&
  362. ((msg_type == DHCP_TYPE_RESPONSE) || (msg_type == DHCP_TYPE_ACK))
  363. ) {
  364. *is_bcast_p = 1;
  365. modify_dhcp_resp(new_buf, new_size);
  366. }
  367. return 0;
  368. }
  369. static int is_valid_arp(void *buf, unsigned int size)
  370. {
  371. __u8 *ptr = buf;
  372. __u16 tmp;
  373. if (size != 60) {
  374. eprintf("");
  375. return 0;
  376. }
  377. if (be16_to_cpu(*((__u16 *) ptr)) != ARP_PROT_TYPE)
  378. return 0;
  379. if (be16_to_cpu(*((__u16 *) (ptr + 4))) != IPOIB_HW_TYPE)
  380. return 0;
  381. if (be16_to_cpu(*((__u16 *) (ptr + 6))) != IPV4_PROT_TYPE)
  382. return 0;
  383. if (ptr[8] != 20) /* hw addr len */
  384. return 0;
  385. if (ptr[9] != 4) /* protocol len = 4 for IP */
  386. return 0;
  387. tmp = be16_to_cpu(*((__u16 *) (ptr + 10)));
  388. if ((tmp != ARP_OP_REQUESET) && (tmp != ARP_OP_REPLY))
  389. return 0;
  390. return 1;
  391. }
  392. static int ipoib_handle_rcv(void *buf, void **out_buf_p,
  393. unsigned int *new_size_p, int *is_bcast_p)
  394. {
  395. __u16 prot_type;
  396. int rc;
  397. prot_type = get_prot_type(buf);
  398. switch (prot_type) {
  399. case ARP_PROT_TYPE:
  400. tprintf("");
  401. if (is_valid_arp(buf, *new_size_p)) {
  402. tprintf("got valid arp");
  403. rc = handle_arp_packet(buf, out_buf_p, new_size_p);
  404. if (rc) {
  405. eprintf("");
  406. return rc;
  407. }
  408. if (!out_buf_p) {
  409. tprintf("");
  410. }
  411. tprintf("arp for me");
  412. *is_bcast_p = 1;
  413. return rc;
  414. } else {
  415. tprintf("got invalid arp");
  416. *out_buf_p = NULL;
  417. return 0;
  418. }
  419. case IPV4_PROT_TYPE:
  420. tprintf("");
  421. rc = handle_ipv4_packet(buf, out_buf_p, new_size_p, is_bcast_p);
  422. return rc;
  423. }
  424. eprintf("prot=0x%x", prot_type);
  425. return -1;
  426. }
  427. static int is_null_mac(const __u8 * mac)
  428. {
  429. __u8 i, tmp = 0;
  430. __u8 lmac[6];
  431. memcpy(lmac, mac, 6);
  432. for (i = 0; i < 6; ++i) {
  433. tmp |= lmac[i];
  434. }
  435. if (tmp == 0)
  436. return 1;
  437. else
  438. return 0;
  439. }
  440. static int find_mac(const __u8 * mac)
  441. {
  442. int i;
  443. const __u8 *tmp = mac + 3;
  444. for (i = 0; i < NUM_MAC_ENTRIES; ++i) {
  445. tprintf("checking 0x%02x:0x%02x:0x%02x valid=%d",
  446. mac_tbl[i].eth_mac_lsb[0], mac_tbl[i].eth_mac_lsb[1],
  447. mac_tbl[i].eth_mac_lsb[2], mac_tbl[i].valid);
  448. if (mac_tbl[i].valid && !memcmp(mac_tbl[i].eth_mac_lsb, tmp, 3))
  449. return i;
  450. }
  451. tprintf("mac: %x:%x:%x - dumping", tmp[0], tmp[1], tmp[2]);
  452. for (i = 0; i < NUM_MAC_ENTRIES; ++i) {
  453. //__u8 *gid= mac_tbl[i].gid.raw;
  454. //__u8 *m= mac_tbl[i].eth_mac_lsb;
  455. /*if (mac_tbl[i].valid) {
  456. tprintf("%d: qpn=0x%lx, "
  457. "gid=%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x, "
  458. "av=0x%lx, "
  459. "youth= %ld, "
  460. "mac=%x:%x:%x\n",
  461. i, mac_tbl[i].qpn,
  462. gid[0], gid[1], gid[2], gid[3], gid[4], gid[5], gid[6], gid[7],
  463. gid[8], gid[9], gid[10], gid[11], gid[12], gid[13], gid[14], gid[15],
  464. mac_tbl[i].av, mac_tbl[i].youth,
  465. m[0], m[1], m[2]);
  466. } */
  467. }
  468. return -1;
  469. }
  470. static int send_bcast_packet(__u16 protocol, const void *data, __u16 size)
  471. {
  472. ud_send_wqe_t snd_wqe, tmp_wqe;
  473. int rc;
  474. int is_good;
  475. void *send_buffer;
  476. snd_wqe = alloc_send_wqe(ipoib_data.ipoib_qph);
  477. if (!snd_wqe) {
  478. eprintf("");
  479. return -1;
  480. }
  481. send_buffer = get_send_wqe_buf(snd_wqe, 0);
  482. *((__u32 *) send_buffer) = cpu_to_be32(protocol << 16);
  483. prep_send_wqe_buf(ipoib_data.ipoib_qph, ipoib_data.bcast_av,
  484. snd_wqe, data, 4, size, 0);
  485. rc = post_send_req(ipoib_data.ipoib_qph, snd_wqe, 1);
  486. if (rc) {
  487. eprintf("");
  488. goto ex;
  489. }
  490. rc = poll_cqe_tout(ipoib_data.snd_cqh, SEND_CQE_POLL_TOUT, &tmp_wqe,
  491. &is_good);
  492. if (rc) {
  493. eprintf("");
  494. goto ex;
  495. }
  496. if (!is_good) {
  497. eprintf("");
  498. rc = -1;
  499. goto ex;
  500. }
  501. if (tmp_wqe != snd_wqe) {
  502. eprintf("");
  503. rc = -1;
  504. goto ex;
  505. }
  506. ex:free_wqe(snd_wqe);
  507. return rc;
  508. }
  509. static int send_ucast_packet(const __u8 * mac, __u16 protocol, const void *data,
  510. __u16 size)
  511. {
  512. ud_send_wqe_t snd_wqe, tmp_wqe;
  513. ud_av_t av;
  514. udqp_t qph;
  515. __u16 dlid;
  516. __u8 sl, rate;
  517. int rc;
  518. int i;
  519. int is_good;
  520. i = find_mac(mac);
  521. if (i < 0) {
  522. tprintf("");
  523. return -1;
  524. }
  525. if (!mac_tbl[i].av) {
  526. rc = get_path_record(&mac_tbl[i].gid, &dlid, &sl, &rate);
  527. if (rc) {
  528. eprintf("");
  529. return -1;
  530. } else {
  531. tprintf("get_path_record() success dlid=0x%x", dlid);
  532. }
  533. /* no av - allocate one */
  534. av = alloc_ud_av();
  535. if (!av) {
  536. eprintf("");
  537. return -1;
  538. }
  539. modify_av_params(av, dlid, 1, sl, rate, &mac_tbl[i].gid,
  540. mac_tbl[i].qpn);
  541. mac_tbl[i].av = av;
  542. } else {
  543. av = mac_tbl[i].av;
  544. }
  545. qph = ipoib_data.ipoib_qph;
  546. snd_wqe = alloc_send_wqe(qph);
  547. if (!snd_wqe) {
  548. eprintf("");
  549. return -1;
  550. }
  551. *((__u32 *) get_send_wqe_buf(snd_wqe, 0)) = cpu_to_be32(protocol << 16);
  552. prep_send_wqe_buf(qph, av, snd_wqe, data, 4, size, 0);
  553. rc = post_send_req(qph, snd_wqe, 1);
  554. if (rc) {
  555. eprintf("");
  556. return -1;
  557. }
  558. rc = poll_cqe_tout(ipoib_data.snd_cqh, SEND_CQE_POLL_TOUT, &tmp_wqe,
  559. &is_good);
  560. if (rc) {
  561. eprintf("");
  562. goto ex;
  563. }
  564. if (!is_good) {
  565. eprintf("");
  566. rc = -1;
  567. goto ex;
  568. }
  569. if (tmp_wqe != snd_wqe) {
  570. eprintf("");
  571. rc = -1;
  572. goto ex;
  573. }
  574. ex:free_wqe(snd_wqe);
  575. return rc;
  576. }
  577. static void *alloc_convert_arp6_msg(const void *data,
  578. struct arp_packet_st *ipoib_arp)
  579. {
  580. void *buf;
  581. const void *p1;
  582. int idx;
  583. __u8 qpn[3];
  584. memcpy(ipoib_arp, arp_packet_template, sizeof arp_packet_template);
  585. buf = ipoib_arp;
  586. /* update opcode */
  587. p1 = arp_mac6_get_opcode(data);
  588. arp_mac20_set_opcode(p1, buf);
  589. /* update sender ip */
  590. p1 = arp_mac6_get_sender_ip(data);
  591. arp_mac20_set_sender_ip(p1, buf);
  592. /* update target ip */
  593. p1 = arp_mac6_get_target_ip(data);
  594. arp_mac20_set_target_ip(p1, buf);
  595. /* update sender mac */
  596. qpn2buf(ipoib_data.ipoib_qpn, qpn);
  597. arp_mac20_set_sender_mac(qpn, ipoib_data.port_gid_raw, buf);
  598. /* update target mac */
  599. p1 = arp_mac6_get_target_mac(data);
  600. if (!is_null_mac(p1)) {
  601. idx = find_mac(p1);
  602. if (idx == -1) {
  603. __u8 *_ptr = (__u8 *) p1;
  604. eprintf("could not find mac %x:%x:%x",
  605. _ptr[3], _ptr[4], _ptr[5]);
  606. return NULL;
  607. }
  608. qpn2buf(mac_tbl[idx].qpn, qpn);
  609. arp_mac20_set_target_mac(qpn, mac_tbl[idx].gid.raw, buf);
  610. }
  611. return buf;
  612. }
  613. static __u16 set_client_id(__u8 * packet)
  614. {
  615. __u8 *ptr;
  616. __u8 y[3];
  617. __u16 new_size;
  618. /* pointer to just after magic cookie */
  619. ptr = packet + 268;
  620. /* find last option */
  621. do {
  622. if (ptr[0] == 255) {
  623. /* found end of options list */
  624. break;
  625. }
  626. ptr = ptr + ptr[1] + 2;
  627. } while (1);
  628. ptr[0] = 61; /* client id option identifier */
  629. ptr[1] = 21; /* length of the option */
  630. ptr[2] = IPOIB_HW_TYPE;
  631. ptr[3] = 0;
  632. qpn2buf(ipoib_data.ipoib_qpn, y);
  633. memcpy(ptr + 4, y, 3);
  634. memcpy(ptr + 7, ipoib_data.port_gid_raw, 16);
  635. ptr[23] = 255;
  636. new_size = (__u16) (ptr + 24 - packet);
  637. if (new_size & 3) {
  638. new_size += (4 - (new_size & 3));
  639. }
  640. return new_size;
  641. }
  642. static __u16 calc_udp_csum(__u8 * packet)
  643. {
  644. __u16 *ptr;
  645. int i;
  646. __u32 sum = 0;
  647. __u16 udp_length, udp_csum;
  648. /* src ip, dst ip */
  649. ptr = (__u16 *) (packet + 12);
  650. for (i = 0; i < 4; ++i) {
  651. sum += be16_to_cpu(ptr[i]);
  652. }
  653. /* udp protocol */
  654. sum += IP_PROT_UDP;
  655. /* udp length */
  656. ptr = (__u16 *) (packet + 24);
  657. udp_length = be16_to_cpu(*ptr);
  658. sum += udp_length;
  659. /* udp part */
  660. ptr = (__u16 *) (packet + 20);
  661. do {
  662. sum += be16_to_cpu(*ptr);
  663. ptr++;
  664. udp_length -= 2;
  665. } while (udp_length);
  666. udp_csum = ~((__u16) ((sum & 0xffff) + (sum >> 16)));
  667. return udp_csum;
  668. }
  669. static __u16 modify_dhcp_request(__u8 * packet, __u16 size)
  670. {
  671. __u16 csum, new_size;
  672. set_hw_type(packet);
  673. zero_hw_len(packet);
  674. zero_chaddr(packet);
  675. set_bcast_flag(packet);
  676. new_size = set_client_id(packet);
  677. if (new_size > size) {
  678. add_udp_len(packet, new_size - size);
  679. } else
  680. new_size = size;
  681. set_udp_csum(packet, 0);
  682. csum = calc_udp_csum(packet);
  683. set_udp_csum(packet, csum);
  684. return new_size;
  685. }
  686. static __u16 copy_dhcp_message(__u8 * buf, const void *packet, __u16 size)
  687. {
  688. memcpy(buf, packet, size);
  689. return size;
  690. }
  691. static void modify_ip_hdr(__u8 * buf, __u16 add_size)
  692. {
  693. __u16 *ptr, ip_csum;
  694. __u16 tmp;
  695. __u32 sum = 0;
  696. __u8 i;
  697. /* update ip length */
  698. ptr = (__u16 *) buf;
  699. tmp = be16_to_cpu(ptr[1]);
  700. ptr[1] = cpu_to_be16(tmp + add_size);
  701. ptr[5] = 0; /* zero csum */
  702. for (i = 0; i < 10; ++i) {
  703. sum += be16_to_cpu(ptr[i]);
  704. }
  705. ip_csum = ~((__u16) ((sum & 0xffff) + (sum >> 16)));
  706. ptr[5] = cpu_to_be16(ip_csum);
  707. }
  708. static void *update_dhcp_request(const void *packet, unsigned int size,
  709. __u16 * new_size_p)
  710. {
  711. __u8 ip_proto, dhcp_message_type;
  712. __u16 dest_port, new_size, orig_size;
  713. static __u8 dhcp_send_buffer[576];
  714. ip_proto = get_ip_protocl_type(packet);
  715. if (ip_proto != IP_PROT_UDP) {
  716. return NULL;
  717. }
  718. dest_port = get_udp_dest_port(packet);
  719. if (dest_port != 0x4300 /*67 */ )
  720. return NULL;
  721. dhcp_message_type = get_dhcp_message_type(packet);
  722. if (dhcp_message_type != DHCP_TYPE_REQUEST)
  723. return NULL;
  724. memset(dhcp_send_buffer, 0, sizeof dhcp_send_buffer);
  725. orig_size = copy_dhcp_message(dhcp_send_buffer, packet, size);
  726. new_size = modify_dhcp_request(dhcp_send_buffer, orig_size);
  727. if (new_size != orig_size) {
  728. modify_ip_hdr(dhcp_send_buffer, new_size - orig_size);
  729. }
  730. *new_size_p = new_size;
  731. return dhcp_send_buffer;
  732. }
  733. static int ipoib_send_packet(const __u8 * mac, __u16 protocol, const void *data,
  734. unsigned int size)
  735. {
  736. const void *packet;
  737. __u16 new_size, dhcp_req_sz;
  738. void *tmp;
  739. int rc;
  740. struct arp_packet_st ipoib_arp;
  741. tprintf("");
  742. if (protocol == ARP_PROT_TYPE) {
  743. /* special treatment for ARP */
  744. tmp = alloc_convert_arp6_msg(data, &ipoib_arp);
  745. if (!tmp) {
  746. eprintf("");
  747. return -1;
  748. }
  749. packet = tmp;
  750. new_size = sizeof(struct arp_packet_st);
  751. tprintf("sending arp");
  752. } else {
  753. tmp = update_dhcp_request(data, size, &dhcp_req_sz);
  754. if (tmp) {
  755. /* it was a dhcp request so we use a special
  756. buffer because we may have to enlarge the size of the packet */
  757. tprintf("sending dhcp");
  758. packet = tmp;
  759. new_size = dhcp_req_sz;
  760. } else {
  761. packet = data;
  762. new_size = size;
  763. tprintf("sending packet");
  764. }
  765. }
  766. //eprintf("press key ..."); getchar();
  767. if (is_bcast_mac(mac)) {
  768. tprintf("");
  769. rc = send_bcast_packet(protocol, packet, new_size);
  770. } else {
  771. tprintf("");
  772. rc = send_ucast_packet(mac, protocol, packet, new_size);
  773. }
  774. return rc;
  775. }
  776. static int ipoib_read_packet(__u16 * prot_p, void *data, unsigned int *size_p,
  777. int *is_bcast_p)
  778. {
  779. int rc;
  780. struct ib_cqe_st ib_cqe;
  781. __u8 num_cqes;
  782. unsigned int new_size;
  783. void *buf, *out_buf;
  784. __u16 prot_type;
  785. rc = ib_poll_cq(ipoib_data.rcv_cqh, &ib_cqe, &num_cqes);
  786. if (rc) {
  787. return rc;
  788. }
  789. if (num_cqes == 0) {
  790. *size_p = 0;
  791. return 0;
  792. }
  793. if (ib_cqe.is_error) {
  794. eprintf("");
  795. rc = -1;
  796. goto ex_func;
  797. }
  798. new_size = ib_cqe.count - GRH_SIZE;
  799. buf = get_rcv_wqe_buf(ib_cqe.wqe, 1);
  800. tprintf("buf=%lx", buf);
  801. rc = ipoib_handle_rcv(buf, &out_buf, &new_size, is_bcast_p);
  802. if (rc) {
  803. eprintf("");
  804. rc = -1;
  805. goto ex_func;
  806. }
  807. if (out_buf) {
  808. tprintf("");
  809. prot_type = get_prot_type(buf);
  810. *size_p = new_size;
  811. tprintf("new_size=%d", new_size);
  812. if (new_size > 1560) {
  813. eprintf("sizzzzzze = %d", new_size);
  814. } else {
  815. memcpy(data, out_buf, new_size);
  816. }
  817. *prot_p = prot_type;
  818. } else {
  819. tprintf("skip message");
  820. *size_p = 0;
  821. }
  822. ex_func:
  823. if (free_wqe(ib_cqe.wqe)) {
  824. eprintf("");
  825. }
  826. return rc;
  827. }
  828. static int ipoib_init(struct pci_device *pci)
  829. {
  830. int rc;
  831. udqp_t qph;
  832. int i;
  833. tprintf("");
  834. rc = ib_driver_init(pci, &qph);
  835. if (rc)
  836. return rc;
  837. tprintf("");
  838. ipoib_data.ipoib_qph = qph;
  839. ipoib_data.ipoib_qpn = ib_get_qpn(qph);
  840. if(print_info)
  841. printf("local ipoib qpn=0x%x\n", ipoib_data.ipoib_qpn);
  842. ipoib_data.bcast_av = ib_data.bcast_av;
  843. ipoib_data.port_gid_raw = ib_data.port_gid.raw;
  844. ipoib_data.snd_cqh = ib_data.ipoib_snd_cq;
  845. ipoib_data.rcv_cqh = ib_data.ipoib_rcv_cq;
  846. mac_counter = 1;
  847. youth_counter = 0;
  848. for (i = 0; i < NUM_MAC_ENTRIES; ++i) {
  849. mac_tbl[i].valid = 0;
  850. mac_tbl[i].av = NULL;
  851. }
  852. return 0;
  853. }
  854. static int ipoib_close(int fw_fatal)
  855. {
  856. int rc;
  857. rc = ib_driver_close(fw_fatal);
  858. return rc;
  859. }