patch-2.4.26 linux-2.4.26/net/packet/af_packet.c
Next file: linux-2.4.26/net/sched/Config.in
Previous file: linux-2.4.26/net/netsyms.c
Back to the patch index
Back to the overall index
- Lines: 186
- Date:
2004-04-14 06:05:41.000000000 -0700
- Orig file:
linux-2.4.25/net/packet/af_packet.c
- Orig date:
2003-08-25 04:44:44.000000000 -0700
diff -urN linux-2.4.25/net/packet/af_packet.c linux-2.4.26/net/packet/af_packet.c
@@ -34,6 +34,8 @@
* Alexey Kuznetsov : Untied from IPv4 stack.
* Cyrus Durgin : Fixed kerneld for kmod.
* Michal Ostrowski : Module initialization cleanup.
+ * Ulises Alonso : Frame number limit removal and
+ * packet_set_ring memory leak.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -176,29 +178,46 @@
struct packet_opt
{
+ struct tpacket_stats stats;
+#ifdef CONFIG_PACKET_MMAP
+ unsigned long *pg_vec;
+ unsigned int head;
+ unsigned int frames_per_block;
+ unsigned int frame_size;
+ unsigned int frame_max;
+ int copy_thresh;
+#endif
struct packet_type prot_hook;
spinlock_t bind_lock;
char running; /* prot_hook is attached*/
int ifindex; /* bound device */
- struct tpacket_stats stats;
#ifdef CONFIG_PACKET_MULTICAST
struct packet_mclist *mclist;
#endif
#ifdef CONFIG_PACKET_MMAP
atomic_t mapped;
- unsigned long *pg_vec;
unsigned int pg_vec_order;
unsigned int pg_vec_pages;
unsigned int pg_vec_len;
-
- struct tpacket_hdr **iovec;
- unsigned int frame_size;
- unsigned int iovmax;
- unsigned int head;
- int copy_thresh;
#endif
};
+#ifdef CONFIG_PACKET_MMAP
+
+static inline unsigned long packet_lookup_frame(struct packet_opt *po, unsigned int position)
+{
+ unsigned int pg_vec_pos, frame_offset;
+ unsigned long frame;
+
+ pg_vec_pos = position / po->frames_per_block;
+ frame_offset = position % po->frames_per_block;
+
+ frame = (unsigned long) (po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size));
+
+ return frame;
+}
+#endif
+
void packet_sock_destruct(struct sock *sk)
{
BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
@@ -590,11 +609,11 @@
snaplen = skb->len-skb->data_len;
spin_lock(&sk->receive_queue.lock);
- h = po->iovec[po->head];
+ h = (struct tpacket_hdr *)packet_lookup_frame(po, po->head);
if (h->tp_status)
goto ring_is_full;
- po->head = po->head != po->iovmax ? po->head+1 : 0;
+ po->head = po->head != po->frame_max ? po->head+1 : 0;
po->stats.tp_packets++;
if (copy_skb) {
status |= TP_STATUS_COPY;
@@ -1555,10 +1574,13 @@
unsigned int mask = datagram_poll(file, sock, wait);
spin_lock_bh(&sk->receive_queue.lock);
- if (po->iovec) {
- unsigned last = po->head ? po->head-1 : po->iovmax;
+ if (po->pg_vec) {
+ unsigned last = po->head ? po->head-1 : po->frame_max;
+ struct tpacket_hdr *h;
+
+ h = (struct tpacket_hdr *)packet_lookup_frame(po, last);
- if (po->iovec[last]->tp_status)
+ if (h->tp_status)
mask |= POLLIN | POLLRDNORM;
}
spin_unlock_bh(&sk->receive_queue.lock);
@@ -1618,16 +1640,18 @@
static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
{
unsigned long *pg_vec = NULL;
- struct tpacket_hdr **io_vec = NULL;
struct packet_opt *po = sk->protinfo.af_packet;
int order = 0;
int err = 0;
if (req->tp_block_nr) {
int i, l;
- int frames_per_block;
/* Sanity tests and some calculations */
+
+ if (po->pg_vec)
+ return -EBUSY;
+
if ((int)req->tp_block_size <= 0)
return -EINVAL;
if (req->tp_block_size&(PAGE_SIZE-1))
@@ -1636,10 +1660,11 @@
return -EINVAL;
if (req->tp_frame_size&(TPACKET_ALIGNMENT-1))
return -EINVAL;
- frames_per_block = req->tp_block_size/req->tp_frame_size;
- if (frames_per_block <= 0)
+
+ po->frames_per_block = req->tp_block_size/req->tp_frame_size;
+ if (po->frames_per_block <= 0)
return -EINVAL;
- if (frames_per_block*req->tp_block_nr != req->tp_frame_nr)
+ if (po->frames_per_block*req->tp_block_nr != req->tp_frame_nr)
return -EINVAL;
/* OK! */
@@ -1666,20 +1691,16 @@
}
/* Page vector is allocated */
- /* Draw frames */
- io_vec = kmalloc(req->tp_frame_nr*sizeof(struct tpacket_hdr*), GFP_KERNEL);
- if (io_vec == NULL)
- goto out_free_pgvec;
- memset(io_vec, 0, req->tp_frame_nr*sizeof(struct tpacket_hdr*));
-
l = 0;
for (i=0; i<req->tp_block_nr; i++) {
unsigned long ptr = pg_vec[i];
+ struct tpacket_hdr *header;
int k;
- for (k=0; k<frames_per_block; k++, l++) {
- io_vec[l] = (struct tpacket_hdr*)ptr;
- io_vec[l]->tp_status = TP_STATUS_KERNEL;
+ for (k=0; k<po->frames_per_block; k++) {
+
+ header = (struct tpacket_hdr*)ptr;
+ header->tp_status = TP_STATUS_KERNEL;
ptr += req->tp_frame_size;
}
}
@@ -1704,8 +1725,7 @@
spin_lock_bh(&sk->receive_queue.lock);
pg_vec = XC(po->pg_vec, pg_vec);
- io_vec = XC(po->iovec, io_vec);
- po->iovmax = req->tp_frame_nr-1;
+ po->frame_max = req->tp_frame_nr-1;
po->head = 0;
po->frame_size = req->tp_frame_size;
spin_unlock_bh(&sk->receive_queue.lock);
@@ -1714,7 +1734,7 @@
req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
- po->prot_hook.func = po->iovec ? tpacket_rcv : packet_rcv;
+ po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
skb_queue_purge(&sk->receive_queue);
#undef XC
if (atomic_read(&po->mapped))
@@ -1728,9 +1748,6 @@
release_sock(sk);
- if (io_vec)
- kfree(io_vec);
-
out_free_pgvec:
if (pg_vec)
free_pg_vec(pg_vec, order, req->tp_block_nr);
FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)