| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
* s_bsd_kqueue.c: FreeBSD kqueue compatible network routines. |
| 4 |
* |
| 5 |
* Originally by Adrian Chadd <adrian@creative.net.au> |
| 6 |
* Copyright (C) 2005 Hybrid Development Team |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 21 |
* USA |
| 22 |
* |
| 23 |
* $Id: s_bsd_kqueue.c,v 1.45 2005/09/29 06:35:41 metalrock Exp $ |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "stdinc.h" |
| 27 |
#include <sys/event.h> |
| 28 |
#include "fdlist.h" |
| 29 |
#include "ircd.h" |
| 30 |
#include "memory.h" |
| 31 |
#include "s_bsd.h" |
| 32 |
#include "s_log.h" |
| 33 |
|
| 34 |
#define KE_LENGTH 128 |
| 35 |
|
| 36 |
/* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */ |
| 37 |
|
| 38 |
#ifndef EV_SET |
| 39 |
#define EV_SET(kevp, a, b, c, d, e, f) do { \ |
| 40 |
(kevp)->ident = (a); \ |
| 41 |
(kevp)->filter = (b); \ |
| 42 |
(kevp)->flags = (c); \ |
| 43 |
(kevp)->fflags = (d); \ |
| 44 |
(kevp)->data = (e); \ |
| 45 |
(kevp)->udata = (f); \ |
| 46 |
} while(0) |
| 47 |
#endif |
| 48 |
|
| 49 |
static fde_t kqfd; |
| 50 |
static struct kevent kq_fdlist[KE_LENGTH]; /* kevent buffer */ |
| 51 |
static int kqoff; /* offset into the buffer */ |
| 52 |
void init_netio(void); |
| 53 |
|
| 54 |
/* |
| 55 |
* init_netio |
| 56 |
* |
| 57 |
* This is a needed exported function which will be called to initialise |
| 58 |
* the network loop code. |
| 59 |
*/ |
| 60 |
void |
| 61 |
init_netio(void) |
| 62 |
{ |
| 63 |
int fd; |
| 64 |
|
| 65 |
if ((fd = kqueue()) < 0) |
| 66 |
{ |
| 67 |
ilog(L_CRIT, "init_netio: Couldn't open kqueue fd!"); |
| 68 |
exit(115); /* Whee! */ |
| 69 |
} |
| 70 |
|
| 71 |
fd_open(&kqfd, fd, 0, "kqueue() file descriptor"); |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* Write a single update to the kqueue list. |
| 76 |
*/ |
| 77 |
static void |
| 78 |
kq_update_events(int fd, int filter, int what) |
| 79 |
{ |
| 80 |
static struct timespec zero_timespec = {0, 0}; |
| 81 |
struct kevent *kep = kq_fdlist + kqoff; |
| 82 |
|
| 83 |
EV_SET(kep, (uintptr_t) fd, (short) filter, what, 0, 0, NULL); |
| 84 |
|
| 85 |
if (kqoff == KE_LENGTH) |
| 86 |
{ |
| 87 |
kevent(kqfd.fd, kq_fdlist, kqoff, NULL, 0, &zero_timespec); |
| 88 |
kqoff = 0; |
| 89 |
} |
| 90 |
else |
| 91 |
kqoff++; |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* comm_setselect |
| 96 |
* |
| 97 |
* This is a needed exported function which will be called to register |
| 98 |
* and deregister interest in a pending IO state for a given FD. |
| 99 |
*/ |
| 100 |
void |
| 101 |
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
| 102 |
void *client_data, time_t timeout) |
| 103 |
{ |
| 104 |
int new_events, diff; |
| 105 |
|
| 106 |
if ((type & COMM_SELECT_READ)) |
| 107 |
{ |
| 108 |
F->read_handler = handler; |
| 109 |
F->read_data = client_data; |
| 110 |
} |
| 111 |
|
| 112 |
if ((type & COMM_SELECT_WRITE)) |
| 113 |
{ |
| 114 |
F->write_handler = handler; |
| 115 |
F->write_data = client_data; |
| 116 |
} |
| 117 |
|
| 118 |
new_events = (F->read_handler ? COMM_SELECT_READ : 0) | |
| 119 |
(F->write_handler ? COMM_SELECT_WRITE : 0); |
| 120 |
|
| 121 |
if (timeout != 0) |
| 122 |
F->timeout = CurrentTime + (timeout / 1000); |
| 123 |
|
| 124 |
diff = new_events ^ F->evcache; |
| 125 |
|
| 126 |
if ((diff & COMM_SELECT_READ)) |
| 127 |
kq_update_events(F->fd, EVFILT_READ, |
| 128 |
(new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE); |
| 129 |
if ((diff & COMM_SELECT_WRITE)) |
| 130 |
kq_update_events(F->fd, EVFILT_WRITE, |
| 131 |
(new_events & COMM_SELECT_WRITE) ? EV_ADD : EV_DELETE); |
| 132 |
|
| 133 |
F->evcache = new_events; |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* comm_select |
| 138 |
* |
| 139 |
* Called to do the new-style IO, courtesy of squid (like most of this |
| 140 |
* new IO code). This routine handles the stuff we've hidden in |
| 141 |
* comm_setselect and fd_table[] and calls callbacks for IO ready |
| 142 |
* events. |
| 143 |
*/ |
| 144 |
void |
| 145 |
comm_select(void) |
| 146 |
{ |
| 147 |
int num, i; |
| 148 |
static struct kevent ke[KE_LENGTH]; |
| 149 |
struct timespec poll_time; |
| 150 |
PF *hdl; |
| 151 |
fde_t *F; |
| 152 |
|
| 153 |
/* |
| 154 |
* remember we are doing NANOseconds here, not micro/milli. God knows |
| 155 |
* why jlemon used a timespec, but hey, he wrote the interface, not I |
| 156 |
* -- Adrian |
| 157 |
*/ |
| 158 |
poll_time.tv_sec = 0; |
| 159 |
poll_time.tv_nsec = SELECT_DELAY * 1000000; |
| 160 |
num = kevent(kqfd.fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time); |
| 161 |
kqoff = 0; |
| 162 |
|
| 163 |
set_time(); |
| 164 |
|
| 165 |
if (num < 0) |
| 166 |
{ |
| 167 |
#ifdef HAVE_USLEEP |
| 168 |
usleep(50000); /* avoid 99% CPU in comm_select */ |
| 169 |
#endif |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
for (i = 0; i < num; i++) |
| 174 |
{ |
| 175 |
F = lookup_fd(ke[i].ident); |
| 176 |
if (F == NULL || !F->flags.open || (ke[i].flags & EV_ERROR)) |
| 177 |
continue; |
| 178 |
|
| 179 |
if (ke[i].filter == EVFILT_READ) |
| 180 |
if ((hdl = F->read_handler) != NULL) |
| 181 |
{ |
| 182 |
F->read_handler = NULL; |
| 183 |
hdl(F, F->read_data); |
| 184 |
if (!F->flags.open) |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
if (ke[i].filter == EVFILT_WRITE) |
| 189 |
if ((hdl = F->write_handler) != NULL) |
| 190 |
{ |
| 191 |
F->write_handler = NULL; |
| 192 |
hdl(F, F->write_data); |
| 193 |
if (!F->flags.open) |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
comm_setselect(F, 0, NULL, NULL, 0); |
| 198 |
} |
| 199 |
} |