session: use no for ports in transport port allocator 93/40493/3
authorFlorin Coras <fcoras@cisco.com>
Sat, 9 Mar 2024 23:51:50 +0000 (15:51 -0800)
committerFlorin Coras <florin.coras@gmail.com>
Sun, 10 Mar 2024 00:13:01 +0000 (00:13 +0000)
Type: improvement

Change-Id: Ia1d8aaa3c51938cfa15dd09102471f52ebe67a3d
Signed-off-by: Florin Coras <fcoras@cisco.com>
src/vnet/session/transport.c
src/vnet/tcp/tcp.c
src/vnet/udp/udp.c

index e6eb9d7..1c2a926 100644 (file)
@@ -528,11 +528,13 @@ transport_release_local_endpoint (u8 proto, ip46_address_t *lcl_ip, u16 port)
   u32 lepi;
 
   lepi = transport_endpoint_lookup (&tm->local_endpoints_table, proto, lcl_ip,
-                                   clib_net_to_host_u16 (port));
+                                   port);
   if (lepi == ENDPOINT_INVALID_INDEX)
     return -1;
 
-  lep = pool_elt_at_index (tm->local_endpoints, lepi);
+  /* First worker may be cleaning up ports so avoid touching free bitmap */
+  lep = &tm->local_endpoints[lepi];
+  ASSERT (lep->refcnt >= 1);
 
   /* Local endpoint no longer in use, program cleanup */
   if (!clib_atomic_sub_fetch (&lep->refcnt, 1))
@@ -583,7 +585,7 @@ transport_share_local_endpoint (u8 proto, ip46_address_t * lcl_ip, u16 port)
    * used to allocate and free ports. So, pool has only one writer and
    * potentially many readers. Listeners are allocated with barrier */
   lepi = transport_endpoint_lookup (&tm->local_endpoints_table, proto, lcl_ip,
-                                   clib_net_to_host_u16 (port));
+                                   port);
   if (lepi != ENDPOINT_INVALID_INDEX)
     {
       lep = pool_elt_at_index (tm->local_endpoints, lepi);
@@ -594,6 +596,8 @@ transport_share_local_endpoint (u8 proto, ip46_address_t * lcl_ip, u16 port)
 /**
  * Allocate local port and add if successful add entry to local endpoint
  * table to mark the pair as used.
+ *
+ * @return port in net order or -1 if port cannot be allocated
  */
 int
 transport_alloc_local_port (u8 proto, ip46_address_t *lcl_addr,
@@ -619,7 +623,10 @@ transport_alloc_local_port (u8 proto, ip46_address_t *lcl_addr,
        {
          port = random_u32 (&tm->port_allocator_seed) & PORT_MASK;
          if (PREDICT_TRUE (port >= min && port < max))
-           break;
+           {
+             port = clib_host_to_net_u16 (port);
+             break;
+           }
        }
 
       if (!transport_endpoint_mark_used (proto, lcl_addr, port))
@@ -733,10 +740,9 @@ transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
     }
   else
     {
-      port = clib_net_to_host_u16 (rmt_cfg->peer.port);
-      *lcl_port = port;
+      *lcl_port = rmt_cfg->peer.port;
 
-      if (!transport_endpoint_mark_used (proto, lcl_addr, port))
+      if (!transport_endpoint_mark_used (proto, lcl_addr, rmt_cfg->peer.port))
        return 0;
 
       /* IP:port pair already in use, check if 6-tuple available */
@@ -746,7 +752,7 @@ transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
        return SESSION_E_PORTINUSE;
 
       /* 6-tuple is available so increment lcl endpoint refcount */
-      transport_share_local_endpoint (proto, lcl_addr, port);
+      transport_share_local_endpoint (proto, lcl_addr, rmt_cfg->peer.port);
 
       return 0;
     }
index a6eb35a..c00685d 100644 (file)
@@ -831,7 +831,7 @@ tcp_session_open (transport_endpoint_cfg_t * rmt)
   ip_copy (&tc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
   ip_copy (&tc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
   tc->c_rmt_port = rmt->port;
-  tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
+  tc->c_lcl_port = lcl_port;
   tc->c_is_ip4 = rmt->is_ip4;
   tc->c_proto = TRANSPORT_PROTO_TCP;
   tc->c_fib_index = rmt->fib_index;
index 0a0247a..669dd03 100644 (file)
@@ -420,7 +420,7 @@ udp_open_connection (transport_endpoint_cfg_t * rmt)
                                            lcl_port);
          lcl_port =
            transport_alloc_local_port (TRANSPORT_PROTO_UDP, &lcl_addr, rmt);
-         if (lcl_port < 1)
+         if ((int) lcl_port < 1)
            return SESSION_E_PORTINUSE;
        }
     }
@@ -432,7 +432,7 @@ udp_open_connection (transport_endpoint_cfg_t * rmt)
   ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
   ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
   uc->c_rmt_port = rmt->port;
-  uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
+  uc->c_lcl_port = lcl_port;
   uc->c_is_ip4 = rmt->is_ip4;
   uc->c_proto = TRANSPORT_PROTO_UDP;
   uc->c_fib_index = rmt->fib_index;