opa_vnic_encap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright(c) 2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. /*
  48. * This file contains OPA VNIC encapsulation/decapsulation function.
  49. */
  50. #include <linux/if_ether.h>
  51. #include <linux/if_vlan.h>
  52. #include "opa_vnic_internal.h"
  53. /* OPA 16B Header fields */
  54. #define OPA_16B_LID_MASK 0xFFFFFull
  55. #define OPA_16B_SLID_HIGH_SHFT 8
  56. #define OPA_16B_SLID_MASK 0xF00ull
  57. #define OPA_16B_DLID_MASK 0xF000ull
  58. #define OPA_16B_DLID_HIGH_SHFT 12
  59. #define OPA_16B_LEN_SHFT 20
  60. #define OPA_16B_SC_SHFT 20
  61. #define OPA_16B_RC_SHFT 25
  62. #define OPA_16B_PKEY_SHFT 16
  63. #define OPA_VNIC_L4_HDR_SHFT 16
  64. /* L2+L4 hdr len is 20 bytes (5 quad words) */
  65. #define OPA_VNIC_HDR_QW_LEN 5
  66. static inline void opa_vnic_make_header(u8 *hdr, u32 slid, u32 dlid, u16 len,
  67. u16 pkey, u16 entropy, u8 sc, u8 rc,
  68. u8 l4_type, u16 l4_hdr)
  69. {
  70. /* h[1]: LT=1, 16B L2=10 */
  71. u32 h[OPA_VNIC_HDR_QW_LEN] = {0, 0xc0000000, 0, 0, 0};
  72. h[2] = l4_type;
  73. h[3] = entropy;
  74. h[4] = l4_hdr << OPA_VNIC_L4_HDR_SHFT;
  75. /* Extract and set 4 upper bits and 20 lower bits of the lids */
  76. h[0] |= (slid & OPA_16B_LID_MASK);
  77. h[2] |= ((slid >> (20 - OPA_16B_SLID_HIGH_SHFT)) & OPA_16B_SLID_MASK);
  78. h[1] |= (dlid & OPA_16B_LID_MASK);
  79. h[2] |= ((dlid >> (20 - OPA_16B_DLID_HIGH_SHFT)) & OPA_16B_DLID_MASK);
  80. h[0] |= (len << OPA_16B_LEN_SHFT);
  81. h[1] |= (rc << OPA_16B_RC_SHFT);
  82. h[1] |= (sc << OPA_16B_SC_SHFT);
  83. h[2] |= ((u32)pkey << OPA_16B_PKEY_SHFT);
  84. memcpy(hdr, h, OPA_VNIC_HDR_LEN);
  85. }
  86. /*
  87. * Using a simple hash table for mac table implementation with the last octet
  88. * of mac address as a key.
  89. */
  90. static void opa_vnic_free_mac_tbl(struct hlist_head *mactbl)
  91. {
  92. struct opa_vnic_mac_tbl_node *node;
  93. struct hlist_node *tmp;
  94. int bkt;
  95. if (!mactbl)
  96. return;
  97. vnic_hash_for_each_safe(mactbl, bkt, tmp, node, hlist) {
  98. hash_del(&node->hlist);
  99. kfree(node);
  100. }
  101. kfree(mactbl);
  102. }
  103. static struct hlist_head *opa_vnic_alloc_mac_tbl(void)
  104. {
  105. u32 size = sizeof(struct hlist_head) * OPA_VNIC_MAC_TBL_SIZE;
  106. struct hlist_head *mactbl;
  107. mactbl = kzalloc(size, GFP_KERNEL);
  108. if (!mactbl)
  109. return ERR_PTR(-ENOMEM);
  110. vnic_hash_init(mactbl);
  111. return mactbl;
  112. }
  113. /* opa_vnic_release_mac_tbl - empty and free the mac table */
  114. void opa_vnic_release_mac_tbl(struct opa_vnic_adapter *adapter)
  115. {
  116. struct hlist_head *mactbl;
  117. mutex_lock(&adapter->mactbl_lock);
  118. mactbl = rcu_access_pointer(adapter->mactbl);
  119. rcu_assign_pointer(adapter->mactbl, NULL);
  120. synchronize_rcu();
  121. opa_vnic_free_mac_tbl(mactbl);
  122. adapter->info.vport.mac_tbl_digest = 0;
  123. mutex_unlock(&adapter->mactbl_lock);
  124. }
  125. /*
  126. * opa_vnic_query_mac_tbl - query the mac table for a section
  127. *
  128. * This function implements query of specific function of the mac table.
  129. * The function also expects the requested range to be valid.
  130. */
  131. void opa_vnic_query_mac_tbl(struct opa_vnic_adapter *adapter,
  132. struct opa_veswport_mactable *tbl)
  133. {
  134. struct opa_vnic_mac_tbl_node *node;
  135. struct hlist_head *mactbl;
  136. int bkt;
  137. u16 loffset, lnum_entries;
  138. rcu_read_lock();
  139. mactbl = rcu_dereference(adapter->mactbl);
  140. if (!mactbl)
  141. goto get_mac_done;
  142. loffset = be16_to_cpu(tbl->offset);
  143. lnum_entries = be16_to_cpu(tbl->num_entries);
  144. vnic_hash_for_each(mactbl, bkt, node, hlist) {
  145. struct __opa_vnic_mactable_entry *nentry = &node->entry;
  146. struct opa_veswport_mactable_entry *entry;
  147. if ((node->index < loffset) ||
  148. (node->index >= (loffset + lnum_entries)))
  149. continue;
  150. /* populate entry in the tbl corresponding to the index */
  151. entry = &tbl->tbl_entries[node->index - loffset];
  152. memcpy(entry->mac_addr, nentry->mac_addr,
  153. ARRAY_SIZE(entry->mac_addr));
  154. memcpy(entry->mac_addr_mask, nentry->mac_addr_mask,
  155. ARRAY_SIZE(entry->mac_addr_mask));
  156. entry->dlid_sd = cpu_to_be32(nentry->dlid_sd);
  157. }
  158. tbl->mac_tbl_digest = cpu_to_be32(adapter->info.vport.mac_tbl_digest);
  159. get_mac_done:
  160. rcu_read_unlock();
  161. }
  162. /*
  163. * opa_vnic_update_mac_tbl - update mac table section
  164. *
  165. * This function updates the specified section of the mac table.
  166. * The procedure includes following steps.
  167. * - Allocate a new mac (hash) table.
  168. * - Add the specified entries to the new table.
  169. * (except the ones that are requested to be deleted).
  170. * - Add all the other entries from the old mac table.
  171. * - If there is a failure, free the new table and return.
  172. * - Switch to the new table.
  173. * - Free the old table and return.
  174. *
  175. * The function also expects the requested range to be valid.
  176. */
  177. int opa_vnic_update_mac_tbl(struct opa_vnic_adapter *adapter,
  178. struct opa_veswport_mactable *tbl)
  179. {
  180. struct opa_vnic_mac_tbl_node *node, *new_node;
  181. struct hlist_head *new_mactbl, *old_mactbl;
  182. int i, bkt, rc = 0;
  183. u8 key;
  184. u16 loffset, lnum_entries;
  185. mutex_lock(&adapter->mactbl_lock);
  186. /* allocate new mac table */
  187. new_mactbl = opa_vnic_alloc_mac_tbl();
  188. if (IS_ERR(new_mactbl)) {
  189. mutex_unlock(&adapter->mactbl_lock);
  190. return PTR_ERR(new_mactbl);
  191. }
  192. loffset = be16_to_cpu(tbl->offset);
  193. lnum_entries = be16_to_cpu(tbl->num_entries);
  194. /* add updated entries to the new mac table */
  195. for (i = 0; i < lnum_entries; i++) {
  196. struct __opa_vnic_mactable_entry *nentry;
  197. struct opa_veswport_mactable_entry *entry =
  198. &tbl->tbl_entries[i];
  199. u8 *mac_addr = entry->mac_addr;
  200. u8 empty_mac[ETH_ALEN] = { 0 };
  201. v_dbg("new mac entry %4d: %02x:%02x:%02x:%02x:%02x:%02x %x\n",
  202. loffset + i, mac_addr[0], mac_addr[1], mac_addr[2],
  203. mac_addr[3], mac_addr[4], mac_addr[5],
  204. entry->dlid_sd);
  205. /* if the entry is being removed, do not add it */
  206. if (!memcmp(mac_addr, empty_mac, ARRAY_SIZE(empty_mac)))
  207. continue;
  208. node = kzalloc(sizeof(*node), GFP_KERNEL);
  209. if (!node) {
  210. rc = -ENOMEM;
  211. goto updt_done;
  212. }
  213. node->index = loffset + i;
  214. nentry = &node->entry;
  215. memcpy(nentry->mac_addr, entry->mac_addr,
  216. ARRAY_SIZE(nentry->mac_addr));
  217. memcpy(nentry->mac_addr_mask, entry->mac_addr_mask,
  218. ARRAY_SIZE(nentry->mac_addr_mask));
  219. nentry->dlid_sd = be32_to_cpu(entry->dlid_sd);
  220. key = node->entry.mac_addr[OPA_VNIC_MAC_HASH_IDX];
  221. vnic_hash_add(new_mactbl, &node->hlist, key);
  222. }
  223. /* add other entries from current mac table to new mac table */
  224. old_mactbl = rcu_access_pointer(adapter->mactbl);
  225. if (!old_mactbl)
  226. goto switch_tbl;
  227. vnic_hash_for_each(old_mactbl, bkt, node, hlist) {
  228. if ((node->index >= loffset) &&
  229. (node->index < (loffset + lnum_entries)))
  230. continue;
  231. new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
  232. if (!new_node) {
  233. rc = -ENOMEM;
  234. goto updt_done;
  235. }
  236. new_node->index = node->index;
  237. memcpy(&new_node->entry, &node->entry, sizeof(node->entry));
  238. key = new_node->entry.mac_addr[OPA_VNIC_MAC_HASH_IDX];
  239. vnic_hash_add(new_mactbl, &new_node->hlist, key);
  240. }
  241. switch_tbl:
  242. /* switch to new table */
  243. rcu_assign_pointer(adapter->mactbl, new_mactbl);
  244. synchronize_rcu();
  245. adapter->info.vport.mac_tbl_digest = be32_to_cpu(tbl->mac_tbl_digest);
  246. updt_done:
  247. /* upon failure, free the new table; otherwise, free the old table */
  248. if (rc)
  249. opa_vnic_free_mac_tbl(new_mactbl);
  250. else
  251. opa_vnic_free_mac_tbl(old_mactbl);
  252. mutex_unlock(&adapter->mactbl_lock);
  253. return rc;
  254. }
  255. /* opa_vnic_chk_mac_tbl - check mac table for dlid */
  256. static uint32_t opa_vnic_chk_mac_tbl(struct opa_vnic_adapter *adapter,
  257. struct ethhdr *mac_hdr)
  258. {
  259. struct opa_vnic_mac_tbl_node *node;
  260. struct hlist_head *mactbl;
  261. u32 dlid = 0;
  262. u8 key;
  263. rcu_read_lock();
  264. mactbl = rcu_dereference(adapter->mactbl);
  265. if (unlikely(!mactbl))
  266. goto chk_done;
  267. key = mac_hdr->h_dest[OPA_VNIC_MAC_HASH_IDX];
  268. vnic_hash_for_each_possible(mactbl, node, hlist, key) {
  269. struct __opa_vnic_mactable_entry *entry = &node->entry;
  270. /* if related to source mac, skip */
  271. if (unlikely(OPA_VNIC_DLID_SD_IS_SRC_MAC(entry->dlid_sd)))
  272. continue;
  273. if (!memcmp(node->entry.mac_addr, mac_hdr->h_dest,
  274. ARRAY_SIZE(node->entry.mac_addr))) {
  275. /* mac address found */
  276. dlid = OPA_VNIC_DLID_SD_GET_DLID(node->entry.dlid_sd);
  277. break;
  278. }
  279. }
  280. chk_done:
  281. rcu_read_unlock();
  282. return dlid;
  283. }
  284. /* opa_vnic_get_dlid - find and return the DLID */
  285. static uint32_t opa_vnic_get_dlid(struct opa_vnic_adapter *adapter,
  286. struct sk_buff *skb, u8 def_port)
  287. {
  288. struct __opa_veswport_info *info = &adapter->info;
  289. struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
  290. u32 dlid;
  291. dlid = opa_vnic_chk_mac_tbl(adapter, mac_hdr);
  292. if (dlid)
  293. return dlid;
  294. if (is_multicast_ether_addr(mac_hdr->h_dest)) {
  295. dlid = info->vesw.u_mcast_dlid;
  296. } else {
  297. if (is_local_ether_addr(mac_hdr->h_dest)) {
  298. dlid = ((uint32_t)mac_hdr->h_dest[5] << 16) |
  299. ((uint32_t)mac_hdr->h_dest[4] << 8) |
  300. mac_hdr->h_dest[3];
  301. if (unlikely(!dlid))
  302. v_warn("Null dlid in MAC address\n");
  303. } else if (def_port != OPA_VNIC_INVALID_PORT) {
  304. if (def_port < OPA_VESW_MAX_NUM_DEF_PORT)
  305. dlid = info->vesw.u_ucast_dlid[def_port];
  306. }
  307. }
  308. return dlid;
  309. }
  310. /* opa_vnic_get_sc - return the service class */
  311. static u8 opa_vnic_get_sc(struct __opa_veswport_info *info,
  312. struct sk_buff *skb)
  313. {
  314. struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
  315. u16 vlan_tci;
  316. u8 sc;
  317. if (!__vlan_get_tag(skb, &vlan_tci)) {
  318. u8 pcp = OPA_VNIC_VLAN_PCP(vlan_tci);
  319. if (is_multicast_ether_addr(mac_hdr->h_dest))
  320. sc = info->vport.pcp_to_sc_mc[pcp];
  321. else
  322. sc = info->vport.pcp_to_sc_uc[pcp];
  323. } else {
  324. if (is_multicast_ether_addr(mac_hdr->h_dest))
  325. sc = info->vport.non_vlan_sc_mc;
  326. else
  327. sc = info->vport.non_vlan_sc_uc;
  328. }
  329. return sc;
  330. }
  331. u8 opa_vnic_get_vl(struct opa_vnic_adapter *adapter, struct sk_buff *skb)
  332. {
  333. struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
  334. struct __opa_veswport_info *info = &adapter->info;
  335. u8 vl;
  336. if (skb_vlan_tag_present(skb)) {
  337. u8 pcp = skb_vlan_tag_get(skb) >> VLAN_PRIO_SHIFT;
  338. if (is_multicast_ether_addr(mac_hdr->h_dest))
  339. vl = info->vport.pcp_to_vl_mc[pcp];
  340. else
  341. vl = info->vport.pcp_to_vl_uc[pcp];
  342. } else {
  343. if (is_multicast_ether_addr(mac_hdr->h_dest))
  344. vl = info->vport.non_vlan_vl_mc;
  345. else
  346. vl = info->vport.non_vlan_vl_uc;
  347. }
  348. return vl;
  349. }
  350. /* opa_vnic_get_rc - return the routing control */
  351. static u8 opa_vnic_get_rc(struct __opa_veswport_info *info,
  352. struct sk_buff *skb)
  353. {
  354. u8 proto, rout_ctrl;
  355. switch (vlan_get_protocol(skb)) {
  356. case htons(ETH_P_IPV6):
  357. proto = ipv6_hdr(skb)->nexthdr;
  358. if (proto == IPPROTO_TCP)
  359. rout_ctrl = OPA_VNIC_ENCAP_RC_EXT(info->vesw.rc,
  360. IPV6_TCP);
  361. else if (proto == IPPROTO_UDP)
  362. rout_ctrl = OPA_VNIC_ENCAP_RC_EXT(info->vesw.rc,
  363. IPV6_UDP);
  364. else
  365. rout_ctrl = OPA_VNIC_ENCAP_RC_EXT(info->vesw.rc, IPV6);
  366. break;
  367. case htons(ETH_P_IP):
  368. proto = ip_hdr(skb)->protocol;
  369. if (proto == IPPROTO_TCP)
  370. rout_ctrl = OPA_VNIC_ENCAP_RC_EXT(info->vesw.rc,
  371. IPV4_TCP);
  372. else if (proto == IPPROTO_UDP)
  373. rout_ctrl = OPA_VNIC_ENCAP_RC_EXT(info->vesw.rc,
  374. IPV4_UDP);
  375. else
  376. rout_ctrl = OPA_VNIC_ENCAP_RC_EXT(info->vesw.rc, IPV4);
  377. break;
  378. default:
  379. rout_ctrl = OPA_VNIC_ENCAP_RC_EXT(info->vesw.rc, DEFAULT);
  380. }
  381. return rout_ctrl;
  382. }
  383. /* opa_vnic_calc_entropy - calculate the packet entropy */
  384. u8 opa_vnic_calc_entropy(struct sk_buff *skb)
  385. {
  386. u32 hash = skb_get_hash(skb);
  387. /* store XOR of all bytes in lower 8 bits */
  388. hash ^= hash >> 8;
  389. hash ^= hash >> 16;
  390. /* return lower 8 bits as entropy */
  391. return (u8)(hash & 0xFF);
  392. }
  393. /* opa_vnic_get_def_port - get default port based on entropy */
  394. static inline u8 opa_vnic_get_def_port(struct opa_vnic_adapter *adapter,
  395. u8 entropy)
  396. {
  397. u8 flow_id;
  398. /* Add the upper and lower 4-bits of entropy to get the flow id */
  399. flow_id = ((entropy & 0xf) + (entropy >> 4));
  400. return adapter->flow_tbl[flow_id & (OPA_VNIC_FLOW_TBL_SIZE - 1)];
  401. }
  402. /* Calculate packet length including OPA header, crc and padding */
  403. static inline int opa_vnic_wire_length(struct sk_buff *skb)
  404. {
  405. u32 pad_len;
  406. /* padding for 8 bytes size alignment */
  407. pad_len = -(skb->len + OPA_VNIC_ICRC_TAIL_LEN) & 0x7;
  408. pad_len += OPA_VNIC_ICRC_TAIL_LEN;
  409. return (skb->len + pad_len) >> 3;
  410. }
  411. /* opa_vnic_encap_skb - encapsulate skb packet with OPA header and meta data */
  412. void opa_vnic_encap_skb(struct opa_vnic_adapter *adapter, struct sk_buff *skb)
  413. {
  414. struct __opa_veswport_info *info = &adapter->info;
  415. struct opa_vnic_skb_mdata *mdata;
  416. u8 def_port, sc, rc, entropy, *hdr;
  417. u16 len, l4_hdr;
  418. u32 dlid;
  419. hdr = skb_push(skb, OPA_VNIC_HDR_LEN);
  420. entropy = opa_vnic_calc_entropy(skb);
  421. def_port = opa_vnic_get_def_port(adapter, entropy);
  422. len = opa_vnic_wire_length(skb);
  423. dlid = opa_vnic_get_dlid(adapter, skb, def_port);
  424. sc = opa_vnic_get_sc(info, skb);
  425. rc = opa_vnic_get_rc(info, skb);
  426. l4_hdr = info->vesw.vesw_id;
  427. mdata = skb_push(skb, sizeof(*mdata));
  428. mdata->vl = opa_vnic_get_vl(adapter, skb);
  429. mdata->entropy = entropy;
  430. mdata->flags = 0;
  431. if (unlikely(!dlid)) {
  432. mdata->flags = OPA_VNIC_SKB_MDATA_ENCAP_ERR;
  433. return;
  434. }
  435. opa_vnic_make_header(hdr, info->vport.encap_slid, dlid, len,
  436. info->vesw.pkey, entropy, sc, rc,
  437. OPA_VNIC_L4_ETHR, l4_hdr);
  438. }