remove zlib from image builder lists
[openwrt.git] / openwrt / target / linux / generic-2.4 / patches / 223-pf_ring.patch
1 diff --unified --recursive --new-file linux-2.4.30/include/linux/ring.h linux-2.4.30-1-686-smp-ring3/include/linux/ring.h
2 --- linux-2.4.30/include/linux/ring.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.4.30-1-686-smp-ring3/include/linux/ring.h 2005-10-22 23:08:27.388011250 +0200
4 @@ -0,0 +1,108 @@
5 +/*
6 + * Definitions for packet ring
7 + *
8 + * 2004 - Luca Deri <deri@ntop.org>
9 + */
10 +#ifndef __RING_H
11 +#define __RING_H
12 +
13 +
14 +#define INCLUDE_MAC_INFO
15 +
16 +#ifdef INCLUDE_MAC_INFO
17 +#define SKB_DISPLACEMENT 14 /* Include MAC address information */
18 +#else
19 +#define SKB_DISPLACEMENT 0 /* Do NOT include MAC address information */
20 +#endif
21 +
22 +#define RING_MAGIC
23 +#define RING_MAGIC_VALUE 0x88
24 +#define RING_FLOWSLOT_VERSION 5
25 +#define RING_VERSION "3.0"
26 +
27 +#define SO_ADD_TO_CLUSTER 99
28 +#define SO_REMOVE_FROM_CLUSTER 100
29 +#define SO_SET_REFLECTOR 101
30 +
31 +/* *********************************** */
32 +
33 +#ifndef HAVE_PCAP
34 +struct pcap_pkthdr {
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) */
38 +};
39 +#endif
40 +
41 +/* *********************************** */
42 +
43 +enum cluster_type {
44 + cluster_per_flow = 0,
45 + cluster_round_robin
46 +};
47 +
48 +/* *********************************** */
49 +
50 +#define RING_MIN_SLOT_SIZE (60+sizeof(struct pcap_pkthdr))
51 +#define RING_MAX_SLOT_SIZE (1514+sizeof(struct pcap_pkthdr))
52 +
53 +/* *********************************** */
54 +
55 +typedef struct flowSlotInfo {
56 + u_int16_t version, sample_rate;
57 + u_int32_t tot_slots, slot_len, tot_mem;
58 +
59 + u_int64_t tot_pkts, tot_lost;
60 + u_int64_t tot_insert, tot_read;
61 + u_int16_t insert_idx;
62 + u_int16_t remove_idx;
63 +} FlowSlotInfo;
64 +
65 +/* *********************************** */
66 +
67 +typedef struct flowSlot {
68 +#ifdef RING_MAGIC
69 + u_char magic; /* It must alwasy be zero */
70 +#endif
71 + u_char slot_state; /* 0=empty, 1=full */
72 + u_char bucket; /* bucket[bucketLen] */
73 +} FlowSlot;
74 +
75 +/* *********************************** */
76 +
77 +#ifdef __KERNEL__
78 +
79 +FlowSlotInfo* getRingPtr(void);
80 +int allocateRing(char *deviceName, u_int numSlots,
81 + u_int bucketLen, u_int sampleRate);
82 +unsigned int pollRing(struct file *fp, struct poll_table_struct * wait);
83 +void deallocateRing(void);
84 +
85 +/* ************************* */
86 +
87 +typedef int (*handle_ring_skb)(struct sk_buff *skb,
88 + u_char recv_packet, u_char real_skb);
89 +extern handle_ring_skb get_skb_ring_handler(void);
90 +extern void set_skb_ring_handler(handle_ring_skb the_handler);
91 +extern void do_skb_ring_handler(struct sk_buff *skb,
92 + u_char recv_packet, u_char real_skb);
93 +
94 +typedef int (*handle_ring_buffer)(struct net_device *dev,
95 + char *data, int len);
96 +extern handle_ring_buffer get_buffer_ring_handler(void);
97 +extern void set_buffer_ring_handler(handle_ring_buffer the_handler);
98 +extern int do_buffer_ring_handler(struct net_device *dev,
99 + char *data, int len);
100 +#endif /* __KERNEL__ */
101 +
102 +/* *********************************** */
103 +
104 +#define PF_RING 27 /* Packet Ring */
105 +#define SOCK_RING PF_RING
106 +
107 +/* ioctl() */
108 +#define SIORINGPOLL 0x8888
109 +
110 +/* *********************************** */
111 +
112 +#endif /* __RING_H */
113 diff --unified --recursive --new-file linux-2.4.30/include/net/sock.h linux-2.4.30-1-686-smp-ring3/include/net/sock.h
114 --- linux-2.4.30/include/net/sock.h 2004-11-17 12:54:22.000000000 +0100
115 +++ linux-2.4.30-1-686-smp-ring3/include/net/sock.h 2005-10-22 23:08:27.976048000 +0200
116 @@ -699,6 +699,9 @@
117 #if defined (CONFIG_PACKET) || defined(CONFIG_PACKET_MODULE)
118 struct packet_opt *af_packet;
119 #endif
120 +#if defined(CONFIG_RING) || defined(CONFIG_RING_MODULE)
121 + struct ring_opt *pf_ring;
122 +#endif
123 #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
124 x25_cb *x25;
125 #endif
126 diff --unified --recursive --new-file linux-2.4.30/include/net/sock.h.ORG linux-2.4.30-1-686-smp-ring3/include/net/sock.h.ORG
127 --- linux-2.4.30/include/net/sock.h.ORG 1970-01-01 01:00:00.000000000 +0100
128 +++ linux-2.4.30-1-686-smp-ring3/include/net/sock.h.ORG 2005-10-22 23:08:27.940045750 +0200
129 @@ -0,0 +1,1400 @@
130 +/*
131 + * INET An implementation of the TCP/IP protocol suite for the LINUX
132 + * operating system. INET is implemented using the BSD Socket
133 + * interface as the means of communication with the user level.
134 + *
135 + * Definitions for the AF_INET socket handler.
136 + *
137 + * Version: @(#)sock.h 1.0.4 05/13/93
138 + *
139 + * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
140 + * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
141 + * Corey Minyard <wf-rch!minyard@relay.EU.net>
142 + * Florian La Roche <flla@stud.uni-sb.de>
143 + *
144 + * Fixes:
145 + * Alan Cox : Volatiles in skbuff pointers. See
146 + * skbuff comments. May be overdone,
147 + * better to prove they can be removed
148 + * than the reverse.
149 + * Alan Cox : Added a zapped field for tcp to note
150 + * a socket is reset and must stay shut up
151 + * Alan Cox : New fields for options
152 + * Pauline Middelink : identd support
153 + * Alan Cox : Eliminate low level recv/recvfrom
154 + * David S. Miller : New socket lookup architecture.
155 + * Steve Whitehouse: Default routines for sock_ops
156 + *
157 + * This program is free software; you can redistribute it and/or
158 + * modify it under the terms of the GNU General Public License
159 + * as published by the Free Software Foundation; either version
160 + * 2 of the License, or (at your option) any later version.
161 + */
162 +#ifndef _SOCK_H
163 +#define _SOCK_H
164 +
165 +#include <linux/config.h>
166 +#include <linux/timer.h>
167 +#include <linux/cache.h>
168 +#include <linux/in.h> /* struct sockaddr_in */
169 +
170 +#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
171 +#include <linux/in6.h> /* struct sockaddr_in6 */
172 +#include <linux/ipv6.h> /* dest_cache, inet6_options */
173 +#include <linux/icmpv6.h>
174 +#include <net/if_inet6.h> /* struct ipv6_mc_socklist */
175 +#endif
176 +
177 +#if defined(CONFIG_INET) || defined (CONFIG_INET_MODULE)
178 +#include <linux/icmp.h>
179 +#endif
180 +#include <linux/tcp.h> /* struct tcphdr */
181 +#if defined(CONFIG_IP_SCTP) || defined (CONFIG_IP_SCTP_MODULE)
182 +#include <net/sctp/structs.h> /* struct sctp_opt */
183 +#endif
184 +
185 +#include <linux/netdevice.h>
186 +#include <linux/skbuff.h> /* struct sk_buff */
187 +#include <net/protocol.h> /* struct inet_protocol */
188 +#if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
189 +#include <net/x25.h>
190 +#endif
191 +#if defined(CONFIG_WAN_ROUTER) || defined(CONFIG_WAN_ROUTER_MODULE)
192 +#include <linux/if_wanpipe.h>
193 +#endif
194 +
195 +#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
196 +#include <net/ax25.h>
197 +#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
198 +#include <net/netrom.h>
199 +#endif
200 +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
201 +#include <net/rose.h>
202 +#endif
203 +#endif
204 +
205 +#if defined(CONFIG_PPPOE) || defined(CONFIG_PPPOE_MODULE)
206 +#include <linux/if_pppox.h>
207 +#include <linux/ppp_channel.h> /* struct ppp_channel */
208 +#endif
209 +
210 +#if defined(CONFIG_IPX) || defined(CONFIG_IPX_MODULE)
211 +#if defined(CONFIG_SPX) || defined(CONFIG_SPX_MODULE)
212 +#include <net/spx.h>
213 +#else
214 +#include <net/ipx.h>
215 +#endif /* CONFIG_SPX */
216 +#endif /* CONFIG_IPX */
217 +
218 +#if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
219 +#include <linux/atalk.h>
220 +#endif
221 +
222 +#if defined(CONFIG_DECNET) || defined(CONFIG_DECNET_MODULE)
223 +#include <net/dn.h>
224 +#endif
225 +
226 +#if defined(CONFIG_IRDA) || defined(CONFIG_IRDA_MODULE)
227 +#include <net/irda/irda.h>
228 +#endif
229 +
230 +#if defined(CONFIG_ATM) || defined(CONFIG_ATM_MODULE)
231 +struct atm_vcc;
232 +#endif
233 +
234 +#ifdef CONFIG_FILTER
235 +#include <linux/filter.h>
236 +#endif
237 +
238 +#include <asm/atomic.h>
239 +#include <net/dst.h>
240 +
241 +
242 +/* The AF_UNIX specific socket options */
243 +struct unix_opt {
244 + struct unix_address *addr;
245 + struct dentry * dentry;
246 + struct vfsmount * mnt;
247 + struct semaphore readsem;
248 + struct sock * other;
249 + struct sock ** list;
250 + struct sock * gc_tree;
251 + atomic_t inflight;
252 + rwlock_t lock;
253 + wait_queue_head_t peer_wait;
254 +};
255 +
256 +
257 +/* Once the IPX ncpd patches are in these are going into protinfo. */
258 +#if defined(CONFIG_IPX) || defined(CONFIG_IPX_MODULE)
259 +struct ipx_opt {
260 + ipx_address dest_addr;
261 + ipx_interface *intrfc;
262 + unsigned short port;
263 +#ifdef CONFIG_IPX_INTERN
264 + unsigned char node[IPX_NODE_LEN];
265 +#endif
266 + unsigned short type;
267 +/*
268 + * To handle special ncp connection-handling sockets for mars_nwe,
269 + * the connection number must be stored in the socket.
270 + */
271 + unsigned short ipx_ncp_conn;
272 +};
273 +#endif
274 +
275 +#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
276 +struct ipv6_pinfo {
277 + struct in6_addr saddr;
278 + struct in6_addr rcv_saddr;
279 + struct in6_addr daddr;
280 + struct in6_addr *daddr_cache;
281 +
282 + __u32 flow_label;
283 + __u32 frag_size;
284 + int hop_limit;
285 + int mcast_hops;
286 + int mcast_oif;
287 +
288 + /* pktoption flags */
289 + union {
290 + struct {
291 + __u8 srcrt:2,
292 + rxinfo:1,
293 + rxhlim:1,
294 + hopopts:1,
295 + dstopts:1,
296 + authhdr:1,
297 + rxflow:1;
298 + } bits;
299 + __u8 all;
300 + } rxopt;
301 +
302 + /* sockopt flags */
303 + __u8 mc_loop:1,
304 + recverr:1,
305 + sndflow:1,
306 + pmtudisc:2,
307 + ipv6only:1;
308 +
309 + struct ipv6_mc_socklist *ipv6_mc_list;
310 + struct ipv6_ac_socklist *ipv6_ac_list;
311 + struct ipv6_fl_socklist *ipv6_fl_list;
312 + __u32 dst_cookie;
313 +
314 + struct ipv6_txoptions *opt;
315 + struct sk_buff *pktoptions;
316 +};
317 +
318 +struct raw6_opt {
319 + __u32 checksum; /* perform checksum */
320 + __u32 offset; /* checksum offset */
321 +
322 + struct icmp6_filter filter;
323 +};
324 +
325 +#define __ipv6_only_sock(sk) ((sk)->net_pinfo.af_inet6.ipv6only)
326 +#define ipv6_only_sock(sk) ((sk)->family == PF_INET6 && \
327 + (sk)->net_pinfo.af_inet6.ipv6only)
328 +#else
329 +#define __ipv6_only_sock(sk) 0
330 +#define ipv6_only_sock(sk) 0
331 +#endif /* IPV6 */
332 +
333 +#if defined(CONFIG_INET) || defined(CONFIG_INET_MODULE)
334 +struct raw_opt {
335 + struct icmp_filter filter;
336 +};
337 +#endif
338 +
339 +#if defined(CONFIG_INET) || defined (CONFIG_INET_MODULE)
340 +struct inet_opt
341 +{
342 + int ttl; /* TTL setting */
343 + int tos; /* TOS */
344 + unsigned cmsg_flags;
345 + struct ip_options *opt;
346 + unsigned char hdrincl; /* Include headers ? */
347 + __u8 mc_ttl; /* Multicasting TTL */
348 + __u8 mc_loop; /* Loopback */
349 + unsigned recverr : 1,
350 + freebind : 1;
351 + __u16 id; /* ID counter for DF pkts */
352 + __u8 pmtudisc;
353 + int mc_index; /* Multicast device index */
354 + __u32 mc_addr;
355 + struct ip_mc_socklist *mc_list; /* Group array */
356 +};
357 +#endif
358 +
359 +#if defined(CONFIG_PPPOE) || defined (CONFIG_PPPOE_MODULE)
360 +struct pppoe_opt
361 +{
362 + struct net_device *dev; /* device associated with socket*/
363 + struct pppoe_addr pa; /* what this socket is bound to*/
364 + struct sockaddr_pppox relay; /* what socket data will be
365 + relayed to (PPPoE relaying) */
366 +};
367 +
368 +struct pppox_opt
369 +{
370 + struct ppp_channel chan;
371 + struct sock *sk;
372 + struct pppox_opt *next; /* for hash table */
373 + union {
374 + struct pppoe_opt pppoe;
375 + } proto;
376 +};
377 +#define pppoe_dev proto.pppoe.dev
378 +#define pppoe_pa proto.pppoe.pa
379 +#define pppoe_relay proto.pppoe.relay
380 +#endif
381 +
382 +/* This defines a selective acknowledgement block. */
383 +struct tcp_sack_block {
384 + __u32 start_seq;
385 + __u32 end_seq;
386 +};
387 +
388 +enum tcp_congestion_algo {
389 + TCP_RENO=0,
390 + TCP_VEGAS,
391 + TCP_WESTWOOD,
392 + TCP_BIC,
393 +};
394 +
395 +struct tcp_opt {
396 + int tcp_header_len; /* Bytes of tcp header to send */
397 +
398 +/*
399 + * Header prediction flags
400 + * 0x5?10 << 16 + snd_wnd in net byte order
401 + */
402 + __u32 pred_flags;
403 +
404 +/*
405 + * RFC793 variables by their proper names. This means you can
406 + * read the code and the spec side by side (and laugh ...)
407 + * See RFC793 and RFC1122. The RFC writes these in capitals.
408 + */
409 + __u32 rcv_nxt; /* What we want to receive next */
410 + __u32 snd_nxt; /* Next sequence we send */
411 +
412 + __u32 snd_una; /* First byte we want an ack for */
413 + __u32 snd_sml; /* Last byte of the most recently transmitted small packet */
414 + __u32 rcv_tstamp; /* timestamp of last received ACK (for keepalives) */
415 + __u32 lsndtime; /* timestamp of last sent data packet (for restart window) */
416 +
417 + /* Delayed ACK control data */
418 + struct {
419 + __u8 pending; /* ACK is pending */
420 + __u8 quick; /* Scheduled number of quick acks */
421 + __u8 pingpong; /* The session is interactive */
422 + __u8 blocked; /* Delayed ACK was blocked by socket lock*/
423 + __u32 ato; /* Predicted tick of soft clock */
424 + unsigned long timeout; /* Currently scheduled timeout */
425 + __u32 lrcvtime; /* timestamp of last received data packet*/
426 + __u16 last_seg_size; /* Size of last incoming segment */
427 + __u16 rcv_mss; /* MSS used for delayed ACK decisions */
428 + } ack;
429 +
430 + /* Data for direct copy to user */
431 + struct {
432 + struct sk_buff_head prequeue;
433 + struct task_struct *task;
434 + struct iovec *iov;
435 + int memory;
436 + int len;
437 + } ucopy;
438 +
439 + __u32 snd_wl1; /* Sequence for window update */
440 + __u32 snd_wnd; /* The window we expect to receive */
441 + __u32 max_window; /* Maximal window ever seen from peer */
442 + __u32 pmtu_cookie; /* Last pmtu seen by socket */
443 + __u16 mss_cache; /* Cached effective mss, not including SACKS */
444 + __u16 mss_clamp; /* Maximal mss, negotiated at connection setup */
445 + __u16 ext_header_len; /* Network protocol overhead (IP/IPv6 options) */
446 + __u8 ca_state; /* State of fast-retransmit machine */
447 + __u8 retransmits; /* Number of unrecovered RTO timeouts. */
448 +
449 + __u8 reordering; /* Packet reordering metric. */
450 + __u8 queue_shrunk; /* Write queue has been shrunk recently.*/
451 + __u8 defer_accept; /* User waits for some data after accept() */
452 +
453 +/* RTT measurement */
454 + __u8 backoff; /* backoff */
455 + __u32 srtt; /* smothed round trip time << 3 */
456 + __u32 mdev; /* medium deviation */
457 + __u32 mdev_max; /* maximal mdev for the last rtt period */
458 + __u32 rttvar; /* smoothed mdev_max */
459 + __u32 rtt_seq; /* sequence number to update rttvar */
460 + __u32 rto; /* retransmit timeout */
461 +
462 + __u32 packets_out; /* Packets which are "in flight" */
463 + __u32 left_out; /* Packets which leaved network */
464 + __u32 retrans_out; /* Retransmitted packets out */
465 +
466 +
467 +/*
468 + * Slow start and congestion control (see also Nagle, and Karn & Partridge)
469 + */
470 + __u32 snd_ssthresh; /* Slow start size threshold */
471 + __u32 snd_cwnd; /* Sending congestion window */
472 + __u16 snd_cwnd_cnt; /* Linear increase counter */
473 + __u16 snd_cwnd_clamp; /* Do not allow snd_cwnd to grow above this */
474 + __u32 snd_cwnd_used;
475 + __u32 snd_cwnd_stamp;
476 +
477 + /* Two commonly used timers in both sender and receiver paths. */
478 + unsigned long timeout;
479 + struct timer_list retransmit_timer; /* Resend (no ack) */
480 + struct timer_list delack_timer; /* Ack delay */
481 +
482 + struct sk_buff_head out_of_order_queue; /* Out of order segments go here */
483 +
484 + struct tcp_func *af_specific; /* Operations which are AF_INET{4,6} specific */
485 + struct sk_buff *send_head; /* Front of stuff to transmit */
486 + struct page *sndmsg_page; /* Cached page for sendmsg */
487 + u32 sndmsg_off; /* Cached offset for sendmsg */
488 +
489 + __u32 rcv_wnd; /* Current receiver window */
490 + __u32 rcv_wup; /* rcv_nxt on last window update sent */
491 + __u32 write_seq; /* Tail(+1) of data held in tcp send buffer */
492 + __u32 pushed_seq; /* Last pushed seq, required to talk to windows */
493 + __u32 copied_seq; /* Head of yet unread data */
494 +/*
495 + * Options received (usually on last packet, some only on SYN packets).
496 + */
497 + char tstamp_ok, /* TIMESTAMP seen on SYN packet */
498 + wscale_ok, /* Wscale seen on SYN packet */
499 + sack_ok; /* SACK seen on SYN packet */
500 + char saw_tstamp; /* Saw TIMESTAMP on last packet */
501 + __u8 snd_wscale; /* Window scaling received from sender */
502 + __u8 rcv_wscale; /* Window scaling to send to receiver */
503 + __u8 nonagle; /* Disable Nagle algorithm? */
504 + __u8 keepalive_probes; /* num of allowed keep alive probes */
505 +
506 +/* PAWS/RTTM data */
507 + __u32 rcv_tsval; /* Time stamp value */
508 + __u32 rcv_tsecr; /* Time stamp echo reply */
509 + __u32 ts_recent; /* Time stamp to echo next */
510 + long ts_recent_stamp;/* Time we stored ts_recent (for aging) */
511 +
512 +/* SACKs data */
513 + __u16 user_mss; /* mss requested by user in ioctl */
514 + __u8 dsack; /* D-SACK is scheduled */
515 + __u8 eff_sacks; /* Size of SACK array to send with next packet */
516 + struct tcp_sack_block duplicate_sack[1]; /* D-SACK block */
517 + struct tcp_sack_block selective_acks[4]; /* The SACKS themselves*/
518 +
519 + __u32 window_clamp; /* Maximal window to advertise */
520 + __u32 rcv_ssthresh; /* Current window clamp */
521 + __u8 probes_out; /* unanswered 0 window probes */
522 + __u8 num_sacks; /* Number of SACK blocks */
523 + __u16 advmss; /* Advertised MSS */
524 +
525 + __u8 syn_retries; /* num of allowed syn retries */
526 + __u8 ecn_flags; /* ECN status bits. */
527 + __u16 prior_ssthresh; /* ssthresh saved at recovery start */
528 + __u32 lost_out; /* Lost packets */
529 + __u32 sacked_out; /* SACK'd packets */
530 + __u32 fackets_out; /* FACK'd packets */
531 + __u32 high_seq; /* snd_nxt at onset of congestion */
532 +
533 + __u32 retrans_stamp; /* Timestamp of the last retransmit,
534 + * also used in SYN-SENT to remember stamp of
535 + * the first SYN. */
536 + __u32 undo_marker; /* tracking retrans started here. */
537 + int undo_retrans; /* number of undoable retransmissions. */
538 + __u32 urg_seq; /* Seq of received urgent pointer */
539 + __u16 urg_data; /* Saved octet of OOB data and control flags */
540 + __u8 pending; /* Scheduled timer event */
541 + __u8 urg_mode; /* In urgent mode */
542 + __u32 snd_up; /* Urgent pointer */
543 +
544 + /* The syn_wait_lock is necessary only to avoid tcp_get_info having
545 + * to grab the main lock sock while browsing the listening hash
546 + * (otherwise it's deadlock prone).
547 + * This lock is acquired in read mode only from tcp_get_info() and
548 + * it's acquired in write mode _only_ from code that is actively
549 + * changing the syn_wait_queue. All readers that are holding
550 + * the master sock lock don't need to grab this lock in read mode
551 + * too as the syn_wait_queue writes are always protected from
552 + * the main sock lock.
553 + */
554 + rwlock_t syn_wait_lock;
555 + struct tcp_listen_opt *listen_opt;
556 +
557 + /* FIFO of established children */
558 + struct open_request *accept_queue;
559 + struct open_request *accept_queue_tail;
560 +
561 + int write_pending; /* A write to socket waits to start. */
562 +
563 + unsigned int keepalive_time; /* time before keep alive takes place */
564 + unsigned int keepalive_intvl; /* time interval between keep alive probes */
565 + int linger2;
566 +
567 + __u8 adv_cong; /* Using Vegas, Westwood, or BIC */
568 + __u8 frto_counter; /* Number of new acks after RTO */
569 + __u32 frto_highmark; /* snd_nxt when RTO occurred */
570 +
571 + unsigned long last_synq_overflow;
572 +
573 +/* Receiver side RTT estimation */
574 + struct {
575 + __u32 rtt;
576 + __u32 seq;
577 + __u32 time;
578 + } rcv_rtt_est;
579 +
580 +/* Receiver queue space */
581 + struct {
582 + int space;
583 + __u32 seq;
584 + __u32 time;
585 + } rcvq_space;
586 +
587 +/* TCP Westwood structure */
588 + struct {
589 + __u32 bw_ns_est; /* first bandwidth estimation..not too smoothed 8) */
590 + __u32 bw_est; /* bandwidth estimate */
591 + __u32 rtt_win_sx; /* here starts a new evaluation... */
592 + __u32 bk;
593 + __u32 snd_una; /* used for evaluating the number of acked bytes */
594 + __u32 cumul_ack;
595 + __u32 accounted;
596 + __u32 rtt;
597 + __u32 rtt_min; /* minimum observed RTT */
598 + } westwood;
599 +
600 +/* Vegas variables */
601 + struct {
602 + __u32 beg_snd_nxt; /* right edge during last RTT */
603 + __u32 beg_snd_una; /* left edge during last RTT */
604 + __u32 beg_snd_cwnd; /* saves the size of the cwnd */
605 + __u8 doing_vegas_now;/* if true, do vegas for this RTT */
606 + __u16 cntRTT; /* # of RTTs measured within last RTT */
607 + __u32 minRTT; /* min of RTTs measured within last RTT (in usec) */
608 + __u32 baseRTT; /* the min of all Vegas RTT measurements seen (in usec) */
609 + } vegas;
610 +
611 + /* BI TCP Parameters */
612 + struct {
613 + __u32 cnt; /* increase cwnd by 1 after this number of ACKs */
614 + __u32 last_max_cwnd; /* last maximium snd_cwnd */
615 + __u32 last_cwnd; /* the last snd_cwnd */
616 + __u32 last_stamp; /* time when updated last_cwnd */
617 + } bictcp;
618 +};
619 +
620 +
621 +/*
622 + * This structure really needs to be cleaned up.
623 + * Most of it is for TCP, and not used by any of
624 + * the other protocols.
625 + */
626 +
627 +/*
628 + * The idea is to start moving to a newer struct gradualy
629 + *
630 + * IMHO the newer struct should have the following format:
631 + *
632 + * struct sock {
633 + * sockmem [mem, proto, callbacks]
634 + *
635 + * union or struct {
636 + * ax25;
637 + * } ll_pinfo;
638 + *
639 + * union {
640 + * ipv4;
641 + * ipv6;
642 + * ipx;
643 + * netrom;
644 + * rose;
645 + * x25;
646 + * } net_pinfo;
647 + *
648 + * union {
649 + * tcp;
650 + * udp;
651 + * spx;
652 + * netrom;
653 + * } tp_pinfo;
654 + *
655 + * }
656 + *
657 + * The idea failed because IPv6 transition asssumes dual IP/IPv6 sockets.
658 + * So, net_pinfo is IPv6 are really, and protinfo unifies all another
659 + * private areas.
660 + */
661 +
662 +/* Define this to get the sk->debug debugging facility. */
663 +#define SOCK_DEBUGGING
664 +#ifdef SOCK_DEBUGGING
665 +#define SOCK_DEBUG(sk, msg...) do { if((sk) && ((sk)->debug)) printk(KERN_DEBUG msg); } while (0)
666 +#else
667 +#define SOCK_DEBUG(sk, msg...) do { } while (0)
668 +#endif
669 +
670 +/* This is the per-socket lock. The spinlock provides a synchronization
671 + * between user contexts and software interrupt processing, whereas the
672 + * mini-semaphore synchronizes multiple users amongst themselves.
673 + */
674 +typedef struct {
675 + spinlock_t slock;
676 + unsigned int users;
677 + wait_queue_head_t wq;
678 +} socket_lock_t;
679 +
680 +#define sock_lock_init(__sk) \
681 +do { spin_lock_init(&((__sk)->lock.slock)); \
682 + (__sk)->lock.users = 0; \
683 + init_waitqueue_head(&((__sk)->lock.wq)); \
684 +} while(0)
685 +
686 +struct sock {
687 + /* Socket demultiplex comparisons on incoming packets. */
688 + __u32 daddr; /* Foreign IPv4 addr */
689 + __u32 rcv_saddr; /* Bound local IPv4 addr */
690 + __u16 dport; /* Destination port */
691 + unsigned short num; /* Local port */
692 + int bound_dev_if; /* Bound device index if != 0 */
693 +
694 + /* Main hash linkage for various protocol lookup tables. */
695 + struct sock *next;
696 + struct sock **pprev;
697 + struct sock *bind_next;
698 + struct sock **bind_pprev;
699 +
700 + volatile unsigned char state, /* Connection state */
701 + zapped; /* In ax25 & ipx means not linked */
702 + __u16 sport; /* Source port */
703 +
704 + unsigned short family; /* Address family */
705 + unsigned char reuse; /* SO_REUSEADDR setting */
706 + unsigned char shutdown;
707 + atomic_t refcnt; /* Reference count */
708 +
709 + socket_lock_t lock; /* Synchronizer... */
710 + int rcvbuf; /* Size of receive buffer in bytes */
711 +
712 + wait_queue_head_t *sleep; /* Sock wait queue */
713 + struct dst_entry *dst_cache; /* Destination cache */
714 + rwlock_t dst_lock;
715 + atomic_t rmem_alloc; /* Receive queue bytes committed */
716 + struct sk_buff_head receive_queue; /* Incoming packets */
717 + atomic_t wmem_alloc; /* Transmit queue bytes committed */
718 + struct sk_buff_head write_queue; /* Packet sending queue */
719 + atomic_t omem_alloc; /* "o" is "option" or "other" */
720 + int wmem_queued; /* Persistent queue size */
721 + int forward_alloc; /* Space allocated forward. */
722 + __u32 saddr; /* Sending source */
723 + unsigned int allocation; /* Allocation mode */
724 + int sndbuf; /* Size of send buffer in bytes */
725 + struct sock *prev;
726 +
727 + /* Not all are volatile, but some are, so we might as well say they all are.
728 + * XXX Make this a flag word -DaveM
729 + */
730 + volatile char dead,
731 + done,
732 + urginline,
733 + keepopen,
734 + linger,
735 + destroy,
736 + no_check,
737 + broadcast,
738 + bsdism;
739 + unsigned char debug;
740 + unsigned char rcvtstamp;
741 + unsigned char use_write_queue;
742 + unsigned char userlocks;
743 + /* Hole of 3 bytes. Try to pack. */
744 + int route_caps;
745 + int proc;
746 + unsigned long lingertime;
747 +
748 + int hashent;
749 + struct sock *pair;
750 +
751 + /* The backlog queue is special, it is always used with
752 + * the per-socket spinlock held and requires low latency
753 + * access. Therefore we special case it's implementation.
754 + */
755 + struct {
756 + struct sk_buff *head;
757 + struct sk_buff *tail;
758 + } backlog;
759 +
760 + rwlock_t callback_lock;
761 +
762 + /* Error queue, rarely used. */
763 + struct sk_buff_head error_queue;
764 +
765 + struct proto *prot;
766 +
767 +#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
768 + union {
769 + struct ipv6_pinfo af_inet6;
770 + } net_pinfo;
771 +#endif
772 +
773 + union {
774 + struct tcp_opt af_tcp;
775 +#if defined(CONFIG_IP_SCTP) || defined (CONFIG_IP_SCTP_MODULE)
776 + struct sctp_opt af_sctp;
777 +#endif
778 +#if defined(CONFIG_INET) || defined (CONFIG_INET_MODULE)
779 + struct raw_opt tp_raw4;
780 +#endif
781 +#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
782 + struct raw6_opt tp_raw;
783 +#endif /* CONFIG_IPV6 */
784 +#if defined(CONFIG_SPX) || defined (CONFIG_SPX_MODULE)
785 + struct spx_opt af_spx;
786 +#endif /* CONFIG_SPX */
787 +
788 + } tp_pinfo;
789 +
790 + int err, err_soft; /* Soft holds errors that don't
791 + cause failure but are the cause
792 + of a persistent failure not just
793 + 'timed out' */
794 + unsigned short ack_backlog;
795 + unsigned short max_ack_backlog;
796 + __u32 priority;
797 + unsigned short type;
798 + unsigned char localroute; /* Route locally only */
799 + unsigned char protocol;
800 + struct ucred peercred;
801 + int rcvlowat;
802 + long rcvtimeo;
803 + long sndtimeo;
804 +
805 +#ifdef CONFIG_FILTER
806 + /* Socket Filtering Instructions */
807 + struct sk_filter *filter;
808 +#endif /* CONFIG_FILTER */
809 +
810 + /* This is where all the private (optional) areas that don't
811 + * overlap will eventually live.
812 + */
813 + union {
814 + void *destruct_hook;
815 + struct unix_opt af_unix;
816 +#if defined(CONFIG_INET) || defined (CONFIG_INET_MODULE)
817 + struct inet_opt af_inet;
818 +#endif
819 +#if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
820 + struct atalk_sock af_at;
821 +#endif
822 +#if defined(CONFIG_IPX) || defined(CONFIG_IPX_MODULE)
823 + struct ipx_opt af_ipx;
824 +#endif
825 +#if defined (CONFIG_DECNET) || defined(CONFIG_DECNET_MODULE)
826 + struct dn_scp dn;
827 +#endif
828 +#if defined (CONFIG_PACKET) || defined(CONFIG_PACKET_MODULE)
829 + struct packet_opt *af_packet;
830 +#endif
831 +#if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
832 + x25_cb *x25;
833 +#endif
834 +#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
835 + ax25_cb *ax25;
836 +#endif
837 +#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
838 + nr_cb *nr;
839 +#endif
840 +#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
841 + rose_cb *rose;
842 +#endif
843 +#if defined(CONFIG_PPPOE) || defined(CONFIG_PPPOE_MODULE)
844 + struct pppox_opt *pppox;
845 +#endif
846 + struct netlink_opt *af_netlink;
847 +#if defined(CONFIG_ECONET) || defined(CONFIG_ECONET_MODULE)
848 + struct econet_opt *af_econet;
849 +#endif
850 +#if defined(CONFIG_ATM) || defined(CONFIG_ATM_MODULE)
851 + struct atm_vcc *af_atm;
852 +#endif
853 +#if defined(CONFIG_IRDA) || defined(CONFIG_IRDA_MODULE)
854 + struct irda_sock *irda;
855 +#endif
856 +#if defined(CONFIG_WAN_ROUTER) || defined(CONFIG_WAN_ROUTER_MODULE)
857 + struct wanpipe_opt *af_wanpipe;
858 +#endif
859 + } protinfo;
860 +
861 +
862 + /* This part is used for the timeout functions. */
863 + struct timer_list timer; /* This is the sock cleanup timer. */
864 + struct timeval stamp;
865 +
866 + /* Identd and reporting IO signals */
867 + struct socket *socket;
868 +
869 + /* RPC layer private data */
870 + void *user_data;
871 +
872 + /* Callbacks */
873 + void (*state_change)(struct sock *sk);
874 + void (*data_ready)(struct sock *sk,int bytes);
875 + void (*write_space)(struct sock *sk);
876 + void (*error_report)(struct sock *sk);
877 +
878 + int (*backlog_rcv) (struct sock *sk,
879 + struct sk_buff *skb);
880 + void (*destruct)(struct sock *sk);
881 +};
882 +
883 +/* The per-socket spinlock must be held here. */
884 +#define sk_add_backlog(__sk, __skb) \
885 +do { if((__sk)->backlog.tail == NULL) { \
886 + (__sk)->backlog.head = \
887 + (__sk)->backlog.tail = (__skb); \
888 + } else { \
889 + ((__sk)->backlog.tail)->next = (__skb); \
890 + (__sk)->backlog.tail = (__skb); \
891 + } \
892 + (__skb)->next = NULL; \
893 +} while(0)
894 +
895 +/* IP protocol blocks we attach to sockets.
896 + * socket layer -> transport layer interface
897 + * transport -> network interface is defined by struct inet_proto
898 + */
899 +struct proto {
900 + void (*close)(struct sock *sk,
901 + long timeout);
902 + int (*connect)(struct sock *sk,
903 + struct sockaddr *uaddr,
904 + int addr_len);
905 + int (*disconnect)(struct sock *sk, int flags);
906 +
907 + struct sock * (*accept) (struct sock *sk, int flags, int *err);
908 +
909 + int (*ioctl)(struct sock *sk, int cmd,
910 + unsigned long arg);
911 + int (*init)(struct sock *sk);
912 + int (*destroy)(struct sock *sk);
913 + void (*shutdown)(struct sock *sk, int how);
914 + int (*setsockopt)(struct sock *sk, int level,
915 + int optname, char *optval, int optlen);
916 + int (*getsockopt)(struct sock *sk, int level,
917 + int optname, char *optval,
918 + int *option);
919 + int (*sendmsg)(struct sock *sk, struct msghdr *msg,
920 + int len);
921 + int (*recvmsg)(struct sock *sk, struct msghdr *msg,
922 + int len, int noblock, int flags,
923 + int *addr_len);
924 + int (*bind)(struct sock *sk,
925 + struct sockaddr *uaddr, int addr_len);
926 +
927 + int (*backlog_rcv) (struct sock *sk,
928 + struct sk_buff *skb);
929 +
930 + /* Keeping track of sk's, looking them up, and port selection methods. */
931 + void (*hash)(struct sock *sk);
932 + void (*unhash)(struct sock *sk);
933 + int (*get_port)(struct sock *sk, unsigned short snum);
934 +
935 + char name[32];
936 +
937 + struct {
938 + int inuse;
939 + u8 __pad[SMP_CACHE_BYTES - sizeof(int)];
940 + } stats[NR_CPUS];
941 +};
942 +
943 +/* Called with local bh disabled */
944 +static __inline__ void sock_prot_inc_use(struct proto *prot)
945 +{
946 + prot->stats[smp_processor_id()].inuse++;
947 +}
948 +
949 +static __inline__ void sock_prot_dec_use(struct proto *prot)
950 +{
951 + prot->stats[smp_processor_id()].inuse--;
952 +}
953 +
954 +/* About 10 seconds */
955 +#define SOCK_DESTROY_TIME (10*HZ)
956 +
957 +/* Sockets 0-1023 can't be bound to unless you are superuser */
958 +#define PROT_SOCK 1024
959 +
960 +#define SHUTDOWN_MASK 3
961 +#define RCV_SHUTDOWN 1
962 +#define SEND_SHUTDOWN 2
963 +
964 +#define SOCK_SNDBUF_LOCK 1
965 +#define SOCK_RCVBUF_LOCK 2
966 +#define SOCK_BINDADDR_LOCK 4
967 +#define SOCK_BINDPORT_LOCK 8
968 +
969 +
970 +/* Used by processes to "lock" a socket state, so that
971 + * interrupts and bottom half handlers won't change it
972 + * from under us. It essentially blocks any incoming
973 + * packets, so that we won't get any new data or any
974 + * packets that change the state of the socket.
975 + *
976 + * While locked, BH processing will add new packets to
977 + * the backlog queue. This queue is processed by the
978 + * owner of the socket lock right before it is released.
979 + *
980 + * Since ~2.3.5 it is also exclusive sleep lock serializing
981 + * accesses from user process context.
982 + */
983 +extern void __lock_sock(struct sock *sk);
984 +extern void __release_sock(struct sock *sk);
985 +#define lock_sock(__sk) \
986 +do { spin_lock_bh(&((__sk)->lock.slock)); \
987 + if ((__sk)->lock.users != 0) \
988 + __lock_sock(__sk); \
989 + (__sk)->lock.users = 1; \
990 + spin_unlock_bh(&((__sk)->lock.slock)); \
991 +} while(0)
992 +
993 +#define release_sock(__sk) \
994 +do { spin_lock_bh(&((__sk)->lock.slock)); \
995 + if ((__sk)->backlog.tail != NULL) \
996 + __release_sock(__sk); \
997 + (__sk)->lock.users = 0; \
998 + if (waitqueue_active(&((__sk)->lock.wq))) wake_up(&((__sk)->lock.wq)); \
999 + spin_unlock_bh(&((__sk)->lock.slock)); \
1000 +} while(0)
1001 +
1002 +/* BH context may only use the following locking interface. */
1003 +#define bh_lock_sock(__sk) spin_lock(&((__sk)->lock.slock))
1004 +#define bh_unlock_sock(__sk) spin_unlock(&((__sk)->lock.slock))
1005 +
1006 +extern struct sock * sk_alloc(int family, int priority, int zero_it);
1007 +extern void sk_free(struct sock *sk);
1008 +
1009 +extern struct sk_buff *sock_wmalloc(struct sock *sk,
1010 + unsigned long size, int force,
1011 + int priority);
1012 +extern struct sk_buff *sock_rmalloc(struct sock *sk,
1013 + unsigned long size, int force,
1014 + int priority);
1015 +extern void sock_wfree(struct sk_buff *skb);
1016 +extern void sock_rfree(struct sk_buff *skb);
1017 +
1018 +extern int sock_setsockopt(struct socket *sock, int level,
1019 + int op, char *optval,
1020 + int optlen);
1021 +
1022 +extern int sock_getsockopt(struct socket *sock, int level,
1023 + int op, char *optval,
1024 + int *optlen);
1025 +extern struct sk_buff *sock_alloc_send_skb(struct sock *sk,
1026 + unsigned long size,
1027 + int noblock,
1028 + int *errcode);
1029 +extern struct sk_buff *sock_alloc_send_pskb(struct sock *sk,
1030 + unsigned long header_len,
1031 + unsigned long data_len,
1032 + int noblock,
1033 + int *errcode);
1034 +extern void *sock_kmalloc(struct sock *sk, int size, int priority);
1035 +extern void sock_kfree_s(struct sock *sk, void *mem, int size);
1036 +
1037 +/*
1038 + * Functions to fill in entries in struct proto_ops when a protocol
1039 + * does not implement a particular function.
1040 + */
1041 +extern int sock_no_release(struct socket *);
1042 +extern int sock_no_bind(struct socket *,
1043 + struct sockaddr *, int);
1044 +extern int sock_no_connect(struct socket *,
1045 + struct sockaddr *, int, int);
1046 +extern int sock_no_socketpair(struct socket *,
1047 + struct socket *);
1048 +extern int sock_no_accept(struct socket *,
1049 + struct socket *, int);
1050 +extern int sock_no_getname(struct socket *,
1051 + struct sockaddr *, int *, int);
1052 +extern unsigned int sock_no_poll(struct file *, struct socket *,
1053 + struct poll_table_struct *);
1054 +extern int sock_no_ioctl(struct socket *, unsigned int,
1055 + unsigned long);
1056 +extern int sock_no_listen(struct socket *, int);
1057 +extern int sock_no_shutdown(struct socket *, int);
1058 +extern int sock_no_getsockopt(struct socket *, int , int,
1059 + char *, int *);
1060 +extern int sock_no_setsockopt(struct socket *, int, int,
1061 + char *, int);
1062 +extern int sock_no_fcntl(struct socket *,
1063 + unsigned int, unsigned long);
1064 +extern int sock_no_sendmsg(struct socket *,
1065 + struct msghdr *, int,
1066 + struct scm_cookie *);
1067 +extern int sock_no_recvmsg(struct socket *,
1068 + struct msghdr *, int, int,
1069 + struct scm_cookie *);
1070 +extern int sock_no_mmap(struct file *file,
1071 + struct socket *sock,
1072 + struct vm_area_struct *vma);
1073 +extern ssize_t sock_no_sendpage(struct socket *sock,
1074 + struct page *page,
1075 + int offset, size_t size,
1076 + int flags);
1077 +
1078 +/*
1079 + * Default socket callbacks and setup code
1080 + */
1081 +
1082 +extern void sock_def_destruct(struct sock *);
1083 +
1084 +/* Initialise core socket variables */
1085 +extern void sock_init_data(struct socket *sock, struct sock *sk);
1086 +
1087 +extern void sklist_remove_socket(struct sock **list, struct sock *sk);
1088 +extern void sklist_insert_socket(struct sock **list, struct sock *sk);
1089 +extern void sklist_destroy_socket(struct sock **list, struct sock *sk);
1090 +
1091 +#ifdef CONFIG_FILTER
1092 +
1093 +/**
1094 + * sk_filter - run a packet through a socket filter
1095 + * @sk: sock associated with &sk_buff
1096 + * @skb: buffer to filter
1097 + * @needlock: set to 1 if the sock is not locked by caller.
1098 + *
1099 + * Run the filter code and then cut skb->data to correct size returned by
1100 + * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
1101 + * than pkt_len we keep whole skb->data. This is the socket level
1102 + * wrapper to sk_run_filter. It returns 0 if the packet should
1103 + * be accepted or -EPERM if the packet should be tossed.
1104 + */
1105 +
1106 +static inline int sk_filter(struct sock *sk, struct sk_buff *skb, int needlock)
1107 +{
1108 + int err = 0;
1109 +
1110 + if (sk->filter) {
1111 + struct sk_filter *filter;
1112 +
1113 + if (needlock)
1114 + bh_lock_sock(sk);
1115 +
1116 + filter = sk->filter;
1117 + if (filter) {
1118 + int pkt_len = sk_run_filter(skb, filter->insns,
1119 + filter->len);
1120 + if (!pkt_len)
1121 + err = -EPERM;
1122 + else
1123 + skb_trim(skb, pkt_len);
1124 + }
1125 +
1126 + if (needlock)
1127 + bh_unlock_sock(sk);
1128 + }
1129 + return err;
1130 +}
1131 +
1132 +/**
1133 + * sk_filter_release: Release a socket filter
1134 + * @sk: socket
1135 + * @fp: filter to remove
1136 + *
1137 + * Remove a filter from a socket and release its resources.
1138 + */
1139 +
1140 +static inline void sk_filter_release(struct sock *sk, struct sk_filter *fp)
1141 +{
1142 + unsigned int size = sk_filter_len(fp);
1143 +
1144 + atomic_sub(size, &sk->omem_alloc);
1145 +
1146 + if (atomic_dec_and_test(&fp->refcnt))
1147 + kfree(fp);
1148 +}
1149 +
1150 +static inline void sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1151 +{
1152 + atomic_inc(&fp->refcnt);
1153 + atomic_add(sk_filter_len(fp), &sk->omem_alloc);
1154 +}
1155 +
1156 +#else
1157 +
1158 +static inline int sk_filter(struct sock *sk, struct sk_buff *skb, int needlock)
1159 +{
1160 + return 0;
1161 +}
1162 +
1163 +#endif /* CONFIG_FILTER */
1164 +
1165 +/*
1166 + * Socket reference counting postulates.
1167 + *
1168 + * * Each user of socket SHOULD hold a reference count.
1169 + * * Each access point to socket (an hash table bucket, reference from a list,
1170 + * running timer, skb in flight MUST hold a reference count.
1171 + * * When reference count hits 0, it means it will never increase back.
1172 + * * When reference count hits 0, it means that no references from
1173 + * outside exist to this socket and current process on current CPU
1174 + * is last user and may/should destroy this socket.
1175 + * * sk_free is called from any context: process, BH, IRQ. When
1176 + * it is called, socket has no references from outside -> sk_free
1177 + * may release descendant resources allocated by the socket, but
1178 + * to the time when it is called, socket is NOT referenced by any
1179 + * hash tables, lists etc.
1180 + * * Packets, delivered from outside (from network or from another process)
1181 + * and enqueued on receive/error queues SHOULD NOT grab reference count,
1182 + * when they sit in queue. Otherwise, packets will leak to hole, when
1183 + * socket is looked up by one cpu and unhasing is made by another CPU.
1184 + * It is true for udp/raw, netlink (leak to receive and error queues), tcp
1185 + * (leak to backlog). Packet socket does all the processing inside
1186 + * BR_NETPROTO_LOCK, so that it has not this race condition. UNIX sockets
1187 + * use separate SMP lock, so that they are prone too.
1188 + */
1189 +
1190 +/* Grab socket reference count. This operation is valid only
1191 + when sk is ALREADY grabbed f.e. it is found in hash table
1192 + or a list and the lookup is made under lock preventing hash table
1193 + modifications.
1194 + */
1195 +
1196 +static inline void sock_hold(struct sock *sk)
1197 +{
1198 + atomic_inc(&sk->refcnt);
1199 +}
1200 +
1201 +/* Ungrab socket in the context, which assumes that socket refcnt
1202 + cannot hit zero, f.e. it is true in context of any socketcall.
1203 + */
1204 +static inline void __sock_put(struct sock *sk)
1205 +{
1206 + atomic_dec(&sk->refcnt);
1207 +}
1208 +
1209 +/* Ungrab socket and destroy it, if it was the last reference. */
1210 +static inline void sock_put(struct sock *sk)
1211 +{
1212 + if (atomic_dec_and_test(&sk->refcnt))
1213 + sk_free(sk);
1214 +}
1215 +
1216 +/* Detach socket from process context.
1217 + * Announce socket dead, detach it from wait queue and inode.
1218 + * Note that parent inode held reference count on this struct sock,
1219 + * we do not release it in this function, because protocol
1220 + * probably wants some additional cleanups or even continuing
1221 + * to work with this socket (TCP).
1222 + */
1223 +static inline void sock_orphan(struct sock *sk)
1224 +{
1225 + write_lock_bh(&sk->callback_lock);
1226 + sk->dead = 1;
1227 + sk->socket = NULL;
1228 + sk->sleep = NULL;
1229 + write_unlock_bh(&sk->callback_lock);
1230 +}
1231 +
1232 +static inline void sock_graft(struct sock *sk, struct socket *parent)
1233 +{
1234 + write_lock_bh(&sk->callback_lock);
1235 + sk->sleep = &parent->wait;
1236 + parent->sk = sk;
1237 + sk->socket = parent;
1238 + write_unlock_bh(&sk->callback_lock);
1239 +}
1240 +
1241 +static inline int sock_i_uid(struct sock *sk)
1242 +{
1243 + int uid;
1244 +
1245 + read_lock(&sk->callback_lock);
1246 + uid = sk->socket ? sk->socket->inode->i_uid : 0;
1247 + read_unlock(&sk->callback_lock);
1248 + return uid;
1249 +}
1250 +
1251 +static inline unsigned long sock_i_ino(struct sock *sk)
1252 +{
1253 + unsigned long ino;
1254 +
1255 + read_lock(&sk->callback_lock);
1256 + ino = sk->socket ? sk->socket->inode->i_ino : 0;
1257 + read_unlock(&sk->callback_lock);
1258 + return ino;
1259 +}
1260 +
1261 +static inline struct dst_entry *
1262 +__sk_dst_get(struct sock *sk)
1263 +{
1264 + return sk->dst_cache;
1265 +}
1266 +
1267 +static inline struct dst_entry *
1268 +sk_dst_get(struct sock *sk)
1269 +{
1270 + struct dst_entry *dst;
1271 +
1272 + read_lock(&sk->dst_lock);
1273 + dst = sk->dst_cache;
1274 + if (dst)
1275 + dst_hold(dst);
1276 + read_unlock(&sk->dst_lock);
1277 + return dst;
1278 +}
1279 +
1280 +static inline void
1281 +__sk_dst_set(struct sock *sk, struct dst_entry *dst)
1282 +{
1283 + struct dst_entry *old_dst;
1284 +
1285 + old_dst = sk->dst_cache;
1286 + sk->dst_cache = dst;
1287 + dst_release(old_dst);
1288 +}
1289 +
1290 +static inline void
1291 +sk_dst_set(struct sock *sk, struct dst_entry *dst)
1292 +{
1293 + write_lock(&sk->dst_lock);
1294 + __sk_dst_set(sk, dst);
1295 + write_unlock(&sk->dst_lock);
1296 +}
1297 +
1298 +static inline void
1299 +__sk_dst_reset(struct sock *sk)
1300 +{
1301 + struct dst_entry *old_dst;
1302 +
1303 + old_dst = sk->dst_cache;
1304 + sk->dst_cache = NULL;
1305 + dst_release(old_dst);
1306 +}
1307 +
1308 +static inline void
1309 +sk_dst_reset(struct sock *sk)
1310 +{
1311 + write_lock(&sk->dst_lock);
1312 + __sk_dst_reset(sk);
1313 + write_unlock(&sk->dst_lock);
1314 +}
1315 +
1316 +static inline struct dst_entry *
1317 +__sk_dst_check(struct sock *sk, u32 cookie)
1318 +{
1319 + struct dst_entry *dst = sk->dst_cache;
1320 +
1321 + if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
1322 + sk->dst_cache = NULL;
1323 + return NULL;
1324 + }
1325 +
1326 + return dst;
1327 +}
1328 +
1329 +static inline struct dst_entry *
1330 +sk_dst_check(struct sock *sk, u32 cookie)
1331 +{
1332 + struct dst_entry *dst = sk_dst_get(sk);
1333 +
1334 + if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
1335 + sk_dst_reset(sk);
1336 + return NULL;
1337 + }
1338 +
1339 + return dst;
1340 +}
1341 +
1342 +
1343 +/*
1344 + * Queue a received datagram if it will fit. Stream and sequenced
1345 + * protocols can't normally use this as they need to fit buffers in
1346 + * and play with them.
1347 + *
1348 + * Inlined as it's very short and called for pretty much every
1349 + * packet ever received.
1350 + */
1351 +
1352 +static inline void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1353 +{
1354 + sock_hold(sk);
1355 + skb->sk = sk;
1356 + skb->destructor = sock_wfree;
1357 + atomic_add(skb->truesize, &sk->wmem_alloc);
1358 +}
1359 +
1360 +static inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
1361 +{
1362 + skb->sk = sk;
1363 + skb->destructor = sock_rfree;
1364 + atomic_add(skb->truesize, &sk->rmem_alloc);
1365 +}
1366 +
1367 +static inline int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1368 +{
1369 + int err = 0;
1370 + int skb_len;
1371 +
1372 + /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
1373 + number of warnings when compiling with -W --ANK
1374 + */
1375 + if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf) {
1376 + err = -ENOMEM;
1377 + goto out;
1378 + }
1379 +
1380 + /* It would be deadlock, if sock_queue_rcv_skb is used
1381 + with socket lock! We assume that users of this
1382 + function are lock free.
1383 + */
1384 + err = sk_filter(sk, skb, 1);
1385 + if (err)
1386 + goto out;
1387 +
1388 + skb->dev = NULL;
1389 + skb_set_owner_r(skb, sk);
1390 +
1391 + /* Cache the SKB length before we tack it onto the receive
1392 + * queue. Once it is added it no longer belongs to us and
1393 + * may be freed by other threads of control pulling packets
1394 + * from the queue.
1395 + */
1396 + skb_len = skb->len;
1397 +
1398 + skb_queue_tail(&sk->receive_queue, skb);
1399 + if (!sk->dead)
1400 + sk->data_ready(sk,skb_len);
1401 +out:
1402 + return err;
1403 +}
1404 +
1405 +static inline int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
1406 +{
1407 + /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
1408 + number of warnings when compiling with -W --ANK
1409 + */
1410 + if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf)
1411 + return -ENOMEM;
1412 + skb_set_owner_r(skb, sk);
1413 + skb_queue_tail(&sk->error_queue,skb);
1414 + if (!sk->dead)
1415 + sk->data_ready(sk,skb->len);
1416 + return 0;
1417 +}
1418 +
1419 +/*
1420 + * Recover an error report and clear atomically
1421 + */
1422 +
1423 +static inline int sock_error(struct sock *sk)
1424 +{
1425 + int err=xchg(&sk->err,0);
1426 + return -err;
1427 +}
1428 +
1429 +static inline unsigned long sock_wspace(struct sock *sk)
1430 +{
1431 + int amt = 0;
1432 +
1433 + if (!(sk->shutdown & SEND_SHUTDOWN)) {
1434 + amt = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1435 + if (amt < 0)
1436 + amt = 0;
1437 + }
1438 + return amt;
1439 +}
1440 +
1441 +static inline void sk_wake_async(struct sock *sk, int how, int band)
1442 +{
1443 + if (sk->socket && sk->socket->fasync_list)
1444 + sock_wake_async(sk->socket, how, band);
1445 +}
1446 +
1447 +#define SOCK_MIN_SNDBUF 2048
1448 +#define SOCK_MIN_RCVBUF 256
1449 +
1450 +/*
1451 + * Default write policy as shown to user space via poll/select/SIGIO
1452 + */
1453 +static inline int sock_writeable(struct sock *sk)
1454 +{
1455 + return atomic_read(&sk->wmem_alloc) < (sk->sndbuf / 2);
1456 +}
1457 +
1458 +static inline int gfp_any(void)
1459 +{
1460 + return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
1461 +}
1462 +
1463 +static inline long sock_rcvtimeo(struct sock *sk, int noblock)
1464 +{
1465 + return noblock ? 0 : sk->rcvtimeo;
1466 +}
1467 +
1468 +static inline long sock_sndtimeo(struct sock *sk, int noblock)
1469 +{
1470 + return noblock ? 0 : sk->sndtimeo;
1471 +}
1472 +
1473 +static inline int sock_rcvlowat(struct sock *sk, int waitall, int len)
1474 +{
1475 + return (waitall ? len : min_t(int, sk->rcvlowat, len)) ? : 1;
1476 +}
1477 +
1478 +/* Alas, with timeout socket operations are not restartable.
1479 + * Compare this to poll().
1480 + */
1481 +static inline int sock_intr_errno(long timeo)
1482 +{
1483 + return timeo == MAX_SCHEDULE_TIMEOUT ? -ERESTARTSYS : -EINTR;
1484 +}
1485 +
1486 +static __inline__ void
1487 +sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
1488 +{
1489 + if (sk->rcvtstamp)
1490 + put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP, sizeof(skb->stamp), &skb->stamp);
1491 + else
1492 + sk->stamp = skb->stamp;
1493 +}
1494 +
1495 +/*
1496 + * Enable debug/info messages
1497 + */
1498 +
1499 +#if 0
1500 +#define NETDEBUG(x) do { } while (0)
1501 +#else
1502 +#define NETDEBUG(x) do { x; } while (0)
1503 +#endif
1504 +
1505 +/*
1506 + * Macros for sleeping on a socket. Use them like this:
1507 + *
1508 + * SOCK_SLEEP_PRE(sk)
1509 + * if (condition)
1510 + * schedule();
1511 + * SOCK_SLEEP_POST(sk)
1512 + *
1513 + */
1514 +
1515 +#define SOCK_SLEEP_PRE(sk) { struct task_struct *tsk = current; \
1516 + DECLARE_WAITQUEUE(wait, tsk); \
1517 + tsk->state = TASK_INTERRUPTIBLE; \
1518 + add_wait_queue((sk)->sleep, &wait); \
1519 + release_sock(sk);
1520 +
1521 +#define SOCK_SLEEP_POST(sk) tsk->state = TASK_RUNNING; \
1522 + remove_wait_queue((sk)->sleep, &wait); \
1523 + lock_sock(sk); \
1524 + }
1525 +
1526 +extern __u32 sysctl_wmem_max;
1527 +extern __u32 sysctl_rmem_max;
1528 +
1529 +#endif /* _SOCK_H */
1530 diff --unified --recursive --new-file linux-2.4.30/net/Config.in linux-2.4.30-1-686-smp-ring3/net/Config.in
1531 --- linux-2.4.30/net/Config.in 2005-01-19 15:10:13.000000000 +0100
1532 +++ linux-2.4.30-1-686-smp-ring3/net/Config.in 2005-10-22 23:08:28.028051250 +0200
1533 @@ -15,6 +15,9 @@
1534 bool ' Network packet filtering debugging' CONFIG_NETFILTER_DEBUG
1535 fi
1536 bool 'Socket Filtering' CONFIG_FILTER
1537 +if [ "$CONFIG_EXPERIMENTAL" = "y" -a "$CONFIG_FILTER" = "y" ]; then
1538 + source net/ring/Config.in
1539 +fi
1540 tristate 'Unix domain sockets' CONFIG_UNIX
1541 bool 'TCP/IP networking' CONFIG_INET
1542 if [ "$CONFIG_INET" = "y" ]; then
1543 diff --unified --recursive --new-file linux-2.4.30/net/Config.in.ORG linux-2.4.30-1-686-smp-ring3/net/Config.in.ORG
1544 --- linux-2.4.30/net/Config.in.ORG 1970-01-01 01:00:00.000000000 +0100
1545 +++ linux-2.4.30-1-686-smp-ring3/net/Config.in.ORG 2005-10-22 23:08:28.020050750 +0200
1546 @@ -0,0 +1,107 @@
1547 +#
1548 +# Network configuration
1549 +#
1550 +mainmenu_option next_comment
1551 +comment 'Networking options'
1552 +tristate 'Packet socket' CONFIG_PACKET
1553 +if [ "$CONFIG_PACKET" != "n" ]; then
1554 + bool ' Packet socket: mmapped IO' CONFIG_PACKET_MMAP
1555 +fi
1556 +
1557 +tristate 'Netlink device emulation' CONFIG_NETLINK_DEV
1558 +
1559 +bool 'Network packet filtering (replaces ipchains)' CONFIG_NETFILTER
1560 +if [ "$CONFIG_NETFILTER" = "y" ]; then
1561 + bool ' Network packet filtering debugging' CONFIG_NETFILTER_DEBUG
1562 +fi
1563 +bool 'Socket Filtering' CONFIG_FILTER
1564 +tristate 'Unix domain sockets' CONFIG_UNIX
1565 +bool 'TCP/IP networking' CONFIG_INET
1566 +if [ "$CONFIG_INET" = "y" ]; then
1567 + source net/ipv4/Config.in
1568 + if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
1569 +# IPv6 as module will cause a CRASH if you try to unload it
1570 + tristate ' The IPv6 protocol (EXPERIMENTAL)' CONFIG_IPV6
1571 + if [ "$CONFIG_IPV6" != "n" ]; then
1572 + source net/ipv6/Config.in
1573 + fi
1574 + fi
1575 + if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
1576 + source net/khttpd/Config.in
1577 + fi
1578 + if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
1579 + source net/sctp/Config.in
1580 + fi
1581 +fi
1582 +if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
1583 + tristate 'Asynchronous Transfer Mode (ATM) (EXPERIMENTAL)' CONFIG_ATM
1584 + if [ "$CONFIG_ATM" = "y" -o "$CONFIG_ATM" = "m" ]; then
1585 + if [ "$CONFIG_INET" = "y" ]; then
1586 + dep_tristate ' Classical IP over ATM' CONFIG_ATM_CLIP $CONFIG_ATM
1587 + if [ "$CONFIG_ATM_CLIP" != "n" ]; then
1588 + bool ' Do NOT send ICMP if no neighbour' CONFIG_ATM_CLIP_NO_ICMP
1589 + fi
1590 + fi
1591 + dep_tristate ' LAN Emulation (LANE) support' CONFIG_ATM_LANE $CONFIG_ATM
1592 + if [ "$CONFIG_INET" = "y" -a "$CONFIG_ATM_LANE" != "n" ]; then
1593 + tristate ' Multi-Protocol Over ATM (MPOA) support' CONFIG_ATM_MPOA
1594 + fi
1595 + dep_tristate ' RFC1483/2684 Bridged protocols' CONFIG_ATM_BR2684 $CONFIG_ATM
1596 + if [ "$CONFIG_ATM_BR2684" != "n" ]; then
1597 + bool ' Per-VC IP filter kludge' CONFIG_ATM_BR2684_IPFILTER
1598 + fi
1599 + fi
1600 +fi
1601 +tristate '802.1Q VLAN Support' CONFIG_VLAN_8021Q
1602 +
1603 +comment ' '
1604 +tristate 'The IPX protocol' CONFIG_IPX
1605 +if [ "$CONFIG_IPX" != "n" ]; then
1606 + source net/ipx/Config.in
1607 +fi
1608 +
1609 +tristate 'Appletalk protocol support' CONFIG_ATALK
1610 +if [ "$CONFIG_ATALK" != "n" ]; then
1611 + source drivers/net/appletalk/Config.in
1612 +fi
1613 +
1614 +tristate 'DECnet Support' CONFIG_DECNET
1615 +if [ "$CONFIG_DECNET" != "n" ]; then
1616 + source net/decnet/Config.in
1617 +fi
1618 +dep_tristate '802.1d Ethernet Bridging' CONFIG_BRIDGE $CONFIG_INET
1619 +if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
1620 + tristate 'CCITT X.25 Packet Layer (EXPERIMENTAL)' CONFIG_X25
1621 + tristate 'LAPB Data Link Driver (EXPERIMENTAL)' CONFIG_LAPB
1622 + bool '802.2 LLC (EXPERIMENTAL)' CONFIG_LLC
1623 + bool 'Frame Diverter (EXPERIMENTAL)' CONFIG_NET_DIVERT
1624 +# if [ "$CONFIG_LLC" = "y" ]; then
1625 +# bool ' Netbeui (EXPERIMENTAL)' CONFIG_NETBEUI
1626 +# fi
1627 + if [ "$CONFIG_INET" = "y" ]; then
1628 + tristate 'Acorn Econet/AUN protocols (EXPERIMENTAL)' CONFIG_ECONET
1629 + if [ "$CONFIG_ECONET" != "n" ]; then
1630 + bool ' AUN over UDP' CONFIG_ECONET_AUNUDP
1631 + bool ' Native Econet' CONFIG_ECONET_NATIVE
1632 + fi
1633 + fi
1634 + tristate 'WAN router' CONFIG_WAN_ROUTER
1635 + bool 'Fast switching (read help!)' CONFIG_NET_FASTROUTE
1636 + bool 'Forwarding between high speed interfaces' CONFIG_NET_HW_FLOWCONTROL
1637 +fi
1638 +
1639 +mainmenu_option next_comment
1640 +comment 'QoS and/or fair queueing'
1641 +bool 'QoS and/or fair queueing' CONFIG_NET_SCHED
1642 +if [ "$CONFIG_NET_SCHED" = "y" ]; then
1643 + source net/sched/Config.in
1644 +fi
1645 +#bool 'Network code profiler' CONFIG_NET_PROFILE
1646 +endmenu
1647 +
1648 +mainmenu_option next_comment
1649 +comment 'Network testing'
1650 +dep_tristate 'Packet Generator (USE WITH CAUTION)' CONFIG_NET_PKTGEN $CONFIG_PROC_FS
1651 +endmenu
1652 +
1653 +endmenu
1654 diff --unified --recursive --new-file linux-2.4.30/net/Makefile linux-2.4.30-1-686-smp-ring3/net/Makefile
1655 --- linux-2.4.30/net/Makefile 2004-08-08 01:26:06.000000000 +0200
1656 +++ linux-2.4.30-1-686-smp-ring3/net/Makefile 2005-10-22 23:08:27.928045000 +0200
1657 @@ -7,7 +7,7 @@
1658
1659 O_TARGET := network.o
1660
1661 -mod-subdirs := ipv4/netfilter ipv6/netfilter ipx irda bluetooth atm netlink sched core sctp 802
1662 +mod-subdirs := ipv4/netfilter ipv6/netfilter ipx irda bluetooth atm netlink sched core sctp 802 ring
1663 export-objs := netsyms.o
1664
1665 subdir-y := core ethernet
1666 @@ -46,6 +46,7 @@
1667 subdir-$(CONFIG_DECNET) += decnet
1668 subdir-$(CONFIG_ECONET) += econet
1669 subdir-$(CONFIG_VLAN_8021Q) += 8021q
1670 +subdir-$(CONFIG_RING) += ring
1671
1672 ifeq ($(CONFIG_NETFILTER),y)
1673 mod-subdirs += ipv4/ipvs
1674 diff --unified --recursive --new-file linux-2.4.30/net/Makefile.ORG linux-2.4.30-1-686-smp-ring3/net/Makefile.ORG
1675 --- linux-2.4.30/net/Makefile.ORG 1970-01-01 01:00:00.000000000 +0100
1676 +++ linux-2.4.30-1-686-smp-ring3/net/Makefile.ORG 2005-10-22 23:08:27.916044250 +0200
1677 @@ -0,0 +1,61 @@
1678 +#
1679 +# Makefile for the linux networking.
1680 +#
1681 +# 2 Sep 2000, Christoph Hellwig <hch@infradead.org>
1682 +# Rewritten to use lists instead of if-statements.
1683 +#
1684 +
1685 +O_TARGET := network.o
1686 +
1687 +mod-subdirs := ipv4/netfilter ipv6/netfilter ipx irda bluetooth atm netlink sched core sctp 802
1688 +export-objs := netsyms.o
1689 +
1690 +subdir-y := core ethernet
1691 +subdir-m := ipv4 # hum?
1692 +
1693 +
1694 +subdir-$(CONFIG_NET) += 802 sched netlink
1695 +subdir-$(CONFIG_IPV6) += ipv6
1696 +subdir-$(CONFIG_INET) += ipv4
1697 +subdir-$(CONFIG_NETFILTER) += ipv4/netfilter
1698 +subdir-$(CONFIG_UNIX) += unix
1699 +subdir-$(CONFIG_IP_SCTP) += sctp
1700 +
1701 +ifneq ($(CONFIG_IPV6),n)
1702 +ifneq ($(CONFIG_IPV6),)
1703 +subdir-$(CONFIG_NETFILTER) += ipv6/netfilter
1704 +endif
1705 +endif
1706 +
1707 +subdir-$(CONFIG_KHTTPD) += khttpd
1708 +subdir-$(CONFIG_PACKET) += packet
1709 +subdir-$(CONFIG_NET_SCHED) += sched
1710 +subdir-$(CONFIG_BRIDGE) += bridge
1711 +subdir-$(CONFIG_IPX) += ipx
1712 +subdir-$(CONFIG_ATALK) += appletalk
1713 +subdir-$(CONFIG_WAN_ROUTER) += wanrouter
1714 +subdir-$(CONFIG_X25) += x25
1715 +subdir-$(CONFIG_LAPB) += lapb
1716 +subdir-$(CONFIG_NETROM) += netrom
1717 +subdir-$(CONFIG_ROSE) += rose
1718 +subdir-$(CONFIG_AX25) += ax25
1719 +subdir-$(CONFIG_IRDA) += irda
1720 +subdir-$(CONFIG_BLUEZ) += bluetooth
1721 +subdir-$(CONFIG_SUNRPC) += sunrpc
1722 +subdir-$(CONFIG_ATM) += atm
1723 +subdir-$(CONFIG_DECNET) += decnet
1724 +subdir-$(CONFIG_ECONET) += econet
1725 +subdir-$(CONFIG_VLAN_8021Q) += 8021q
1726 +
1727 +ifeq ($(CONFIG_NETFILTER),y)
1728 + mod-subdirs += ipv4/ipvs
1729 + subdir-$(CONFIG_IP_VS) += ipv4/ipvs
1730 +endif
1731 +
1732 +obj-y := socket.o $(join $(subdir-y), $(patsubst %,/%.o,$(notdir $(subdir-y))))
1733 +ifeq ($(CONFIG_NET),y)
1734 +obj-$(CONFIG_MODULES) += netsyms.o
1735 +obj-$(CONFIG_SYSCTL) += sysctl_net.o
1736 +endif
1737 +
1738 +include $(TOPDIR)/Rules.make
1739 diff --unified --recursive --new-file linux-2.4.30/net/core/dev.c linux-2.4.30-1-686-smp-ring3/net/core/dev.c
1740 --- linux-2.4.30/net/core/dev.c 2005-04-04 03:42:20.000000000 +0200
1741 +++ linux-2.4.30-1-686-smp-ring3/net/core/dev.c 2005-10-22 23:08:27.900043250 +0200
1742 @@ -104,6 +104,56 @@
1743 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
1744 #include <net/iw_handler.h>
1745 #endif /* CONFIG_NET_RADIO || CONFIG_NET_PCMCIA_RADIO */
1746 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
1747 +
1748 +/* #define RING_DEBUG */
1749 +
1750 +#include <linux/ring.h>
1751 +#include <linux/version.h>
1752 +
1753 +static handle_ring_skb ring_handler = NULL;
1754 +
1755 +handle_ring_skb get_skb_ring_handler() { return(ring_handler); }
1756 +
1757 +void set_skb_ring_handler(handle_ring_skb the_handler) {
1758 + ring_handler = the_handler;
1759 +}
1760 +
1761 +void do_skb_ring_handler(struct sk_buff *skb,
1762 + u_char recv_packet, u_char real_skb) {
1763 + if(ring_handler)
1764 + ring_handler(skb, recv_packet, real_skb);
1765 +}
1766 +
1767 +/* ******************* */
1768 +
1769 +static handle_ring_buffer buffer_ring_handler = NULL;
1770 +
1771 +handle_ring_buffer get_buffer_ring_handler() { return(buffer_ring_handler); }
1772 +
1773 +void set_buffer_ring_handler(handle_ring_buffer the_handler) {
1774 + buffer_ring_handler = the_handler;
1775 +}
1776 +
1777 +int do_buffer_ring_handler(struct net_device *dev, char *data, int len) {
1778 + if(buffer_ring_handler) {
1779 + buffer_ring_handler(dev, data, len);
1780 + return(1);
1781 + } else
1782 + return(0);
1783 +}
1784 +
1785 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
1786 +EXPORT_SYMBOL(get_skb_ring_handler);
1787 +EXPORT_SYMBOL(set_skb_ring_handler);
1788 +EXPORT_SYMBOL(do_skb_ring_handler);
1789 +
1790 +EXPORT_SYMBOL(get_buffer_ring_handler);
1791 +EXPORT_SYMBOL(set_buffer_ring_handler);
1792 +EXPORT_SYMBOL(do_buffer_ring_handler);
1793 +#endif
1794 +
1795 +#endif
1796 #ifdef CONFIG_PLIP
1797 extern int plip_init(void);
1798 #endif
1799 @@ -1066,6 +1116,10 @@
1800 return -ENOMEM;
1801 }
1802
1803 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
1804 + if(ring_handler) ring_handler(skb, 0, 1);
1805 +#endif /* CONFIG_RING */
1806 +
1807 /* Grab device queue */
1808 spin_lock_bh(&dev->queue_lock);
1809 q = dev->qdisc;
1810 @@ -1278,6 +1332,13 @@
1811 struct softnet_data *queue;
1812 unsigned long flags;
1813
1814 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
1815 + if(ring_handler && ring_handler(skb, 1, 1)) {
1816 + /* The packet has been copied into a ring */
1817 + return(NET_RX_SUCCESS);
1818 + }
1819 +#endif /* CONFIG_RING */
1820 +
1821 if (skb->stamp.tv_sec == 0)
1822 do_gettimeofday(&skb->stamp);
1823
1824 @@ -1464,6 +1525,13 @@
1825 int ret = NET_RX_DROP;
1826 unsigned short type;
1827
1828 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
1829 + if(ring_handler && ring_handler(skb, 1, 1)) {
1830 + /* The packet has been copied into a ring */
1831 + return(NET_RX_SUCCESS);
1832 + }
1833 +#endif /* CONFIG_RING */
1834 +
1835 if (skb->stamp.tv_sec == 0)
1836 do_gettimeofday(&skb->stamp);
1837
1838 diff --unified --recursive --new-file linux-2.4.30/net/core/dev.c.ORG linux-2.4.30-1-686-smp-ring3/net/core/dev.c.ORG
1839 --- linux-2.4.30/net/core/dev.c.ORG 1970-01-01 01:00:00.000000000 +0100
1840 +++ linux-2.4.30-1-686-smp-ring3/net/core/dev.c.ORG 2005-10-22 23:08:27.472016500 +0200
1841 @@ -0,0 +1,2926 @@
1842 +/*
1843 + * NET3 Protocol independent device support routines.
1844 + *
1845 + * This program is free software; you can redistribute it and/or
1846 + * modify it under the terms of the GNU General Public License
1847 + * as published by the Free Software Foundation; either version
1848 + * 2 of the License, or (at your option) any later version.
1849 + *
1850 + * Derived from the non IP parts of dev.c 1.0.19
1851 + * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
1852 + * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
1853 + * Mark Evans, <evansmp@uhura.aston.ac.uk>
1854 + *
1855 + * Additional Authors:
1856 + * Florian la Roche <rzsfl@rz.uni-sb.de>
1857 + * Alan Cox <gw4pts@gw4pts.ampr.org>
1858 + * David Hinds <dahinds@users.sourceforge.net>
1859 + * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
1860 + * Adam Sulmicki <adam@cfar.umd.edu>
1861 + * Pekka Riikonen <priikone@poesidon.pspt.fi>
1862 + *
1863 + * Changes:
1864 + * D.J. Barrow : Fixed bug where dev->refcnt gets set to 2
1865 + * if register_netdev gets called before
1866 + * net_dev_init & also removed a few lines
1867 + * of code in the process.
1868 + * Alan Cox : device private ioctl copies fields back.
1869 + * Alan Cox : Transmit queue code does relevant stunts to
1870 + * keep the queue safe.
1871 + * Alan Cox : Fixed double lock.
1872 + * Alan Cox : Fixed promisc NULL pointer trap
1873 + * ???????? : Support the full private ioctl range
1874 + * Alan Cox : Moved ioctl permission check into drivers
1875 + * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
1876 + * Alan Cox : 100 backlog just doesn't cut it when
1877 + * you start doing multicast video 8)
1878 + * Alan Cox : Rewrote net_bh and list manager.
1879 + * Alan Cox : Fix ETH_P_ALL echoback lengths.
1880 + * Alan Cox : Took out transmit every packet pass
1881 + * Saved a few bytes in the ioctl handler
1882 + * Alan Cox : Network driver sets packet type before calling netif_rx. Saves
1883 + * a function call a packet.
1884 + * Alan Cox : Hashed net_bh()
1885 + * Richard Kooijman: Timestamp fixes.
1886 + * Alan Cox : Wrong field in SIOCGIFDSTADDR
1887 + * Alan Cox : Device lock protection.
1888 + * Alan Cox : Fixed nasty side effect of device close changes.
1889 + * Rudi Cilibrasi : Pass the right thing to set_mac_address()
1890 + * Dave Miller : 32bit quantity for the device lock to make it work out
1891 + * on a Sparc.
1892 + * Bjorn Ekwall : Added KERNELD hack.
1893 + * Alan Cox : Cleaned up the backlog initialise.
1894 + * Craig Metz : SIOCGIFCONF fix if space for under
1895 + * 1 device.
1896 + * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
1897 + * is no device open function.
1898 + * Andi Kleen : Fix error reporting for SIOCGIFCONF
1899 + * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
1900 + * Cyrus Durgin : Cleaned for KMOD
1901 + * Adam Sulmicki : Bug Fix : Network Device Unload
1902 + * A network device unload needs to purge
1903 + * the backlog queue.
1904 + * Paul Rusty Russell : SIOCSIFNAME
1905 + * Pekka Riikonen : Netdev boot-time settings code
1906 + * Andrew Morton : Make unregister_netdevice wait indefinitely on dev->refcnt
1907 + * J Hadi Salim : - Backlog queue sampling
1908 + * - netif_rx() feedback
1909 + */
1910 +
1911 +#include <asm/uaccess.h>
1912 +#include <asm/system.h>
1913 +#include <asm/bitops.h>
1914 +#include <linux/config.h>
1915 +#include <linux/types.h>
1916 +#include <linux/kernel.h>
1917 +#include <linux/sched.h>
1918 +#include <linux/string.h>
1919 +#include <linux/mm.h>
1920 +#include <linux/socket.h>
1921 +#include <linux/sockios.h>
1922 +#include <linux/errno.h>
1923 +#include <linux/interrupt.h>
1924 +#include <linux/if_ether.h>
1925 +#include <linux/netdevice.h>
1926 +#include <linux/etherdevice.h>
1927 +#include <linux/notifier.h>
1928 +#include <linux/skbuff.h>
1929 +#include <linux/brlock.h>
1930 +#include <net/sock.h>
1931 +#include <linux/rtnetlink.h>
1932 +#include <linux/proc_fs.h>
1933 +#include <linux/stat.h>
1934 +#include <linux/if_bridge.h>
1935 +#include <linux/divert.h>
1936 +#include <net/dst.h>
1937 +#include <net/pkt_sched.h>
1938 +#include <net/profile.h>
1939 +#include <net/checksum.h>
1940 +#include <linux/highmem.h>
1941 +#include <linux/init.h>
1942 +#include <linux/kmod.h>
1943 +#include <linux/module.h>
1944 +#if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
1945 +#include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
1946 +#include <net/iw_handler.h>
1947 +#endif /* CONFIG_NET_RADIO || CONFIG_NET_PCMCIA_RADIO */
1948 +#ifdef CONFIG_PLIP
1949 +extern int plip_init(void);
1950 +#endif
1951 +
1952 +
1953 +/* This define, if set, will randomly drop a packet when congestion
1954 + * is more than moderate. It helps fairness in the multi-interface
1955 + * case when one of them is a hog, but it kills performance for the
1956 + * single interface case so it is off now by default.
1957 + */
1958 +#undef RAND_LIE
1959 +
1960 +/* Setting this will sample the queue lengths and thus congestion
1961 + * via a timer instead of as each packet is received.
1962 + */
1963 +#undef OFFLINE_SAMPLE
1964 +
1965 +NET_PROFILE_DEFINE(dev_queue_xmit)
1966 +NET_PROFILE_DEFINE(softnet_process)
1967 +
1968 +const char *if_port_text[] = {
1969 + "unknown",
1970 + "BNC",
1971 + "10baseT",
1972 + "AUI",
1973 + "100baseT",
1974 + "100baseTX",
1975 + "100baseFX"
1976 +};
1977 +
1978 +/*
1979 + * The list of packet types we will receive (as opposed to discard)
1980 + * and the routines to invoke.
1981 + *
1982 + * Why 16. Because with 16 the only overlap we get on a hash of the
1983 + * low nibble of the protocol value is RARP/SNAP/X.25.
1984 + *
1985 + * NOTE: That is no longer true with the addition of VLAN tags. Not
1986 + * sure which should go first, but I bet it won't make much
1987 + * difference if we are running VLANs. The good news is that
1988 + * this protocol won't be in the list unless compiled in, so
1989 + * the average user (w/out VLANs) will not be adversly affected.
1990 + * --BLG
1991 + *
1992 + * 0800 IP
1993 + * 8100 802.1Q VLAN
1994 + * 0001 802.3
1995 + * 0002 AX.25
1996 + * 0004 802.2
1997 + * 8035 RARP
1998 + * 0005 SNAP
1999 + * 0805 X.25
2000 + * 0806 ARP
2001 + * 8137 IPX
2002 + * 0009 Localtalk
2003 + * 86DD IPv6
2004 + */
2005 +
2006 +static struct packet_type *ptype_base[16]; /* 16 way hashed list */
2007 +static struct packet_type *ptype_all = NULL; /* Taps */
2008 +
2009 +#ifdef OFFLINE_SAMPLE
2010 +static void sample_queue(unsigned long dummy);
2011 +static struct timer_list samp_timer = { function: sample_queue };
2012 +#endif
2013 +
2014 +#ifdef CONFIG_HOTPLUG
2015 +static int net_run_sbin_hotplug(struct net_device *dev, char *action);
2016 +#else
2017 +#define net_run_sbin_hotplug(dev, action) ({ 0; })
2018 +#endif
2019 +
2020 +/*
2021 + * Our notifier list
2022 + */
2023 +
2024 +static struct notifier_block *netdev_chain=NULL;
2025 +
2026 +/*
2027 + * Device drivers call our routines to queue packets here. We empty the
2028 + * queue in the local softnet handler.
2029 + */
2030 +struct softnet_data softnet_data[NR_CPUS] __cacheline_aligned;
2031 +
2032 +#ifdef CONFIG_NET_FASTROUTE
2033 +int netdev_fastroute;
2034 +int netdev_fastroute_obstacles;
2035 +#endif
2036 +
2037 +
2038 +/******************************************************************************************
2039 +
2040 + Protocol management and registration routines
2041 +
2042 +*******************************************************************************************/
2043 +
2044 +/*
2045 + * For efficiency
2046 + */
2047 +
2048 +int netdev_nit=0;
2049 +
2050 +/*
2051 + * Add a protocol ID to the list. Now that the input handler is
2052 + * smarter we can dispense with all the messy stuff that used to be
2053 + * here.
2054 + *
2055 + * BEWARE!!! Protocol handlers, mangling input packets,
2056 + * MUST BE last in hash buckets and checking protocol handlers
2057 + * MUST start from promiscous ptype_all chain in net_bh.
2058 + * It is true now, do not change it.
2059 + * Explantion follows: if protocol handler, mangling packet, will
2060 + * be the first on list, it is not able to sense, that packet
2061 + * is cloned and should be copied-on-write, so that it will
2062 + * change it and subsequent readers will get broken packet.
2063 + * --ANK (980803)
2064 + */
2065 +
2066 +/**
2067 + * dev_add_pack - add packet handler
2068 + * @pt: packet type declaration
2069 + *
2070 + * Add a protocol handler to the networking stack. The passed &packet_type
2071 + * is linked into kernel lists and may not be freed until it has been
2072 + * removed from the kernel lists.
2073 + */
2074 +
2075 +void dev_add_pack(struct packet_type *pt)
2076 +{
2077 + int hash;
2078 +
2079 + br_write_lock_bh(BR_NETPROTO_LOCK);
2080 +
2081 +#ifdef CONFIG_NET_FASTROUTE
2082 + /* Hack to detect packet socket */
2083 + if ((pt->data) && ((int)(pt->data)!=1)) {
2084 + netdev_fastroute_obstacles++;
2085 + dev_clear_fastroute(pt->dev);
2086 + }
2087 +#endif
2088 + if (pt->type == htons(ETH_P_ALL)) {
2089 + netdev_nit++;
2090 + pt->next=ptype_all;
2091 + ptype_all=pt;
2092 + } else {
2093 + hash=ntohs(pt->type)&15;
2094 + pt->next = ptype_base[hash];
2095 + ptype_base[hash] = pt;
2096 + }
2097 + br_write_unlock_bh(BR_NETPROTO_LOCK);
2098 +}
2099 +
2100 +
2101 +/**
2102 + * dev_remove_pack - remove packet handler
2103 + * @pt: packet type declaration
2104 + *
2105 + * Remove a protocol handler that was previously added to the kernel
2106 + * protocol handlers by dev_add_pack(). The passed &packet_type is removed
2107 + * from the kernel lists and can be freed or reused once this function
2108 + * returns.
2109 + */
2110 +
2111 +void dev_remove_pack(struct packet_type *pt)
2112 +{
2113 + struct packet_type **pt1;
2114 +
2115 + br_write_lock_bh(BR_NETPROTO_LOCK);
2116 +
2117 + if (pt->type == htons(ETH_P_ALL)) {
2118 + netdev_nit--;
2119 + pt1=&ptype_all;
2120 + } else {
2121 + pt1=&ptype_base[ntohs(pt->type)&15];
2122 + }
2123 +
2124 + for (; (*pt1) != NULL; pt1 = &((*pt1)->next)) {
2125 + if (pt == (*pt1)) {
2126 + *pt1 = pt->next;
2127 +#ifdef CONFIG_NET_FASTROUTE
2128 + if (pt->data)
2129 + netdev_fastroute_obstacles--;
2130 +#endif
2131 + br_write_unlock_bh(BR_NETPROTO_LOCK);
2132 + return;
2133 + }
2134 + }
2135 + br_write_unlock_bh(BR_NETPROTO_LOCK);
2136 + printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
2137 +}
2138 +
2139 +/******************************************************************************
2140 +
2141 + Device Boot-time Settings Routines
2142 +
2143 +*******************************************************************************/
2144 +
2145 +/* Boot time configuration table */
2146 +static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
2147 +
2148 +/**
2149 + * netdev_boot_setup_add - add new setup entry
2150 + * @name: name of the device
2151 + * @map: configured settings for the device
2152 + *
2153 + * Adds new setup entry to the dev_boot_setup list. The function
2154 + * returns 0 on error and 1 on success. This is a generic routine to
2155 + * all netdevices.
2156 + */
2157 +int netdev_boot_setup_add(char *name, struct ifmap *map)
2158 +{
2159 + struct netdev_boot_setup *s;
2160 + int i;
2161 +
2162 + s = dev_boot_setup;
2163 + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
2164 + if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
2165 + memset(s[i].name, 0, sizeof(s[i].name));
2166 + strcpy(s[i].name, name);
2167 + memcpy(&s[i].map, map, sizeof(s[i].map));
2168 + break;
2169 + }
2170 + }
2171 +
2172 + if (i >= NETDEV_BOOT_SETUP_MAX)
2173 + return 0;
2174 +
2175 + return 1;
2176 +}
2177 +
2178 +/**
2179 + * netdev_boot_setup_check - check boot time settings
2180 + * @dev: the netdevice
2181 + *
2182 + * Check boot time settings for the device.
2183 + * The found settings are set for the device to be used
2184 + * later in the device probing.
2185 + * Returns 0 if no settings found, 1 if they are.
2186 + */
2187 +int netdev_boot_setup_check(struct net_device *dev)
2188 +{
2189 + struct netdev_boot_setup *s;
2190 + int i;
2191 +
2192 + s = dev_boot_setup;
2193 + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
2194 + if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
2195 + !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
2196 + dev->irq = s[i].map.irq;
2197 + dev->base_addr = s[i].map.base_addr;
2198 + dev->mem_start = s[i].map.mem_start;
2199 + dev->mem_end = s[i].map.mem_end;
2200 + return 1;
2201 + }
2202 + }
2203 + return 0;
2204 +}
2205 +
2206 +/*
2207 + * Saves at boot time configured settings for any netdevice.
2208 + */
2209 +int __init netdev_boot_setup(char *str)
2210 +{
2211 + int ints[5];
2212 + struct ifmap map;
2213 +
2214 + str = get_options(str, ARRAY_SIZE(ints), ints);
2215 + if (!str || !*str)
2216 + return 0;
2217 +
2218 + /* Save settings */
2219 + memset(&map, 0, sizeof(map));
2220 + if (ints[0] > 0)
2221 + map.irq = ints[1];
2222 + if (ints[0] > 1)
2223 + map.base_addr = ints[2];
2224 + if (ints[0] > 2)
2225 + map.mem_start = ints[3];
2226 + if (ints[0] > 3)
2227 + map.mem_end = ints[4];
2228 +
2229 + /* Add new entry to the list */
2230 + return netdev_boot_setup_add(str, &map);
2231 +}
2232 +
2233 +__setup("netdev=", netdev_boot_setup);
2234 +
2235 +/*****************************************************************************************
2236 +
2237 + Device Interface Subroutines
2238 +
2239 +******************************************************************************************/
2240 +
2241 +/**
2242 + * __dev_get_by_name - find a device by its name
2243 + * @name: name to find
2244 + *
2245 + * Find an interface by name. Must be called under RTNL semaphore
2246 + * or @dev_base_lock. If the name is found a pointer to the device
2247 + * is returned. If the name is not found then %NULL is returned. The
2248 + * reference counters are not incremented so the caller must be
2249 + * careful with locks.
2250 + */
2251 +
2252 +
2253 +struct net_device *__dev_get_by_name(const char *name)
2254 +{
2255 + struct net_device *dev;
2256 +
2257 + for (dev = dev_base; dev != NULL; dev = dev->next) {
2258 + if (strncmp(dev->name, name, IFNAMSIZ) == 0)
2259 + return dev;
2260 + }
2261 + return NULL;
2262 +}
2263 +
2264 +/**
2265 + * dev_get_by_name - find a device by its name
2266 + * @name: name to find
2267 + *
2268 + * Find an interface by name. This can be called from any
2269 + * context and does its own locking. The returned handle has
2270 + * the usage count incremented and the caller must use dev_put() to
2271 + * release it when it is no longer needed. %NULL is returned if no
2272 + * matching device is found.
2273 + */
2274 +
2275 +struct net_device *dev_get_by_name(const char *name)
2276 +{
2277 + struct net_device *dev;
2278 +
2279 + read_lock(&dev_base_lock);
2280 + dev = __dev_get_by_name(name);
2281 + if (dev)
2282 + dev_hold(dev);
2283 + read_unlock(&dev_base_lock);
2284 + return dev;
2285 +}
2286 +
2287 +/*
2288 + Return value is changed to int to prevent illegal usage in future.
2289 + It is still legal to use to check for device existence.
2290 +
2291 + User should understand, that the result returned by this function
2292 + is meaningless, if it was not issued under rtnl semaphore.
2293 + */
2294 +
2295 +/**
2296 + * dev_get - test if a device exists
2297 + * @name: name to test for
2298 + *
2299 + * Test if a name exists. Returns true if the name is found. In order
2300 + * to be sure the name is not allocated or removed during the test the
2301 + * caller must hold the rtnl semaphore.
2302 + *
2303 + * This function primarily exists for back compatibility with older
2304 + * drivers.
2305 + */
2306 +
2307 +int dev_get(const char *name)
2308 +{
2309 + struct net_device *dev;
2310 +
2311 + read_lock(&dev_base_lock);
2312 + dev = __dev_get_by_name(name);
2313 + read_unlock(&dev_base_lock);
2314 + return dev != NULL;
2315 +}
2316 +
2317 +/**
2318 + * __dev_get_by_index - find a device by its ifindex
2319 + * @ifindex: index of device
2320 + *
2321 + * Search for an interface by index. Returns %NULL if the device
2322 + * is not found or a pointer to the device. The device has not
2323 + * had its reference counter increased so the caller must be careful
2324 + * about locking. The caller must hold either the RTNL semaphore
2325 + * or @dev_base_lock.
2326 + */
2327 +
2328 +struct net_device * __dev_get_by_index(int ifindex)
2329 +{
2330 + struct net_device *dev;
2331 +
2332 + for (dev = dev_base; dev != NULL; dev = dev->next) {
2333 + if (dev->ifindex == ifindex)
2334 + return dev;
2335 + }
2336 + return NULL;
2337 +}
2338 +
2339 +
2340 +/**
2341 + * dev_get_by_index - find a device by its ifindex
2342 + * @ifindex: index of device
2343 + *
2344 + * Search for an interface by index. Returns NULL if the device
2345 + * is not found or a pointer to the device. The device returned has
2346 + * had a reference added and the pointer is safe until the user calls
2347 + * dev_put to indicate they have finished with it.
2348 + */
2349 +
2350 +struct net_device * dev_get_by_index(int ifindex)
2351 +{
2352 + struct net_device *dev;
2353 +
2354 + read_lock(&dev_base_lock);
2355 + dev = __dev_get_by_index(ifindex);
2356 + if (dev)
2357 + dev_hold(dev);
2358 + read_unlock(&dev_base_lock);
2359 + return dev;
2360 +}
2361 +
2362 +/**
2363 + * dev_getbyhwaddr - find a device by its hardware address
2364 + * @type: media type of device
2365 + * @ha: hardware address
2366 + *
2367 + * Search for an interface by MAC address. Returns NULL if the device
2368 + * is not found or a pointer to the device. The caller must hold the
2369 + * rtnl semaphore. The returned device has not had its ref count increased
2370 + * and the caller must therefore be careful about locking
2371 + *
2372 + * BUGS:
2373 + * If the API was consistent this would be __dev_get_by_hwaddr
2374 + */
2375 +
2376 +struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
2377 +{
2378 + struct net_device *dev;
2379 +
2380 + ASSERT_RTNL();
2381 +
2382 + for (dev = dev_base; dev != NULL; dev = dev->next) {
2383 + if (dev->type == type &&
2384 + memcmp(dev->dev_addr, ha, dev->addr_len) == 0)
2385 + return dev;
2386 + }
2387 + return NULL;
2388 +}
2389 +
2390 +/**
2391 + * dev_get_by_flags - find any device with given flags
2392 + * @if_flags: IFF_* values
2393 + * @mask: bitmask of bits in if_flags to check
2394 + *
2395 + * Search for any interface with the given flags. Returns NULL if a device
2396 + * is not found or a pointer to the device. The device returned has
2397 + * had a reference added and the pointer is safe until the user calls
2398 + * dev_put to indicate they have finished with it.
2399 + */
2400 +
2401 +struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
2402 +{
2403 + struct net_device *dev;
2404 +
2405 + read_lock(&dev_base_lock);
2406 + dev = __dev_get_by_flags(if_flags, mask);
2407 + if (dev)
2408 + dev_hold(dev);
2409 + read_unlock(&dev_base_lock);
2410 + return dev;
2411 +}
2412 +
2413 +/**
2414 + * __dev_get_by_flags - find any device with given flags
2415 + * @if_flags: IFF_* values
2416 + * @mask: bitmask of bits in if_flags to check
2417 + *
2418 + * Search for any interface with the given flags. Returns NULL if a device
2419 + * is not found or a pointer to the device. The caller must hold either
2420 + * the RTNL semaphore or @dev_base_lock.
2421 + */
2422 +
2423 +struct net_device *__dev_get_by_flags(unsigned short if_flags, unsigned short mask)
2424 +{
2425 + struct net_device *dev;
2426 +
2427 + for (dev = dev_base; dev != NULL; dev = dev->next) {
2428 + if (((dev->flags ^ if_flags) & mask) == 0)
2429 + return dev;
2430 + }
2431 + return NULL;
2432 +}
2433 +
2434 +/**
2435 + * dev_alloc_name - allocate a name for a device
2436 + * @dev: device
2437 + * @name: name format string
2438 + *
2439 + * Passed a format string - eg "lt%d" it will try and find a suitable
2440 + * id. Not efficient for many devices, not called a lot. The caller
2441 + * must hold the dev_base or rtnl lock while allocating the name and
2442 + * adding the device in order to avoid duplicates. Returns the number
2443 + * of the unit assigned or a negative errno code.
2444 + */
2445 +
2446 +int dev_alloc_name(struct net_device *dev, const char *name)
2447 +{
2448 + int i;
2449 + char buf[32];
2450 + char *p;
2451 +
2452 + /*
2453 + * Verify the string as this thing may have come from
2454 + * the user. There must be either one "%d" and no other "%"
2455 + * characters, or no "%" characters at all.
2456 + */
2457 + p = strchr(name, '%');
2458 + if (p && (p[1] != 'd' || strchr(p+2, '%')))
2459 + return -EINVAL;
2460 +
2461 + /*
2462 + * If you need over 100 please also fix the algorithm...
2463 + */
2464 + for (i = 0; i < 100; i++) {
2465 + snprintf(buf,sizeof(buf),name,i);
2466 + if (__dev_get_by_name(buf) == NULL) {
2467 + strcpy(dev->name, buf);
2468 + return i;
2469 + }
2470 + }
2471 + return -ENFILE; /* Over 100 of the things .. bail out! */
2472 +}
2473 +
2474 +/**
2475 + * dev_alloc - allocate a network device and name
2476 + * @name: name format string
2477 + * @err: error return pointer
2478 + *
2479 + * Passed a format string, eg. "lt%d", it will allocate a network device
2480 + * and space for the name. %NULL is returned if no memory is available.
2481 + * If the allocation succeeds then the name is assigned and the
2482 + * device pointer returned. %NULL is returned if the name allocation
2483 + * failed. The cause of an error is returned as a negative errno code
2484 + * in the variable @err points to.
2485 + *
2486 + * The caller must hold the @dev_base or RTNL locks when doing this in
2487 + * order to avoid duplicate name allocations.
2488 + */
2489 +
2490 +struct net_device *dev_alloc(const char *name, int *err)
2491 +{
2492 + struct net_device *dev=kmalloc(sizeof(struct net_device), GFP_KERNEL);
2493 + if (dev == NULL) {
2494 + *err = -ENOBUFS;
2495 + return NULL;
2496 + }
2497 + memset(dev, 0, sizeof(struct net_device));
2498 + *err = dev_alloc_name(dev, name);
2499 + if (*err < 0) {
2500 + kfree(dev);
2501 + return NULL;
2502 + }
2503 + return dev;
2504 +}
2505 +
2506 +/**
2507 + * netdev_state_change - device changes state
2508 + * @dev: device to cause notification
2509 + *
2510 + * Called to indicate a device has changed state. This function calls
2511 + * the notifier chains for netdev_chain and sends a NEWLINK message
2512 + * to the routing socket.
2513 + */
2514 +
2515 +void netdev_state_change(struct net_device *dev)
2516 +{
2517 + if (dev->flags&IFF_UP) {
2518 + notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
2519 + rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2520 + }
2521 +}
2522 +
2523 +
2524 +#ifdef CONFIG_KMOD
2525 +
2526 +/**
2527 + * dev_load - load a network module
2528 + * @name: name of interface
2529 + *
2530 + * If a network interface is not present and the process has suitable
2531 + * privileges this function loads the module. If module loading is not
2532 + * available in this kernel then it becomes a nop.
2533 + */
2534 +
2535 +void dev_load(const char *name)
2536 +{
2537 + if (!dev_get(name) && capable(CAP_SYS_MODULE))
2538 + request_module(name);
2539 +}
2540 +
2541 +#else
2542 +
2543 +extern inline void dev_load(const char *unused){;}
2544 +
2545 +#endif
2546 +
2547 +static int default_rebuild_header(struct sk_buff *skb)
2548 +{
2549 + printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n", skb->dev ? skb->dev->name : "NULL!!!");
2550 + kfree_skb(skb);
2551 + return 1;
2552 +}
2553 +
2554 +/**
2555 + * dev_open - prepare an interface for use.
2556 + * @dev: device to open
2557 + *
2558 + * Takes a device from down to up state. The device's private open
2559 + * function is invoked and then the multicast lists are loaded. Finally
2560 + * the device is moved into the up state and a %NETDEV_UP message is
2561 + * sent to the netdev notifier chain.
2562 + *
2563 + * Calling this function on an active interface is a nop. On a failure
2564 + * a negative errno code is returned.
2565 + */
2566 +
2567 +int dev_open(struct net_device *dev)
2568 +{
2569 + int ret = 0;
2570 +
2571 + /*
2572 + * Is it already up?
2573 + */
2574 +
2575 + if (dev->flags&IFF_UP)
2576 + return 0;
2577 +
2578 + /*
2579 + * Is it even present?
2580 + */
2581 + if (!netif_device_present(dev))
2582 + return -ENODEV;
2583 +
2584 + /*
2585 + * Call device private open method
2586 + */
2587 + if (try_inc_mod_count(dev->owner)) {
2588 + set_bit(__LINK_STATE_START, &dev->state);
2589 + if (dev->open) {
2590 + ret = dev->open(dev);
2591 + if (ret != 0) {
2592 + clear_bit(__LINK_STATE_START, &dev->state);
2593 + if (dev->owner)
2594 + __MOD_DEC_USE_COUNT(dev->owner);
2595 + }
2596 + }
2597 + } else {
2598 + ret = -ENODEV;
2599 + }
2600 +
2601 + /*
2602 + * If it went open OK then:
2603 + */
2604 +
2605 + if (ret == 0)
2606 + {
2607 + /*
2608 + * Set the flags.
2609 + */
2610 + dev->flags |= IFF_UP;
2611 +
2612 + /*
2613 + * Initialize multicasting status
2614 + */
2615 + dev_mc_upload(dev);
2616 +
2617 + /*
2618 + * Wakeup transmit queue engine
2619 + */
2620 + dev_activate(dev);
2621 +
2622 + /*
2623 + * ... and announce new interface.
2624 + */
2625 + notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
2626 + }
2627 + return(ret);
2628 +}
2629 +
2630 +#ifdef CONFIG_NET_FASTROUTE
2631 +
2632 +static void dev_do_clear_fastroute(struct net_device *dev)
2633 +{
2634 + if (dev->accept_fastpath) {
2635 + int i;
2636 +
2637 + for (i=0; i<=NETDEV_FASTROUTE_HMASK; i++) {
2638 + struct dst_entry *dst;
2639 +
2640 + write_lock_irq(&dev->fastpath_lock);
2641 + dst = dev->fastpath[i];
2642 + dev->fastpath[i] = NULL;
2643 + write_unlock_irq(&dev->fastpath_lock);
2644 +
2645 + dst_release(dst);
2646 + }
2647 + }
2648 +}
2649 +
2650 +void dev_clear_fastroute(struct net_device *dev)
2651 +{
2652 + if (dev) {
2653 + dev_do_clear_fastroute(dev);
2654 + } else {
2655 + read_lock(&dev_base_lock);
2656 + for (dev = dev_base; dev; dev = dev->next)
2657 + dev_do_clear_fastroute(dev);
2658 + read_unlock(&dev_base_lock);
2659 + }
2660 +}
2661 +#endif
2662 +
2663 +/**
2664 + * dev_close - shutdown an interface.
2665 + * @dev: device to shutdown
2666 + *
2667 + * This function moves an active device into down state. A
2668 + * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
2669 + * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
2670 + * chain.
2671 + */
2672 +
2673 +int dev_close(struct net_device *dev)
2674 +{
2675 + if (!(dev->flags&IFF_UP))
2676 + return 0;
2677 +
2678 + /*
2679 + * Tell people we are going down, so that they can
2680 + * prepare to death, when device is still operating.
2681 + */
2682 + notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
2683 +
2684 + dev_deactivate(dev);
2685 +
2686 + clear_bit(__LINK_STATE_START, &dev->state);
2687 +
2688 + /* Synchronize to scheduled poll. We cannot touch poll list,
2689 + * it can be even on different cpu. So just clear netif_running(),
2690 + * and wait when poll really will happen. Actually, the best place
2691 + * for this is inside dev->stop() after device stopped its irq
2692 + * engine, but this requires more changes in devices. */
2693 +
2694 + smp_mb__after_clear_bit(); /* Commit netif_running(). */
2695 + while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
2696 + /* No hurry. */
2697 + current->state = TASK_INTERRUPTIBLE;
2698 + schedule_timeout(1);
2699 + }
2700 +
2701 + /*
2702 + * Call the device specific close. This cannot fail.
2703 + * Only if device is UP
2704 + *
2705 + * We allow it to be called even after a DETACH hot-plug
2706 + * event.
2707 + */
2708 +
2709 + if (dev->stop)
2710 + dev->stop(dev);
2711 +
2712 + /*
2713 + * Device is now down.
2714 + */
2715 +
2716 + dev->flags &= ~IFF_UP;
2717 +#ifdef CONFIG_NET_FASTROUTE
2718 + dev_clear_fastroute(dev);
2719 +#endif
2720 +
2721 + /*
2722 + * Tell people we are down
2723 + */
2724 + notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
2725 +
2726 + /*
2727 + * Drop the module refcount
2728 + */
2729 + if (dev->owner)
2730 + __MOD_DEC_USE_COUNT(dev->owner);
2731 +
2732 + return(0);
2733 +}
2734 +
2735 +
2736 +/*
2737 + * Device change register/unregister. These are not inline or static
2738 + * as we export them to the world.
2739 + */
2740 +
2741 +/**
2742 + * register_netdevice_notifier - register a network notifier block
2743 + * @nb: notifier
2744 + *
2745 + * Register a notifier to be called when network device events occur.
2746 + * The notifier passed is linked into the kernel structures and must
2747 + * not be reused until it has been unregistered. A negative errno code
2748 + * is returned on a failure.
2749 + */
2750 +
2751 +int register_netdevice_notifier(struct notifier_block *nb)
2752 +{
2753 + return notifier_chain_register(&netdev_chain, nb);
2754 +}
2755 +
2756 +/**
2757 + * unregister_netdevice_notifier - unregister a network notifier block
2758 + * @nb: notifier
2759 + *
2760 + * Unregister a notifier previously registered by
2761 + * register_netdevice_notifier(). The notifier is unlinked into the
2762 + * kernel structures and may then be reused. A negative errno code
2763 + * is returned on a failure.
2764 + */
2765 +
2766 +int unregister_netdevice_notifier(struct notifier_block *nb)
2767 +{
2768 + return notifier_chain_unregister(&netdev_chain,nb);
2769 +}
2770 +
2771 +/*
2772 + * Support routine. Sends outgoing frames to any network
2773 + * taps currently in use.
2774 + */
2775 +
2776 +void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
2777 +{
2778 + struct packet_type *ptype;
2779 + do_gettimeofday(&skb->stamp);
2780 +
2781 + br_read_lock(BR_NETPROTO_LOCK);
2782 + for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next)
2783 + {
2784 + /* Never send packets back to the socket
2785 + * they originated from - MvS (miquels@drinkel.ow.org)
2786 + */
2787 + if ((ptype->dev == dev || !ptype->dev) &&
2788 + ((struct sock *)ptype->data != skb->sk))
2789 + {
2790 + struct sk_buff *skb2;
2791 + if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
2792 + break;
2793 +
2794 + /* skb->nh should be correctly
2795 + set by sender, so that the second statement is
2796 + just protection against buggy protocols.
2797 + */
2798 + skb2->mac.raw = skb2->data;
2799 +
2800 + if (skb2->nh.raw < skb2->data || skb2->nh.raw > skb2->tail) {
2801 + if (net_ratelimit())
2802 + printk(KERN_CRIT "protocol %04x is buggy, dev %s\n", skb2->protocol, dev->name);
2803 + skb2->nh.raw = skb2->data;
2804 + }
2805 +
2806 + skb2->h.raw = skb2->nh.raw;
2807 + skb2->pkt_type = PACKET_OUTGOING;
2808 + ptype->func(skb2, skb->dev, ptype);
2809 + }
2810 + }
2811 + br_read_unlock(BR_NETPROTO_LOCK);
2812 +}
2813 +
2814 +/* Calculate csum in the case, when packet is misrouted.
2815 + * If it failed by some reason, ignore and send skb with wrong
2816 + * checksum.
2817 + */
2818 +struct sk_buff * skb_checksum_help(struct sk_buff *skb)
2819 +{
2820 + int offset;
2821 + unsigned int csum;
2822 +
2823 + offset = skb->h.raw - skb->data;
2824 + if (offset > (int)skb->len)
2825 + BUG();
2826 + csum = skb_checksum(skb, offset, skb->len-offset, 0);
2827 +
2828 + offset = skb->tail - skb->h.raw;
2829 + if (offset <= 0)
2830 + BUG();
2831 + if (skb->csum+2 > offset)
2832 + BUG();
2833 +
2834 + *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
2835 + skb->ip_summed = CHECKSUM_NONE;
2836 + return skb;
2837 +}
2838 +
2839 +#ifdef CONFIG_HIGHMEM
2840 +/* Actually, we should eliminate this check as soon as we know, that:
2841 + * 1. IOMMU is present and allows to map all the memory.
2842 + * 2. No high memory really exists on this machine.
2843 + */
2844 +
2845 +static inline int
2846 +illegal_highdma(struct net_device *dev, struct sk_buff *skb)
2847 +{
2848 + int i;
2849 +
2850 + if (dev->features&NETIF_F_HIGHDMA)
2851 + return 0;
2852 +
2853 + for (i=0; i<skb_shinfo(skb)->nr_frags; i++)
2854 + if (skb_shinfo(skb)->frags[i].page >= highmem_start_page)
2855 + return 1;
2856 +
2857 + return 0;
2858 +}
2859 +#else
2860 +#define illegal_highdma(dev, skb) (0)
2861 +#endif
2862 +
2863 +/**
2864 + * dev_queue_xmit - transmit a buffer
2865 + * @skb: buffer to transmit
2866 + *
2867 + * Queue a buffer for transmission to a network device. The caller must
2868 + * have set the device and priority and built the buffer before calling this
2869 + * function. The function can be called from an interrupt.
2870 + *
2871 + * A negative errno code is returned on a failure. A success does not
2872 + * guarantee the frame will be transmitted as it may be dropped due
2873 + * to congestion or traffic shaping.
2874 + */
2875 +
2876 +int dev_queue_xmit(struct sk_buff *skb)
2877 +{
2878 + struct net_device *dev = skb->dev;
2879 + struct Qdisc *q;
2880 +
2881 + if (skb_shinfo(skb)->frag_list &&
2882 + !(dev->features&NETIF_F_FRAGLIST) &&
2883 + skb_linearize(skb, GFP_ATOMIC) != 0) {
2884 + kfree_skb(skb);
2885 + return -ENOMEM;
2886 + }
2887 +
2888 + /* Fragmented skb is linearized if device does not support SG,
2889 + * or if at least one of fragments is in highmem and device
2890 + * does not support DMA from it.
2891 + */
2892 + if (skb_shinfo(skb)->nr_frags &&
2893 + (!(dev->features&NETIF_F_SG) || illegal_highdma(dev, skb)) &&
2894 + skb_linearize(skb, GFP_ATOMIC) != 0) {
2895 + kfree_skb(skb);
2896 + return -ENOMEM;
2897 + }
2898 +
2899 + /* If packet is not checksummed and device does not support
2900 + * checksumming for this protocol, complete checksumming here.
2901 + */
2902 + if (skb->ip_summed == CHECKSUM_HW &&
2903 + (!(dev->features&(NETIF_F_HW_CSUM|NETIF_F_NO_CSUM)) &&
2904 + (!(dev->features&NETIF_F_IP_CSUM) ||
2905 + skb->protocol != htons(ETH_P_IP)))) {
2906 + if ((skb = skb_checksum_help(skb)) == NULL)
2907 + return -ENOMEM;
2908 + }
2909 +
2910 + /* Grab device queue */
2911 + spin_lock_bh(&dev->queue_lock);
2912 + q = dev->qdisc;
2913 + if (q->enqueue) {
2914 + int ret = q->enqueue(skb, q);
2915 +
2916 + qdisc_run(dev);
2917 +
2918 + spin_unlock_bh(&dev->queue_lock);
2919 + return ret == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : ret;
2920 + }
2921 +
2922 + /* The device has no queue. Common case for software devices:
2923 + loopback, all the sorts of tunnels...
2924 +
2925 + Really, it is unlikely that xmit_lock protection is necessary here.
2926 + (f.e. loopback and IP tunnels are clean ignoring statistics counters.)
2927 + However, it is possible, that they rely on protection
2928 + made by us here.
2929 +
2930 + Check this and shot the lock. It is not prone from deadlocks.
2931 + Either shot noqueue qdisc, it is even simpler 8)
2932 + */
2933 + if (dev->flags&IFF_UP) {
2934 + int cpu = smp_processor_id();
2935 +
2936 + if (dev->xmit_lock_owner != cpu) {
2937 + spin_unlock(&dev->queue_lock);
2938 + spin_lock(&dev->xmit_lock);
2939 + dev->xmit_lock_owner = cpu;
2940 +
2941 + if (!netif_queue_stopped(dev)) {
2942 + if (netdev_nit)
2943 + dev_queue_xmit_nit(skb,dev);
2944 +
2945 + if (dev->hard_start_xmit(skb, dev) == 0) {
2946 + dev->xmit_lock_owner = -1;
2947 + spin_unlock_bh(&dev->xmit_lock);
2948 + return 0;
2949 + }
2950 + }
2951 + dev->xmit_lock_owner = -1;
2952 + spin_unlock_bh(&dev->xmit_lock);
2953 + if (net_ratelimit())
2954 + printk(KERN_CRIT "Virtual device %s asks to queue packet!\n", dev->name);
2955 + kfree_skb(skb);
2956 + return -ENETDOWN;
2957 + } else {
2958 + /* Recursion is detected! It is possible, unfortunately */
2959 + if (net_ratelimit())
2960 + printk(KERN_CRIT "Dead loop on virtual device %s, fix it urgently!\n", dev->name);
2961 + }
2962 + }
2963 + spin_unlock_bh(&dev->queue_lock);
2964 +
2965 + kfree_skb(skb);
2966 + return -ENETDOWN;
2967 +}
2968 +
2969 +
2970 +/*=======================================================================
2971 + Receiver routines
2972 + =======================================================================*/
2973 +
2974 +int netdev_max_backlog = 300;
2975 +int weight_p = 64; /* old backlog weight */
2976 +/* These numbers are selected based on intuition and some
2977 + * experimentatiom, if you have more scientific way of doing this
2978 + * please go ahead and fix things.
2979 + */
2980 +int no_cong_thresh = 10;
2981 +int no_cong = 20;
2982 +int lo_cong = 100;
2983 +int mod_cong = 290;
2984 +
2985 +struct netif_rx_stats netdev_rx_stat[NR_CPUS];
2986 +
2987 +
2988 +#ifdef CONFIG_NET_HW_FLOWCONTROL
2989 +atomic_t netdev_dropping = ATOMIC_INIT(0);
2990 +static unsigned long netdev_fc_mask = 1;
2991 +unsigned long netdev_fc_xoff = 0;
2992 +spinlock_t netdev_fc_lock = SPIN_LOCK_UNLOCKED;
2993 +
2994 +static struct
2995 +{
2996 + void (*stimul)(struct net_device *);
2997 + struct net_device *dev;
2998 +} netdev_fc_slots[BITS_PER_LONG];
2999 +
3000 +int netdev_register_fc(struct net_device *dev, void (*stimul)(struct net_device *dev))
3001 +{
3002 + int bit = 0;
3003 + unsigned long flags;
3004 +
3005 + spin_lock_irqsave(&netdev_fc_lock, flags);
3006 + if (netdev_fc_mask != ~0UL) {
3007 + bit = ffz(netdev_fc_mask);
3008 + netdev_fc_slots[bit].stimul = stimul;
3009 + netdev_fc_slots[bit].dev = dev;
3010 + set_bit(bit, &netdev_fc_mask);
3011 + clear_bit(bit, &netdev_fc_xoff);
3012 + }
3013 + spin_unlock_irqrestore(&netdev_fc_lock, flags);
3014 + return bit;
3015 +}
3016 +
3017 +void netdev_unregister_fc(int bit)
3018 +{
3019 + unsigned long flags;
3020 +
3021 + spin_lock_irqsave(&netdev_fc_lock, flags);
3022 + if (bit > 0) {
3023 + netdev_fc_slots[bit].stimul = NULL;
3024 + netdev_fc_slots[bit].dev = NULL;
3025 + clear_bit(bit, &netdev_fc_mask);
3026 + clear_bit(bit, &netdev_fc_xoff);
3027 + }
3028 + spin_unlock_irqrestore(&netdev_fc_lock, flags);
3029 +}
3030 +
3031 +static void netdev_wakeup(void)
3032 +{
3033 + unsigned long xoff;
3034 +
3035 + spin_lock(&netdev_fc_lock);
3036 + xoff = netdev_fc_xoff;
3037 + netdev_fc_xoff = 0;
3038 + while (xoff) {
3039 + int i = ffz(~xoff);
3040 + xoff &= ~(1<<i);
3041 + netdev_fc_slots[i].stimul(netdev_fc_slots[i].dev);
3042 + }
3043 + spin_unlock(&netdev_fc_lock);
3044 +}
3045 +#endif
3046 +
3047 +static void get_sample_stats(int cpu)
3048 +{
3049 +#ifdef RAND_LIE
3050 + unsigned long rd;
3051 + int rq;
3052 +#endif
3053 + int blog = softnet_data[cpu].input_pkt_queue.qlen;
3054 + int avg_blog = softnet_data[cpu].avg_blog;
3055 +
3056 + avg_blog = (avg_blog >> 1)+ (blog >> 1);
3057 +
3058 + if (avg_blog > mod_cong) {
3059 + /* Above moderate congestion levels. */
3060 + softnet_data[cpu].cng_level = NET_RX_CN_HIGH;
3061 +#ifdef RAND_LIE
3062 + rd = net_random();
3063 + rq = rd % netdev_max_backlog;
3064 + if (rq < avg_blog) /* unlucky bastard */
3065 + softnet_data[cpu].cng_level = NET_RX_DROP;
3066 +#endif
3067 + } else if (avg_blog > lo_cong) {
3068 + softnet_data[cpu].cng_level = NET_RX_CN_MOD;
3069 +#ifdef RAND_LIE
3070 + rd = net_random();
3071 + rq = rd % netdev_max_backlog;
3072 + if (rq < avg_blog) /* unlucky bastard */
3073 + softnet_data[cpu].cng_level = NET_RX_CN_HIGH;
3074 +#endif
3075 + } else if (avg_blog > no_cong)
3076 + softnet_data[cpu].cng_level = NET_RX_CN_LOW;
3077 + else /* no congestion */
3078 + softnet_data[cpu].cng_level = NET_RX_SUCCESS;
3079 +
3080 + softnet_data[cpu].avg_blog = avg_blog;
3081 +}
3082 +
3083 +#ifdef OFFLINE_SAMPLE
3084 +static void sample_queue(unsigned long dummy)
3085 +{
3086 +/* 10 ms 0r 1ms -- i dont care -- JHS */
3087 + int next_tick = 1;
3088 + int cpu = smp_processor_id();
3089 +
3090 + get_sample_stats(cpu);
3091 + next_tick += jiffies;
3092 + mod_timer(&samp_timer, next_tick);
3093 +}
3094 +#endif
3095 +
3096 +
3097 +/**
3098 + * netif_rx - post buffer to the network code
3099 + * @skb: buffer to post
3100 + *
3101 + * This function receives a packet from a device driver and queues it for
3102 + * the upper (protocol) levels to process. It always succeeds. The buffer
3103 + * may be dropped during processing for congestion control or by the
3104 + * protocol layers.
3105 + *
3106 + * return values:
3107 + * NET_RX_SUCCESS (no congestion)
3108 + * NET_RX_CN_LOW (low congestion)
3109 + * NET_RX_CN_MOD (moderate congestion)
3110 + * NET_RX_CN_HIGH (high congestion)
3111 + * NET_RX_DROP (packet was dropped)
3112 + *
3113 + *
3114 + */
3115 +
3116 +int netif_rx(struct sk_buff *skb)
3117 +{
3118 + int this_cpu = smp_processor_id();
3119 + struct softnet_data *queue;
3120 + unsigned long flags;
3121 +
3122 + if (skb->stamp.tv_sec == 0)
3123 + do_gettimeofday(&skb->stamp);
3124 +
3125 + /* The code is rearranged so that the path is the most
3126 + short when CPU is congested, but is still operating.
3127 + */
3128 + queue = &softnet_data[this_cpu];
3129 +
3130 + local_irq_save(flags);
3131 +
3132 + netdev_rx_stat[this_cpu].total++;
3133 + if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
3134 + if (queue->input_pkt_queue.qlen) {
3135 + if (queue->throttle)
3136 + goto drop;
3137 +
3138 +enqueue:
3139 + dev_hold(skb->dev);
3140 + __skb_queue_tail(&queue->input_pkt_queue,skb);
3141 + local_irq_restore(flags);
3142 +#ifndef OFFLINE_SAMPLE
3143 + get_sample_stats(this_cpu);
3144 +#endif
3145 + return queue->cng_level;
3146 + }
3147 +
3148 + if (queue->throttle) {
3149 + queue->throttle = 0;
3150 +#ifdef CONFIG_NET_HW_FLOWCONTROL
3151 + if (atomic_dec_and_test(&netdev_dropping))
3152 + netdev_wakeup();
3153 +#endif
3154 + }
3155 +
3156 + netif_rx_schedule(&queue->blog_dev);
3157 + goto enqueue;
3158 + }
3159 +
3160 + if (queue->throttle == 0) {
3161 + queue->throttle = 1;
3162 + netdev_rx_stat[this_cpu].throttled++;
3163 +#ifdef CONFIG_NET_HW_FLOWCONTROL
3164 + atomic_inc(&netdev_dropping);
3165 +#endif
3166 + }
3167 +
3168 +drop:
3169 + netdev_rx_stat[this_cpu].dropped++;
3170 + local_irq_restore(flags);
3171 +
3172 + kfree_skb(skb);
3173 + return NET_RX_DROP;
3174 +}
3175 +
3176 +/* Deliver skb to an old protocol, which is not threaded well
3177 + or which do not understand shared skbs.
3178 + */
3179 +static int deliver_to_old_ones(struct packet_type *pt, struct sk_buff *skb, int last)
3180 +{
3181 + static spinlock_t net_bh_lock = SPIN_LOCK_UNLOCKED;
3182 + int ret = NET_RX_DROP;
3183 +
3184 +
3185 + if (!last) {
3186 + skb = skb_clone(skb, GFP_ATOMIC);
3187 + if (skb == NULL)
3188 + return ret;
3189 + }
3190 + if (skb_is_nonlinear(skb) && skb_linearize(skb, GFP_ATOMIC) != 0) {
3191 + kfree_skb(skb);
3192 + return ret;
3193 + }
3194 +
3195 + /* The assumption (correct one) is that old protocols
3196 + did not depened on BHs different of NET_BH and TIMER_BH.
3197 + */
3198 +
3199 + /* Emulate NET_BH with special spinlock */
3200 + spin_lock(&net_bh_lock);
3201 +
3202 + /* Disable timers and wait for all timers completion */
3203 + tasklet_disable(bh_task_vec+TIMER_BH);
3204 +
3205 + ret = pt->func(skb, skb->dev, pt);
3206 +
3207 + tasklet_hi_enable(bh_task_vec+TIMER_BH);
3208 + spin_unlock(&net_bh_lock);
3209 + return ret;
3210 +}
3211 +
3212 +static __inline__ void skb_bond(struct sk_buff *skb)
3213 +{
3214 + struct net_device *dev = skb->dev;
3215 +
3216 + if (dev->master) {
3217 + skb->real_dev = skb->dev;
3218 + skb->dev = dev->master;
3219 + }
3220 +}
3221 +
3222 +static void net_tx_action(struct softirq_action *h)
3223 +{
3224 + int cpu = smp_processor_id();
3225 +
3226 + if (softnet_data[cpu].completion_queue) {
3227 + struct sk_buff *clist;
3228 +
3229 + local_irq_disable();
3230 + clist = softnet_data[cpu].completion_queue;
3231 + softnet_data[cpu].completion_queue = NULL;
3232 + local_irq_enable();
3233 +
3234 + while (clist != NULL) {
3235 + struct sk_buff *skb = clist;
3236 + clist = clist->next;
3237 +
3238 + BUG_TRAP(atomic_read(&skb->users) == 0);
3239 + __kfree_skb(skb);
3240 + }
3241 + }
3242 +
3243 + if (softnet_data[cpu].output_queue) {
3244 + struct net_device *head;
3245 +
3246 + local_irq_disable();
3247 + head = softnet_data[cpu].output_queue;
3248 + softnet_data[cpu].output_queue = NULL;
3249 + local_irq_enable();
3250 +
3251 + while (head != NULL) {
3252 + struct net_device *dev = head;
3253 + head = head->next_sched;
3254 +
3255 + smp_mb__before_clear_bit();
3256 + clear_bit(__LINK_STATE_SCHED, &dev->state);
3257 +
3258 + if (spin_trylock(&dev->queue_lock)) {
3259 + qdisc_run(dev);
3260 + spin_unlock(&dev->queue_lock);
3261 + } else {
3262 + netif_schedule(dev);
3263 + }
3264 + }
3265 + }
3266 +}
3267 +
3268 +
3269 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3270 +void (*br_handle_frame_hook)(struct sk_buff *skb) = NULL;
3271 +#endif
3272 +
3273 +static __inline__ int handle_bridge(struct sk_buff *skb,
3274 + struct packet_type *pt_prev)
3275 +{
3276 + int ret = NET_RX_DROP;
3277 +
3278 + if (pt_prev) {
3279 + if (!pt_prev->data)
3280 + ret = deliver_to_old_ones(pt_prev, skb, 0);
3281 + else {
3282 + atomic_inc(&skb->users);
3283 + ret = pt_prev->func(skb, skb->dev, pt_prev);
3284 + }
3285 + }
3286 +
3287 + br_handle_frame_hook(skb);
3288 + return ret;
3289 +}
3290 +
3291 +
3292 +#ifdef CONFIG_NET_DIVERT
3293 +static inline int handle_diverter(struct sk_buff *skb)
3294 +{
3295 + /* if diversion is supported on device, then divert */
3296 + if (skb->dev->divert && skb->dev->divert->divert)
3297 + divert_frame(skb);
3298 + return 0;
3299 +}
3300 +#endif /* CONFIG_NET_DIVERT */
3301 +
3302 +int netif_receive_skb(struct sk_buff *skb)
3303 +{
3304 + struct packet_type *ptype, *pt_prev;
3305 + int ret = NET_RX_DROP;
3306 + unsigned short type;
3307 +
3308 + if (skb->stamp.tv_sec == 0)
3309 + do_gettimeofday(&skb->stamp);
3310 +
3311 + skb_bond(skb);
3312 +
3313 + netdev_rx_stat[smp_processor_id()].total++;
3314 +
3315 +#ifdef CONFIG_NET_FASTROUTE
3316 + if (skb->pkt_type == PACKET_FASTROUTE) {
3317 + netdev_rx_stat[smp_processor_id()].fastroute_deferred_out++;
3318 + return dev_queue_xmit(skb);
3319 + }
3320 +#endif
3321 +
3322 + skb->h.raw = skb->nh.raw = skb->data;
3323 +
3324 + pt_prev = NULL;
3325 + for (ptype = ptype_all; ptype; ptype = ptype->next) {
3326 + if (!ptype->dev || ptype->dev == skb->dev) {
3327 + if (pt_prev) {
3328 + if (!pt_prev->data) {
3329 + ret = deliver_to_old_ones(pt_prev, skb, 0);
3330 + } else {
3331 + atomic_inc(&skb->users);
3332 + ret = pt_prev->func(skb, skb->dev, pt_prev);
3333 + }
3334 + }
3335 + pt_prev = ptype;
3336 + }
3337 + }
3338 +
3339 +#ifdef CONFIG_NET_DIVERT
3340 + if (skb->dev->divert && skb->dev->divert->divert)
3341 + ret = handle_diverter(skb);
3342 +#endif /* CONFIG_NET_DIVERT */
3343 +
3344 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3345 + if (skb->dev->br_port != NULL && br_handle_frame_hook != NULL &&
3346 + skb->pkt_type != PACKET_LOOPBACK) {
3347 + return handle_bridge(skb, pt_prev);
3348 + }
3349 +#endif
3350 +
3351 + type = skb->protocol;
3352 + for (ptype=ptype_base[ntohs(type)&15];ptype;ptype=ptype->next) {
3353 + if (ptype->type == type &&
3354 + (!ptype->dev || ptype->dev == skb->dev)) {
3355 + if (pt_prev) {
3356 + if (!pt_prev->data) {
3357 + ret = deliver_to_old_ones(pt_prev, skb, 0);
3358 + } else {
3359 + atomic_inc(&skb->users);
3360 + ret = pt_prev->func(skb, skb->dev, pt_prev);
3361 + }
3362 + }
3363 + pt_prev = ptype;
3364 + }
3365 + }
3366 +
3367 + if (pt_prev) {
3368 + if (!pt_prev->data) {
3369 + ret = deliver_to_old_ones(pt_prev, skb, 1);
3370 + } else {
3371 + ret = pt_prev->func(skb, skb->dev, pt_prev);
3372 + }
3373 + } else {
3374 + kfree_skb(skb);
3375 + /* Jamal, now you will not able to escape explaining
3376 + * me how you were going to use this. :-)
3377 + */
3378 + ret = NET_RX_DROP;
3379 + }
3380 +
3381 + return ret;
3382 +}
3383 +
3384 +static int process_backlog(struct net_device *backlog_dev, int *budget)
3385 +{
3386 + int work = 0;
3387 + int quota = min(backlog_dev->quota, *budget);
3388 + int this_cpu = smp_processor_id();
3389 + struct softnet_data *queue = &softnet_data[this_cpu];
3390 + unsigned long start_time = jiffies;
3391 +
3392 + for (;;) {
3393 + struct sk_buff *skb;
3394 + struct net_device *dev;
3395 +
3396 + local_irq_disable();
3397 + skb = __skb_dequeue(&queue->input_pkt_queue);
3398 + if (skb == NULL)
3399 + goto job_done;
3400 + local_irq_enable();
3401 +
3402 + dev = skb->dev;
3403 +
3404 + netif_receive_skb(skb);
3405 +
3406 + dev_put(dev);
3407 +
3408 + work++;
3409 +
3410 + if (work >= quota || jiffies - start_time > 1)
3411 + break;
3412 +
3413 +#ifdef CONFIG_NET_HW_FLOWCONTROL
3414 + if (queue->throttle && queue->input_pkt_queue.qlen < no_cong_thresh ) {
3415 + queue->throttle = 0;
3416 + if (atomic_dec_and_test(&netdev_dropping)) {
3417 + netdev_wakeup();
3418 + break;
3419 + }
3420 + }
3421 +#endif
3422 + }
3423 +
3424 + backlog_dev->quota -= work;
3425 + *budget -= work;
3426 + return -1;
3427 +
3428 +job_done:
3429 + backlog_dev->quota -= work;
3430 + *budget -= work;
3431 +
3432 + list_del(&backlog_dev->poll_list);
3433 + smp_mb__before_clear_bit();
3434 + netif_poll_enable(backlog_dev);
3435 +
3436 + if (queue->throttle) {
3437 + queue->throttle = 0;
3438 +#ifdef CONFIG_NET_HW_FLOWCONTROL
3439 + if (atomic_dec_and_test(&netdev_dropping))
3440 + netdev_wakeup();
3441 +#endif
3442 + }
3443 + local_irq_enable();
3444 + return 0;
3445 +}
3446 +
3447 +static void net_rx_action(struct softirq_action *h)
3448 +{
3449 + int this_cpu = smp_processor_id();
3450 + struct softnet_data *queue = &softnet_data[this_cpu];
3451 + unsigned long start_time = jiffies;
3452 + int budget = netdev_max_backlog;
3453 +
3454 + br_read_lock(BR_NETPROTO_LOCK);
3455 + local_irq_disable();
3456 +
3457 + while (!list_empty(&queue->poll_list)) {
3458 + struct net_device *dev;
3459 +
3460 + if (budget <= 0 || jiffies - start_time > 1)
3461 + goto softnet_break;
3462 +
3463 + local_irq_enable();
3464 +
3465 + dev = list_entry(queue->poll_list.next, struct net_device, poll_list);
3466 +
3467 + if (dev->quota <= 0 || dev->poll(dev, &budget)) {
3468 + local_irq_disable();
3469 + list_del(&dev->poll_list);
3470 + list_add_tail(&dev->poll_list, &queue->poll_list);
3471 + if (dev->quota < 0)
3472 + dev->quota += dev->weight;
3473 + else
3474 + dev->quota = dev->weight;
3475 + } else {
3476 + dev_put(dev);
3477 + local_irq_disable();
3478 + }
3479 + }
3480 +
3481 + local_irq_enable();
3482 + br_read_unlock(BR_NETPROTO_LOCK);
3483 + return;
3484 +
3485 +softnet_break:
3486 + netdev_rx_stat[this_cpu].time_squeeze++;
3487 + __cpu_raise_softirq(this_cpu, NET_RX_SOFTIRQ);
3488 +
3489 + local_irq_enable();
3490 + br_read_unlock(BR_NETPROTO_LOCK);
3491 +}
3492 +
3493 +static gifconf_func_t * gifconf_list [NPROTO];
3494 +
3495 +/**
3496 + * register_gifconf - register a SIOCGIF handler
3497 + * @family: Address family
3498 + * @gifconf: Function handler
3499 + *
3500 + * Register protocol dependent address dumping routines. The handler
3501 + * that is passed must not be freed or reused until it has been replaced
3502 + * by another handler.
3503 + */
3504 +
3505 +int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
3506 +{
3507 + if (family>=NPROTO)
3508 + return -EINVAL;
3509 + gifconf_list[family] = gifconf;
3510 + return 0;
3511 +}
3512 +
3513 +
3514 +/*
3515 + * Map an interface index to its name (SIOCGIFNAME)
3516 + */
3517 +
3518 +/*
3519 + * We need this ioctl for efficient implementation of the
3520 + * if_indextoname() function required by the IPv6 API. Without
3521 + * it, we would have to search all the interfaces to find a
3522 + * match. --pb
3523 + */
3524 +
3525 +static int dev_ifname(struct ifreq *arg)
3526 +{
3527 + struct net_device *dev;
3528 + struct ifreq ifr;
3529 +
3530 + /*
3531 + * Fetch the caller's info block.
3532 + */
3533 +
3534 + if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3535 + return -EFAULT;
3536 +
3537 + read_lock(&dev_base_lock);
3538 + dev = __dev_get_by_index(ifr.ifr_ifindex);
3539 + if (!dev) {
3540 + read_unlock(&dev_base_lock);
3541 + return -ENODEV;
3542 + }
3543 +
3544 + strcpy(ifr.ifr_name, dev->name);
3545 + read_unlock(&dev_base_lock);
3546 +
3547 + if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
3548 + return -EFAULT;
3549 + return 0;
3550 +}
3551 +
3552 +/*
3553 + * Perform a SIOCGIFCONF call. This structure will change
3554 + * size eventually, and there is nothing I can do about it.
3555 + * Thus we will need a 'compatibility mode'.
3556 + */
3557 +
3558 +static int dev_ifconf(char *arg)
3559 +{
3560 + struct ifconf ifc;
3561 + struct net_device *dev;
3562 + char *pos;
3563 + int len;
3564 + int total;
3565 + int i;
3566 +
3567 + /*
3568 + * Fetch the caller's info block.
3569 + */
3570 +
3571 + if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
3572 + return -EFAULT;
3573 +
3574 + pos = ifc.ifc_buf;
3575 + len = ifc.ifc_len;
3576 +
3577 + /*
3578 + * Loop over the interfaces, and write an info block for each.
3579 + */
3580 +
3581 + total = 0;
3582 + for (dev = dev_base; dev != NULL; dev = dev->next) {
3583 + for (i=0; i<NPROTO; i++) {
3584 + if (gifconf_list[i]) {
3585 + int done;
3586 + if (pos==NULL) {
3587 + done = gifconf_list[i](dev, NULL, 0);
3588 + } else {
3589 + done = gifconf_list[i](dev, pos+total, len-total);
3590 + }
3591 + if (done<0) {
3592 + return -EFAULT;
3593 + }
3594 + total += done;
3595 + }
3596 + }
3597 + }
3598 +
3599 + /*
3600 + * All done. Write the updated control block back to the caller.
3601 + */
3602 + ifc.ifc_len = total;
3603 +
3604 + if (copy_to_user(arg, &ifc, sizeof(struct ifconf)))
3605 + return -EFAULT;
3606 +
3607 + /*
3608 + * Both BSD and Solaris return 0 here, so we do too.
3609 + */
3610 + return 0;
3611 +}
3612 +
3613 +/*
3614 + * This is invoked by the /proc filesystem handler to display a device
3615 + * in detail.
3616 + */
3617 +
3618 +#ifdef CONFIG_PROC_FS
3619 +
3620 +static int sprintf_stats(char *buffer, struct net_device *dev)
3621 +{
3622 + struct net_device_stats *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
3623 + int size;
3624 +
3625 + if (stats)
3626 + size = sprintf(buffer, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu %8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
3627 + dev->name,
3628 + stats->rx_bytes,
3629 + stats->rx_packets, stats->rx_errors,
3630 + stats->rx_dropped + stats->rx_missed_errors,
3631 + stats->rx_fifo_errors,
3632 + stats->rx_length_errors + stats->rx_over_errors
3633 + + stats->rx_crc_errors + stats->rx_frame_errors,
3634 + stats->rx_compressed, stats->multicast,
3635 + stats->tx_bytes,
3636 + stats->tx_packets, stats->tx_errors, stats->tx_dropped,
3637 + stats->tx_fifo_errors, stats->collisions,
3638 + stats->tx_carrier_errors + stats->tx_aborted_errors
3639 + + stats->tx_window_errors + stats->tx_heartbeat_errors,
3640 + stats->tx_compressed);
3641 + else
3642 + size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
3643 +
3644 + return size;
3645 +}
3646 +
3647 +/*
3648 + * Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
3649 + * to create /proc/net/dev
3650 + */
3651 +
3652 +static int dev_get_info(char *buffer, char **start, off_t offset, int length)
3653 +{
3654 + int len = 0;
3655 + off_t begin = 0;
3656 + off_t pos = 0;
3657 + int size;
3658 + struct net_device *dev;
3659 +
3660 +
3661 + size = sprintf(buffer,
3662 + "Inter-| Receive | Transmit\n"
3663 + " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n");
3664 +
3665 + pos += size;
3666 + len += size;
3667 +
3668 +
3669 + read_lock(&dev_base_lock);
3670 + for (dev = dev_base; dev != NULL; dev = dev->next) {
3671 + size = sprintf_stats(buffer+len, dev);
3672 + len += size;
3673 + pos = begin + len;
3674 +
3675 + if (pos < offset) {
3676 + len = 0;
3677 + begin = pos;
3678 + }
3679 + if (pos > offset + length)
3680 + break;
3681 + }
3682 + read_unlock(&dev_base_lock);
3683 +
3684 + *start = buffer + (offset - begin); /* Start of wanted data */
3685 + len -= (offset - begin); /* Start slop */
3686 + if (len > length)
3687 + len = length; /* Ending slop */
3688 + if (len < 0)
3689 + len = 0;
3690 + return len;
3691 +}
3692 +
3693 +static int dev_proc_stats(char *buffer, char **start, off_t offset,
3694 + int length, int *eof, void *data)
3695 +{
3696 + int i, lcpu;
3697 + int len=0;
3698 +
3699 + for (lcpu=0; lcpu<smp_num_cpus; lcpu++) {
3700 + i = cpu_logical_map(lcpu);
3701 + len += sprintf(buffer+len, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
3702 + netdev_rx_stat[i].total,
3703 + netdev_rx_stat[i].dropped,
3704 + netdev_rx_stat[i].time_squeeze,
3705 + netdev_rx_stat[i].throttled,
3706 + netdev_rx_stat[i].fastroute_hit,
3707 + netdev_rx_stat[i].fastroute_success,
3708 + netdev_rx_stat[i].fastroute_defer,
3709 + netdev_rx_stat[i].fastroute_deferred_out,
3710 +#if 0
3711 + netdev_rx_stat[i].fastroute_latency_reduction
3712 +#else
3713 + netdev_rx_stat[i].cpu_collision
3714 +#endif
3715 + );
3716 + }
3717 +
3718 + len -= offset;
3719 +
3720 + if (len > length)
3721 + len = length;
3722 + if (len < 0)
3723 + len = 0;
3724 +
3725 + *start = buffer + offset;
3726 + *eof = 1;
3727 +
3728 + return len;
3729 +}
3730 +
3731 +#endif /* CONFIG_PROC_FS */
3732 +
3733 +
3734 +/**
3735 + * netdev_set_master - set up master/slave pair
3736 + * @slave: slave device
3737 + * @master: new master device
3738 + *
3739 + * Changes the master device of the slave. Pass %NULL to break the
3740 + * bonding. The caller must hold the RTNL semaphore. On a failure
3741 + * a negative errno code is returned. On success the reference counts
3742 + * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
3743 + * function returns zero.
3744 + */
3745 +
3746 +int netdev_set_master(struct net_device *slave, struct net_device *master)
3747 +{
3748 + struct net_device *old = slave->master;
3749 +
3750 + ASSERT_RTNL();
3751 +
3752 + if (master) {
3753 + if (old)
3754 + return -EBUSY;
3755 + dev_hold(master);
3756 + }
3757 +
3758 + br_write_lock_bh(BR_NETPROTO_LOCK);
3759 + slave->master = master;
3760 + br_write_unlock_bh(BR_NETPROTO_LOCK);
3761 +
3762 + if (old)
3763 + dev_put(old);
3764 +
3765 + if (master)
3766 + slave->flags |= IFF_SLAVE;
3767 + else
3768 + slave->flags &= ~IFF_SLAVE;
3769 +
3770 + rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
3771 + return 0;
3772 +}
3773 +
3774 +/**
3775 + * dev_set_promiscuity - update promiscuity count on a device
3776 + * @dev: device
3777 + * @inc: modifier
3778 + *
3779 + * Add or remove promsicuity from a device. While the count in the device
3780 + * remains above zero the interface remains promiscuous. Once it hits zero
3781 + * the device reverts back to normal filtering operation. A negative inc
3782 + * value is used to drop promiscuity on the device.
3783 + */
3784 +
3785 +void dev_set_promiscuity(struct net_device *dev, int inc)
3786 +{
3787 + unsigned short old_flags = dev->flags;
3788 +
3789 + dev->flags |= IFF_PROMISC;
3790 + if ((dev->promiscuity += inc) == 0)
3791 + dev->flags &= ~IFF_PROMISC;
3792 + if (dev->flags^old_flags) {
3793 +#ifdef CONFIG_NET_FASTROUTE
3794 + if (dev->flags&IFF_PROMISC) {
3795 + netdev_fastroute_obstacles++;
3796 + dev_clear_fastroute(dev);
3797 + } else
3798 + netdev_fastroute_obstacles--;
3799 +#endif
3800 + dev_mc_upload(dev);
3801 + printk(KERN_INFO "device %s %s promiscuous mode\n",
3802 + dev->name, (dev->flags&IFF_PROMISC) ? "entered" : "left");
3803 + }
3804 +}
3805 +
3806 +/**
3807 + * dev_set_allmulti - update allmulti count on a device
3808 + * @dev: device
3809 + * @inc: modifier
3810 + *
3811 + * Add or remove reception of all multicast frames to a device. While the
3812 + * count in the device remains above zero the interface remains listening
3813 + * to all interfaces. Once it hits zero the device reverts back to normal
3814 + * filtering operation. A negative @inc value is used to drop the counter
3815 + * when releasing a resource needing all multicasts.
3816 + */
3817 +
3818 +void dev_set_allmulti(struct net_device *dev, int inc)
3819 +{
3820 + unsigned short old_flags = dev->flags;
3821 +
3822 + dev->flags |= IFF_ALLMULTI;
3823 + if ((dev->allmulti += inc) == 0)
3824 + dev->flags &= ~IFF_ALLMULTI;
3825 + if (dev->flags^old_flags)
3826 + dev_mc_upload(dev);
3827 +}
3828 +
3829 +int dev_change_flags(struct net_device *dev, unsigned flags)
3830 +{
3831 + int ret;
3832 + int old_flags = dev->flags;
3833 +
3834 + /*
3835 + * Set the flags on our device.
3836 + */
3837 +
3838 + dev->flags = (flags & (IFF_DEBUG|IFF_NOTRAILERS|IFF_NOARP|IFF_DYNAMIC|
3839 + IFF_MULTICAST|IFF_PORTSEL|IFF_AUTOMEDIA)) |
3840 + (dev->flags & (IFF_UP|IFF_VOLATILE|IFF_PROMISC|IFF_ALLMULTI));
3841 +
3842 + /*
3843 + * Load in the correct multicast list now the flags have changed.
3844 + */
3845 +
3846 + dev_mc_upload(dev);
3847 +
3848 + /*
3849 + * Have we downed the interface. We handle IFF_UP ourselves
3850 + * according to user attempts to set it, rather than blindly
3851 + * setting it.
3852 + */
3853 +
3854 + ret = 0;
3855 + if ((old_flags^flags)&IFF_UP) /* Bit is different ? */
3856 + {
3857 + ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
3858 +
3859 + if (ret == 0)
3860 + dev_mc_upload(dev);
3861 + }
3862 +
3863 + if (dev->flags&IFF_UP &&
3864 + ((old_flags^dev->flags)&~(IFF_UP|IFF_PROMISC|IFF_ALLMULTI|IFF_VOLATILE)))
3865 + notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
3866 +
3867 + if ((flags^dev->gflags)&IFF_PROMISC) {
3868 + int inc = (flags&IFF_PROMISC) ? +1 : -1;
3869 + dev->gflags ^= IFF_PROMISC;
3870 + dev_set_promiscuity(dev, inc);
3871 + }
3872 +
3873 + /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
3874 + is important. Some (broken) drivers set IFF_PROMISC, when
3875 + IFF_ALLMULTI is requested not asking us and not reporting.
3876 + */
3877 + if ((flags^dev->gflags)&IFF_ALLMULTI) {
3878 + int inc = (flags&IFF_ALLMULTI) ? +1 : -1;
3879 + dev->gflags ^= IFF_ALLMULTI;
3880 + dev_set_allmulti(dev, inc);
3881 + }
3882 +
3883 + if (old_flags^dev->flags)
3884 + rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags^dev->flags);
3885 +
3886 + return ret;
3887 +}
3888 +
3889 +/*
3890 + * Perform the SIOCxIFxxx calls.
3891 + */
3892 +
3893 +static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
3894 +{
3895 + struct net_device *dev;
3896 + int err;
3897 +
3898 + if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
3899 + return -ENODEV;
3900 +
3901 + switch(cmd)
3902 + {
3903 + case SIOCGIFFLAGS: /* Get interface flags */
3904 + ifr->ifr_flags = (dev->flags&~(IFF_PROMISC|IFF_ALLMULTI|IFF_RUNNING))
3905 + |(dev->gflags&(IFF_PROMISC|IFF_ALLMULTI));
3906 + if (netif_running(dev) && netif_carrier_ok(dev))
3907 + ifr->ifr_flags |= IFF_RUNNING;
3908 + return 0;
3909 +
3910 + case SIOCSIFFLAGS: /* Set interface flags */
3911 + return dev_change_flags(dev, ifr->ifr_flags);
3912 +
3913 + case SIOCGIFMETRIC: /* Get the metric on the interface (currently unused) */
3914 + ifr->ifr_metric = 0;
3915 + return 0;
3916 +
3917 + case SIOCSIFMETRIC: /* Set the metric on the interface (currently unused) */
3918 + return -EOPNOTSUPP;
3919 +
3920 + case SIOCGIFMTU: /* Get the MTU of a device */
3921 + ifr->ifr_mtu = dev->mtu;
3922 + return 0;
3923 +
3924 + case SIOCSIFMTU: /* Set the MTU of a device */
3925 + if (ifr->ifr_mtu == dev->mtu)
3926 + return 0;
3927 +
3928 + /*
3929 + * MTU must be positive.
3930 + */
3931 +
3932 + if (ifr->ifr_mtu<0)
3933 + return -EINVAL;
3934 +
3935 + if (!netif_device_present(dev))
3936 + return -ENODEV;
3937 +
3938 + if (dev->change_mtu)
3939 + err = dev->change_mtu(dev, ifr->ifr_mtu);
3940 + else {
3941 + dev->mtu = ifr->ifr_mtu;
3942 + err = 0;
3943 + }
3944 + if (!err && dev->flags&IFF_UP)
3945 + notifier_call_chain(&netdev_chain, NETDEV_CHANGEMTU, dev);
3946 + return err;
3947 +
3948 + case SIOCGIFHWADDR:
3949 + memcpy(ifr->ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
3950 + ifr->ifr_hwaddr.sa_family=dev->type;
3951 + return 0;
3952 +
3953 + case SIOCSIFHWADDR:
3954 + if (dev->set_mac_address == NULL)
3955 + return -EOPNOTSUPP;
3956 + if (ifr->ifr_hwaddr.sa_family!=dev->type)
3957 + return -EINVAL;
3958 + if (!netif_device_present(dev))
3959 + return -ENODEV;
3960 + err = dev->set_mac_address(dev, &ifr->ifr_hwaddr);
3961 + if (!err)
3962 + notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
3963 + return err;
3964 +
3965 + case SIOCSIFHWBROADCAST:
3966 + if (ifr->ifr_hwaddr.sa_family!=dev->type)
3967 + return -EINVAL;
3968 + memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data, MAX_ADDR_LEN);
3969 + notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
3970 + return 0;
3971 +
3972 + case SIOCGIFMAP:
3973 + ifr->ifr_map.mem_start=dev->mem_start;
3974 + ifr->ifr_map.mem_end=dev->mem_end;
3975 + ifr->ifr_map.base_addr=dev->base_addr;
3976 + ifr->ifr_map.irq=dev->irq;
3977 + ifr->ifr_map.dma=dev->dma;
3978 + ifr->ifr_map.port=dev->if_port;
3979 + return 0;
3980 +
3981 + case SIOCSIFMAP:
3982 + if (dev->set_config) {
3983 + if (!netif_device_present(dev))
3984 + return -ENODEV;
3985 + return dev->set_config(dev,&ifr->ifr_map);
3986 + }
3987 + return -EOPNOTSUPP;
3988 +
3989 + case SIOCADDMULTI:
3990 + if (dev->set_multicast_list == NULL ||
3991 + ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3992 + return -EINVAL;
3993 + if (!netif_device_present(dev))
3994 + return -ENODEV;
3995 + dev_mc_add(dev,ifr->ifr_hwaddr.sa_data, dev->addr_len, 1);
3996 + return 0;
3997 +
3998 + case SIOCDELMULTI:
3999 + if (dev->set_multicast_list == NULL ||
4000 + ifr->ifr_hwaddr.sa_family!=AF_UNSPEC)
4001 + return -EINVAL;
4002 + if (!netif_device_present(dev))
4003 + return -ENODEV;
4004 + dev_mc_delete(dev,ifr->ifr_hwaddr.sa_data,dev->addr_len, 1);
4005 + return 0;
4006 +
4007 + case SIOCGIFINDEX:
4008 + ifr->ifr_ifindex = dev->ifindex;
4009 + return 0;
4010 +
4011 + case SIOCGIFTXQLEN:
4012 + ifr->ifr_qlen = dev->tx_queue_len;
4013 + return 0;
4014 +
4015 + case SIOCSIFTXQLEN:
4016 + if (ifr->ifr_qlen<0)
4017 + return -EINVAL;
4018 + dev->tx_queue_len = ifr->ifr_qlen;
4019 + return 0;
4020 +
4021 + case SIOCSIFNAME:
4022 + if (dev->flags&IFF_UP)
4023 + return -EBUSY;
4024 + /* Check if name contains a wildcard */
4025 + if (strchr(ifr->ifr_newname, '%')) {
4026 + char format[IFNAMSIZ + 1];
4027 + int ret;
4028 + memcpy(format, ifr->ifr_newname, IFNAMSIZ);
4029 + format[IFNAMSIZ-1] = 0;
4030 + /* Find a free name based on format.
4031 + * dev_alloc_name() replaces "%d" with at max
4032 + * 2 digits, so no name overflow. - Jean II */
4033 + ret = dev_alloc_name(dev, format);
4034 + if (ret < 0)
4035 + return ret;
4036 + /* Copy the new name back to caller. */
4037 + strncpy(ifr->ifr_newname, dev->name, IFNAMSIZ);
4038 + } else {
4039 + if (__dev_get_by_name(ifr->ifr_newname))
4040 + return -EEXIST;
4041 + memcpy(dev->name, ifr->ifr_newname, IFNAMSIZ);
4042 + dev->name[IFNAMSIZ-1] = 0;
4043 + }
4044 + notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
4045 + return 0;
4046 +
4047 + /*
4048 + * Unknown or private ioctl
4049 + */
4050 +
4051 + default:
4052 + if ((cmd >= SIOCDEVPRIVATE &&
4053 + cmd <= SIOCDEVPRIVATE + 15) ||
4054 + cmd == SIOCBONDENSLAVE ||
4055 + cmd == SIOCBONDRELEASE ||
4056 + cmd == SIOCBONDSETHWADDR ||
4057 + cmd == SIOCBONDSLAVEINFOQUERY ||
4058 + cmd == SIOCBONDINFOQUERY ||
4059 + cmd == SIOCBONDCHANGEACTIVE ||
4060 + cmd == SIOCGMIIPHY ||
4061 + cmd == SIOCGMIIREG ||
4062 + cmd == SIOCSMIIREG ||
4063 + cmd == SIOCWANDEV) {
4064 + if (dev->do_ioctl) {
4065 + if (!netif_device_present(dev))
4066 + return -ENODEV;
4067 + return dev->do_ioctl(dev, ifr, cmd);
4068 + }
4069 + return -EOPNOTSUPP;
4070 + }
4071 +
4072 + }
4073 + return -EINVAL;
4074 +}
4075 +
4076 +/*
4077 + * This function handles all "interface"-type I/O control requests. The actual
4078 + * 'doing' part of this is dev_ifsioc above.
4079 + */
4080 +
4081 +/**
4082 + * dev_ioctl - network device ioctl
4083 + * @cmd: command to issue
4084 + * @arg: pointer to a struct ifreq in user space
4085 + *
4086 + * Issue ioctl functions to devices. This is normally called by the
4087 + * user space syscall interfaces but can sometimes be useful for
4088 + * other purposes. The return value is the return from the syscall if
4089 + * positive or a negative errno code on error.
4090 + */
4091 +
4092 +int dev_ioctl(unsigned int cmd, void *arg)
4093 +{
4094 + struct ifreq ifr;
4095 + int ret;
4096 + char *colon;
4097 +
4098 + /* One special case: SIOCGIFCONF takes ifconf argument
4099 + and requires shared lock, because it sleeps writing
4100 + to user space.
4101 + */
4102 +
4103 + if (cmd == SIOCGIFCONF) {
4104 + rtnl_shlock();
4105 + ret = dev_ifconf((char *) arg);
4106 + rtnl_shunlock();
4107 + return ret;
4108 + }
4109 + if (cmd == SIOCGIFNAME) {
4110 + return dev_ifname((struct ifreq *)arg);
4111 + }
4112 +
4113 + if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
4114 + return -EFAULT;
4115 +
4116 + ifr.ifr_name[IFNAMSIZ-1] = 0;
4117 +
4118 + colon = strchr(ifr.ifr_name, ':');
4119 + if (colon)
4120 + *colon = 0;
4121 +
4122 + /*
4123 + * See which interface the caller is talking about.
4124 + */
4125 +
4126 + switch(cmd)
4127 + {
4128 + /*
4129 + * These ioctl calls:
4130 + * - can be done by all.
4131 + * - atomic and do not require locking.
4132 + * - return a value
4133 + */
4134 +
4135 + case SIOCGIFFLAGS:
4136 + case SIOCGIFMETRIC:
4137 + case SIOCGIFMTU:
4138 + case SIOCGIFHWADDR:
4139 + case SIOCGIFSLAVE:
4140 + case SIOCGIFMAP:
4141 + case SIOCGIFINDEX:
4142 + case SIOCGIFTXQLEN:
4143 + dev_load(ifr.ifr_name);
4144 + read_lock(&dev_base_lock);
4145 + ret = dev_ifsioc(&ifr, cmd);
4146 + read_unlock(&dev_base_lock);
4147 + if (!ret) {
4148 + if (colon)
4149 + *colon = ':';
4150 + if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
4151 + return -EFAULT;
4152 + }
4153 + return ret;
4154 +
4155 + case SIOCETHTOOL:
4156 + dev_load(ifr.ifr_name);
4157 + rtnl_lock();
4158 + ret = dev_ethtool(&ifr);
4159 + rtnl_unlock();
4160 + if (!ret) {
4161 + if (colon)
4162 + *colon = ':';
4163 + if (copy_to_user(arg, &ifr,
4164 + sizeof(struct ifreq)))
4165 + ret = -EFAULT;
4166 + }
4167 + return ret;
4168 +
4169 + /*
4170 + * These ioctl calls:
4171 + * - require superuser power.
4172 + * - require strict serialization.
4173 + * - return a value
4174 + */
4175 +
4176 + case SIOCSIFNAME:
4177 + case SIOCGMIIPHY:
4178 + case SIOCGMIIREG:
4179 + if (!capable(CAP_NET_ADMIN))
4180 + return -EPERM;
4181 + dev_load(ifr.ifr_name);
4182 + dev_probe_lock();
4183 + rtnl_lock();
4184 + ret = dev_ifsioc(&ifr, cmd);
4185 + rtnl_unlock();
4186 + dev_probe_unlock();
4187 + if (!ret) {
4188 + if (colon)
4189 + *colon = ':';
4190 + if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
4191 + return -EFAULT;
4192 + }
4193 + return ret;
4194 +
4195 + /*
4196 + * These ioctl calls:
4197 + * - require superuser power.
4198 + * - require strict serialization.
4199 + * - do not return a value
4200 + */
4201 +
4202 + case SIOCSIFFLAGS:
4203 + case SIOCSIFMETRIC:
4204 + case SIOCSIFMTU:
4205 + case SIOCSIFMAP:
4206 + case SIOCSIFHWADDR:
4207 + case SIOCSIFSLAVE:
4208 + case SIOCADDMULTI:
4209 + case SIOCDELMULTI:
4210 + case SIOCSIFHWBROADCAST:
4211 + case SIOCSIFTXQLEN:
4212 + case SIOCSMIIREG:
4213 + case SIOCBONDENSLAVE:
4214 + case SIOCBONDRELEASE:
4215 + case SIOCBONDSETHWADDR:
4216 + case SIOCBONDSLAVEINFOQUERY:
4217 + case SIOCBONDINFOQUERY:
4218 + case SIOCBONDCHANGEACTIVE:
4219 + if (!capable(CAP_NET_ADMIN))
4220 + return -EPERM;
4221 + dev_load(ifr.ifr_name);
4222 + dev_probe_lock();
4223 + rtnl_lock();
4224 + ret = dev_ifsioc(&ifr, cmd);
4225 + rtnl_unlock();
4226 + dev_probe_unlock();
4227 + return ret;
4228 +
4229 + case SIOCGIFMEM:
4230 + /* Get the per device memory space. We can add this but currently
4231 + do not support it */
4232 + case SIOCSIFMEM:
4233 + /* Set the per device memory buffer space. Not applicable in our case */
4234 + case SIOCSIFLINK:
4235 + return -EINVAL;
4236 +
4237 + /*
4238 + * Unknown or private ioctl.
4239 + */
4240 +
4241 + default:
4242 + if (cmd == SIOCWANDEV ||
4243 + (cmd >= SIOCDEVPRIVATE &&
4244 + cmd <= SIOCDEVPRIVATE + 15)) {
4245 + dev_load(ifr.ifr_name);
4246 + dev_probe_lock();
4247 + rtnl_lock();
4248 + ret = dev_ifsioc(&ifr, cmd);
4249 + rtnl_unlock();
4250 + dev_probe_unlock();
4251 + if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
4252 + return -EFAULT;
4253 + return ret;
4254 + }
4255 +#ifdef WIRELESS_EXT
4256 + /* Take care of Wireless Extensions */
4257 + if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
4258 + /* If command is `set a parameter', or
4259 + * `get the encoding parameters', check if
4260 + * the user has the right to do it */
4261 + if (IW_IS_SET(cmd) || (cmd == SIOCGIWENCODE)) {
4262 + if(!capable(CAP_NET_ADMIN))
4263 + return -EPERM;
4264 + }
4265 + dev_load(ifr.ifr_name);
4266 + rtnl_lock();
4267 + /* Follow me in net/core/wireless.c */
4268 + ret = wireless_process_ioctl(&ifr, cmd);
4269 + rtnl_unlock();
4270 + if (!ret && IW_IS_GET(cmd) &&
4271 + copy_to_user(arg, &ifr, sizeof(struct ifreq)))
4272 + return -EFAULT;
4273 + return ret;
4274 + }
4275 +#endif /* WIRELESS_EXT */
4276 + return -EINVAL;
4277 + }
4278 +}
4279 +
4280 +
4281 +/**
4282 + * dev_new_index - allocate an ifindex
4283 + *
4284 + * Returns a suitable unique value for a new device interface
4285 + * number. The caller must hold the rtnl semaphore or the
4286 + * dev_base_lock to be sure it remains unique.
4287 + */
4288 +
4289 +int dev_new_index(void)
4290 +{
4291 + static int ifindex;
4292 + for (;;) {
4293 + if (++ifindex <= 0)
4294 + ifindex=1;
4295 + if (__dev_get_by_index(ifindex) == NULL)
4296 + return ifindex;
4297 + }
4298 +}
4299 +
4300 +static int dev_boot_phase = 1;
4301 +
4302 +/**
4303 + * register_netdevice - register a network device
4304 + * @dev: device to register
4305 + *
4306 + * Take a completed network device structure and add it to the kernel
4307 + * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
4308 + * chain. 0 is returned on success. A negative errno code is returned
4309 + * on a failure to set up the device, or if the name is a duplicate.
4310 + *
4311 + * Callers must hold the rtnl semaphore. See the comment at the
4312 + * end of Space.c for details about the locking. You may want
4313 + * register_netdev() instead of this.
4314 + *
4315 + * BUGS:
4316 + * The locking appears insufficient to guarantee two parallel registers
4317 + * will not get the same name.
4318 + */
4319 +
4320 +int net_dev_init(void);
4321 +
4322 +int register_netdevice(struct net_device *dev)
4323 +{
4324 + struct net_device *d, **dp;
4325 +#ifdef CONFIG_NET_DIVERT
4326 + int ret;
4327 +#endif
4328 +
4329 + spin_lock_init(&dev->queue_lock);
4330 + spin_lock_init(&dev->xmit_lock);
4331 + dev->xmit_lock_owner = -1;
4332 +#ifdef CONFIG_NET_FASTROUTE
4333 + dev->fastpath_lock=RW_LOCK_UNLOCKED;
4334 +#endif
4335 +
4336 + if (dev_boot_phase)
4337 + net_dev_init();
4338 +
4339 +#ifdef CONFIG_NET_DIVERT
4340 + ret = alloc_divert_blk(dev);
4341 + if (ret)
4342 + return ret;
4343 +#endif /* CONFIG_NET_DIVERT */
4344 +
4345 + dev->iflink = -1;
4346 +
4347 + /* Init, if this function is available */
4348 + if (dev->init && dev->init(dev) != 0) {
4349 +#ifdef CONFIG_NET_DIVERT
4350 + free_divert_blk(dev);
4351 +#endif
4352 + return -EIO;
4353 + }
4354 +
4355 + dev->ifindex = dev_new_index();
4356 + if (dev->iflink == -1)
4357 + dev->iflink = dev->ifindex;
4358 +
4359 + /* Check for existence, and append to tail of chain */
4360 + for (dp=&dev_base; (d=*dp) != NULL; dp=&d->next) {
4361 + if (d == dev || strcmp(d->name, dev->name) == 0) {
4362 +#ifdef CONFIG_NET_DIVERT
4363 + free_divert_blk(dev);
4364 +#endif
4365 + return -EEXIST;
4366 + }
4367 + }
4368 +
4369 + /* Fix illegal SG+CSUM combinations. */
4370 + if ((dev->features & NETIF_F_SG) &&
4371 + !(dev->features & (NETIF_F_IP_CSUM |
4372 + NETIF_F_NO_CSUM |
4373 + NETIF_F_HW_CSUM))) {
4374 + printk("%s: Dropping NETIF_F_SG since no checksum feature.\n",
4375 + dev->name);
4376 + dev->features &= ~NETIF_F_SG;
4377 + }
4378 +
4379 + /*
4380 + * nil rebuild_header routine,
4381 + * that should be never called and used as just bug trap.
4382 + */
4383 +
4384 + if (dev->rebuild_header == NULL)
4385 + dev->rebuild_header = default_rebuild_header;
4386 +
4387 + /*
4388 + * Default initial state at registry is that the
4389 + * device is present.
4390 + */
4391 +
4392 + set_bit(__LINK_STATE_PRESENT, &dev->state);
4393 +
4394 + dev->next = NULL;
4395 + dev_init_scheduler(dev);
4396 + write_lock_bh(&dev_base_lock);
4397 + *dp = dev;
4398 + dev_hold(dev);
4399 + dev->deadbeaf = 0;
4400 + write_unlock_bh(&dev_base_lock);
4401 +
4402 + /* Notify protocols, that a new device appeared. */
4403 + notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
4404 +
4405 + net_run_sbin_hotplug(dev, "register");
4406 +
4407 + return 0;
4408 +}
4409 +
4410 +/**
4411 + * netdev_finish_unregister - complete unregistration
4412 + * @dev: device
4413 + *
4414 + * Destroy and free a dead device. A value of zero is returned on
4415 + * success.
4416 + */
4417 +
4418 +int netdev_finish_unregister(struct net_device *dev)
4419 +{
4420 + BUG_TRAP(dev->ip_ptr==NULL);
4421 + BUG_TRAP(dev->ip6_ptr==NULL);
4422 + BUG_TRAP(dev->dn_ptr==NULL);
4423 +
4424 + if (!dev->deadbeaf) {
4425 + printk(KERN_ERR "Freeing alive device %p, %s\n", dev, dev->name);
4426 + return 0;
4427 + }
4428 +#ifdef NET_REFCNT_DEBUG
4429 + printk(KERN_DEBUG "netdev_finish_unregister: %s%s.\n", dev->name,
4430 + (dev->features & NETIF_F_DYNALLOC)?"":", old style");
4431 +#endif
4432 + if (dev->destructor)
4433 + dev->destructor(dev);
4434 + if (dev->features & NETIF_F_DYNALLOC)
4435 + kfree(dev);
4436 + return 0;
4437 +}
4438 +
4439 +/**
4440 + * unregister_netdevice - remove device from the kernel
4441 + * @dev: device
4442 + *
4443 + * This function shuts down a device interface and removes it
4444 + * from the kernel tables. On success 0 is returned, on a failure
4445 + * a negative errno code is returned.
4446 + *
4447 + * Callers must hold the rtnl semaphore. See the comment at the
4448 + * end of Space.c for details about the locking. You may want
4449 + * unregister_netdev() instead of this.
4450 + */
4451 +
4452 +int unregister_netdevice(struct net_device *dev)
4453 +{
4454 + unsigned long now, warning_time;
4455 + struct net_device *d, **dp;
4456 +
4457 + /* If device is running, close it first. */
4458 + if (dev->flags & IFF_UP)
4459 + dev_close(dev);
4460 +
4461 + BUG_TRAP(dev->deadbeaf==0);
4462 + dev->deadbeaf = 1;
4463 +
4464 + /* And unlink it from device chain. */
4465 + for (dp = &dev_base; (d=*dp) != NULL; dp=&d->next) {
4466 + if (d == dev) {
4467 + write_lock_bh(&dev_base_lock);
4468 + *dp = d->next;
4469 + write_unlock_bh(&dev_base_lock);
4470 + break;
4471 + }
4472 + }
4473 + if (d == NULL) {
4474 + printk(KERN_DEBUG "unregister_netdevice: device %s/%p never was registered\n", dev->name, dev);
4475 + return -ENODEV;
4476 + }
4477 +
4478 + /* Synchronize to net_rx_action. */
4479 + br_write_lock_bh(BR_NETPROTO_LOCK);
4480 + br_write_unlock_bh(BR_NETPROTO_LOCK);
4481 +
4482 + if (dev_boot_phase == 0) {
4483 +#ifdef CONFIG_NET_FASTROUTE
4484 + dev_clear_fastroute(dev);
4485 +#endif
4486 +
4487 + /* Shutdown queueing discipline. */
4488 + dev_shutdown(dev);
4489 +
4490 + net_run_sbin_hotplug(dev, "unregister");
4491 +
4492 + /* Notify protocols, that we are about to destroy
4493 + this device. They should clean all the things.
4494 + */
4495 + notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
4496 +
4497 + /*
4498 + * Flush the multicast chain
4499 + */
4500 + dev_mc_discard(dev);
4501 + }
4502 +
4503 + if (dev->uninit)
4504 + dev->uninit(dev);
4505 +
4506 + /* Notifier chain MUST detach us from master device. */
4507 + BUG_TRAP(dev->master==NULL);
4508 +
4509 +#ifdef CONFIG_NET_DIVERT
4510 + free_divert_blk(dev);
4511 +#endif
4512 +
4513 + if (dev->features & NETIF_F_DYNALLOC) {
4514 +#ifdef NET_REFCNT_DEBUG
4515 + if (atomic_read(&dev->refcnt) != 1)
4516 + printk(KERN_DEBUG "unregister_netdevice: holding %s refcnt=%d\n", dev->name, atomic_read(&dev->refcnt)-1);
4517 +#endif
4518 + dev_put(dev);
4519 + return 0;
4520 + }
4521 +
4522 + /* Last reference is our one */
4523 + if (atomic_read(&dev->refcnt) == 1) {
4524 + dev_put(dev);
4525 + return 0;
4526 + }
4527 +
4528 +#ifdef NET_REFCNT_DEBUG
4529 + printk("unregister_netdevice: waiting %s refcnt=%d\n", dev->name, atomic_read(&dev->refcnt));
4530 +#endif
4531 +
4532 + /* EXPLANATION. If dev->refcnt is not now 1 (our own reference)
4533 + it means that someone in the kernel still has a reference
4534 + to this device and we cannot release it.
4535 +
4536 + "New style" devices have destructors, hence we can return from this
4537 + function and destructor will do all the work later. As of kernel 2.4.0
4538 + there are very few "New Style" devices.
4539 +
4540 + "Old style" devices expect that the device is free of any references
4541 + upon exit from this function.
4542 + We cannot return from this function until all such references have
4543 + fallen away. This is because the caller of this function will probably
4544 + immediately kfree(*dev) and then be unloaded via sys_delete_module.
4545 +
4546 + So, we linger until all references fall away. The duration of the
4547 + linger is basically unbounded! It is driven by, for example, the
4548 + current setting of sysctl_ipfrag_time.
4549 +
4550 + After 1 second, we start to rebroadcast unregister notifications
4551 + in hope that careless clients will release the device.
4552 +
4553 + */
4554 +
4555 + now = warning_time = jiffies;
4556 + while (atomic_read(&dev->refcnt) != 1) {
4557 + if ((jiffies - now) > 1*HZ) {
4558 + /* Rebroadcast unregister notification */
4559 + notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
4560 + }
4561 + current->state = TASK_INTERRUPTIBLE;
4562 + schedule_timeout(HZ/4);
4563 + current->state = TASK_RUNNING;
4564 + if ((jiffies - warning_time) > 10*HZ) {
4565 + printk(KERN_EMERG "unregister_netdevice: waiting for %s to "
4566 + "become free. Usage count = %d\n",
4567 + dev->name, atomic_read(&dev->refcnt));
4568 + warning_time = jiffies;
4569 + }
4570 + }
4571 + dev_put(dev);
4572 + return 0;
4573 +}
4574 +
4575 +
4576 +/*
4577 + * Initialize the DEV module. At boot time this walks the device list and
4578 + * unhooks any devices that fail to initialise (normally hardware not
4579 + * present) and leaves us with a valid list of present and active devices.
4580 + *
4581 + */
4582 +
4583 +extern void net_device_init(void);
4584 +extern void ip_auto_config(void);
4585 +struct proc_dir_entry *proc_net_drivers;
4586 +#ifdef CONFIG_NET_DIVERT
4587 +extern void dv_init(void);
4588 +#endif /* CONFIG_NET_DIVERT */
4589 +
4590 +
4591 +/*
4592 + * Callers must hold the rtnl semaphore. See the comment at the
4593 + * end of Space.c for details about the locking.
4594 + */
4595 +int __init net_dev_init(void)
4596 +{
4597 + struct net_device *dev, **dp;
4598 + int i;
4599 +
4600 + if (!dev_boot_phase)
4601 + return 0;
4602 +
4603 +
4604 +#ifdef CONFIG_NET_DIVERT
4605 + dv_init();
4606 +#endif /* CONFIG_NET_DIVERT */
4607 +
4608 + /*
4609 + * Initialise the packet receive queues.
4610 + */
4611 +
4612 + for (i = 0; i < NR_CPUS; i++) {
4613 + struct softnet_data *queue;
4614 +
4615 + queue = &softnet_data[i];
4616 + skb_queue_head_init(&queue->input_pkt_queue);
4617 + queue->throttle = 0;
4618 + queue->cng_level = 0;
4619 + queue->avg_blog = 10; /* arbitrary non-zero */
4620 + queue->completion_queue = NULL;
4621 + INIT_LIST_HEAD(&queue->poll_list);
4622 + set_bit(__LINK_STATE_START, &queue->blog_dev.state);
4623 + queue->blog_dev.weight = weight_p;
4624 + queue->blog_dev.poll = process_backlog;
4625 + atomic_set(&queue->blog_dev.refcnt, 1);
4626 + }
4627 +
4628 +#ifdef CONFIG_NET_PROFILE
4629 + net_profile_init();
4630 + NET_PROFILE_REGISTER(dev_queue_xmit);
4631 + NET_PROFILE_REGISTER(softnet_process);
4632 +#endif
4633 +
4634 +#ifdef OFFLINE_SAMPLE
4635 + samp_timer.expires = jiffies + (10 * HZ);
4636 + add_timer(&samp_timer);
4637 +#endif
4638 +
4639 + /*
4640 + * Add the devices.
4641 + * If the call to dev->init fails, the dev is removed
4642 + * from the chain disconnecting the device until the
4643 + * next reboot.
4644 + *
4645 + * NB At boot phase networking is dead. No locking is required.
4646 + * But we still preserve dev_base_lock for sanity.
4647 + */
4648 +
4649 + dp = &dev_base;
4650 + while ((dev = *dp) != NULL) {
4651 + spin_lock_init(&dev->queue_lock);
4652 + spin_lock_init(&dev->xmit_lock);
4653 +#ifdef CONFIG_NET_FASTROUTE
4654 + dev->fastpath_lock = RW_LOCK_UNLOCKED;
4655 +#endif
4656 + dev->xmit_lock_owner = -1;
4657 + dev->iflink = -1;
4658 + dev_hold(dev);
4659 +
4660 + /*
4661 + * Allocate name. If the init() fails
4662 + * the name will be reissued correctly.
4663 + */
4664 + if (strchr(dev->name, '%'))
4665 + dev_alloc_name(dev, dev->name);
4666 +
4667 + /*
4668 + * Check boot time settings for the device.
4669 + */
4670 + netdev_boot_setup_check(dev);
4671 +
4672 + if (dev->init && dev->init(dev)) {
4673 + /*
4674 + * It failed to come up. It will be unhooked later.
4675 + * dev_alloc_name can now advance to next suitable
4676 + * name that is checked next.
4677 + */
4678 + dev->deadbeaf = 1;
4679 + dp = &dev->next;
4680 + } else {
4681 + dp = &dev->next;
4682 + dev->ifindex = dev_new_index();
4683 + if (dev->iflink == -1)
4684 + dev->iflink = dev->ifindex;
4685 + if (dev->rebuild_header == NULL)
4686 + dev->rebuild_header = default_rebuild_header;
4687 + dev_init_scheduler(dev);
4688 + set_bit(__LINK_STATE_PRESENT, &dev->state);
4689 + }
4690 + }
4691 +
4692 + /*
4693 + * Unhook devices that failed to come up
4694 + */
4695 + dp = &dev_base;
4696 + while ((dev = *dp) != NULL) {
4697 + if (dev->deadbeaf) {
4698 + write_lock_bh(&dev_base_lock);
4699 + *dp = dev->next;
4700 + write_unlock_bh(&dev_base_lock);
4701 + dev_put(dev);
4702 + } else {
4703 + dp = &dev->next;
4704 + }
4705 + }
4706 +
4707 +#ifdef CONFIG_PROC_FS
4708 + proc_net_create("dev", 0, dev_get_info);
4709 + create_proc_read_entry("net/softnet_stat", 0, 0, dev_proc_stats, NULL);
4710 + proc_net_drivers = proc_mkdir("net/drivers", 0);
4711 +#ifdef WIRELESS_EXT
4712 + /* Available in net/core/wireless.c */
4713 + proc_net_create("wireless", 0, dev_get_wireless_info);
4714 +#endif /* WIRELESS_EXT */
4715 +#endif /* CONFIG_PROC_FS */
4716 +
4717 + dev_boot_phase = 0;
4718 +
4719 + open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
4720 + open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
4721 +
4722 + dst_init();
4723 + dev_mcast_init();
4724 +
4725 +#ifdef CONFIG_NET_SCHED
4726 + pktsched_init();
4727 +#endif
4728 + /*
4729 + * Initialise network devices
4730 + */
4731 +
4732 + net_device_init();
4733 +
4734 + return 0;
4735 +}
4736 +
4737 +#ifdef CONFIG_HOTPLUG
4738 +
4739 +/* Notify userspace when a netdevice event occurs,
4740 + * by running '/sbin/hotplug net' with certain
4741 + * environment variables set.
4742 + */
4743 +
4744 +static int net_run_sbin_hotplug(struct net_device *dev, char *action)
4745 +{
4746 + char *argv[3], *envp[5], ifname[12 + IFNAMSIZ], action_str[32];
4747 + int i;
4748 +
4749 + sprintf(ifname, "INTERFACE=%s", dev->name);
4750 + sprintf(action_str, "ACTION=%s", action);
4751 +
4752 + i = 0;
4753 + argv[i++] = hotplug_path;
4754 + argv[i++] = "net";
4755 + argv[i] = 0;
4756 +
4757 + i = 0;
4758 + /* minimal command environment */
4759 + envp [i++] = "HOME=/";
4760 + envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4761 + envp [i++] = ifname;
4762 + envp [i++] = action_str;
4763 + envp [i] = 0;
4764 +
4765 + return call_usermodehelper(argv [0], argv, envp);
4766 +}
4767 +#endif
4768 diff --unified --recursive --new-file linux-2.4.30/net/netsyms.c linux-2.4.30-1-686-smp-ring3/net/netsyms.c
4769 --- linux-2.4.30/net/netsyms.c 2005-04-04 03:42:20.000000000 +0200
4770 +++ linux-2.4.30-1-686-smp-ring3/net/netsyms.c 2005-10-22 23:08:28.016050500 +0200
4771 @@ -628,3 +628,18 @@
4772 #endif /* CONFIG_NET_RADIO || CONFIG_NET_PCMCIA_RADIO */
4773
4774 #endif /* CONFIG_NET */
4775 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
4776 +#include <linux/version.h>
4777 +
4778 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
4779 +#include <linux/ring.h>
4780 +
4781 +EXPORT_SYMBOL(get_skb_ring_handler);
4782 +EXPORT_SYMBOL(set_skb_ring_handler);
4783 +EXPORT_SYMBOL(do_skb_ring_handler);
4784 +EXPORT_SYMBOL(get_buffer_ring_handler);
4785 +EXPORT_SYMBOL(set_buffer_ring_handler);
4786 +EXPORT_SYMBOL(do_buffer_ring_handler);
4787 +#endif
4788 +
4789 +#endif
4790 diff --unified --recursive --new-file linux-2.4.30/net/ring/Config.in linux-2.4.30-1-686-smp-ring3/net/ring/Config.in
4791 --- linux-2.4.30/net/ring/Config.in 1970-01-01 01:00:00.000000000 +0100
4792 +++ linux-2.4.30-1-686-smp-ring3/net/ring/Config.in 2005-10-22 23:08:28.048052500 +0200
4793 @@ -0,0 +1,4 @@
4794 +#
4795 +# PF_RING
4796 +#
4797 +tristate ' PF_RING (EXPERIMENTAL)' CONFIG_RING
4798 diff --unified --recursive --new-file linux-2.4.30/net/ring/Makefile linux-2.4.30-1-686-smp-ring3/net/ring/Makefile
4799 --- linux-2.4.30/net/ring/Makefile 1970-01-01 01:00:00.000000000 +0100
4800 +++ linux-2.4.30-1-686-smp-ring3/net/ring/Makefile 2005-10-22 23:08:27.420013250 +0200
4801 @@ -0,0 +1,16 @@
4802 +#
4803 +# Makefile for the ring driver.
4804 +#
4805 +
4806 +O_TARGET := ring.o
4807 +
4808 +export-objs := ring_packet.o
4809 +
4810 +obj-y := ring_packet.o
4811 +
4812 +ifeq ($(CONFIG_RING),m)
4813 + obj-m += $(O_TARGET)
4814 +endif
4815 +
4816 +include $(TOPDIR)/Rules.make
4817 +
4818 diff --unified --recursive --new-file linux-2.4.30/net/ring/ring_packet.c linux-2.4.30-1-686-smp-ring3/net/ring/ring_packet.c
4819 --- linux-2.4.30/net/ring/ring_packet.c 1970-01-01 01:00:00.000000000 +0100
4820 +++ linux-2.4.30-1-686-smp-ring3/net/ring/ring_packet.c 2005-10-22 23:08:27.440014500 +0200
4821 @@ -0,0 +1,1623 @@
4822 +/*
4823 + *
4824 + * (C) 2004-05 - Luca Deri <deri@ntop.org>
4825 + *
4826 + * This code includes patches courtesy of
4827 + * - Jeff Randall <jrandall@nexvu.com>
4828 + * - Helmut Manck <helmut.manck@secunet.com>
4829 + * - Brad Doctor <bdoctor@ps-ax.com>
4830 + *
4831 + */
4832 +
4833 +/* FIX: add an entry inside the /proc filesystem */
4834 +
4835 +#include <linux/version.h>
4836 +#include <linux/config.h>
4837 +#include <linux/module.h>
4838 +#include <linux/kernel.h>
4839 +#include <linux/socket.h>
4840 +#include <linux/skbuff.h>
4841 +#include <linux/rtnetlink.h>
4842 +#include <linux/in.h>
4843 +#include <linux/in6.h>
4844 +#include <linux/init.h>
4845 +#include <linux/filter.h>
4846 +#include <linux/ring.h>
4847 +#include <linux/ip.h>
4848 +#include <linux/tcp.h>
4849 +#include <linux/udp.h>
4850 +#include <linux/list.h>
4851 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4852 +#include <net/xfrm.h>
4853 +#else
4854 +#include <linux/poll.h>
4855 +#endif
4856 +#include <net/sock.h>
4857 +#include <asm/io.h> /* needed for virt_to_phys() */
4858 +
4859 +/* #define RING_DEBUG */
4860 +
4861 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11))
4862 +static inline int remap_page_range(struct vm_area_struct *vma,
4863 + unsigned long uvaddr,
4864 + unsigned long paddr,
4865 + unsigned long size,
4866 + pgprot_t prot) {
4867 + return(remap_pfn_range(vma, uvaddr, paddr >> PAGE_SHIFT,
4868 + size, prot));
4869 +}
4870 +#endif
4871 +
4872 +/* ************************************************* */
4873 +
4874 +#define CLUSTER_LEN 8
4875 +
4876 +struct ring_cluster {
4877 + u_short cluster_id; /* 0 = no cluster */
4878 + u_short num_cluster_elements;
4879 + enum cluster_type hashing_mode;
4880 + u_short hashing_id;
4881 + struct sock *sk[CLUSTER_LEN];
4882 + struct ring_cluster *next; /* NULL = last element of the cluster */
4883 +};
4884 +
4885 +/* ************************************************* */
4886 +
4887 +struct ring_element {
4888 + struct list_head list;
4889 + struct sock *sk;
4890 +};
4891 +
4892 +/* ************************************************* */
4893 +
4894 +struct ring_opt {
4895 + struct net_device *ring_netdev;
4896 +
4897 + /* Cluster */
4898 + u_short cluster_id; /* 0 = no cluster */
4899 +
4900 + /* Reflector */
4901 + struct net_device *reflector_dev;
4902 +
4903 + /* Packet buffers */
4904 + unsigned long order;
4905 +
4906 + /* Ring Slots */
4907 + unsigned long ring_memory;
4908 + FlowSlotInfo *slots_info; /* Basically it points to ring_memory */
4909 + char *ring_slots; /* Basically it points to ring_memory
4910 + +sizeof(FlowSlotInfo) */
4911 +
4912 + /* Packet Sampling */
4913 + u_int pktToSample, sample_rate;
4914 +
4915 + /* BPF Filter */
4916 + struct sk_filter *bpfFilter;
4917 +
4918 + /* Locks */
4919 + atomic_t num_ring_slots_waiters;
4920 + wait_queue_head_t ring_slots_waitqueue;
4921 + rwlock_t ring_index_lock;
4922 +
4923 + /* Indexes (Internal) */
4924 + u_int insert_page_id, insert_slot_id;
4925 +};
4926 +
4927 +/* ************************************************* */
4928 +
4929 +/* List of all ring sockets. */
4930 +static struct list_head ring_table;
4931 +
4932 +/* List of all clusters */
4933 +static struct ring_cluster *ring_cluster_list;
4934 +
4935 +static rwlock_t ring_mgmt_lock = RW_LOCK_UNLOCKED;
4936 +
4937 +/* ********************************** */
4938 +
4939 +/* Forward */
4940 +static struct proto_ops ring_ops;
4941 +
4942 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
4943 +static struct proto ring_proto;
4944 +#endif
4945 +
4946 +static int skb_ring_handler(struct sk_buff *skb, u_char recv_packet,
4947 + u_char real_skb);
4948 +static int buffer_ring_handler(struct net_device *dev, char *data, int len);
4949 +static int remove_from_cluster(struct sock *sock, struct ring_opt *pfr);
4950 +
4951 +/* Extern */
4952 +
4953 +/* ********************************** */
4954 +
4955 +/* Defaults */
4956 +static u_int bucket_len = 128, num_slots = 4096, sample_rate = 1,
4957 + transparent_mode = 0, enable_tx_capture = 0;
4958 +
4959 +MODULE_PARM(bucket_len, "i");
4960 +MODULE_PARM_DESC(bucket_len, "Number of ring buckets");
4961 +MODULE_PARM(num_slots, "i");
4962 +MODULE_PARM_DESC(num_slots, "Number of ring slots");
4963 +MODULE_PARM(sample_rate, "i");
4964 +MODULE_PARM_DESC(sample_rate, "Ring packet sample rate");
4965 +MODULE_PARM(transparent_mode, "i");
4966 +MODULE_PARM_DESC(transparent_mode,
4967 + "Set to 1 to set transparent mode "
4968 + "(slower but backwards compatible)");
4969 +MODULE_PARM(enable_tx_capture, "i");
4970 +MODULE_PARM_DESC(enable_tx_capture, "Set to 1 to capture outgoing packets");
4971 +
4972 +/* ********************************** */
4973 +
4974 +#define MIN_QUEUED_PKTS 64
4975 +#define MAX_QUEUE_LOOPS 64
4976 +
4977 +
4978 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4979 +#define ring_sk_datatype(__sk) ((struct ring_opt *)__sk)
4980 +#define ring_sk(__sk) ((__sk)->sk_protinfo)
4981 +#else
4982 +#define ring_sk_datatype(a) (a)
4983 +#define ring_sk(__sk) ((__sk)->protinfo.pf_ring)
4984 +#endif
4985 +
4986 +#define _rdtsc() ({ uint64_t x; asm volatile("rdtsc" : "=A" (x)); x; })
4987 +
4988 +/*
4989 + int dev_queue_xmit(struct sk_buff *skb)
4990 + skb->dev;
4991 + struct net_device *dev_get_by_name(const char *name)
4992 +*/
4993 +
4994 +/* ********************************** */
4995 +
4996 +static void ring_sock_destruct(struct sock *sk) {
4997 +
4998 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4999 + skb_queue_purge(&sk->sk_receive_queue);
5000 +
5001 + if (!sock_flag(sk, SOCK_DEAD)) {
5002 +#if defined(RING_DEBUG)
5003 + printk("Attempt to release alive ring socket: %p\n", sk);
5004 +#endif
5005 + return;
5006 + }
5007 +
5008 + BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
5009 + BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
5010 +#else
5011 +
5012 + BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
5013 + BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
5014 +
5015 + if (!sk->dead) {
5016 +#if defined(RING_DEBUG)
5017 + printk("Attempt to release alive ring socket: %p\n", sk);
5018 +#endif
5019 + return;
5020 + }
5021 +#endif
5022 +
5023 + kfree(ring_sk(sk));
5024 +
5025 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
5026 + MOD_DEC_USE_COUNT;
5027 +#endif
5028 +}
5029 +
5030 +/* ********************************** */
5031 +/*
5032 + * ring_insert()
5033 + *
5034 + * store the sk in a new element and add it
5035 + * to the head of the list.
5036 + */
5037 +static inline void ring_insert(struct sock *sk) {
5038 + struct ring_element *next;
5039 +
5040 +#if defined(RING_DEBUG)
5041 + printk("RING: ring_insert()\n");
5042 +#endif
5043 +
5044 + next = kmalloc(sizeof(struct ring_element), GFP_ATOMIC);
5045 + if(next != NULL) {
5046 + next->sk = sk;
5047 + write_lock_irq(&ring_mgmt_lock);
5048 + list_add(&next->list, &ring_table);
5049 + write_unlock_irq(&ring_mgmt_lock);
5050 + } else {
5051 + if (net_ratelimit())
5052 + printk("RING: could not kmalloc slot!!\n");
5053 + }
5054 +}
5055 +
5056 +/* ********************************** */
5057 +/*
5058 + * ring_remove()
5059 + *
5060 + * For each of the elements in the list:
5061 + * - check if this is the element we want to delete
5062 + * - if it is, remove it from the list, and free it.
5063 + *
5064 + * stop when we find the one we're looking for (break),
5065 + * or when we reach the end of the list.
5066 + */
5067 +static inline void ring_remove(struct sock *sk) {
5068 + struct list_head *ptr;
5069 + struct ring_element *entry;
5070 +
5071 +
5072 + for(ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
5073 + entry = list_entry(ptr, struct ring_element, list);
5074 +
5075 + if(entry->sk == sk) {
5076 + write_lock_irq(&ring_mgmt_lock);
5077 + list_del(ptr);
5078 + kfree(ptr);
5079 + write_unlock_irq(&ring_mgmt_lock);
5080 + break;
5081 + }
5082 + }
5083 +
5084 +}
5085 +
5086 +/* ********************************** */
5087 +
5088 +static u_int32_t num_queued_pkts(struct ring_opt *pfr) {
5089 +
5090 + if(pfr->ring_slots != NULL) {
5091 +
5092 + u_int32_t tot_insert = pfr->slots_info->insert_idx,
5093 +#if defined(RING_DEBUG)
5094 + tot_read = pfr->slots_info->tot_read, tot_pkts;
5095 +#else
5096 + tot_read = pfr->slots_info->tot_read;
5097 +#endif
5098 +
5099 + if(tot_insert >= tot_read) {
5100 +#if defined(RING_DEBUG)
5101 + tot_pkts = tot_insert-tot_read;
5102 +#endif
5103 + return(tot_insert-tot_read);
5104 + } else {
5105 +#if defined(RING_DEBUG)
5106 + tot_pkts = ((u_int32_t)-1)+tot_insert-tot_read;
5107 +#endif
5108 + return(((u_int32_t)-1)+tot_insert-tot_read);
5109 + }
5110 +
5111 +#if defined(RING_DEBUG)
5112 + printk("-> num_queued_pkts=%d [tot_insert=%d][tot_read=%d]\n",
5113 + tot_pkts, tot_insert, tot_read);
5114 +#endif
5115 +
5116 + } else
5117 + return(0);
5118 +}
5119 +
5120 +/* ********************************** */
5121 +
5122 +static inline FlowSlot* get_insert_slot(struct ring_opt *pfr) {
5123 +#if defined(RING_DEBUG)
5124 + printk("get_insert_slot(%d)\n", pfr->slots_info->insert_idx);
5125 +#endif
5126 +
5127 + if(pfr->ring_slots != NULL) {
5128 + FlowSlot *slot = (FlowSlot*)&(pfr->ring_slots[pfr->slots_info->insert_idx
5129 + *pfr->slots_info->slot_len]);
5130 + return(slot);
5131 + } else
5132 + return(NULL);
5133 +}
5134 +
5135 +/* ********************************** */
5136 +
5137 +static inline FlowSlot* get_remove_slot(struct ring_opt *pfr) {
5138 +#if defined(RING_DEBUG)
5139 + printk("get_remove_slot(%d)\n", pfr->slots_info->remove_idx);
5140 +#endif
5141 +
5142 + if(pfr->ring_slots != NULL)
5143 + return((FlowSlot*)&(pfr->ring_slots[pfr->slots_info->remove_idx*
5144 + pfr->slots_info->slot_len]));
5145 + else
5146 + return(NULL);
5147 +}
5148 +
5149 +/* ********************************** */
5150 +
5151 +static void add_skb_to_ring(struct sk_buff *skb,
5152 + struct ring_opt *pfr,
5153 + u_char recv_packet,
5154 + u_char real_skb /* 1=skb 0=faked skb */) {
5155 + FlowSlot *theSlot;
5156 + int idx, displ;
5157 +
5158 + if(recv_packet) {
5159 + /* Hack for identifying a packet received by the e1000 */
5160 + if(real_skb) {
5161 + displ = SKB_DISPLACEMENT;
5162 + } else
5163 + displ = 0; /* Received by the e1000 wrapper */
5164 + } else
5165 + displ = 0;
5166 +
5167 + write_lock(&pfr->ring_index_lock);
5168 + pfr->slots_info->tot_pkts++;
5169 + write_unlock(&pfr->ring_index_lock);
5170 +
5171 + /* BPF Filtering (from af_packet.c) */
5172 + if(pfr->bpfFilter != NULL) {
5173 + unsigned res = 1, len;
5174 +
5175 + len = skb->len-skb->data_len;
5176 +
5177 + write_lock(&pfr->ring_index_lock);
5178 + skb->data -= displ;
5179 + res = sk_run_filter(skb, pfr->bpfFilter->insns, pfr->bpfFilter->len);
5180 + skb->data += displ;
5181 + write_unlock(&pfr->ring_index_lock);
5182 +
5183 + if(res == 0) {
5184 + /* Filter failed */
5185 +
5186 +#if defined(RING_DEBUG)
5187 + printk("add_skb_to_ring(skb): Filter failed [len=%d][tot=%llu]"
5188 + "[insertIdx=%d][pkt_type=%d][cloned=%d]\n",
5189 + (int)skb->len, pfr->slots_info->tot_pkts,
5190 + pfr->slots_info->insert_idx,
5191 + skb->pkt_type, skb->cloned);
5192 +#endif
5193 +
5194 + return;
5195 + }
5196 + }
5197 +
5198 + /* ************************** */
5199 +
5200 + if(pfr->sample_rate > 1) {
5201 + if(pfr->pktToSample == 0) {
5202 + write_lock(&pfr->ring_index_lock);
5203 + pfr->pktToSample = pfr->sample_rate;
5204 + write_unlock(&pfr->ring_index_lock);
5205 + } else {
5206 + write_lock(&pfr->ring_index_lock);
5207 + pfr->pktToSample--;
5208 + write_unlock(&pfr->ring_index_lock);
5209 +
5210 +#if defined(RING_DEBUG)
5211 + printk("add_skb_to_ring(skb): sampled packet [len=%d]"
5212 + "[tot=%llu][insertIdx=%d][pkt_type=%d][cloned=%d]\n",
5213 + (int)skb->len, pfr->slots_info->tot_pkts,
5214 + pfr->slots_info->insert_idx,
5215 + skb->pkt_type, skb->cloned);
5216 +#endif
5217 + return;
5218 + }
5219 + }
5220 +
5221 + /* ************************************* */
5222 +
5223 + if((pfr->reflector_dev != NULL)
5224 + && (!netif_queue_stopped(pfr->reflector_dev))) {
5225 + int cpu = smp_processor_id();
5226 +
5227 + /* increase reference counter so that this skb is not freed */
5228 + atomic_inc(&skb->users);
5229 +
5230 + skb->data -= displ;
5231 +
5232 + /* send it */
5233 + if (pfr->reflector_dev->xmit_lock_owner != cpu) {
5234 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
5235 + pfr->reflector_dev->xmit_lock_owner = cpu;
5236 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
5237 +
5238 + if (pfr->reflector_dev->hard_start_xmit(skb,
5239 + pfr->reflector_dev) == 0) {
5240 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
5241 + pfr->reflector_dev->xmit_lock_owner = -1;
5242 + skb->data += displ;
5243 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
5244 +#if defined(RING_DEBUG)
5245 + printk("++ hard_start_xmit succeeded\n");
5246 +#endif
5247 + return; /* OK */
5248 + }
5249 +
5250 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
5251 + pfr->reflector_dev->xmit_lock_owner = -1;
5252 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
5253 + }
5254 +
5255 +#if defined(RING_DEBUG)
5256 + printk("++ hard_start_xmit failed\n");
5257 +#endif
5258 + skb->data += displ;
5259 + return; /* -ENETDOWN */
5260 + }
5261 +
5262 + /* ************************************* */
5263 +
5264 +#if defined(RING_DEBUG)
5265 + printk("add_skb_to_ring(skb) [len=%d][tot=%llu][insertIdx=%d]"
5266 + "[pkt_type=%d][cloned=%d]\n",
5267 + (int)skb->len, pfr->slots_info->tot_pkts,
5268 + pfr->slots_info->insert_idx,
5269 + skb->pkt_type, skb->cloned);
5270 +#endif
5271 +
5272 + idx = pfr->slots_info->insert_idx;
5273 + theSlot = get_insert_slot(pfr);
5274 +
5275 + if((theSlot != NULL) && (theSlot->slot_state == 0)) {
5276 + struct pcap_pkthdr *hdr;
5277 + unsigned int bucketSpace;
5278 + char *bucket;
5279 +
5280 + /* Update Index */
5281 + idx++;
5282 +
5283 + if(idx == pfr->slots_info->tot_slots) {
5284 + write_lock(&pfr->ring_index_lock);
5285 + pfr->slots_info->insert_idx = 0;
5286 + write_unlock(&pfr->ring_index_lock);
5287 + } else {
5288 + write_lock(&pfr->ring_index_lock);
5289 + pfr->slots_info->insert_idx = idx;
5290 + write_unlock(&pfr->ring_index_lock);
5291 + }
5292 +
5293 + bucketSpace = pfr->slots_info->slot_len
5294 +#ifdef RING_MAGIC
5295 + - sizeof(u_char)
5296 +#endif
5297 + - sizeof(u_char) /* flowSlot.slot_state */
5298 + - sizeof(struct pcap_pkthdr)
5299 + - 1 /* 10 */ /* safe boundary */;
5300 +
5301 + bucket = &theSlot->bucket;
5302 + hdr = (struct pcap_pkthdr*)bucket;
5303 +
5304 + if(skb->stamp.tv_sec == 0) do_gettimeofday(&skb->stamp);
5305 +
5306 + hdr->ts.tv_sec = skb->stamp.tv_sec, hdr->ts.tv_usec = skb->stamp.tv_usec;
5307 + hdr->caplen = skb->len+displ;
5308 +
5309 + if(hdr->caplen > bucketSpace)
5310 + hdr->caplen = bucketSpace;
5311 +
5312 + hdr->len = skb->len+displ;
5313 + memcpy(&bucket[sizeof(struct pcap_pkthdr)],
5314 + skb->data-displ, hdr->caplen);
5315 +
5316 +#if defined(RING_DEBUG)
5317 + {
5318 + static unsigned int lastLoss = 0;
5319 +
5320 + if(pfr->slots_info->tot_lost
5321 + && (lastLoss != pfr->slots_info->tot_lost)) {
5322 + printk("add_skb_to_ring(%d): [bucketSpace=%d]"
5323 + "[hdr.caplen=%d][skb->len=%d]"
5324 + "[pcap_pkthdr=%d][removeIdx=%d]"
5325 + "[loss=%lu][page=%u][slot=%u]\n",
5326 + idx-1, bucketSpace, hdr->caplen, skb->len,
5327 + sizeof(struct pcap_pkthdr),
5328 + pfr->slots_info->remove_idx,
5329 + (long unsigned int)pfr->slots_info->tot_lost,
5330 + pfr->insert_page_id, pfr->insert_slot_id);
5331 +
5332 + lastLoss = pfr->slots_info->tot_lost;
5333 + }
5334 + }
5335 +#endif
5336 +
5337 + write_lock(&pfr->ring_index_lock);
5338 + pfr->slots_info->tot_insert++;
5339 + theSlot->slot_state = 1;
5340 + write_unlock(&pfr->ring_index_lock);
5341 + } else {
5342 + write_lock(&pfr->ring_index_lock);
5343 + pfr->slots_info->tot_lost++;
5344 + write_unlock(&pfr->ring_index_lock);
5345 +
5346 +#if defined(RING_DEBUG)
5347 + printk("add_skb_to_ring(skb): packet lost [loss=%lu]"
5348 + "[removeIdx=%u][insertIdx=%u]\n",
5349 + (long unsigned int)pfr->slots_info->tot_lost,
5350 + pfr->slots_info->remove_idx, pfr->slots_info->insert_idx);
5351 +#endif
5352 + }
5353 +
5354 + /* wakeup in case of poll() */
5355 + if(waitqueue_active(&pfr->ring_slots_waitqueue))
5356 + wake_up_interruptible(&pfr->ring_slots_waitqueue);
5357 +}
5358 +
5359 +/* ********************************** */
5360 +
5361 +static u_int hash_skb(struct ring_cluster *cluster_ptr,
5362 + struct sk_buff *skb, u_char recv_packet) {
5363 + u_int idx;
5364 + int displ;
5365 + struct iphdr *ip;
5366 +
5367 + if(cluster_ptr->hashing_mode == cluster_round_robin) {
5368 + idx = cluster_ptr->hashing_id++;
5369 + } else {
5370 + /* Per-flow clustering */
5371 + if(skb->len > sizeof(struct iphdr)+sizeof(struct tcphdr)) {
5372 + if(recv_packet)
5373 + displ = 0;
5374 + else
5375 + displ = SKB_DISPLACEMENT;
5376 +
5377 + /*
5378 + skb->data+displ
5379 +
5380 + Always points to to the IP part of the packet
5381 + */
5382 +
5383 + ip = (struct iphdr*)(skb->data+displ);
5384 +
5385 + idx = ip->saddr+ip->daddr+ip->protocol;
5386 +
5387 + if(ip->protocol == IPPROTO_TCP) {
5388 + struct tcphdr *tcp = (struct tcphdr*)(skb->data+displ
5389 + +sizeof(struct iphdr));
5390 + idx += tcp->source+tcp->dest;
5391 + } else if(ip->protocol == IPPROTO_UDP) {
5392 + struct udphdr *udp = (struct udphdr*)(skb->data+displ
5393 + +sizeof(struct iphdr));
5394 + idx += udp->source+udp->dest;
5395 + }
5396 + } else
5397 + idx = skb->len;
5398 + }
5399 +
5400 + return(idx % cluster_ptr->num_cluster_elements);
5401 +}
5402 +
5403 +/* ********************************** */
5404 +
5405 +static int skb_ring_handler(struct sk_buff *skb,
5406 + u_char recv_packet,
5407 + u_char real_skb /* 1=skb 0=faked skb */) {
5408 + struct sock *skElement;
5409 + int rc = 0;
5410 + struct list_head *ptr;
5411 + struct ring_cluster *cluster_ptr;
5412 +
5413 +#ifdef PROFILING
5414 + uint64_t rdt = _rdtsc(), rdt1, rdt2;
5415 +#endif
5416 +
5417 + if((!skb) /* Invalid skb */
5418 + || ((!enable_tx_capture) && (!recv_packet))) {
5419 + /*
5420 + An outgoing packet is about to be sent out
5421 + but we decided not to handle transmitted
5422 + packets.
5423 + */
5424 + return(0);
5425 + }
5426 +
5427 +#if defined(RING_DEBUG)
5428 + if(0) {
5429 + printk("skb_ring_handler() [len=%d][dev=%s]\n", skb->len,
5430 + skb->dev->name == NULL ? "<NULL>" : skb->dev->name);
5431 + }
5432 +#endif
5433 +
5434 +#ifdef PROFILING
5435 + rdt1 = _rdtsc();
5436 +#endif
5437 +
5438 + /* [1] Check unclustered sockets */
5439 + for (ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
5440 + struct ring_opt *pfr;
5441 + struct ring_element *entry;
5442 +
5443 + entry = list_entry(ptr, struct ring_element, list);
5444 +
5445 + read_lock(&ring_mgmt_lock);
5446 + skElement = entry->sk;
5447 + pfr = ring_sk(skElement);
5448 + read_unlock(&ring_mgmt_lock);
5449 +
5450 + if((pfr != NULL)
5451 + && (pfr->cluster_id == 0 /* No cluster */)
5452 + && (pfr->ring_slots != NULL)
5453 + && (pfr->ring_netdev == skb->dev)) {
5454 + /* We've found the ring where the packet can be stored */
5455 + read_lock(&ring_mgmt_lock);
5456 + add_skb_to_ring(skb, pfr, recv_packet, real_skb);
5457 + read_unlock(&ring_mgmt_lock);
5458 +
5459 + rc = 1; /* Ring found: we've done our job */
5460 + }
5461 + }
5462 +
5463 + /* [2] Check socket clusters */
5464 + cluster_ptr = ring_cluster_list;
5465 +
5466 + while(cluster_ptr != NULL) {
5467 + struct ring_opt *pfr;
5468 +
5469 + if(cluster_ptr->num_cluster_elements > 0) {
5470 + u_int skb_hash = hash_skb(cluster_ptr, skb, recv_packet);
5471 +
5472 + read_lock(&ring_mgmt_lock);
5473 + skElement = cluster_ptr->sk[skb_hash];
5474 + read_unlock(&ring_mgmt_lock);
5475 +
5476 + if(skElement != NULL) {
5477 + pfr = ring_sk(skElement);
5478 +
5479 + if((pfr != NULL)
5480 + && (pfr->ring_slots != NULL)
5481 + && (pfr->ring_netdev == skb->dev)) {
5482 + /* We've found the ring where the packet can be stored */
5483 + read_lock(&ring_mgmt_lock);
5484 + add_skb_to_ring(skb, pfr, recv_packet, real_skb);
5485 + read_unlock(&ring_mgmt_lock);
5486 +
5487 + rc = 1; /* Ring found: we've done our job */
5488 + }
5489 + }
5490 + }
5491 +
5492 + cluster_ptr = cluster_ptr->next;
5493 + }
5494 +
5495 +#ifdef PROFILING
5496 + rdt1 = _rdtsc()-rdt1;
5497 +#endif
5498 +
5499 +#ifdef PROFILING
5500 + rdt2 = _rdtsc();
5501 +#endif
5502 +
5503 + if(transparent_mode) rc = 0;
5504 +
5505 + if((rc != 0) && real_skb)
5506 + dev_kfree_skb(skb); /* Free the skb */
5507 +
5508 +#ifdef PROFILING
5509 + rdt2 = _rdtsc()-rdt2;
5510 + rdt = _rdtsc()-rdt;
5511 +
5512 +#if defined(RING_DEBUG)
5513 + printk("# cycles: %d [lock costed %d %d%%][free costed %d %d%%]\n",
5514 + (int)rdt, rdt-rdt1,
5515 + (int)((float)((rdt-rdt1)*100)/(float)rdt),
5516 + rdt2,
5517 + (int)((float)(rdt2*100)/(float)rdt));
5518 +#endif
5519 +#endif
5520 +
5521 + return(rc); /* 0 = packet not handled */
5522 +}
5523 +
5524 +/* ********************************** */
5525 +
5526 +struct sk_buff skb;
5527 +
5528 +static int buffer_ring_handler(struct net_device *dev,
5529 + char *data, int len) {
5530 +
5531 +#if defined(RING_DEBUG)
5532 + printk("buffer_ring_handler: [dev=%s][len=%d]\n",
5533 + dev->name == NULL ? "<NULL>" : dev->name, len);
5534 +#endif
5535 +
5536 + skb.dev = dev, skb.len = len, skb.data = data,
5537 + skb.data_len = len, skb.stamp.tv_sec = 0; /* Calculate the time */
5538 +
5539 + skb_ring_handler(&skb, 1, 0 /* fake skb */);
5540 +
5541 + return(0);
5542 +}
5543 +
5544 +/* ********************************** */
5545 +
5546 +static int ring_create(struct socket *sock, int protocol) {
5547 + struct sock *sk;
5548 + struct ring_opt *pfr;
5549 + int err;
5550 +
5551 +#if defined(RING_DEBUG)
5552 + printk("RING: ring_create()\n");
5553 +#endif
5554 +
5555 + /* Are you root, superuser or so ? */
5556 + if(!capable(CAP_NET_ADMIN))
5557 + return -EPERM;
5558 +
5559 + if(sock->type != SOCK_RAW)
5560 + return -ESOCKTNOSUPPORT;
5561 +
5562 + if(protocol != htons(ETH_P_ALL))
5563 + return -EPROTONOSUPPORT;
5564 +
5565 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
5566 + MOD_INC_USE_COUNT;
5567 +#endif
5568 +
5569 + err = -ENOMEM;
5570 +
5571 + // BD: -- broke this out to keep it more simple and clear as to what the
5572 + // options are.
5573 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5574 +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11))
5575 + sk = sk_alloc(PF_RING, GFP_KERNEL, 1, NULL);
5576 +#endif
5577 +#endif
5578 +
5579 + // BD: API changed in 2.6.12, ref:
5580 + // http://svn.clkao.org/svnweb/linux/revision/?rev=28201
5581 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
5582 + sk = sk_alloc(PF_RING, GFP_ATOMIC, &ring_proto, 1);
5583 +#endif
5584 +
5585 + if (sk == NULL)
5586 + goto out;
5587 +
5588 + sock->ops = &ring_ops;
5589 + sock_init_data(sock, sk);
5590 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5591 +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11))
5592 + sk_set_owner(sk, THIS_MODULE);
5593 +#endif
5594 +#endif
5595 +
5596 + err = -ENOMEM;
5597 + ring_sk(sk) = ring_sk_datatype(kmalloc(sizeof(*pfr), GFP_KERNEL));
5598 +
5599 + if (!(pfr = ring_sk(sk))) {
5600 + sk_free(sk);
5601 + goto out;
5602 + }
5603 + memset(pfr, 0, sizeof(*pfr));
5604 + init_waitqueue_head(&pfr->ring_slots_waitqueue);
5605 + pfr->ring_index_lock = RW_LOCK_UNLOCKED;
5606 + atomic_set(&pfr->num_ring_slots_waiters, 0);
5607 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5608 + sk->sk_family = PF_RING;
5609 + sk->sk_destruct = ring_sock_destruct;
5610 +#else
5611 + sk->family = PF_RING;
5612 + sk->destruct = ring_sock_destruct;
5613 + sk->num = protocol;
5614 +#endif
5615 +
5616 + ring_insert(sk);
5617 +
5618 +#if defined(RING_DEBUG)
5619 + printk("RING: ring_create() - created\n");
5620 +#endif
5621 +
5622 + return(0);
5623 + out:
5624 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
5625 + MOD_DEC_USE_COUNT;
5626 +#endif
5627 + return err;
5628 +}
5629 +
5630 +/* *********************************************** */
5631 +
5632 +static int ring_release(struct socket *sock)
5633 +{
5634 + struct sock *sk = sock->sk;
5635 + struct ring_opt *pfr = ring_sk(sk);
5636 +
5637 + if(!sk)
5638 + return 0;
5639 +
5640 +#if defined(RING_DEBUG)
5641 + printk("RING: called ring_release\n");
5642 +#endif
5643 +
5644 +#if defined(RING_DEBUG)
5645 + printk("RING: ring_release entered\n");
5646 +#endif
5647 +
5648 + ring_remove(sk);
5649 +
5650 + sock_orphan(sk);
5651 + sock->sk = NULL;
5652 +
5653 + /* Free the ring buffer */
5654 + if(pfr->ring_memory) {
5655 + struct page *page, *page_end;
5656 +
5657 + page_end = virt_to_page(pfr->ring_memory + (PAGE_SIZE << pfr->order) - 1);
5658 + for(page = virt_to_page(pfr->ring_memory); page <= page_end; page++)
5659 + ClearPageReserved(page);
5660 +
5661 + free_pages(pfr->ring_memory, pfr->order);
5662 + }
5663 +
5664 + kfree(pfr);
5665 + ring_sk(sk) = NULL;
5666 +
5667 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5668 + skb_queue_purge(&sk->sk_write_queue);
5669 +#endif
5670 + sock_put(sk);
5671 +
5672 +#if defined(RING_DEBUG)
5673 + printk("RING: ring_release leaving\n");
5674 +#endif
5675 +
5676 + return 0;
5677 +}
5678 +
5679 +/* ********************************** */
5680 +/*
5681 + * We create a ring for this socket and bind it to the specified device
5682 + */
5683 +static int packet_ring_bind(struct sock *sk, struct net_device *dev)
5684 +{
5685 + u_int the_slot_len;
5686 + u_int32_t tot_mem;
5687 + struct ring_opt *pfr = ring_sk(sk);
5688 + struct page *page, *page_end;
5689 +
5690 + if(!dev) return(-1);
5691 +
5692 +#if defined(RING_DEBUG)
5693 + printk("RING: packet_ring_bind(%s) called\n", dev->name);
5694 +#endif
5695 +
5696 + /* **********************************************
5697 +
5698 + *************************************
5699 + * *
5700 + * FlowSlotInfo *
5701 + * *
5702 + ************************************* <-+
5703 + * FlowSlot * |
5704 + ************************************* |
5705 + * FlowSlot * |
5706 + ************************************* +- num_slots
5707 + * FlowSlot * |
5708 + ************************************* |
5709 + * FlowSlot * |
5710 + ************************************* <-+
5711 +
5712 + ********************************************** */
5713 +
5714 + the_slot_len = sizeof(u_char) /* flowSlot.slot_state */
5715 + + sizeof(u_short) /* flowSlot.slot_len */
5716 + + bucket_len /* flowSlot.bucket */;
5717 +
5718 + tot_mem = sizeof(FlowSlotInfo) + num_slots*the_slot_len;
5719 +
5720 + /*
5721 + Calculate the value of the order parameter used later.
5722 + See http://www.linuxjournal.com/article.php?sid=1133
5723 + */
5724 + for(pfr->order = 0;(PAGE_SIZE << pfr->order) < tot_mem; pfr->order++) ;
5725 +
5726 + /*
5727 + We now try to allocate the memory as required. If we fail
5728 + we try to allocate a smaller amount or memory (hence a
5729 + smaller ring).
5730 + */
5731 + while((pfr->ring_memory = __get_free_pages(GFP_ATOMIC, pfr->order)) == 0)
5732 + if(pfr->order-- == 0)
5733 + break;
5734 +
5735 + if(pfr->order == 0) {
5736 +#if defined(RING_DEBUG)
5737 + printk("ERROR: not enough memory\n");
5738 +#endif
5739 + return(-1);
5740 + } else {
5741 +#if defined(RING_DEBUG)
5742 + printk("RING: succesfully allocated %lu KB [tot_mem=%d][order=%ld]\n",
5743 + PAGE_SIZE >> (10 - pfr->order), tot_mem, pfr->order);
5744 +#endif
5745 + }
5746 +
5747 + tot_mem = PAGE_SIZE << pfr->order;
5748 + memset((char*)pfr->ring_memory, 0, tot_mem);
5749 +
5750 + /* Now we need to reserve the pages */
5751 + page_end = virt_to_page(pfr->ring_memory + (PAGE_SIZE << pfr->order) - 1);
5752 + for(page = virt_to_page(pfr->ring_memory); page <= page_end; page++)
5753 + SetPageReserved(page);
5754 +
5755 + pfr->slots_info = (FlowSlotInfo*)pfr->ring_memory;
5756 + pfr->ring_slots = (char*)(pfr->ring_memory+sizeof(FlowSlotInfo));
5757 +
5758 + pfr->slots_info->version = RING_FLOWSLOT_VERSION;
5759 + pfr->slots_info->slot_len = the_slot_len;
5760 + pfr->slots_info->tot_slots = (tot_mem-sizeof(FlowSlotInfo))/the_slot_len;
5761 + pfr->slots_info->tot_mem = tot_mem;
5762 + pfr->slots_info->sample_rate = sample_rate;
5763 +
5764 +#if defined(RING_DEBUG)
5765 + printk("RING: allocated %d slots [slot_len=%d][tot_mem=%u]\n",
5766 + pfr->slots_info->tot_slots, pfr->slots_info->slot_len,
5767 + pfr->slots_info->tot_mem);
5768 +#endif
5769 +
5770 +#ifdef RING_MAGIC
5771 + {
5772 + int i;
5773 +
5774 + for(i=0; i<pfr->slots_info->tot_slots; i++) {
5775 + unsigned long idx = i*pfr->slots_info->slot_len;
5776 + FlowSlot *slot = (FlowSlot*)&pfr->ring_slots[idx];
5777 + slot->magic = RING_MAGIC_VALUE; slot->slot_state = 0;
5778 + }
5779 + }
5780 +#endif
5781 +
5782 + pfr->insert_page_id = 1, pfr->insert_slot_id = 0;
5783 +
5784 + /*
5785 + IMPORTANT
5786 + Leave this statement here as last one. In fact when
5787 + the ring_netdev != NULL the socket is ready to be used.
5788 + */
5789 + pfr->ring_netdev = dev;
5790 +
5791 + return(0);
5792 +}
5793 +
5794 +/* ************************************* */
5795 +
5796 +/* Bind to a device */
5797 +static int ring_bind(struct socket *sock,
5798 + struct sockaddr *sa, int addr_len)
5799 +{
5800 + struct sock *sk=sock->sk;
5801 + struct net_device *dev = NULL;
5802 +
5803 +#if defined(RING_DEBUG)
5804 + printk("RING: ring_bind() called\n");
5805 +#endif
5806 +
5807 + /*
5808 + * Check legality
5809 + */
5810 + if (addr_len != sizeof(struct sockaddr))
5811 + return -EINVAL;
5812 + if (sa->sa_family != PF_RING)
5813 + return -EINVAL;
5814 +
5815 + /* Safety check: add trailing zero if missing */
5816 + sa->sa_data[sizeof(sa->sa_data)-1] = '\0';
5817 +
5818 +#if defined(RING_DEBUG)
5819 + printk("RING: searching device %s\n", sa->sa_data);
5820 +#endif
5821 +
5822 + if((dev = __dev_get_by_name(sa->sa_data)) == NULL) {
5823 +#if defined(RING_DEBUG)
5824 + printk("RING: search failed\n");
5825 +#endif
5826 + return(-EINVAL);
5827 + } else
5828 + return(packet_ring_bind(sk, dev));
5829 +}
5830 +
5831 +/* ************************************* */
5832 +
5833 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5834 +
5835 +volatile void* virt_to_kseg(volatile void* address) {
5836 + pte_t *pte;
5837 + pud_t *pud;
5838 + unsigned long addr = (unsigned long)address;
5839 +
5840 + pud = pud_offset(pgd_offset_k((unsigned long) address),
5841 + (unsigned long) address);
5842 +
5843 + /*
5844 + High-memory support courtesy of
5845 + Brad Doctor <bdoctor@ps-ax.com>
5846 + */
5847 +#if defined(CONFIG_X86_PAE) && (!defined(CONFIG_NOHIGHMEM))
5848 + pte = pte_offset_map(pmd_offset(pud, addr), addr);
5849 +#else
5850 + pte = pmd_offset_map(pud, addr);
5851 +#endif
5852 +
5853 + return((volatile void*)pte_page(*pte));
5854 +}
5855 +
5856 +#else /* 2.4 */
5857 +
5858 +/* http://www.scs.ch/~frey/linux/memorymap.html */
5859 +volatile void *virt_to_kseg(volatile void *address)
5860 +{
5861 + pgd_t *pgd; pmd_t *pmd; pte_t *ptep, pte;
5862 + unsigned long va, ret = 0UL;
5863 +
5864 + va=VMALLOC_VMADDR((unsigned long)address);
5865 +
5866 + /* get the page directory. Use the kernel memory map. */
5867 + pgd = pgd_offset_k(va);
5868 +
5869 + /* check whether we found an entry */
5870 + if (!pgd_none(*pgd))
5871 + {
5872 + /* get the page middle directory */
5873 + pmd = pmd_offset(pgd, va);
5874 + /* check whether we found an entry */
5875 + if (!pmd_none(*pmd))
5876 + {
5877 + /* get a pointer to the page table entry */
5878 + ptep = pte_offset(pmd, va);
5879 + pte = *ptep;
5880 + /* check for a valid page */
5881 + if (pte_present(pte))
5882 + {
5883 + /* get the address the page is refering to */
5884 + ret = (unsigned long)page_address(pte_page(pte));
5885 + /* add the offset within the page to the page address */
5886 + ret |= (va & (PAGE_SIZE -1));
5887 + }
5888 + }
5889 + }
5890 + return((volatile void *)ret);
5891 +}
5892 +#endif
5893 +
5894 +/* ************************************* */
5895 +
5896 +static int ring_mmap(struct file *file,
5897 + struct socket *sock,
5898 + struct vm_area_struct *vma)
5899 +{
5900 + struct sock *sk = sock->sk;
5901 + struct ring_opt *pfr = ring_sk(sk);
5902 + unsigned long size, start;
5903 + u_int pagesToMap;
5904 + char *ptr;
5905 +
5906 +#if defined(RING_DEBUG)
5907 + printk("RING: ring_mmap() called\n");
5908 +#endif
5909 +
5910 + if(pfr->ring_memory == 0) {
5911 +#if defined(RING_DEBUG)
5912 + printk("RING: ring_mmap() failed: mapping area to an unbound socket\n");
5913 +#endif
5914 + return -EINVAL;
5915 + }
5916 +
5917 + size = (unsigned long)(vma->vm_end-vma->vm_start);
5918 +
5919 + if(size % PAGE_SIZE) {
5920 +#if defined(RING_DEBUG)
5921 + printk("RING: ring_mmap() failed: len is not multiple of PAGE_SIZE\n");
5922 +#endif
5923 + return(-EINVAL);
5924 + }
5925 +
5926 + /* if userspace tries to mmap beyond end of our buffer, fail */
5927 + if(size > pfr->slots_info->tot_mem) {
5928 +#if defined(RING_DEBUG)
5929 + printk("proc_mmap() failed: area too large [%ld > %d]\n", size, pfr->slots_info->tot_mem);
5930 +#endif
5931 + return(-EINVAL);
5932 + }
5933 +
5934 + pagesToMap = size/PAGE_SIZE;
5935 +
5936 +#if defined(RING_DEBUG)
5937 + printk("RING: ring_mmap() called. %d pages to map\n", pagesToMap);
5938 +#endif
5939 +
5940 +#if defined(RING_DEBUG)
5941 + printk("RING: mmap [slot_len=%d][tot_slots=%d] for ring on device %s\n",
5942 + pfr->slots_info->slot_len, pfr->slots_info->tot_slots,
5943 + pfr->ring_netdev->name);
5944 +#endif
5945 +
5946 + /* we do not want to have this area swapped out, lock it */
5947 + vma->vm_flags |= VM_LOCKED;
5948 + start = vma->vm_start;
5949 +
5950 + /* Ring slots start from page 1 (page 0 is reserved for FlowSlotInfo) */
5951 + ptr = (char*)(start+PAGE_SIZE);
5952 +
5953 + if(remap_page_range(
5954 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5955 + vma,
5956 +#endif
5957 + start,
5958 + __pa(pfr->ring_memory),
5959 + PAGE_SIZE*pagesToMap, vma->vm_page_prot)) {
5960 +#if defined(RING_DEBUG)
5961 + printk("remap_page_range() failed\n");
5962 +#endif
5963 + return(-EAGAIN);
5964 + }
5965 +
5966 +#if defined(RING_DEBUG)
5967 + printk("proc_mmap(pagesToMap=%d): success.\n", pagesToMap);
5968 +#endif
5969 +
5970 + return 0;
5971 +}
5972 +
5973 +/* ************************************* */
5974 +
5975 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5976 +static int ring_recvmsg(struct kiocb *iocb, struct socket *sock,
5977 + struct msghdr *msg, size_t len, int flags)
5978 +#else
5979 + static int ring_recvmsg(struct socket *sock, struct msghdr *msg, int len,
5980 + int flags, struct scm_cookie *scm)
5981 +#endif
5982 +{
5983 + FlowSlot* slot;
5984 + struct ring_opt *pfr = ring_sk(sock->sk);
5985 + u_int32_t queued_pkts, num_loops = 0;
5986 +
5987 +#if defined(RING_DEBUG)
5988 + printk("ring_recvmsg called\n");
5989 +#endif
5990 +
5991 + slot = get_remove_slot(pfr);
5992 +
5993 + while((queued_pkts = num_queued_pkts(pfr)) < MIN_QUEUED_PKTS) {
5994 + wait_event_interruptible(pfr->ring_slots_waitqueue, 1);
5995 +
5996 +#if defined(RING_DEBUG)
5997 + printk("-> ring_recvmsg returning %d [queued_pkts=%d][num_loops=%d]\n",
5998 + slot->slot_state, queued_pkts, num_loops);
5999 +#endif
6000 +
6001 + if(queued_pkts > 0) {
6002 + if(num_loops++ > MAX_QUEUE_LOOPS)
6003 + break;
6004 + }
6005 + }
6006 +
6007 +#if defined(RING_DEBUG)
6008 + if(slot != NULL)
6009 + printk("ring_recvmsg is returning [queued_pkts=%d][num_loops=%d]\n",
6010 + queued_pkts, num_loops);
6011 +#endif
6012 +
6013 + return(queued_pkts);
6014 +}
6015 +
6016 +/* ************************************* */
6017 +
6018 +unsigned int ring_poll(struct file * file,
6019 + struct socket *sock, poll_table *wait)
6020 +{
6021 + FlowSlot* slot;
6022 + struct ring_opt *pfr = ring_sk(sock->sk);
6023 +
6024 +#if defined(RING_DEBUG)
6025 + printk("poll called\n");
6026 +#endif
6027 +
6028 + slot = get_remove_slot(pfr);
6029 +
6030 + if((slot != NULL) && (slot->slot_state == 0))
6031 + poll_wait(file, &pfr->ring_slots_waitqueue, wait);
6032 +
6033 +#if defined(RING_DEBUG)
6034 + printk("poll returning %d\n", slot->slot_state);
6035 +#endif
6036 +
6037 + if((slot != NULL) && (slot->slot_state == 1))
6038 + return(POLLIN | POLLRDNORM);
6039 + else
6040 + return(0);
6041 +}
6042 +
6043 +/* ************************************* */
6044 +
6045 +int add_to_cluster_list(struct ring_cluster *el,
6046 + struct sock *sock) {
6047 +
6048 + if(el->num_cluster_elements == CLUSTER_LEN)
6049 + return(-1); /* Cluster full */
6050 +
6051 + ring_sk_datatype(ring_sk(sock))->cluster_id = el->cluster_id;
6052 + el->sk[el->num_cluster_elements] = sock;
6053 + el->num_cluster_elements++;
6054 + return(0);
6055 +}
6056 +
6057 +/* ************************************* */
6058 +
6059 +int remove_from_cluster_list(struct ring_cluster *el,
6060 + struct sock *sock) {
6061 + int i, j;
6062 +
6063 + for(i=0; i<CLUSTER_LEN; i++)
6064 + if(el->sk[i] == sock) {
6065 + el->num_cluster_elements--;
6066 +
6067 + if(el->num_cluster_elements > 0) {
6068 + /* The cluster contains other elements */
6069 + for(j=i; j<CLUSTER_LEN-1; j++)
6070 + el->sk[j] = el->sk[j+1];
6071 +
6072 + el->sk[CLUSTER_LEN-1] = NULL;
6073 + } else {
6074 + /* Empty cluster */
6075 + memset(el->sk, 0, sizeof(el->sk));
6076 + }
6077 +
6078 + return(0);
6079 + }
6080 +
6081 + return(-1); /* Not found */
6082 +}
6083 +
6084 +/* ************************************* */
6085 +
6086 +static int remove_from_cluster(struct sock *sock,
6087 + struct ring_opt *pfr)
6088 +{
6089 + struct ring_cluster *el;
6090 +
6091 +#if defined(RING_DEBUG)
6092 + printk("--> remove_from_cluster(%d)\n", pfr->cluster_id);
6093 +#endif
6094 +
6095 + if(pfr->cluster_id == 0 /* 0 = No Cluster */)
6096 + return(0); /* Noting to do */
6097 +
6098 + el = ring_cluster_list;
6099 +
6100 + while(el != NULL) {
6101 + if(el->cluster_id == pfr->cluster_id) {
6102 + return(remove_from_cluster_list(el, sock));
6103 + } else
6104 + el = el->next;
6105 + }
6106 +
6107 + return(-EINVAL); /* Not found */
6108 +}
6109 +
6110 +/* ************************************* */
6111 +
6112 +static int add_to_cluster(struct sock *sock,
6113 + struct ring_opt *pfr,
6114 + u_short cluster_id)
6115 +{
6116 + struct ring_cluster *el;
6117 +
6118 +#ifndef RING_DEBUG
6119 + printk("--> add_to_cluster(%d)\n", cluster_id);
6120 +#endif
6121 +
6122 + if(cluster_id == 0 /* 0 = No Cluster */) return(-EINVAL);
6123 +
6124 + if(pfr->cluster_id != 0)
6125 + remove_from_cluster(sock, pfr);
6126 +
6127 + el = ring_cluster_list;
6128 +
6129 + while(el != NULL) {
6130 + if(el->cluster_id == cluster_id) {
6131 + return(add_to_cluster_list(el, sock));
6132 + } else
6133 + el = el->next;
6134 + }
6135 +
6136 + /* There's no existing cluster. We need to create one */
6137 + if((el = kmalloc(sizeof(struct ring_cluster), GFP_KERNEL)) == NULL)
6138 + return(-ENOMEM);
6139 +
6140 + el->cluster_id = cluster_id;
6141 + el->num_cluster_elements = 1;
6142 + el->hashing_mode = cluster_per_flow; /* Default */
6143 + el->hashing_id = 0;
6144 +
6145 + memset(el->sk, 0, sizeof(el->sk));
6146 + el->sk[0] = sock;
6147 + el->next = ring_cluster_list;
6148 + ring_cluster_list = el;
6149 + pfr->cluster_id = cluster_id;
6150 +
6151 + return(0); /* 0 = OK */
6152 +}
6153 +
6154 +/* ************************************* */
6155 +
6156 +/* Code taken/inspired from core/sock.c */
6157 +static int ring_setsockopt(struct socket *sock,
6158 + int level, int optname,
6159 + char *optval, int optlen)
6160 +{
6161 + struct ring_opt *pfr = ring_sk(sock->sk);
6162 + int val, found, ret = 0;
6163 + u_int cluster_id;
6164 + char devName[8];
6165 +
6166 + if((optlen<sizeof(int)) || (pfr == NULL))
6167 + return(-EINVAL);
6168 +
6169 + if (get_user(val, (int *)optval))
6170 + return -EFAULT;
6171 +
6172 + found = 1;
6173 +
6174 + switch(optname)
6175 + {
6176 + case SO_ATTACH_FILTER:
6177 + ret = -EINVAL;
6178 + if (optlen == sizeof(struct sock_fprog)) {
6179 + unsigned int fsize;
6180 + struct sock_fprog fprog;
6181 + struct sk_filter *filter;
6182 +
6183 + ret = -EFAULT;
6184 +
6185 + /*
6186 + NOTE
6187 +
6188 + Do not call copy_from_user within a held
6189 + splinlock (e.g. ring_mgmt_lock) as this caused
6190 + problems when certain debugging was enabled under
6191 + 2.6.5 -- including hard lockups of the machine.
6192 + */
6193 + if(copy_from_user(&fprog, optval, sizeof(fprog)))
6194 + break;
6195 +
6196 + fsize = sizeof(struct sock_filter) * fprog.len;
6197 + filter = kmalloc(fsize, GFP_KERNEL);
6198 +
6199 + if(filter == NULL) {
6200 + ret = -ENOMEM;
6201 + break;
6202 + }
6203 +
6204 + if(copy_from_user(filter->insns, fprog.filter, fsize))
6205 + break;
6206 +
6207 + filter->len = fprog.len;
6208 +
6209 + if(sk_chk_filter(filter->insns, filter->len) != 0) {
6210 + /* Bad filter specified */
6211 + kfree(filter);
6212 + pfr->bpfFilter = NULL;
6213 + break;
6214 + }
6215 +
6216 + /* get the lock, set the filter, release the lock */
6217 + write_lock(&ring_mgmt_lock);
6218 + pfr->bpfFilter = filter;
6219 + write_unlock(&ring_mgmt_lock);
6220 + }
6221 + ret = 0;
6222 + break;
6223 +
6224 + case SO_DETACH_FILTER:
6225 + write_lock(&ring_mgmt_lock);
6226 + found = 1;
6227 + if(pfr->bpfFilter != NULL) {
6228 + kfree(pfr->bpfFilter);
6229 + pfr->bpfFilter = NULL;
6230 + write_unlock(&ring_mgmt_lock);
6231 + break;
6232 + }
6233 + ret = -ENONET;
6234 + break;
6235 +
6236 + case SO_ADD_TO_CLUSTER:
6237 + if (optlen!=sizeof(val))
6238 + return -EINVAL;
6239 +
6240 + if (copy_from_user(&cluster_id, optval, sizeof(cluster_id)))
6241 + return -EFAULT;
6242 +
6243 + write_lock(&ring_mgmt_lock);
6244 + ret = add_to_cluster(sock->sk, pfr, cluster_id);
6245 + write_unlock(&ring_mgmt_lock);
6246 + break;
6247 +
6248 + case SO_REMOVE_FROM_CLUSTER:
6249 + write_lock(&ring_mgmt_lock);
6250 + ret = remove_from_cluster(sock->sk, pfr);
6251 + write_unlock(&ring_mgmt_lock);
6252 + break;
6253 +
6254 + case SO_SET_REFLECTOR:
6255 + if(optlen >= (sizeof(devName)-1))
6256 + return -EINVAL;
6257 +
6258 + if(optlen > 0) {
6259 + if(copy_from_user(devName, optval, optlen))
6260 + return -EFAULT;
6261 + }
6262 +
6263 + devName[optlen] = '\0';
6264 +
6265 +#if defined(RING_DEBUG)
6266 + printk("+++ SO_SET_REFLECTOR(%s)\n", devName);
6267 +#endif
6268 +
6269 + write_lock(&ring_mgmt_lock);
6270 + pfr->reflector_dev = dev_get_by_name(devName);
6271 + write_unlock(&ring_mgmt_lock);
6272 +
6273 +#if defined(RING_DEBUG)
6274 + if(pfr->reflector_dev != NULL)
6275 + printk("SO_SET_REFLECTOR(%s): succeded\n", devName);
6276 + else
6277 + printk("SO_SET_REFLECTOR(%s): device unknown\n", devName);
6278 +#endif
6279 + break;
6280 +
6281 + default:
6282 + found = 0;
6283 + break;
6284 + }
6285 +
6286 + if(found)
6287 + return(ret);
6288 + else
6289 + return(sock_setsockopt(sock, level, optname, optval, optlen));
6290 +}
6291 +
6292 +/* ************************************* */
6293 +
6294 +static int ring_ioctl(struct socket *sock,
6295 + unsigned int cmd, unsigned long arg)
6296 +{
6297 + switch(cmd)
6298 + {
6299 + case SIOCGIFFLAGS:
6300 + case SIOCSIFFLAGS:
6301 + case SIOCGIFCONF:
6302 + case SIOCGIFMETRIC:
6303 + case SIOCSIFMETRIC:
6304 + case SIOCGIFMEM:
6305 + case SIOCSIFMEM:
6306 + case SIOCGIFMTU:
6307 + case SIOCSIFMTU:
6308 + case SIOCSIFLINK:
6309 + case SIOCGIFHWADDR:
6310 + case SIOCSIFHWADDR:
6311 + case SIOCSIFMAP:
6312 + case SIOCGIFMAP:
6313 + case SIOCSIFSLAVE:
6314 + case SIOCGIFSLAVE:
6315 + case SIOCGIFINDEX:
6316 + case SIOCGIFNAME:
6317 + case SIOCGIFCOUNT:
6318 + case SIOCSIFHWBROADCAST:
6319 + return(dev_ioctl(cmd,(void *) arg));
6320 +
6321 + default:
6322 + return -EOPNOTSUPP;
6323 + }
6324 +
6325 + return 0;
6326 +}
6327 +
6328 +/* ************************************* */
6329 +
6330 +static struct proto_ops ring_ops = {
6331 + .family = PF_RING,
6332 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
6333 + .owner = THIS_MODULE,
6334 +#endif
6335 +
6336 + /* Operations that make no sense on ring sockets. */
6337 + .connect = sock_no_connect,
6338 + .socketpair = sock_no_socketpair,
6339 + .accept = sock_no_accept,
6340 + .getname = sock_no_getname,
6341 + .listen = sock_no_listen,
6342 + .shutdown = sock_no_shutdown,
6343 + .sendpage = sock_no_sendpage,
6344 + .sendmsg = sock_no_sendmsg,
6345 + .getsockopt = sock_no_getsockopt,
6346 +
6347 + /* Now the operations that really occur. */
6348 + .release = ring_release,
6349 + .bind = ring_bind,
6350 + .mmap = ring_mmap,
6351 + .poll = ring_poll,
6352 + .setsockopt = ring_setsockopt,
6353 + .ioctl = ring_ioctl,
6354 + .recvmsg = ring_recvmsg,
6355 +};
6356 +
6357 +/* ************************************ */
6358 +
6359 +static struct net_proto_family ring_family_ops = {
6360 + .family = PF_RING,
6361 + .create = ring_create,
6362 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
6363 + .owner = THIS_MODULE,
6364 +#endif
6365 +};
6366 +
6367 +// BD: API changed in 2.6.12, ref:
6368 +// http://svn.clkao.org/svnweb/linux/revision/?rev=28201
6369 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
6370 +static struct proto ring_proto = {
6371 + .name = "PF_RING",
6372 + .owner = THIS_MODULE,
6373 + .obj_size = sizeof(struct sock),
6374 +};
6375 +#endif
6376 +
6377 +/* ************************************ */
6378 +
6379 +static void __exit ring_exit(void)
6380 +{
6381 + struct list_head *ptr;
6382 + struct ring_element *entry;
6383 +
6384 + for(ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
6385 + entry = list_entry(ptr, struct ring_element, list);
6386 + kfree(entry);
6387 + }
6388 +
6389 + while(ring_cluster_list != NULL) {
6390 + struct ring_cluster *next = ring_cluster_list->next;
6391 + kfree(ring_cluster_list);
6392 + ring_cluster_list = next;
6393 + }
6394 +
6395 + set_skb_ring_handler(NULL);
6396 + set_buffer_ring_handler(NULL);
6397 + sock_unregister(PF_RING);
6398 +
6399 + printk("PF_RING shut down.\n");
6400 +}
6401 +
6402 +/* ************************************ */
6403 +
6404 +static int __init ring_init(void)
6405 +{
6406 + printk("Welcome to PF_RING %s\n(C) 2004 L.Deri <deri@ntop.org>\n",
6407 + RING_VERSION);
6408 +
6409 + INIT_LIST_HEAD(&ring_table);
6410 + ring_cluster_list = NULL;
6411 +
6412 + sock_register(&ring_family_ops);
6413 +
6414 + set_skb_ring_handler(skb_ring_handler);
6415 + set_buffer_ring_handler(buffer_ring_handler);
6416 +
6417 + if(get_buffer_ring_handler() != buffer_ring_handler) {
6418 + printk("PF_RING: set_buffer_ring_handler FAILED\n");
6419 +
6420 + set_skb_ring_handler(NULL);
6421 + set_buffer_ring_handler(NULL);
6422 + sock_unregister(PF_RING);
6423 + return -1;
6424 + } else {
6425 + printk("PF_RING: bucket length %d bytes\n", bucket_len);
6426 + printk("PF_RING: ring slots %d\n", num_slots);
6427 + printk("PF_RING: sample rate %d [1=no sampling]\n", sample_rate);
6428 + printk("PF_RING: capture TX %s\n",
6429 + enable_tx_capture ? "Yes [RX+TX]" : "No [RX only]");
6430 + printk("PF_RING: transparent mode %s\n",
6431 + transparent_mode ? "Yes" : "No");
6432 +
6433 + printk("PF_RING initialized correctly.\n");
6434 + return 0;
6435 + }
6436 +}
6437 +
6438 +module_init(ring_init);
6439 +module_exit(ring_exit);
6440 +MODULE_LICENSE("GPL");
6441 +
6442 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
6443 +MODULE_ALIAS_NETPROTO(PF_RING);
6444 +#endif
This page took 0.336169 seconds and 5 git commands to generate.