1 /* 2 * Copyright 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * ring.c - packet ring buffer functions 17 */ 18 #ifndef __RING_H__ 19 #define __RING_H__ 20 21 #include <linux/if.h> 22 #include <linux/if_packet.h> 23 24 #include "clatd.h" 25 26 struct tun_data; 27 28 // Frame size. Must be a multiple of TPACKET_ALIGNMENT (=16) 29 // Why the 16? http://lxr.free-electrons.com/source/net/packet/af_packet.c?v=3.4#L1764 30 #define TP_FRAME_SIZE (TPACKET_ALIGN(MAXMTU) + TPACKET_ALIGN(TPACKET2_HDRLEN) + 16) 31 32 // Block size. Must be a multiple of the page size, and a power of two for efficient memory use. 33 #define TP_BLOCK_SIZE 65536 34 35 // In order to save memory, our frames are not an exact divider of the block size. Therefore, the 36 // mmaped region will have gaps corresponding to the empty space at the end of each block. 37 #define TP_FRAMES (TP_BLOCK_SIZE / TP_FRAME_SIZE) 38 #define TP_FRAME_GAP (TP_BLOCK_SIZE % TP_FRAME_SIZE) 39 40 // TODO: Make this configurable. This requires some refactoring because the packet socket is 41 // opened before we drop privileges, but the configuration file is read after. A value of 16 42 // results in 656 frames (1048576 bytes). 43 #define TP_NUM_BLOCKS 16 44 45 struct packet_ring { 46 uint8_t *base; 47 struct tpacket2_hdr *next; 48 int slot, numslots; 49 int block, numblocks; 50 }; 51 52 int ring_create(struct tun_data *tunnel); 53 void ring_read(struct packet_ring *ring, int write_fd, int to_ipv6); 54 55 #endif 56