1 diff --unified --recursive --new-file linux-2.6.15-rc6/include/linux/ring.h linux-2.6.15-rc6-1-686-smp-ring3/include/linux/ring.h
2 --- linux-2.6.15-rc6/include/linux/ring.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.15-rc6-1-686-smp-ring3/include/linux/ring.h 2005-12-24 00:24:01.000000000 +0100
6 + * Definitions for packet ring
8 + * 2004 - Luca Deri <deri@ntop.org>
14 +#define INCLUDE_MAC_INFO
16 +#ifdef INCLUDE_MAC_INFO
17 +#define SKB_DISPLACEMENT 14 /* Include MAC address information */
19 +#define SKB_DISPLACEMENT 0 /* Do NOT include MAC address information */
23 +#define RING_MAGIC_VALUE 0x88
24 +#define RING_FLOWSLOT_VERSION 6
25 +#define RING_VERSION "3.1"
27 +#define SO_ADD_TO_CLUSTER 99
28 +#define SO_REMOVE_FROM_CLUSTER 100
29 +#define SO_SET_REFLECTOR 101
31 +/* *********************************** */
35 + struct timeval ts; /* time stamp */
36 + u_int32_t caplen; /* length of portion present */
37 + u_int32_t len; /* length this packet (off wire) */
41 +/* *********************************** */
44 + cluster_per_flow = 0,
48 +/* *********************************** */
50 +#define RING_MIN_SLOT_SIZE (60+sizeof(struct pcap_pkthdr))
51 +#define RING_MAX_SLOT_SIZE (1514+sizeof(struct pcap_pkthdr))
53 +/* *********************************** */
55 +typedef struct flowSlotInfo {
56 + u_int16_t version, sample_rate;
57 + u_int32_t tot_slots, slot_len, data_len, tot_mem;
59 + u_int64_t tot_pkts, tot_lost;
60 + u_int64_t tot_insert, tot_read;
61 + u_int32_t insert_idx, remove_idx;
64 +/* *********************************** */
66 +typedef struct flowSlot {
68 + u_char magic; /* It must alwasy be zero */
70 + u_char slot_state; /* 0=empty, 1=full */
71 + u_char bucket; /* bucket[bucketLen] */
74 +/* *********************************** */
78 +FlowSlotInfo* getRingPtr(void);
79 +int allocateRing(char *deviceName, u_int numSlots,
80 + u_int bucketLen, u_int sampleRate);
81 +unsigned int pollRing(struct file *fp, struct poll_table_struct * wait);
82 +void deallocateRing(void);
84 +/* ************************* */
86 +typedef int (*handle_ring_skb)(struct sk_buff *skb,
87 + u_char recv_packet, u_char real_skb);
88 +extern handle_ring_skb get_skb_ring_handler(void);
89 +extern void set_skb_ring_handler(handle_ring_skb the_handler);
90 +extern void do_skb_ring_handler(struct sk_buff *skb,
91 + u_char recv_packet, u_char real_skb);
93 +typedef int (*handle_ring_buffer)(struct net_device *dev,
94 + char *data, int len);
95 +extern handle_ring_buffer get_buffer_ring_handler(void);
96 +extern void set_buffer_ring_handler(handle_ring_buffer the_handler);
97 +extern int do_buffer_ring_handler(struct net_device *dev,
98 + char *data, int len);
99 +#endif /* __KERNEL__ */
101 +/* *********************************** */
103 +#define PF_RING 27 /* Packet Ring */
104 +#define SOCK_RING PF_RING
107 +#define SIORINGPOLL 0x8888
109 +/* *********************************** */
111 +#endif /* __RING_H */
112 diff --unified --recursive --new-file linux-2.6.15-rc6/net/core/dev.c linux-2.6.15-rc6-1-686-smp-ring3/net/core/dev.c
113 --- linux-2.6.15-rc6/net/core/dev.c 2005-12-19 01:36:54.000000000 +0100
114 +++ linux-2.6.15-rc6-1-686-smp-ring3/net/core/dev.c 2005-12-24 00:24:02.000000000 +0100
116 #endif /* CONFIG_NET_RADIO */
117 #include <asm/current.h>
119 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
121 +/* #define RING_DEBUG */
123 +#include <linux/ring.h>
124 +#include <linux/version.h>
126 +static handle_ring_skb ring_handler = NULL;
128 +handle_ring_skb get_skb_ring_handler() { return(ring_handler); }
130 +void set_skb_ring_handler(handle_ring_skb the_handler) {
131 + ring_handler = the_handler;
134 +void do_skb_ring_handler(struct sk_buff *skb,
135 + u_char recv_packet, u_char real_skb) {
137 + ring_handler(skb, recv_packet, real_skb);
140 +/* ******************* */
142 +static handle_ring_buffer buffer_ring_handler = NULL;
144 +handle_ring_buffer get_buffer_ring_handler() { return(buffer_ring_handler); }
146 +void set_buffer_ring_handler(handle_ring_buffer the_handler) {
147 + buffer_ring_handler = the_handler;
150 +int do_buffer_ring_handler(struct net_device *dev, char *data, int len) {
151 + if(buffer_ring_handler) {
152 + buffer_ring_handler(dev, data, len);
158 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
159 +EXPORT_SYMBOL(get_skb_ring_handler);
160 +EXPORT_SYMBOL(set_skb_ring_handler);
161 +EXPORT_SYMBOL(do_skb_ring_handler);
163 +EXPORT_SYMBOL(get_buffer_ring_handler);
164 +EXPORT_SYMBOL(set_buffer_ring_handler);
165 +EXPORT_SYMBOL(do_buffer_ring_handler);
170 * The list of packet types we will receive (as opposed to discard)
171 * and the routines to invoke.
172 @@ -1296,6 +1346,10 @@
173 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
176 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
177 + if(ring_handler) ring_handler(skb, 0, 1);
178 +#endif /* CONFIG_RING */
180 /* Grab device queue */
181 spin_lock(&dev->queue_lock);
183 @@ -1437,6 +1491,13 @@
187 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
188 + if(ring_handler && ring_handler(skb, 1, 1)) {
189 + /* The packet has been copied into a ring */
190 + return(NET_RX_SUCCESS);
192 +#endif /* CONFIG_RING */
194 if (local_softirq_pending())
197 @@ -1582,6 +1643,13 @@
198 struct net_device *orig_dev;
199 int ret = NET_RX_DROP;
201 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
202 + if(ring_handler && ring_handler(skb, 1, 1)) {
203 + /* The packet has been copied into a ring */
204 + return(NET_RX_SUCCESS);
206 +#endif /* CONFIG_RING */
209 /* if we've gotten here through NAPI, check netpoll */
210 if (skb->dev->poll && netpoll_rx(skb))
211 diff --unified --recursive --new-file linux-2.6.15-rc6/net/Kconfig linux-2.6.15-rc6-1-686-smp-ring3/net/Kconfig
212 --- linux-2.6.15-rc6/net/Kconfig 2005-12-19 01:36:54.000000000 +0100
213 +++ linux-2.6.15-rc6-1-686-smp-ring3/net/Kconfig 2005-12-24 00:24:02.000000000 +0100
215 source "net/unix/Kconfig"
216 source "net/xfrm/Kconfig"
218 +source "net/ring/Kconfig"
220 bool "TCP/IP networking"
222 diff --unified --recursive --new-file linux-2.6.15-rc6/net/Makefile linux-2.6.15-rc6-1-686-smp-ring3/net/Makefile
223 --- linux-2.6.15-rc6/net/Makefile 2005-12-19 01:36:54.000000000 +0100
224 +++ linux-2.6.15-rc6-1-686-smp-ring3/net/Makefile 2005-12-24 00:24:02.000000000 +0100
226 obj-$(CONFIG_DECNET) += decnet/
227 obj-$(CONFIG_ECONET) += econet/
228 obj-$(CONFIG_VLAN_8021Q) += 8021q/
229 +obj-$(CONFIG_RING) += ring/
230 obj-$(CONFIG_IP_DCCP) += dccp/
231 obj-$(CONFIG_IP_SCTP) += sctp/
232 obj-$(CONFIG_IEEE80211) += ieee80211/
233 diff --unified --recursive --new-file linux-2.6.15-rc6/net/ring/Kconfig linux-2.6.15-rc6-1-686-smp-ring3/net/ring/Kconfig
234 --- linux-2.6.15-rc6/net/ring/Kconfig 1970-01-01 01:00:00.000000000 +0100
235 +++ linux-2.6.15-rc6-1-686-smp-ring3/net/ring/Kconfig 2005-12-24 00:24:02.000000000 +0100
238 + tristate "PF_RING sockets (EXPERIMENTAL)"
239 + depends on EXPERIMENTAL
241 + PF_RING socket family, optimized for packet capture.
242 + If a PF_RING socket is bound to an adapter (via the bind() system
243 + call), such adapter will be used in read-only mode until the socket
244 + is destroyed. Whenever an incoming packet is received from the adapter
245 + it will not passed to upper layers, but instead it is copied to a ring
246 + buffer, which in turn is exported to user space applications via mmap.
247 + Please refer to http://luca.ntop.org/Ring.pdf for more.
249 + Say N unless you know what you are doing.
251 diff --unified --recursive --new-file linux-2.6.15-rc6/net/ring/Makefile linux-2.6.15-rc6-1-686-smp-ring3/net/ring/Makefile
252 --- linux-2.6.15-rc6/net/ring/Makefile 1970-01-01 01:00:00.000000000 +0100
253 +++ linux-2.6.15-rc6-1-686-smp-ring3/net/ring/Makefile 2005-12-24 00:24:02.000000000 +0100
256 +# Makefile for the ring driver.
261 +ring-objs := ring_packet.o
262 diff --unified --recursive --new-file linux-2.6.15-rc6/net/ring/ring_packet.c linux-2.6.15-rc6-1-686-smp-ring3/net/ring/ring_packet.c
263 --- linux-2.6.15-rc6/net/ring/ring_packet.c 1970-01-01 01:00:00.000000000 +0100
264 +++ linux-2.6.15-rc6-1-686-smp-ring3/net/ring/ring_packet.c 2005-12-24 00:24:02.000000000 +0100
268 + * (C) 2004-05 - Luca Deri <deri@ntop.org>
270 + * This code includes contributions courtesy of
271 + * - Jeff Randall <jrandall@nexvu.com>
272 + * - Helmut Manck <helmut.manck@secunet.com>
273 + * - Brad Doctor <brad@stillsecure.com>
274 + * - Amit D. Chaudhary <amit_ml@rajgad.com>
280 + add an entry inside the /proc filesystem
283 +#include <linux/version.h>
284 +#include <linux/config.h>
285 +#include <linux/module.h>
286 +#include <linux/kernel.h>
287 +#include <linux/socket.h>
288 +#include <linux/skbuff.h>
289 +#include <linux/rtnetlink.h>
290 +#include <linux/in.h>
291 +#include <linux/in6.h>
292 +#include <linux/init.h>
293 +#include <linux/filter.h>
294 +#include <linux/ring.h>
295 +#include <linux/ip.h>
296 +#include <linux/tcp.h>
297 +#include <linux/udp.h>
298 +#include <linux/list.h>
299 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
300 +#include <net/xfrm.h>
302 +#include <linux/poll.h>
304 +#include <net/sock.h>
305 +#include <asm/io.h> /* needed for virt_to_phys() */
307 +/* #define RING_DEBUG */
309 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11))
310 +static inline int remap_page_range(struct vm_area_struct *vma,
311 + unsigned long uvaddr,
312 + unsigned long paddr,
313 + unsigned long size,
315 + return(remap_pfn_range(vma, uvaddr, paddr >> PAGE_SHIFT,
320 +/* ************************************************* */
322 +#define CLUSTER_LEN 8
324 +struct ring_cluster {
325 + u_short cluster_id; /* 0 = no cluster */
326 + u_short num_cluster_elements;
327 + enum cluster_type hashing_mode;
328 + u_short hashing_id;
329 + struct sock *sk[CLUSTER_LEN];
330 + struct ring_cluster *next; /* NULL = last element of the cluster */
333 +/* ************************************************* */
335 +struct ring_element {
336 + struct list_head list;
340 +/* ************************************************* */
343 + struct net_device *ring_netdev;
346 + u_short cluster_id; /* 0 = no cluster */
349 + struct net_device *reflector_dev;
351 + /* Packet buffers */
352 + unsigned long order;
355 + unsigned long ring_memory;
356 + FlowSlotInfo *slots_info; /* Basically it points to ring_memory */
357 + char *ring_slots; /* Basically it points to ring_memory
358 + +sizeof(FlowSlotInfo) */
360 + /* Packet Sampling */
361 + u_int pktToSample, sample_rate;
364 + struct sk_filter *bpfFilter;
367 + atomic_t num_ring_slots_waiters;
368 + wait_queue_head_t ring_slots_waitqueue;
369 + rwlock_t ring_index_lock;
371 + /* Indexes (Internal) */
372 + u_int insert_page_id, insert_slot_id;
375 +/* ************************************************* */
377 +/* List of all ring sockets. */
378 +static struct list_head ring_table;
380 +/* List of all clusters */
381 +static struct ring_cluster *ring_cluster_list;
383 +static rwlock_t ring_mgmt_lock = RW_LOCK_UNLOCKED;
385 +/* ********************************** */
388 +static struct proto_ops ring_ops;
390 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
391 +static struct proto ring_proto;
394 +static int skb_ring_handler(struct sk_buff *skb, u_char recv_packet,
396 +static int buffer_ring_handler(struct net_device *dev, char *data, int len);
397 +static int remove_from_cluster(struct sock *sock, struct ring_opt *pfr);
401 +/* ********************************** */
404 +static u_int bucket_len = 128, num_slots = 4096, sample_rate = 1,
405 + transparent_mode = 1, enable_tx_capture = 0;
407 +MODULE_PARM(bucket_len, "i");
408 +MODULE_PARM_DESC(bucket_len, "Number of ring buckets");
409 +MODULE_PARM(num_slots, "i");
410 +MODULE_PARM_DESC(num_slots, "Number of ring slots");
411 +MODULE_PARM(sample_rate, "i");
412 +MODULE_PARM_DESC(sample_rate, "Ring packet sample rate");
413 +MODULE_PARM(transparent_mode, "i");
414 +MODULE_PARM_DESC(transparent_mode,
415 + "Set to 1 to set transparent mode "
416 + "(slower but backwards compatible)");
417 +MODULE_PARM(enable_tx_capture, "i");
418 +MODULE_PARM_DESC(enable_tx_capture, "Set to 1 to capture outgoing packets");
420 +/* ********************************** */
422 +#define MIN_QUEUED_PKTS 64
423 +#define MAX_QUEUE_LOOPS 64
426 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
427 +#define ring_sk_datatype(__sk) ((struct ring_opt *)__sk)
428 +#define ring_sk(__sk) ((__sk)->sk_protinfo)
430 +#define ring_sk_datatype(a) (a)
431 +#define ring_sk(__sk) ((__sk)->protinfo.pf_ring)
434 +#define _rdtsc() ({ uint64_t x; asm volatile("rdtsc" : "=A" (x)); x; })
437 + int dev_queue_xmit(struct sk_buff *skb)
439 + struct net_device *dev_get_by_name(const char *name)
442 +/* ********************************** */
444 +static void ring_sock_destruct(struct sock *sk) {
446 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
447 + skb_queue_purge(&sk->sk_receive_queue);
449 + if (!sock_flag(sk, SOCK_DEAD)) {
450 +#if defined(RING_DEBUG)
451 + printk("Attempt to release alive ring socket: %p\n", sk);
456 + BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
457 + BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
460 + BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
461 + BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
464 +#if defined(RING_DEBUG)
465 + printk("Attempt to release alive ring socket: %p\n", sk);
471 + kfree(ring_sk(sk));
473 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
478 +/* ********************************** */
482 + * store the sk in a new element and add it
483 + * to the head of the list.
485 +static inline void ring_insert(struct sock *sk) {
486 + struct ring_element *next;
488 +#if defined(RING_DEBUG)
489 + printk("RING: ring_insert()\n");
492 + next = kmalloc(sizeof(struct ring_element), GFP_ATOMIC);
495 + write_lock_irq(&ring_mgmt_lock);
496 + list_add(&next->list, &ring_table);
497 + write_unlock_irq(&ring_mgmt_lock);
499 + if (net_ratelimit())
500 + printk("RING: could not kmalloc slot!!\n");
504 +/* ********************************** */
508 + * For each of the elements in the list:
509 + * - check if this is the element we want to delete
510 + * - if it is, remove it from the list, and free it.
512 + * stop when we find the one we're looking for (break),
513 + * or when we reach the end of the list.
515 +static inline void ring_remove(struct sock *sk) {
516 + struct list_head *ptr;
517 + struct ring_element *entry;
520 + for(ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
521 + entry = list_entry(ptr, struct ring_element, list);
523 + if(entry->sk == sk) {
524 + write_lock_irq(&ring_mgmt_lock);
527 + write_unlock_irq(&ring_mgmt_lock);
534 +/* ********************************** */
536 +static u_int32_t num_queued_pkts(struct ring_opt *pfr) {
538 + if(pfr->ring_slots != NULL) {
540 + u_int32_t tot_insert = pfr->slots_info->insert_idx,
541 +#if defined(RING_DEBUG)
542 + tot_read = pfr->slots_info->tot_read, tot_pkts;
544 + tot_read = pfr->slots_info->tot_read;
547 + if(tot_insert >= tot_read) {
548 +#if defined(RING_DEBUG)
549 + tot_pkts = tot_insert-tot_read;
551 + return(tot_insert-tot_read);
553 +#if defined(RING_DEBUG)
554 + tot_pkts = ((u_int32_t)-1)+tot_insert-tot_read;
556 + return(((u_int32_t)-1)+tot_insert-tot_read);
559 +#if defined(RING_DEBUG)
560 + printk("-> num_queued_pkts=%d [tot_insert=%d][tot_read=%d]\n",
561 + tot_pkts, tot_insert, tot_read);
568 +/* ********************************** */
570 +static inline FlowSlot* get_insert_slot(struct ring_opt *pfr) {
571 +#if defined(RING_DEBUG)
572 + printk("get_insert_slot(%d)\n", pfr->slots_info->insert_idx);
575 + if(pfr->ring_slots != NULL) {
576 + FlowSlot *slot = (FlowSlot*)&(pfr->ring_slots[pfr->slots_info->insert_idx
577 + *pfr->slots_info->slot_len]);
583 +/* ********************************** */
585 +static inline FlowSlot* get_remove_slot(struct ring_opt *pfr) {
586 +#if defined(RING_DEBUG)
587 + printk("get_remove_slot(%d)\n", pfr->slots_info->remove_idx);
590 + if(pfr->ring_slots != NULL)
591 + return((FlowSlot*)&(pfr->ring_slots[pfr->slots_info->remove_idx*
592 + pfr->slots_info->slot_len]));
597 +/* ********************************** */
599 +static void add_skb_to_ring(struct sk_buff *skb,
600 + struct ring_opt *pfr,
601 + u_char recv_packet,
602 + u_char real_skb /* 1=skb 0=faked skb */) {
607 + /* Hack for identifying a packet received by the e1000 */
609 + displ = SKB_DISPLACEMENT;
611 + displ = 0; /* Received by the e1000 wrapper */
615 + write_lock(&pfr->ring_index_lock);
616 + pfr->slots_info->tot_pkts++;
617 + write_unlock(&pfr->ring_index_lock);
619 + /* BPF Filtering (from af_packet.c) */
620 + if(pfr->bpfFilter != NULL) {
621 + unsigned res = 1, len;
623 + len = skb->len-skb->data_len;
625 + write_lock(&pfr->ring_index_lock);
626 + skb->data -= displ;
627 + res = sk_run_filter(skb, pfr->bpfFilter->insns, pfr->bpfFilter->len);
628 + skb->data += displ;
629 + write_unlock(&pfr->ring_index_lock);
632 + /* Filter failed */
634 +#if defined(RING_DEBUG)
635 + printk("add_skb_to_ring(skb): Filter failed [len=%d][tot=%llu]"
636 + "[insertIdx=%d][pkt_type=%d][cloned=%d]\n",
637 + (int)skb->len, pfr->slots_info->tot_pkts,
638 + pfr->slots_info->insert_idx,
639 + skb->pkt_type, skb->cloned);
646 + /* ************************** */
648 + if(pfr->sample_rate > 1) {
649 + if(pfr->pktToSample == 0) {
650 + write_lock(&pfr->ring_index_lock);
651 + pfr->pktToSample = pfr->sample_rate;
652 + write_unlock(&pfr->ring_index_lock);
654 + write_lock(&pfr->ring_index_lock);
655 + pfr->pktToSample--;
656 + write_unlock(&pfr->ring_index_lock);
658 +#if defined(RING_DEBUG)
659 + printk("add_skb_to_ring(skb): sampled packet [len=%d]"
660 + "[tot=%llu][insertIdx=%d][pkt_type=%d][cloned=%d]\n",
661 + (int)skb->len, pfr->slots_info->tot_pkts,
662 + pfr->slots_info->insert_idx,
663 + skb->pkt_type, skb->cloned);
669 + /* ************************************* */
671 + if((pfr->reflector_dev != NULL)
672 + && (!netif_queue_stopped(pfr->reflector_dev))) {
673 + int cpu = smp_processor_id();
675 + /* increase reference counter so that this skb is not freed */
676 + atomic_inc(&skb->users);
678 + skb->data -= displ;
681 + if (pfr->reflector_dev->xmit_lock_owner != cpu) {
682 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
683 + pfr->reflector_dev->xmit_lock_owner = cpu;
684 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
686 + if (pfr->reflector_dev->hard_start_xmit(skb,
687 + pfr->reflector_dev) == 0) {
688 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
689 + pfr->reflector_dev->xmit_lock_owner = -1;
690 + skb->data += displ;
691 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
692 +#if defined(RING_DEBUG)
693 + printk("++ hard_start_xmit succeeded\n");
698 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
699 + pfr->reflector_dev->xmit_lock_owner = -1;
700 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
703 +#if defined(RING_DEBUG)
704 + printk("++ hard_start_xmit failed\n");
706 + skb->data += displ;
707 + return; /* -ENETDOWN */
710 + /* ************************************* */
712 +#if defined(RING_DEBUG)
713 + printk("add_skb_to_ring(skb) [len=%d][tot=%llu][insertIdx=%d]"
714 + "[pkt_type=%d][cloned=%d]\n",
715 + (int)skb->len, pfr->slots_info->tot_pkts,
716 + pfr->slots_info->insert_idx,
717 + skb->pkt_type, skb->cloned);
720 + idx = pfr->slots_info->insert_idx;
721 + theSlot = get_insert_slot(pfr);
723 + if((theSlot != NULL) && (theSlot->slot_state == 0)) {
724 + struct pcap_pkthdr *hdr;
730 + if(idx == pfr->slots_info->tot_slots) {
731 + write_lock(&pfr->ring_index_lock);
732 + pfr->slots_info->insert_idx = 0;
733 + write_unlock(&pfr->ring_index_lock);
735 + write_lock(&pfr->ring_index_lock);
736 + pfr->slots_info->insert_idx = idx;
737 + write_unlock(&pfr->ring_index_lock);
740 + bucket = &theSlot->bucket;
741 + hdr = (struct pcap_pkthdr*)bucket;
743 + /* BD - API changed for time keeping */
744 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14))
745 + if(skb->stamp.tv_sec == 0) do_gettimeofday(&skb->stamp);
747 + hdr->ts.tv_sec = skb->stamp.tv_sec, hdr->ts.tv_usec = skb->stamp.tv_usec;
749 + if(skb->tstamp.off_sec == 0) __net_timestamp(skb);
751 + hdr->ts.tv_sec = skb->tstamp.off_sec, hdr->ts.tv_usec = skb->tstamp.off_usec;
754 + hdr->caplen = skb->len+displ;
756 + if(hdr->caplen > pfr->slots_info->data_len)
757 + hdr->caplen = pfr->slots_info->data_len;
759 + hdr->len = skb->len+displ;
760 + memcpy(&bucket[sizeof(struct pcap_pkthdr)],
761 + skb->data-displ, hdr->caplen);
763 +#if defined(RING_DEBUG)
765 + static unsigned int lastLoss = 0;
767 + if(pfr->slots_info->tot_lost
768 + && (lastLoss != pfr->slots_info->tot_lost)) {
769 + printk("add_skb_to_ring(%d): [data_len=%d]"
770 + "[hdr.caplen=%d][skb->len=%d]"
771 + "[pcap_pkthdr=%d][removeIdx=%d]"
772 + "[loss=%lu][page=%u][slot=%u]\n",
773 + idx-1, pfr->slots_info->data_len, hdr->caplen, skb->len,
774 + sizeof(struct pcap_pkthdr),
775 + pfr->slots_info->remove_idx,
776 + (long unsigned int)pfr->slots_info->tot_lost,
777 + pfr->insert_page_id, pfr->insert_slot_id);
779 + lastLoss = pfr->slots_info->tot_lost;
784 + write_lock(&pfr->ring_index_lock);
785 + pfr->slots_info->tot_insert++;
786 + theSlot->slot_state = 1;
787 + write_unlock(&pfr->ring_index_lock);
789 + write_lock(&pfr->ring_index_lock);
790 + pfr->slots_info->tot_lost++;
791 + write_unlock(&pfr->ring_index_lock);
793 +#if defined(RING_DEBUG)
794 + printk("add_skb_to_ring(skb): packet lost [loss=%lu]"
795 + "[removeIdx=%u][insertIdx=%u]\n",
796 + (long unsigned int)pfr->slots_info->tot_lost,
797 + pfr->slots_info->remove_idx, pfr->slots_info->insert_idx);
801 + /* wakeup in case of poll() */
802 + if(waitqueue_active(&pfr->ring_slots_waitqueue))
803 + wake_up_interruptible(&pfr->ring_slots_waitqueue);
806 +/* ********************************** */
808 +static u_int hash_skb(struct ring_cluster *cluster_ptr,
809 + struct sk_buff *skb, u_char recv_packet) {
814 + if(cluster_ptr->hashing_mode == cluster_round_robin) {
815 + idx = cluster_ptr->hashing_id++;
817 + /* Per-flow clustering */
818 + if(skb->len > sizeof(struct iphdr)+sizeof(struct tcphdr)) {
822 + displ = SKB_DISPLACEMENT;
827 + Always points to to the IP part of the packet
830 + ip = (struct iphdr*)(skb->data+displ);
832 + idx = ip->saddr+ip->daddr+ip->protocol;
834 + if(ip->protocol == IPPROTO_TCP) {
835 + struct tcphdr *tcp = (struct tcphdr*)(skb->data+displ
836 + +sizeof(struct iphdr));
837 + idx += tcp->source+tcp->dest;
838 + } else if(ip->protocol == IPPROTO_UDP) {
839 + struct udphdr *udp = (struct udphdr*)(skb->data+displ
840 + +sizeof(struct iphdr));
841 + idx += udp->source+udp->dest;
847 + return(idx % cluster_ptr->num_cluster_elements);
850 +/* ********************************** */
852 +static int skb_ring_handler(struct sk_buff *skb,
853 + u_char recv_packet,
854 + u_char real_skb /* 1=skb 0=faked skb */) {
855 + struct sock *skElement;
857 + struct list_head *ptr;
858 + struct ring_cluster *cluster_ptr;
861 + uint64_t rdt = _rdtsc(), rdt1, rdt2;
864 + if((!skb) /* Invalid skb */
865 + || ((!enable_tx_capture) && (!recv_packet))) {
867 + An outgoing packet is about to be sent out
868 + but we decided not to handle transmitted
874 +#if defined(RING_DEBUG)
876 + printk("skb_ring_handler() [len=%d][dev=%s]\n", skb->len,
877 + skb->dev->name == NULL ? "<NULL>" : skb->dev->name);
885 + /* [1] Check unclustered sockets */
886 + for (ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
887 + struct ring_opt *pfr;
888 + struct ring_element *entry;
890 + entry = list_entry(ptr, struct ring_element, list);
892 + read_lock(&ring_mgmt_lock);
893 + skElement = entry->sk;
894 + pfr = ring_sk(skElement);
895 + read_unlock(&ring_mgmt_lock);
898 + && (pfr->cluster_id == 0 /* No cluster */)
899 + && (pfr->ring_slots != NULL)
900 + && (pfr->ring_netdev == skb->dev)) {
901 + /* We've found the ring where the packet can be stored */
902 + read_lock(&ring_mgmt_lock);
903 + add_skb_to_ring(skb, pfr, recv_packet, real_skb);
904 + read_unlock(&ring_mgmt_lock);
906 + rc = 1; /* Ring found: we've done our job */
910 + /* [2] Check socket clusters */
911 + cluster_ptr = ring_cluster_list;
913 + while(cluster_ptr != NULL) {
914 + struct ring_opt *pfr;
916 + if(cluster_ptr->num_cluster_elements > 0) {
917 + u_int skb_hash = hash_skb(cluster_ptr, skb, recv_packet);
919 + read_lock(&ring_mgmt_lock);
920 + skElement = cluster_ptr->sk[skb_hash];
921 + read_unlock(&ring_mgmt_lock);
923 + if(skElement != NULL) {
924 + pfr = ring_sk(skElement);
927 + && (pfr->ring_slots != NULL)
928 + && (pfr->ring_netdev == skb->dev)) {
929 + /* We've found the ring where the packet can be stored */
930 + read_lock(&ring_mgmt_lock);
931 + add_skb_to_ring(skb, pfr, recv_packet, real_skb);
932 + read_unlock(&ring_mgmt_lock);
934 + rc = 1; /* Ring found: we've done our job */
939 + cluster_ptr = cluster_ptr->next;
943 + rdt1 = _rdtsc()-rdt1;
950 + if(transparent_mode) rc = 0;
952 + if((rc != 0) && real_skb)
953 + dev_kfree_skb(skb); /* Free the skb */
956 + rdt2 = _rdtsc()-rdt2;
957 + rdt = _rdtsc()-rdt;
959 +#if defined(RING_DEBUG)
960 + printk("# cycles: %d [lock costed %d %d%%][free costed %d %d%%]\n",
961 + (int)rdt, rdt-rdt1,
962 + (int)((float)((rdt-rdt1)*100)/(float)rdt),
964 + (int)((float)(rdt2*100)/(float)rdt));
968 + return(rc); /* 0 = packet not handled */
971 +/* ********************************** */
975 +static int buffer_ring_handler(struct net_device *dev,
976 + char *data, int len) {
978 +#if defined(RING_DEBUG)
979 + printk("buffer_ring_handler: [dev=%s][len=%d]\n",
980 + dev->name == NULL ? "<NULL>" : dev->name, len);
983 + /* BD - API changed for time keeping */
984 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14))
985 + skb.dev = dev, skb.len = len, skb.data = data,
986 + skb.data_len = len, skb.stamp.tv_sec = 0; /* Calculate the time */
988 + skb.dev = dev, skb.len = len, skb.data = data,
989 + skb.data_len = len, skb.tstamp.off_sec = 0; /* Calculate the time */
992 + skb_ring_handler(&skb, 1, 0 /* fake skb */);
997 +/* ********************************** */
999 +static int ring_create(struct socket *sock, int protocol) {
1001 + struct ring_opt *pfr;
1004 +#if defined(RING_DEBUG)
1005 + printk("RING: ring_create()\n");
1008 + /* Are you root, superuser or so ? */
1009 + if(!capable(CAP_NET_ADMIN))
1012 + if(sock->type != SOCK_RAW)
1013 + return -ESOCKTNOSUPPORT;
1015 + if(protocol != htons(ETH_P_ALL))
1016 + return -EPROTONOSUPPORT;
1018 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
1019 + MOD_INC_USE_COUNT;
1024 + // BD: -- broke this out to keep it more simple and clear as to what the
1026 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1027 +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11))
1028 + sk = sk_alloc(PF_RING, GFP_KERNEL, 1, NULL);
1030 + // BD: API changed in 2.6.12, ref:
1031 + // http://svn.clkao.org/svnweb/linux/revision/?rev=28201
1032 + sk = sk_alloc(PF_RING, GFP_ATOMIC, &ring_proto, 1);
1036 + sk = sk_alloc(PF_RING, GFP_KERNEL, 1);
1042 + sock->ops = &ring_ops;
1043 + sock_init_data(sock, sk);
1044 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1045 +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11))
1046 + sk_set_owner(sk, THIS_MODULE);
1051 + ring_sk(sk) = ring_sk_datatype(kmalloc(sizeof(*pfr), GFP_KERNEL));
1053 + if (!(pfr = ring_sk(sk))) {
1057 + memset(pfr, 0, sizeof(*pfr));
1058 + init_waitqueue_head(&pfr->ring_slots_waitqueue);
1059 + pfr->ring_index_lock = RW_LOCK_UNLOCKED;
1060 + atomic_set(&pfr->num_ring_slots_waiters, 0);
1061 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1062 + sk->sk_family = PF_RING;
1063 + sk->sk_destruct = ring_sock_destruct;
1065 + sk->family = PF_RING;
1066 + sk->destruct = ring_sock_destruct;
1067 + sk->num = protocol;
1072 +#if defined(RING_DEBUG)
1073 + printk("RING: ring_create() - created\n");
1078 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
1079 + MOD_DEC_USE_COUNT;
1084 +/* *********************************************** */
1086 +static int ring_release(struct socket *sock)
1088 + struct sock *sk = sock->sk;
1089 + struct ring_opt *pfr = ring_sk(sk);
1094 +#if defined(RING_DEBUG)
1095 + printk("RING: called ring_release\n");
1098 +#if defined(RING_DEBUG)
1099 + printk("RING: ring_release entered\n");
1107 + /* Free the ring buffer */
1108 + if(pfr->ring_memory) {
1109 + struct page *page, *page_end;
1111 + page_end = virt_to_page(pfr->ring_memory + (PAGE_SIZE << pfr->order) - 1);
1112 + for(page = virt_to_page(pfr->ring_memory); page <= page_end; page++)
1113 + ClearPageReserved(page);
1115 + free_pages(pfr->ring_memory, pfr->order);
1119 + ring_sk(sk) = NULL;
1121 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1122 + skb_queue_purge(&sk->sk_write_queue);
1126 +#if defined(RING_DEBUG)
1127 + printk("RING: ring_release leaving\n");
1133 +/* ********************************** */
1135 + * We create a ring for this socket and bind it to the specified device
1137 +static int packet_ring_bind(struct sock *sk, struct net_device *dev)
1139 + u_int the_slot_len;
1140 + u_int32_t tot_mem;
1141 + struct ring_opt *pfr = ring_sk(sk);
1142 + struct page *page, *page_end;
1144 + if(!dev) return(-1);
1146 +#if defined(RING_DEBUG)
1147 + printk("RING: packet_ring_bind(%s) called\n", dev->name);
1150 + /* **********************************************
1152 + *************************************
1156 + ************************************* <-+
1158 + ************************************* |
1160 + ************************************* +- num_slots
1162 + ************************************* |
1164 + ************************************* <-+
1166 + ********************************************** */
1168 + the_slot_len = sizeof(u_char) /* flowSlot.slot_state */
1172 + + sizeof(struct pcap_pkthdr)
1173 + + bucket_len /* flowSlot.bucket */;
1175 + tot_mem = sizeof(FlowSlotInfo) + num_slots*the_slot_len;
1178 + Calculate the value of the order parameter used later.
1179 + See http://www.linuxjournal.com/article.php?sid=1133
1181 + for(pfr->order = 0;(PAGE_SIZE << pfr->order) < tot_mem; pfr->order++) ;
1184 + We now try to allocate the memory as required. If we fail
1185 + we try to allocate a smaller amount or memory (hence a
1188 + while((pfr->ring_memory = __get_free_pages(GFP_ATOMIC, pfr->order)) == 0)
1189 + if(pfr->order-- == 0)
1192 + if(pfr->order == 0) {
1193 + printk("RING: ERROR not enough memory for ring\n");
1196 + printk("RING: succesfully allocated %lu KB [tot_mem=%d][order=%ld]\n",
1197 + PAGE_SIZE >> (10 - pfr->order), tot_mem, pfr->order);
1200 + tot_mem = PAGE_SIZE << pfr->order;
1201 + memset((char*)pfr->ring_memory, 0, tot_mem);
1203 + /* Now we need to reserve the pages */
1204 + page_end = virt_to_page(pfr->ring_memory + (PAGE_SIZE << pfr->order) - 1);
1205 + for(page = virt_to_page(pfr->ring_memory); page <= page_end; page++)
1206 + SetPageReserved(page);
1208 + pfr->slots_info = (FlowSlotInfo*)pfr->ring_memory;
1209 + pfr->ring_slots = (char*)(pfr->ring_memory+sizeof(FlowSlotInfo));
1211 + pfr->slots_info->version = RING_FLOWSLOT_VERSION;
1212 + pfr->slots_info->slot_len = the_slot_len;
1213 + pfr->slots_info->data_len = bucket_len;
1214 + pfr->slots_info->tot_slots = (tot_mem-sizeof(FlowSlotInfo))/the_slot_len;
1215 + pfr->slots_info->tot_mem = tot_mem;
1216 + pfr->slots_info->sample_rate = sample_rate;
1218 + printk("RING: allocated %d slots [slot_len=%d][tot_mem=%u]\n",
1219 + pfr->slots_info->tot_slots, pfr->slots_info->slot_len,
1220 + pfr->slots_info->tot_mem);
1226 + for(i=0; i<pfr->slots_info->tot_slots; i++) {
1227 + unsigned long idx = i*pfr->slots_info->slot_len;
1228 + FlowSlot *slot = (FlowSlot*)&pfr->ring_slots[idx];
1229 + slot->magic = RING_MAGIC_VALUE; slot->slot_state = 0;
1234 + pfr->insert_page_id = 1, pfr->insert_slot_id = 0;
1238 + Leave this statement here as last one. In fact when
1239 + the ring_netdev != NULL the socket is ready to be used.
1241 + pfr->ring_netdev = dev;
1246 +/* ************************************* */
1248 +/* Bind to a device */
1249 +static int ring_bind(struct socket *sock,
1250 + struct sockaddr *sa, int addr_len)
1252 + struct sock *sk=sock->sk;
1253 + struct net_device *dev = NULL;
1255 +#if defined(RING_DEBUG)
1256 + printk("RING: ring_bind() called\n");
1262 + if (addr_len != sizeof(struct sockaddr))
1264 + if (sa->sa_family != PF_RING)
1267 + /* Safety check: add trailing zero if missing */
1268 + sa->sa_data[sizeof(sa->sa_data)-1] = '\0';
1270 +#if defined(RING_DEBUG)
1271 + printk("RING: searching device %s\n", sa->sa_data);
1274 + if((dev = __dev_get_by_name(sa->sa_data)) == NULL) {
1275 +#if defined(RING_DEBUG)
1276 + printk("RING: search failed\n");
1280 + return(packet_ring_bind(sk, dev));
1283 +/* ************************************* */
1285 +static int ring_mmap(struct file *file,
1286 + struct socket *sock,
1287 + struct vm_area_struct *vma)
1289 + struct sock *sk = sock->sk;
1290 + struct ring_opt *pfr = ring_sk(sk);
1291 + unsigned long size, start;
1295 +#if defined(RING_DEBUG)
1296 + printk("RING: ring_mmap() called\n");
1299 + if(pfr->ring_memory == 0) {
1300 +#if defined(RING_DEBUG)
1301 + printk("RING: ring_mmap() failed: mapping area to an unbound socket\n");
1306 + size = (unsigned long)(vma->vm_end-vma->vm_start);
1308 + if(size % PAGE_SIZE) {
1309 +#if defined(RING_DEBUG)
1310 + printk("RING: ring_mmap() failed: len is not multiple of PAGE_SIZE\n");
1315 + /* if userspace tries to mmap beyond end of our buffer, fail */
1316 + if(size > pfr->slots_info->tot_mem) {
1317 +#if defined(RING_DEBUG)
1318 + printk("proc_mmap() failed: area too large [%ld > %d]\n", size, pfr->slots_info->tot_mem);
1323 + pagesToMap = size/PAGE_SIZE;
1325 +#if defined(RING_DEBUG)
1326 + printk("RING: ring_mmap() called. %d pages to map\n", pagesToMap);
1329 +#if defined(RING_DEBUG)
1330 + printk("RING: mmap [slot_len=%d][tot_slots=%d] for ring on device %s\n",
1331 + pfr->slots_info->slot_len, pfr->slots_info->tot_slots,
1332 + pfr->ring_netdev->name);
1335 + /* we do not want to have this area swapped out, lock it */
1336 + vma->vm_flags |= VM_LOCKED;
1337 + start = vma->vm_start;
1339 + /* Ring slots start from page 1 (page 0 is reserved for FlowSlotInfo) */
1340 + ptr = (char*)(start+PAGE_SIZE);
1342 + if(remap_page_range(
1343 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1347 + __pa(pfr->ring_memory),
1348 + PAGE_SIZE*pagesToMap, vma->vm_page_prot)) {
1349 +#if defined(RING_DEBUG)
1350 + printk("remap_page_range() failed\n");
1355 +#if defined(RING_DEBUG)
1356 + printk("proc_mmap(pagesToMap=%d): success.\n", pagesToMap);
1362 +/* ************************************* */
1364 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1365 +static int ring_recvmsg(struct kiocb *iocb, struct socket *sock,
1366 + struct msghdr *msg, size_t len, int flags)
1368 + static int ring_recvmsg(struct socket *sock, struct msghdr *msg, int len,
1369 + int flags, struct scm_cookie *scm)
1373 + struct ring_opt *pfr = ring_sk(sock->sk);
1374 + u_int32_t queued_pkts, num_loops = 0;
1376 +#if defined(RING_DEBUG)
1377 + printk("ring_recvmsg called\n");
1380 + slot = get_remove_slot(pfr);
1382 + while((queued_pkts = num_queued_pkts(pfr)) < MIN_QUEUED_PKTS) {
1383 + wait_event_interruptible(pfr->ring_slots_waitqueue, 1);
1385 +#if defined(RING_DEBUG)
1386 + printk("-> ring_recvmsg returning %d [queued_pkts=%d][num_loops=%d]\n",
1387 + slot->slot_state, queued_pkts, num_loops);
1390 + if(queued_pkts > 0) {
1391 + if(num_loops++ > MAX_QUEUE_LOOPS)
1396 +#if defined(RING_DEBUG)
1398 + printk("ring_recvmsg is returning [queued_pkts=%d][num_loops=%d]\n",
1399 + queued_pkts, num_loops);
1402 + return(queued_pkts);
1405 +/* ************************************* */
1407 +unsigned int ring_poll(struct file * file,
1408 + struct socket *sock, poll_table *wait)
1411 + struct ring_opt *pfr = ring_sk(sock->sk);
1413 +#if defined(RING_DEBUG)
1414 + printk("poll called\n");
1417 + slot = get_remove_slot(pfr);
1419 + if((slot != NULL) && (slot->slot_state == 0))
1420 + poll_wait(file, &pfr->ring_slots_waitqueue, wait);
1422 +#if defined(RING_DEBUG)
1423 + printk("poll returning %d\n", slot->slot_state);
1426 + if((slot != NULL) && (slot->slot_state == 1))
1427 + return(POLLIN | POLLRDNORM);
1432 +/* ************************************* */
1434 +int add_to_cluster_list(struct ring_cluster *el,
1435 + struct sock *sock) {
1437 + if(el->num_cluster_elements == CLUSTER_LEN)
1438 + return(-1); /* Cluster full */
1440 + ring_sk_datatype(ring_sk(sock))->cluster_id = el->cluster_id;
1441 + el->sk[el->num_cluster_elements] = sock;
1442 + el->num_cluster_elements++;
1446 +/* ************************************* */
1448 +int remove_from_cluster_list(struct ring_cluster *el,
1449 + struct sock *sock) {
1452 + for(i=0; i<CLUSTER_LEN; i++)
1453 + if(el->sk[i] == sock) {
1454 + el->num_cluster_elements--;
1456 + if(el->num_cluster_elements > 0) {
1457 + /* The cluster contains other elements */
1458 + for(j=i; j<CLUSTER_LEN-1; j++)
1459 + el->sk[j] = el->sk[j+1];
1461 + el->sk[CLUSTER_LEN-1] = NULL;
1463 + /* Empty cluster */
1464 + memset(el->sk, 0, sizeof(el->sk));
1470 + return(-1); /* Not found */
1473 +/* ************************************* */
1475 +static int remove_from_cluster(struct sock *sock,
1476 + struct ring_opt *pfr)
1478 + struct ring_cluster *el;
1480 +#if defined(RING_DEBUG)
1481 + printk("--> remove_from_cluster(%d)\n", pfr->cluster_id);
1484 + if(pfr->cluster_id == 0 /* 0 = No Cluster */)
1485 + return(0); /* Noting to do */
1487 + el = ring_cluster_list;
1489 + while(el != NULL) {
1490 + if(el->cluster_id == pfr->cluster_id) {
1491 + return(remove_from_cluster_list(el, sock));
1496 + return(-EINVAL); /* Not found */
1499 +/* ************************************* */
1501 +static int add_to_cluster(struct sock *sock,
1502 + struct ring_opt *pfr,
1503 + u_short cluster_id)
1505 + struct ring_cluster *el;
1508 + printk("--> add_to_cluster(%d)\n", cluster_id);
1511 + if(cluster_id == 0 /* 0 = No Cluster */) return(-EINVAL);
1513 + if(pfr->cluster_id != 0)
1514 + remove_from_cluster(sock, pfr);
1516 + el = ring_cluster_list;
1518 + while(el != NULL) {
1519 + if(el->cluster_id == cluster_id) {
1520 + return(add_to_cluster_list(el, sock));
1525 + /* There's no existing cluster. We need to create one */
1526 + if((el = kmalloc(sizeof(struct ring_cluster), GFP_KERNEL)) == NULL)
1529 + el->cluster_id = cluster_id;
1530 + el->num_cluster_elements = 1;
1531 + el->hashing_mode = cluster_per_flow; /* Default */
1532 + el->hashing_id = 0;
1534 + memset(el->sk, 0, sizeof(el->sk));
1536 + el->next = ring_cluster_list;
1537 + ring_cluster_list = el;
1538 + pfr->cluster_id = cluster_id;
1540 + return(0); /* 0 = OK */
1543 +/* ************************************* */
1545 +/* Code taken/inspired from core/sock.c */
1546 +static int ring_setsockopt(struct socket *sock,
1547 + int level, int optname,
1548 + char *optval, int optlen)
1550 + struct ring_opt *pfr = ring_sk(sock->sk);
1551 + int val, found, ret = 0;
1555 + if((optlen<sizeof(int)) || (pfr == NULL))
1558 + if (get_user(val, (int *)optval))
1565 + case SO_ATTACH_FILTER:
1567 + if (optlen == sizeof(struct sock_fprog)) {
1568 + unsigned int fsize;
1569 + struct sock_fprog fprog;
1570 + struct sk_filter *filter;
1577 + Do not call copy_from_user within a held
1578 + splinlock (e.g. ring_mgmt_lock) as this caused
1579 + problems when certain debugging was enabled under
1580 + 2.6.5 -- including hard lockups of the machine.
1582 + if(copy_from_user(&fprog, optval, sizeof(fprog)))
1585 + fsize = sizeof(struct sock_filter) * fprog.len;
1586 + filter = kmalloc(fsize, GFP_KERNEL);
1588 + if(filter == NULL) {
1593 + if(copy_from_user(filter->insns, fprog.filter, fsize))
1596 + filter->len = fprog.len;
1598 + if(sk_chk_filter(filter->insns, filter->len) != 0) {
1599 + /* Bad filter specified */
1601 + pfr->bpfFilter = NULL;
1605 + /* get the lock, set the filter, release the lock */
1606 + write_lock(&ring_mgmt_lock);
1607 + pfr->bpfFilter = filter;
1608 + write_unlock(&ring_mgmt_lock);
1613 + case SO_DETACH_FILTER:
1614 + write_lock(&ring_mgmt_lock);
1616 + if(pfr->bpfFilter != NULL) {
1617 + kfree(pfr->bpfFilter);
1618 + pfr->bpfFilter = NULL;
1619 + write_unlock(&ring_mgmt_lock);
1625 + case SO_ADD_TO_CLUSTER:
1626 + if (optlen!=sizeof(val))
1629 + if (copy_from_user(&cluster_id, optval, sizeof(cluster_id)))
1632 + write_lock(&ring_mgmt_lock);
1633 + ret = add_to_cluster(sock->sk, pfr, cluster_id);
1634 + write_unlock(&ring_mgmt_lock);
1637 + case SO_REMOVE_FROM_CLUSTER:
1638 + write_lock(&ring_mgmt_lock);
1639 + ret = remove_from_cluster(sock->sk, pfr);
1640 + write_unlock(&ring_mgmt_lock);
1643 + case SO_SET_REFLECTOR:
1644 + if(optlen >= (sizeof(devName)-1))
1648 + if(copy_from_user(devName, optval, optlen))
1652 + devName[optlen] = '\0';
1654 +#if defined(RING_DEBUG)
1655 + printk("+++ SO_SET_REFLECTOR(%s)\n", devName);
1658 + write_lock(&ring_mgmt_lock);
1659 + pfr->reflector_dev = dev_get_by_name(devName);
1660 + write_unlock(&ring_mgmt_lock);
1662 +#if defined(RING_DEBUG)
1663 + if(pfr->reflector_dev != NULL)
1664 + printk("SO_SET_REFLECTOR(%s): succeded\n", devName);
1666 + printk("SO_SET_REFLECTOR(%s): device unknown\n", devName);
1678 + return(sock_setsockopt(sock, level, optname, optval, optlen));
1681 +/* ************************************* */
1683 +static int ring_ioctl(struct socket *sock,
1684 + unsigned int cmd, unsigned long arg)
1688 + case SIOCGIFFLAGS:
1689 + case SIOCSIFFLAGS:
1691 + case SIOCGIFMETRIC:
1692 + case SIOCSIFMETRIC:
1698 + case SIOCGIFHWADDR:
1699 + case SIOCSIFHWADDR:
1702 + case SIOCSIFSLAVE:
1703 + case SIOCGIFSLAVE:
1704 + case SIOCGIFINDEX:
1706 + case SIOCGIFCOUNT:
1707 + case SIOCSIFHWBROADCAST:
1708 + return(dev_ioctl(cmd,(void *) arg));
1711 + return -EOPNOTSUPP;
1717 +/* ************************************* */
1719 +static struct proto_ops ring_ops = {
1720 + .family = PF_RING,
1721 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1722 + .owner = THIS_MODULE,
1725 + /* Operations that make no sense on ring sockets. */
1726 + .connect = sock_no_connect,
1727 + .socketpair = sock_no_socketpair,
1728 + .accept = sock_no_accept,
1729 + .getname = sock_no_getname,
1730 + .listen = sock_no_listen,
1731 + .shutdown = sock_no_shutdown,
1732 + .sendpage = sock_no_sendpage,
1733 + .sendmsg = sock_no_sendmsg,
1734 + .getsockopt = sock_no_getsockopt,
1736 + /* Now the operations that really occur. */
1737 + .release = ring_release,
1738 + .bind = ring_bind,
1739 + .mmap = ring_mmap,
1740 + .poll = ring_poll,
1741 + .setsockopt = ring_setsockopt,
1742 + .ioctl = ring_ioctl,
1743 + .recvmsg = ring_recvmsg,
1746 +/* ************************************ */
1748 +static struct net_proto_family ring_family_ops = {
1749 + .family = PF_RING,
1750 + .create = ring_create,
1751 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1752 + .owner = THIS_MODULE,
1756 +// BD: API changed in 2.6.12, ref:
1757 +// http://svn.clkao.org/svnweb/linux/revision/?rev=28201
1758 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
1759 +static struct proto ring_proto = {
1760 + .name = "PF_RING",
1761 + .owner = THIS_MODULE,
1762 + .obj_size = sizeof(struct sock),
1766 +/* ************************************ */
1768 +static void __exit ring_exit(void)
1770 + struct list_head *ptr;
1771 + struct ring_element *entry;
1773 + for(ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
1774 + entry = list_entry(ptr, struct ring_element, list);
1778 + while(ring_cluster_list != NULL) {
1779 + struct ring_cluster *next = ring_cluster_list->next;
1780 + kfree(ring_cluster_list);
1781 + ring_cluster_list = next;
1784 + set_skb_ring_handler(NULL);
1785 + set_buffer_ring_handler(NULL);
1786 + sock_unregister(PF_RING);
1788 + printk("PF_RING shut down.\n");
1791 +/* ************************************ */
1793 +static int __init ring_init(void)
1795 + printk("Welcome to PF_RING %s\n(C) 2004-05 L.Deri <deri@ntop.org>\n",
1798 + INIT_LIST_HEAD(&ring_table);
1799 + ring_cluster_list = NULL;
1801 + sock_register(&ring_family_ops);
1803 + set_skb_ring_handler(skb_ring_handler);
1804 + set_buffer_ring_handler(buffer_ring_handler);
1806 + if(get_buffer_ring_handler() != buffer_ring_handler) {
1807 + printk("PF_RING: set_buffer_ring_handler FAILED\n");
1809 + set_skb_ring_handler(NULL);
1810 + set_buffer_ring_handler(NULL);
1811 + sock_unregister(PF_RING);
1814 + printk("PF_RING: bucket length %d bytes\n", bucket_len);
1815 + printk("PF_RING: ring slots %d\n", num_slots);
1816 + printk("PF_RING: sample rate %d [1=no sampling]\n", sample_rate);
1817 + printk("PF_RING: capture TX %s\n",
1818 + enable_tx_capture ? "Yes [RX+TX]" : "No [RX only]");
1819 + printk("PF_RING: transparent mode %s\n",
1820 + transparent_mode ? "Yes" : "No");
1822 + printk("PF_RING initialized correctly.\n");
1827 +module_init(ring_init);
1828 +module_exit(ring_exit);
1829 +MODULE_LICENSE("GPL");
1831 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1832 +MODULE_ALIAS_NETPROTO(PF_RING);