opa_vnic_encap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. mutex_unlock(&adapter->mactbl_lock);
  123. }
  124. /*
  125. * opa_vnic_query_mac_tbl - query the mac table for a section
  126. *
  127. * This function implements query of specific function of the mac table.
  128. * The function also expects the requested range to be valid.
  129. */
  130. void opa_vnic_query_mac_tbl(struct opa_vnic_adapter *adapter,
  131. struct opa_veswport_mactable *tbl)
  132. {
  133. struct opa_vnic_mac_tbl_node *node;
  134. struct hlist_head *mactbl;
  135. int bkt;
  136. u16 loffset, lnum_entries;
  137. rcu_read_lock();
  138. mactbl = rcu_dereference(adapter->mactbl);
  139. if (!mactbl)
  140. goto get_mac_done;
  141. loffset = be16_to_cpu(tbl->offset);
  142. lnum_entries = be16_to_cpu(tbl->num_entries);
  143. vnic_hash_for_each(mactbl, bkt, node, hlist) {
  144. struct __opa_vnic_mactable_entry *nentry = &node->entry;
  145. struct opa_veswport_mactable_entry *entry;
  146. if ((node->index < loffset) ||
  147. (node->index >= (loffset + lnum_entries)))
  148. continue;
  149. /* populate entry in the tbl corresponding to the index */
  150. entry = &tbl->tbl_entries[node->index - loffset];
  151. memcpy(entry->mac_addr, nentry->mac_addr,
  152. ARRAY_SIZE(entry->mac_addr));
  153. memcpy(entry->mac_addr_mask, nentry->mac_addr_mask,
  154. ARRAY_SIZE(entry->mac_addr_mask));
  155. entry->dlid_sd = cpu_to_be32(nentry->dlid_sd);
  156. }
  157. tbl->mac_tbl_digest = cpu_to_be32(adapter->info.vport.mac_tbl_digest);
  158. get_mac_done:
  159. rcu_read_unlock();
  160. }
  161. /*
  162. * opa_vnic_update_mac_tbl - update mac table section
  163. *
  164. * This function updates the specified section of the mac table.
  165. * The procedure includes following steps.
  166. * - Allocate a new mac (hash) table.
  167. * - Add the specified entries to the new table.
  168. * (except the ones that are requested to be deleted).
  169. * - Add all the other entries from the old mac table.
  170. * - If there is a failure, free the new table and return.
  171. * - Switch to the new table.
  172. * - Free the old table and return.
  173. *
  174. * The function also expects the requested range to be valid.
  175. */
  176. int opa_vnic_update_mac_tbl(struct opa_vnic_adapter *adapter,
  177. struct opa_veswport_mactable *tbl)
  178. {
  179. struct opa_vnic_mac_tbl_node *node, *new_node;
  180. struct hlist_head *new_mactbl, *old_mactbl;
  181. int i, bkt, rc = 0;
  182. u8 key;
  183. u16 loffset, lnum_entries;
  184. mutex_lock(&adapter->mactbl_lock);
  185. /* allocate new mac table */
  186. new_mactbl = opa_vnic_alloc_mac_tbl();
  187. if (IS_ERR(new_mactbl)) {
  188. mutex_unlock(&adapter->mactbl_lock);
  189. return PTR_ERR(new_mactbl);
  190. }
  191. loffset = be16_to_cpu(tbl->offset);
  192. lnum_entries = be16_to_cpu(tbl->num_entries);
  193. /* add updated entries to the new mac table */
  194. for (i = 0; i < lnum_entries; i++) {
  195. struct __opa_vnic_mactable_entry *nentry;
  196. struct opa_veswport_mactable_entry *entry =
  197. &tbl->tbl_entries[i];
  198. u8 *mac_addr = entry->mac_addr;
  199. u8 empty_mac[ETH_ALEN] = { 0 };
  200. v_dbg("new mac entry %4d: %02x:%02x:%02x:%02x:%02x:%02x %x\n",
  201. loffset + i, mac_addr[0], mac_addr[1], mac_addr[2],
  202. mac_addr[3], mac_addr[4], mac_addr[5],
  203. entry->dlid_sd);
  204. /* if the entry is being removed, do not add it */
  205. if (!memcmp(mac_addr, empty_mac, ARRAY_SIZE(empty_mac)))
  206. continue;
  207. node = kzalloc(sizeof(*node), GFP_KERNEL);
  208. if (!node) {
  209. rc = -ENOMEM;
  210. goto updt_done;
  211. }
  212. node->index = loffset + i;
  213. nentry = &node->entry;
  214. memcpy(nentry->mac_addr, entry->mac_addr,
  215. ARRAY_SIZE(nentry->mac_addr));
  216. memcpy(nentry->mac_addr_mask, entry->mac_addr_mask,
  217. ARRAY_SIZE(nentry->mac_addr_mask));
  218. nentry->dlid_sd = be32_to_cpu(entry->dlid_sd);
  219. key = node->entry.mac_addr[OPA_VNIC_MAC_HASH_IDX];
  220. vnic_hash_add(new_mactbl, &node->hlist, key);
  221. }
  222. /* add other entries from current mac table to new mac table */
  223. old_mactbl = rcu_access_pointer(adapter->mactbl);
  224. if (!old_mactbl)
  225. goto switch_tbl;
  226. vnic_hash_for_each(old_mactbl, bkt, node, hlist) {
  227. if ((node->index >= loffset) &&
  228. (node->index < (loffset + lnum_entries)))
  229. continue;
  230. new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
  231. if (!new_node) {
  232. rc = -ENOMEM;
  233. goto updt_done;
  234. }
  235. new_node->index = node->index;
  236. memcpy(&new_node->entry, &node->entry, sizeof(node->entry));
  237. key = new_node->entry.mac_addr[OPA_VNIC_MAC_HASH_IDX];
  238. vnic_hash_add(new_mactbl, &new_node->hlist, key);
  239. }
  240. switch_tbl:
  241. /* switch to new table */
  242. rcu_assign_pointer(adapter->mactbl, new_mactbl);
  243. synchronize_rcu();
  244. adapter->info.vport.mac_tbl_digest = be32_to_cpu(tbl->mac_tbl_digest);
  245. updt_done:
  246. /* upon failure, free the new table; otherwise, free the old table */
  247. if (rc)
  248. opa_vnic_free_mac_tbl(new_mactbl);
  249. else
  250. opa_vnic_free_mac_tbl(old_mactbl);
  251. mutex_unlock(&adapter->mactbl_lock);
  252. return rc;
  253. }
  254. /* opa_vnic_chk_mac_tbl - check mac table for dlid */
  255. static uint32_t opa_vnic_chk_mac_tbl(struct opa_vnic_adapter *adapter,
  256. struct ethhdr *mac_hdr)
  257. {
  258. struct opa_vnic_mac_tbl_node *node;
  259. struct hlist_head *mactbl;
  260. u32 dlid = 0;
  261. u8 key;
  262. rcu_read_lock();
  263. mactbl = rcu_dereference(adapter->mactbl);
  264. if (unlikely(!mactbl))
  265. goto chk_done;
  266. key = mac_hdr->h_dest[OPA_VNIC_MAC_HASH_IDX];
  267. vnic_hash_for_each_possible(mactbl, node, hlist, key) {
  268. struct __opa_vnic_mactable_entry *entry = &node->entry;
  269. /* if related to source mac, skip */
  270. if (unlikely(OPA_VNIC_DLID_SD_IS_SRC_MAC(entry->dlid_sd)))
  271. continue;
  272. if (!memcmp(node->entry.mac_addr, mac_hdr->h_dest,
  273. ARRAY_SIZE(node->entry.mac_addr))) {
  274. /* mac address found */
  275. dlid = OPA_VNIC_DLID_SD_GET_DLID(node->entry.dlid_sd);
  276. break;
  277. }
  278. }
  279. chk_done:
  280. rcu_read_unlock();
  281. return dlid;
  282. }
  283. /* opa_vnic_get_dlid - find and return the DLID */
  284. static uint32_t opa_vnic_get_dlid(struct opa_vnic_adapter *adapter,
  285. struct sk_buff *skb, u8 def_port)
  286. {
  287. struct __opa_veswport_info *info = &adapter->info;
  288. struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
  289. u32 dlid;
  290. dlid = opa_vnic_chk_mac_tbl(adapter, mac_hdr);
  291. if (dlid)
  292. return dlid;
  293. if (is_multicast_ether_addr(mac_hdr->h_dest)) {
  294. dlid = info->vesw.u_mcast_dlid;
  295. } else {
  296. if (is_local_ether_addr(mac_hdr->h_dest)) {
  297. dlid = ((uint32_t)mac_hdr->h_dest[5] << 16) |
  298. ((uint32_t)mac_hdr->h_dest[4] << 8) |
  299. mac_hdr->h_dest[3];
  300. if (unlikely(!dlid))
  301. v_warn("Null dlid in MAC address\n");
  302. } else if (def_port != OPA_VNIC_INVALID_PORT) {
  303. dlid = info->vesw.u_ucast_dlid[def_port];
  304. }
  305. }
  306. return dlid;
  307. }
  308. /* opa_vnic_get_sc - return the service class */
  309. static u8 opa_vnic_get_sc(struct __opa_veswport_info *info,
  310. struct sk_buff *skb)
  311. {
  312. struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
  313. u16 vlan_tci;
  314. u8 sc;
  315. if (!__vlan_get_tag(skb, &vlan_tci)) {
  316. u8 pcp = OPA_VNIC_VLAN_PCP(vlan_tci);
  317. if (is_multicast_ether_addr(mac_hdr->h_dest))
  318. sc = info->vport.pcp_to_sc_mc[pcp];
  319. else
  320. sc = info->vport.pcp_to_sc_uc[pcp];
  321. } else {
  322. if (is_multicast_ether_addr(mac_hdr->h_dest))
  323. sc = info->vport.non_vlan_sc_mc;
  324. else
  325. sc = info->vport.non_vlan_sc_uc;
  326. }
  327. return sc;
  328. }
  329. u8 opa_vnic_get_vl(struct opa_vnic_adapter *adapter, struct sk_buff *skb)
  330. {
  331. struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
  332. struct __opa_veswport_info *info = &adapter->info;
  333. u8 vl;
  334. if (skb_vlan_tag_present(skb)) {
  335. u8 pcp = skb_vlan_tag_get(skb) >> VLAN_PRIO_SHIFT;
  336. if (is_multicast_ether_addr(mac_hdr->h_dest))
  337. vl = info->vport.pcp_to_vl_mc[pcp];
  338. else
  339. vl = info->vport.pcp_to_vl_uc[pcp];
  340. } else {
  341. if (is_multicast_ether_addr(mac_hdr->h_dest))
  342. vl = info->vport.non_vlan_vl_mc;
  343. else
  344. vl = info->vport.non_vlan_vl_uc;
  345. }
  346. return vl;
  347. }
  348. /* opa_vnic_calc_entropy - calculate the packet entropy */
  349. u8 opa_vnic_calc_entropy(struct opa_vnic_adapter *adapter, struct sk_buff *skb)
  350. {
  351. u16 hash16;
  352. /*
  353. * Get flow based 16-bit hash and then XOR the upper and lower bytes
  354. * to get the entropy.
  355. * __skb_tx_hash limits qcount to 16 bits. Hence, get 15-bit hash.
  356. */
  357. hash16 = __skb_tx_hash(adapter->netdev, skb, BIT(15));
  358. return (u8)((hash16 >> 8) ^ (hash16 & 0xff));
  359. }
  360. /* opa_vnic_get_def_port - get default port based on entropy */
  361. static inline u8 opa_vnic_get_def_port(struct opa_vnic_adapter *adapter,
  362. u8 entropy)
  363. {
  364. u8 flow_id;
  365. /* Add the upper and lower 4-bits of entropy to get the flow id */
  366. flow_id = ((entropy & 0xf) + (entropy >> 4));
  367. return adapter->flow_tbl[flow_id & (OPA_VNIC_FLOW_TBL_SIZE - 1)];
  368. }
  369. /* Calculate packet length including OPA header, crc and padding */
  370. static inline int opa_vnic_wire_length(struct sk_buff *skb)
  371. {
  372. u32 pad_len;
  373. /* padding for 8 bytes size alignment */
  374. pad_len = -(skb->len + OPA_VNIC_ICRC_TAIL_LEN) & 0x7;
  375. pad_len += OPA_VNIC_ICRC_TAIL_LEN;
  376. return (skb->len + pad_len) >> 3;
  377. }
  378. /* opa_vnic_encap_skb - encapsulate skb packet with OPA header and meta data */
  379. void opa_vnic_encap_skb(struct opa_vnic_adapter *adapter, struct sk_buff *skb)
  380. {
  381. struct __opa_veswport_info *info = &adapter->info;
  382. struct opa_vnic_skb_mdata *mdata;
  383. u8 def_port, sc, entropy, *hdr;
  384. u16 len, l4_hdr;
  385. u32 dlid;
  386. hdr = skb_push(skb, OPA_VNIC_HDR_LEN);
  387. entropy = opa_vnic_calc_entropy(adapter, skb);
  388. def_port = opa_vnic_get_def_port(adapter, entropy);
  389. len = opa_vnic_wire_length(skb);
  390. dlid = opa_vnic_get_dlid(adapter, skb, def_port);
  391. sc = opa_vnic_get_sc(info, skb);
  392. l4_hdr = info->vesw.vesw_id;
  393. mdata = (struct opa_vnic_skb_mdata *)skb_push(skb, sizeof(*mdata));
  394. mdata->vl = opa_vnic_get_vl(adapter, skb);
  395. mdata->entropy = entropy;
  396. mdata->flags = 0;
  397. if (unlikely(!dlid)) {
  398. mdata->flags = OPA_VNIC_SKB_MDATA_ENCAP_ERR;
  399. return;
  400. }
  401. opa_vnic_make_header(hdr, info->vport.encap_slid, dlid, len,
  402. info->vesw.pkey, entropy, sc, 0,
  403. OPA_VNIC_L4_ETHR, l4_hdr);
  404. }