Browse code

sip: tcp: hash only the sin[6]_addr to compute the tcp worker thread

Apparently, hashing the complete sockaddr_storage is not really reliable.

Raphael Coeffic authored on 18/11/2013 13:47:57
Showing 2 changed files
... ...
@@ -661,6 +661,19 @@ void tcp_server_socket::on_accept(int fd, short ev, void* arg)
661 661
   trsp->on_accept(fd,ev);
662 662
 }
663 663
 
664
+uint32_t tcp_server_socket::hash_addr(const sockaddr_storage* addr)
665
+{
666
+  unsigned int port = am_get_port(addr);
667
+  uint32_t h=0;
668
+  if(addr->ss_family == AF_INET) {
669
+    h = hashlittle(&SAv4(addr)->sin_addr,sizeof(in_addr),port);
670
+  }
671
+  else {
672
+    h = hashlittle(&SAv6(addr)->sin6_addr,sizeof(in6_addr),port);
673
+  }
674
+  return h;
675
+}
676
+
664 677
 void tcp_server_socket::add_event(struct event_base *evbase)
665 678
 {
666 679
   this->evbase = evbase;
... ...
@@ -711,11 +724,11 @@ void tcp_server_socket::on_accept(int sd, short ev)
711 724
     return;
712 725
   }
713 726
 
714
-  uint32_t h = hashlittle(&src_addr,sizeof(sockaddr_storage),0);
727
+  uint32_t h = hash_addr(&src_addr);
715 728
   unsigned int idx = h % workers.size();
716 729
   
717 730
   // in case of thread pooling, do following in worker thread
718
-  DBG("tcp_trsp_socket::create_connected");
731
+  DBG("tcp_trsp_socket::create_connected (idx = %u)",idx);
719 732
   tcp_trsp_socket::create_connected(this,workers[idx],connection_sd,
720 733
 				    &src_addr,evbase);
721 734
 }
... ...
@@ -723,8 +736,9 @@ void tcp_server_socket::on_accept(int sd, short ev)
723 736
 int tcp_server_socket::send(const sockaddr_storage* sa, const char* msg,
724 737
 			    const int msg_len, unsigned int flags)
725 738
 {
726
-  uint32_t h = hashlittle(sa,sizeof(sockaddr_storage),0);
739
+  uint32_t h = hash_addr(sa);
727 740
   unsigned int idx = h % workers.size();
741
+  DBG("tcp_server_socket::send: idx = %u",idx);
728 742
   return workers[idx]->send(sa,msg,msg_len,flags);
729 743
 }
730 744
 
... ...
@@ -231,6 +231,8 @@ class tcp_server_socket: public trsp_socket
231 231
   /* libevent callback on new connection */
232 232
   static void on_accept(int sd, short ev, void* arg);
233 233
 
234
+  static uint32_t hash_addr(const sockaddr_storage* addr);
235
+
234 236
 public:
235 237
   tcp_server_socket(unsigned short if_num);
236 238
   ~tcp_server_socket() {}