1 #include <string.h> 2 3 #include "mtcp.h" 4 #include "socket.h" 5 #include "debug.h" 6 #include "eventpoll.h" 7 #include "timer.h" 8 #include "config.h" 9 10 /*---------------------------------------------------------------------------*/ 11 void 12 FreeMonListener(mtcp_manager_t mtcp, socket_map_t socket) 13 { 14 struct mon_listener *monitor = socket->monitor_listener; 15 16 #ifdef NEWEV 17 stree_dec_ref(mtcp->ev_store, monitor->stree_dontcare); 18 stree_dec_ref(mtcp->ev_store, monitor->stree_pre_rcv); 19 stree_dec_ref(mtcp->ev_store, monitor->stree_post_snd); 20 21 monitor->stree_dontcare = NULL; 22 monitor->stree_pre_rcv = NULL; 23 monitor->stree_post_snd = NULL; 24 #else 25 CleanupEvB(mtcp, &monitor->dontcare_evb); 26 CleanupEvB(mtcp, &monitor->pre_tcp_evb); 27 CleanupEvB(mtcp, &monitor->post_tcp_evb); 28 #endif 29 30 DestroyEventQueue(monitor->eq); 31 32 /* Clean up monitor filter */ 33 if (ISSET_BPFFILTER(&monitor->stream_syn_fcode)) 34 sfbpf_freecode(&monitor->stream_syn_fcode); 35 if (ISSET_BPFFILTER(&monitor->stream_orphan_fcode)) 36 sfbpf_freecode(&monitor->stream_orphan_fcode); 37 38 memset(monitor, 0, sizeof(struct mon_listener)); 39 } 40 /*---------------------------------------------------------------------------*/ 41 static inline void 42 FreeMonStream(mtcp_manager_t mtcp, socket_map_t socket) 43 { 44 struct mon_stream *mstream = socket->monitor_stream; 45 46 #ifdef NEWEV 47 stree_dec_ref(mtcp->ev_store, mstream->stree_dontcare); 48 stree_dec_ref(mtcp->ev_store, mstream->stree_pre_rcv); 49 stree_dec_ref(mtcp->ev_store, mstream->stree_post_snd); 50 51 mstream->stree_dontcare = NULL; 52 mstream->stree_pre_rcv = NULL; 53 mstream->stree_post_snd = NULL; 54 #else 55 CleanupEvP(&mstream->dontcare_evp); 56 CleanupEvP(&mstream->pre_tcp_evp); 57 CleanupEvP(&mstream->post_tcp_evp); 58 #endif 59 60 mstream->socket = NULL; 61 mstream->stream = NULL; 62 mstream->uctx = NULL; 63 64 mstream->peek_offset[0] = mstream->peek_offset[1] = 0; 65 } 66 /*---------------------------------------------------------------------------*/ 67 /** 68 * XXX - TODO: This is an ugly function.. I will improve it on 2nd iteration 69 */ 70 socket_map_t 71 AllocateSocket(mctx_t mctx, int socktype) 72 { 73 mtcp_manager_t mtcp = g_mtcp[mctx->cpu]; 74 socket_map_t socket = NULL; 75 76 switch (socktype) { 77 case MOS_SOCK_MONITOR_STREAM: 78 mtcp->num_msp++; 79 case MOS_SOCK_MONITOR_STREAM_ACTIVE: 80 case MOS_SOCK_MONITOR_RAW: 81 socket = TAILQ_FIRST(&mtcp->free_msmap); 82 if (!socket) 83 goto alloc_error; 84 TAILQ_REMOVE(&mtcp->free_msmap, socket, link); 85 /* if there is not invalidated events, insert the socket to the end */ 86 /* and find another socket in the free smap list */ 87 if (socket->events) { 88 TRACE_INFO("There are still not invalidate events remaining.\n"); 89 TRACE_DBG("There are still not invalidate events remaining.\n"); 90 TAILQ_INSERT_TAIL(&mtcp->free_msmap, socket, link); 91 socket = NULL; 92 goto alloc_error; 93 } 94 break; 95 96 default: 97 socket = TAILQ_FIRST(&mtcp->free_smap); 98 if (!socket) 99 goto alloc_error; 100 TAILQ_REMOVE(&mtcp->free_smap, socket, link); 101 /* if there is not invalidated events, insert the socket to the end */ 102 /* and find another socket in the free smap list */ 103 if (socket->events) { 104 TRACE_INFO("There are still not invalidate events remaining.\n"); 105 TRACE_DBG("There are still not invalidate events remaining.\n"); 106 TAILQ_INSERT_TAIL(&mtcp->free_smap, socket, link); 107 socket = NULL; 108 goto alloc_error; 109 } 110 socket->stream = NULL; 111 /* 112 * reset a few fields (needed for client socket) 113 * addr = INADDR_ANY, port = INPORT_ANY 114 */ 115 memset(&socket->saddr, 0, sizeof(struct sockaddr_in)); 116 memset(&socket->ep_data, 0, sizeof(mtcp_epoll_data_t)); 117 } 118 119 socket->socktype = socktype; 120 socket->opts = 0; 121 socket->epoll = 0; 122 socket->events = 0; 123 124 return socket; 125 126 alloc_error: 127 TRACE_ERROR("The concurrent sockets are at maximum.\n"); 128 return NULL; 129 } 130 /*---------------------------------------------------------------------------*/ 131 void 132 FreeSocket(mctx_t mctx, int sockid, int socktype) 133 { 134 mtcp_manager_t mtcp = g_mtcp[mctx->cpu]; 135 socket_map_t socket = NULL; 136 137 switch (socktype) { 138 case MOS_SOCK_UNUSED: 139 return; 140 case MOS_SOCK_MONITOR_STREAM_ACTIVE: 141 socket = &mtcp->msmap[sockid]; 142 FreeMonStream(mtcp, socket); 143 TAILQ_INSERT_TAIL(&mtcp->free_msmap, socket, link); 144 break; 145 case MOS_SOCK_MONITOR_STREAM: 146 mtcp->num_msp--; 147 case MOS_SOCK_MONITOR_RAW: 148 socket = &mtcp->msmap[sockid]; 149 FreeMonListener(mtcp, socket); 150 TAILQ_INSERT_TAIL(&mtcp->free_msmap, socket, link); 151 break; 152 default: /* MOS_SOCK_STREAM_* */ 153 socket = &mtcp->smap[sockid]; 154 TAILQ_INSERT_TAIL(&mtcp->free_smap, socket, link); 155 /* insert into free stream map */ 156 mtcp->smap[sockid].stream = NULL; 157 break; 158 } 159 160 socket->socktype = MOS_SOCK_UNUSED; 161 socket->epoll = MOS_EPOLLNONE; 162 socket->events = 0; 163 } 164 /*---------------------------------------------------------------------------*/ 165 socket_map_t 166 GetSocket(mctx_t mctx, int sockid) 167 { 168 if (sockid < 0 || sockid >= g_config.mos->max_concurrency) { 169 errno = EBADF; 170 return NULL; 171 } 172 173 return &g_mtcp[mctx->cpu]->smap[sockid]; 174 } 175 /*---------------------------------------------------------------------------*/ 176