spectrum_ipip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
  3. #include <net/ip_tunnels.h>
  4. #include <net/ip6_tunnel.h>
  5. #include "spectrum_ipip.h"
  6. struct ip_tunnel_parm
  7. mlxsw_sp_ipip_netdev_parms4(const struct net_device *ol_dev)
  8. {
  9. struct ip_tunnel *tun = netdev_priv(ol_dev);
  10. return tun->parms;
  11. }
  12. struct __ip6_tnl_parm
  13. mlxsw_sp_ipip_netdev_parms6(const struct net_device *ol_dev)
  14. {
  15. struct ip6_tnl *tun = netdev_priv(ol_dev);
  16. return tun->parms;
  17. }
  18. static bool mlxsw_sp_ipip_parms4_has_ikey(struct ip_tunnel_parm parms)
  19. {
  20. return !!(parms.i_flags & TUNNEL_KEY);
  21. }
  22. static bool mlxsw_sp_ipip_parms4_has_okey(struct ip_tunnel_parm parms)
  23. {
  24. return !!(parms.o_flags & TUNNEL_KEY);
  25. }
  26. static u32 mlxsw_sp_ipip_parms4_ikey(struct ip_tunnel_parm parms)
  27. {
  28. return mlxsw_sp_ipip_parms4_has_ikey(parms) ?
  29. be32_to_cpu(parms.i_key) : 0;
  30. }
  31. static u32 mlxsw_sp_ipip_parms4_okey(struct ip_tunnel_parm parms)
  32. {
  33. return mlxsw_sp_ipip_parms4_has_okey(parms) ?
  34. be32_to_cpu(parms.o_key) : 0;
  35. }
  36. static union mlxsw_sp_l3addr
  37. mlxsw_sp_ipip_parms4_saddr(struct ip_tunnel_parm parms)
  38. {
  39. return (union mlxsw_sp_l3addr) { .addr4 = parms.iph.saddr };
  40. }
  41. static union mlxsw_sp_l3addr
  42. mlxsw_sp_ipip_parms6_saddr(struct __ip6_tnl_parm parms)
  43. {
  44. return (union mlxsw_sp_l3addr) { .addr6 = parms.laddr };
  45. }
  46. static union mlxsw_sp_l3addr
  47. mlxsw_sp_ipip_parms4_daddr(struct ip_tunnel_parm parms)
  48. {
  49. return (union mlxsw_sp_l3addr) { .addr4 = parms.iph.daddr };
  50. }
  51. static union mlxsw_sp_l3addr
  52. mlxsw_sp_ipip_parms6_daddr(struct __ip6_tnl_parm parms)
  53. {
  54. return (union mlxsw_sp_l3addr) { .addr6 = parms.raddr };
  55. }
  56. union mlxsw_sp_l3addr
  57. mlxsw_sp_ipip_netdev_saddr(enum mlxsw_sp_l3proto proto,
  58. const struct net_device *ol_dev)
  59. {
  60. struct ip_tunnel_parm parms4;
  61. struct __ip6_tnl_parm parms6;
  62. switch (proto) {
  63. case MLXSW_SP_L3_PROTO_IPV4:
  64. parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
  65. return mlxsw_sp_ipip_parms4_saddr(parms4);
  66. case MLXSW_SP_L3_PROTO_IPV6:
  67. parms6 = mlxsw_sp_ipip_netdev_parms6(ol_dev);
  68. return mlxsw_sp_ipip_parms6_saddr(parms6);
  69. }
  70. WARN_ON(1);
  71. return (union mlxsw_sp_l3addr) {0};
  72. }
  73. static __be32 mlxsw_sp_ipip_netdev_daddr4(const struct net_device *ol_dev)
  74. {
  75. struct ip_tunnel_parm parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
  76. return mlxsw_sp_ipip_parms4_daddr(parms4).addr4;
  77. }
  78. static union mlxsw_sp_l3addr
  79. mlxsw_sp_ipip_netdev_daddr(enum mlxsw_sp_l3proto proto,
  80. const struct net_device *ol_dev)
  81. {
  82. struct ip_tunnel_parm parms4;
  83. struct __ip6_tnl_parm parms6;
  84. switch (proto) {
  85. case MLXSW_SP_L3_PROTO_IPV4:
  86. parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
  87. return mlxsw_sp_ipip_parms4_daddr(parms4);
  88. case MLXSW_SP_L3_PROTO_IPV6:
  89. parms6 = mlxsw_sp_ipip_netdev_parms6(ol_dev);
  90. return mlxsw_sp_ipip_parms6_daddr(parms6);
  91. }
  92. WARN_ON(1);
  93. return (union mlxsw_sp_l3addr) {0};
  94. }
  95. bool mlxsw_sp_l3addr_is_zero(union mlxsw_sp_l3addr addr)
  96. {
  97. union mlxsw_sp_l3addr naddr = {0};
  98. return !memcmp(&addr, &naddr, sizeof(naddr));
  99. }
  100. static int
  101. mlxsw_sp_ipip_nexthop_update_gre4(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
  102. struct mlxsw_sp_ipip_entry *ipip_entry)
  103. {
  104. u16 rif_index = mlxsw_sp_ipip_lb_rif_index(ipip_entry->ol_lb);
  105. __be32 daddr4 = mlxsw_sp_ipip_netdev_daddr4(ipip_entry->ol_dev);
  106. char ratr_pl[MLXSW_REG_RATR_LEN];
  107. mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
  108. true, MLXSW_REG_RATR_TYPE_IPIP,
  109. adj_index, rif_index);
  110. mlxsw_reg_ratr_ipip4_entry_pack(ratr_pl, be32_to_cpu(daddr4));
  111. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
  112. }
  113. static int
  114. mlxsw_sp_ipip_fib_entry_op_gre4_rtdp(struct mlxsw_sp *mlxsw_sp,
  115. u32 tunnel_index,
  116. struct mlxsw_sp_ipip_entry *ipip_entry)
  117. {
  118. u16 rif_index = mlxsw_sp_ipip_lb_rif_index(ipip_entry->ol_lb);
  119. char rtdp_pl[MLXSW_REG_RTDP_LEN];
  120. struct ip_tunnel_parm parms;
  121. unsigned int type_check;
  122. bool has_ikey;
  123. u32 daddr4;
  124. u32 ikey;
  125. parms = mlxsw_sp_ipip_netdev_parms4(ipip_entry->ol_dev);
  126. has_ikey = mlxsw_sp_ipip_parms4_has_ikey(parms);
  127. ikey = mlxsw_sp_ipip_parms4_ikey(parms);
  128. mlxsw_reg_rtdp_pack(rtdp_pl, MLXSW_REG_RTDP_TYPE_IPIP, tunnel_index);
  129. type_check = has_ikey ?
  130. MLXSW_REG_RTDP_IPIP_TYPE_CHECK_ALLOW_GRE_KEY :
  131. MLXSW_REG_RTDP_IPIP_TYPE_CHECK_ALLOW_GRE;
  132. /* Linux demuxes tunnels based on packet SIP (which must match tunnel
  133. * remote IP). Thus configure decap so that it filters out packets that
  134. * are not IPv4 or have the wrong SIP. IPIP_DECAP_ERROR trap is
  135. * generated for packets that fail this criterion. Linux then handles
  136. * such packets in slow path and generates ICMP destination unreachable.
  137. */
  138. daddr4 = be32_to_cpu(mlxsw_sp_ipip_netdev_daddr4(ipip_entry->ol_dev));
  139. mlxsw_reg_rtdp_ipip4_pack(rtdp_pl, rif_index,
  140. MLXSW_REG_RTDP_IPIP_SIP_CHECK_FILTER_IPV4,
  141. type_check, has_ikey, daddr4, ikey);
  142. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rtdp), rtdp_pl);
  143. }
  144. static int
  145. mlxsw_sp_ipip_fib_entry_op_gre4_ralue(struct mlxsw_sp *mlxsw_sp,
  146. u32 dip, u8 prefix_len, u16 ul_vr_id,
  147. enum mlxsw_reg_ralue_op op,
  148. u32 tunnel_index)
  149. {
  150. char ralue_pl[MLXSW_REG_RALUE_LEN];
  151. mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_REG_RALXX_PROTOCOL_IPV4, op,
  152. ul_vr_id, prefix_len, dip);
  153. mlxsw_reg_ralue_act_ip2me_tun_pack(ralue_pl, tunnel_index);
  154. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
  155. }
  156. static int mlxsw_sp_ipip_fib_entry_op_gre4(struct mlxsw_sp *mlxsw_sp,
  157. struct mlxsw_sp_ipip_entry *ipip_entry,
  158. enum mlxsw_reg_ralue_op op,
  159. u32 tunnel_index)
  160. {
  161. u16 ul_vr_id = mlxsw_sp_ipip_lb_ul_vr_id(ipip_entry->ol_lb);
  162. __be32 dip;
  163. int err;
  164. err = mlxsw_sp_ipip_fib_entry_op_gre4_rtdp(mlxsw_sp, tunnel_index,
  165. ipip_entry);
  166. if (err)
  167. return err;
  168. dip = mlxsw_sp_ipip_netdev_saddr(MLXSW_SP_L3_PROTO_IPV4,
  169. ipip_entry->ol_dev).addr4;
  170. return mlxsw_sp_ipip_fib_entry_op_gre4_ralue(mlxsw_sp, be32_to_cpu(dip),
  171. 32, ul_vr_id, op,
  172. tunnel_index);
  173. }
  174. static bool mlxsw_sp_ipip_tunnel_complete(enum mlxsw_sp_l3proto proto,
  175. const struct net_device *ol_dev)
  176. {
  177. union mlxsw_sp_l3addr saddr = mlxsw_sp_ipip_netdev_saddr(proto, ol_dev);
  178. union mlxsw_sp_l3addr daddr = mlxsw_sp_ipip_netdev_daddr(proto, ol_dev);
  179. /* Tunnels with unset local or remote address are valid in Linux and
  180. * used for lightweight tunnels (LWT) and Non-Broadcast Multi-Access
  181. * (NBMA) tunnels. In principle these can be offloaded, but the driver
  182. * currently doesn't support this. So punt.
  183. */
  184. return !mlxsw_sp_l3addr_is_zero(saddr) &&
  185. !mlxsw_sp_l3addr_is_zero(daddr);
  186. }
  187. static bool mlxsw_sp_ipip_can_offload_gre4(const struct mlxsw_sp *mlxsw_sp,
  188. const struct net_device *ol_dev,
  189. enum mlxsw_sp_l3proto ol_proto)
  190. {
  191. struct ip_tunnel *tunnel = netdev_priv(ol_dev);
  192. __be16 okflags = TUNNEL_KEY; /* We can't offload any other features. */
  193. bool inherit_ttl = tunnel->parms.iph.ttl == 0;
  194. bool inherit_tos = tunnel->parms.iph.tos & 0x1;
  195. return (tunnel->parms.i_flags & ~okflags) == 0 &&
  196. (tunnel->parms.o_flags & ~okflags) == 0 &&
  197. inherit_ttl && inherit_tos &&
  198. mlxsw_sp_ipip_tunnel_complete(MLXSW_SP_L3_PROTO_IPV4, ol_dev);
  199. }
  200. static struct mlxsw_sp_rif_ipip_lb_config
  201. mlxsw_sp_ipip_ol_loopback_config_gre4(struct mlxsw_sp *mlxsw_sp,
  202. const struct net_device *ol_dev)
  203. {
  204. struct ip_tunnel_parm parms = mlxsw_sp_ipip_netdev_parms4(ol_dev);
  205. enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt;
  206. lb_ipipt = mlxsw_sp_ipip_parms4_has_okey(parms) ?
  207. MLXSW_REG_RITR_LOOPBACK_IPIP_TYPE_IP_IN_GRE_KEY_IN_IP :
  208. MLXSW_REG_RITR_LOOPBACK_IPIP_TYPE_IP_IN_GRE_IN_IP;
  209. return (struct mlxsw_sp_rif_ipip_lb_config){
  210. .lb_ipipt = lb_ipipt,
  211. .okey = mlxsw_sp_ipip_parms4_okey(parms),
  212. .ul_protocol = MLXSW_SP_L3_PROTO_IPV4,
  213. .saddr = mlxsw_sp_ipip_netdev_saddr(MLXSW_SP_L3_PROTO_IPV4,
  214. ol_dev),
  215. };
  216. }
  217. static int
  218. mlxsw_sp_ipip_ol_netdev_change_gre4(struct mlxsw_sp *mlxsw_sp,
  219. struct mlxsw_sp_ipip_entry *ipip_entry,
  220. struct netlink_ext_ack *extack)
  221. {
  222. union mlxsw_sp_l3addr old_saddr, new_saddr;
  223. union mlxsw_sp_l3addr old_daddr, new_daddr;
  224. struct ip_tunnel_parm new_parms;
  225. bool update_tunnel = false;
  226. bool update_decap = false;
  227. bool update_nhs = false;
  228. int err = 0;
  229. new_parms = mlxsw_sp_ipip_netdev_parms4(ipip_entry->ol_dev);
  230. new_saddr = mlxsw_sp_ipip_parms4_saddr(new_parms);
  231. old_saddr = mlxsw_sp_ipip_parms4_saddr(ipip_entry->parms4);
  232. new_daddr = mlxsw_sp_ipip_parms4_daddr(new_parms);
  233. old_daddr = mlxsw_sp_ipip_parms4_daddr(ipip_entry->parms4);
  234. if (!mlxsw_sp_l3addr_eq(&new_saddr, &old_saddr)) {
  235. u16 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
  236. /* Since the local address has changed, if there is another
  237. * tunnel with a matching saddr, both need to be demoted.
  238. */
  239. if (mlxsw_sp_ipip_demote_tunnel_by_saddr(mlxsw_sp,
  240. MLXSW_SP_L3_PROTO_IPV4,
  241. new_saddr, ul_tb_id,
  242. ipip_entry)) {
  243. mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
  244. return 0;
  245. }
  246. update_tunnel = true;
  247. } else if ((mlxsw_sp_ipip_parms4_okey(ipip_entry->parms4) !=
  248. mlxsw_sp_ipip_parms4_okey(new_parms)) ||
  249. ipip_entry->parms4.link != new_parms.link) {
  250. update_tunnel = true;
  251. } else if (!mlxsw_sp_l3addr_eq(&new_daddr, &old_daddr)) {
  252. update_nhs = true;
  253. } else if (mlxsw_sp_ipip_parms4_ikey(ipip_entry->parms4) !=
  254. mlxsw_sp_ipip_parms4_ikey(new_parms)) {
  255. update_decap = true;
  256. }
  257. if (update_tunnel)
  258. err = __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
  259. true, true, true,
  260. extack);
  261. else if (update_nhs)
  262. err = __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
  263. false, false, true,
  264. extack);
  265. else if (update_decap)
  266. err = __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
  267. false, false, false,
  268. extack);
  269. ipip_entry->parms4 = new_parms;
  270. return err;
  271. }
  272. static const struct mlxsw_sp_ipip_ops mlxsw_sp_ipip_gre4_ops = {
  273. .dev_type = ARPHRD_IPGRE,
  274. .ul_proto = MLXSW_SP_L3_PROTO_IPV4,
  275. .nexthop_update = mlxsw_sp_ipip_nexthop_update_gre4,
  276. .fib_entry_op = mlxsw_sp_ipip_fib_entry_op_gre4,
  277. .can_offload = mlxsw_sp_ipip_can_offload_gre4,
  278. .ol_loopback_config = mlxsw_sp_ipip_ol_loopback_config_gre4,
  279. .ol_netdev_change = mlxsw_sp_ipip_ol_netdev_change_gre4,
  280. };
  281. const struct mlxsw_sp_ipip_ops *mlxsw_sp_ipip_ops_arr[] = {
  282. [MLXSW_SP_IPIP_TYPE_GRE4] = &mlxsw_sp_ipip_gre4_ops,
  283. };