Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net/ip_tunnels: Introduce tunnel_id_to_key32() and key32_to_tunnel_id()

Add utility functions to convert a 32 bits key into a 64 bits tunnel and
vice versa.
These functions will be used instead of cloning code in GRE and VXLAN,
and in tc act_iptunnel which will be introduced in a following patch in
this patchset.

Signed-off-by: Amir Vadai <amir@vadai.me>
Signed-off-by: Hadar Hen Zion <hadarh@mellanox.com>
Reviewed-by: Shmulik Ladkani <shmulik.ladkani@gmail.com>
Acked-by: Jiri Benc <jbenc@redhat.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Amir Vadai and committed by
David S. Miller
d817f432 e808bb6e

+23 -41
+2 -2
drivers/net/vxlan.c
··· 1291 1291 struct metadata_dst *tun_dst; 1292 1292 1293 1293 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY, 1294 - vxlan_vni_to_tun_id(vni), sizeof(*md)); 1294 + key32_to_tunnel_id(vni), sizeof(*md)); 1295 1295 1296 1296 if (!tun_dst) 1297 1297 goto drop; ··· 1945 1945 goto drop; 1946 1946 } 1947 1947 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port; 1948 - vni = vxlan_tun_id_to_vni(info->key.tun_id); 1948 + vni = tunnel_id_to_key32(info->key.tun_id); 1949 1949 remote_ip.sa.sa_family = ip_tunnel_info_af(info); 1950 1950 if (remote_ip.sa.sa_family == AF_INET) { 1951 1951 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
+19
include/net/ip_tunnels.h
··· 222 222 return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET; 223 223 } 224 224 225 + static inline __be64 key32_to_tunnel_id(__be32 key) 226 + { 227 + #ifdef __BIG_ENDIAN 228 + return (__force __be64)key; 229 + #else 230 + return (__force __be64)((__force u64)key << 32); 231 + #endif 232 + } 233 + 234 + /* Returns the least-significant 32 bits of a __be64. */ 235 + static inline __be32 tunnel_id_to_key32(__be64 tun_id) 236 + { 237 + #ifdef __BIG_ENDIAN 238 + return (__force __be32)tun_id; 239 + #else 240 + return (__force __be32)((__force u64)tun_id >> 32); 241 + #endif 242 + } 243 + 225 244 #ifdef CONFIG_INET 226 245 227 246 int ip_tunnel_init(struct net_device *dev);
-18
include/net/vxlan.h
··· 350 350 #endif 351 351 } 352 352 353 - static inline __be32 vxlan_tun_id_to_vni(__be64 tun_id) 354 - { 355 - #if defined(__BIG_ENDIAN) 356 - return (__force __be32)tun_id; 357 - #else 358 - return (__force __be32)((__force u64)tun_id >> 32); 359 - #endif 360 - } 361 - 362 - static inline __be64 vxlan_vni_to_tun_id(__be32 vni) 363 - { 364 - #if defined(__BIG_ENDIAN) 365 - return (__force __be64)vni; 366 - #else 367 - return (__force __be64)((u64)(__force u32)vni << 32); 368 - #endif 369 - } 370 - 371 353 static inline size_t vxlan_rco_start(__be32 vni_field) 372 354 { 373 355 return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
+2 -21
net/ipv4/ip_gre.c
··· 246 246 ipgre_err(skb, info, &tpi); 247 247 } 248 248 249 - static __be64 key_to_tunnel_id(__be32 key) 250 - { 251 - #ifdef __BIG_ENDIAN 252 - return (__force __be64)((__force u32)key); 253 - #else 254 - return (__force __be64)((__force u64)key << 32); 255 - #endif 256 - } 257 - 258 - /* Returns the least-significant 32 bits of a __be64. */ 259 - static __be32 tunnel_id_to_key(__be64 x) 260 - { 261 - #ifdef __BIG_ENDIAN 262 - return (__force __be32)x; 263 - #else 264 - return (__force __be32)((__force u64)x >> 32); 265 - #endif 266 - } 267 - 268 249 static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, 269 250 struct ip_tunnel_net *itn, int hdr_len, bool raw_proto) 270 251 { ··· 271 290 __be64 tun_id; 272 291 273 292 flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY); 274 - tun_id = key_to_tunnel_id(tpi->key); 293 + tun_id = key32_to_tunnel_id(tpi->key); 275 294 tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0); 276 295 if (!tun_dst) 277 296 return PACKET_REJECT; ··· 427 446 428 447 flags = tun_info->key.tun_flags & (TUNNEL_CSUM | TUNNEL_KEY); 429 448 gre_build_header(skb, tunnel_hlen, flags, proto, 430 - tunnel_id_to_key(tun_info->key.tun_id), 0); 449 + tunnel_id_to_key32(tun_info->key.tun_id), 0); 431 450 432 451 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; 433 452