blob: 20d2f980a4275c8d362002908b303a654f55b28a [file] [log] [blame]
Lorenzo Colitti9353be22014-12-03 15:18:29 +09001/*
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
26struct 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
Subash Abhinov Kasiviswanathan5c5b5342015-10-26 18:45:04 -060030#define TP_FRAME_SIZE (TPACKET_ALIGN(MAXMRU) + TPACKET_ALIGN(TPACKET2_HDRLEN) + 16)
Lorenzo Colitti9353be22014-12-03 15:18:29 +090031
32// Block size. Must be a multiple of the page size, and a power of two for efficient memory use.
Subash Abhinov Kasiviswanathan5c5b5342015-10-26 18:45:04 -060033#define TP_BLOCK_SIZE 2686976
Lorenzo Colitti9353be22014-12-03 15:18:29 +090034
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
Subash Abhinov Kasiviswanathan5c5b5342015-10-26 18:45:04 -060045#define TP_CSUM_NONE (0)
46#define TP_CSUM_UNNECESSARY (1)
47
Lorenzo Colitti9353be22014-12-03 15:18:29 +090048struct packet_ring {
49 uint8_t *base;
50 struct tpacket2_hdr *next;
51 int slot, numslots;
52 int block, numblocks;
53};
54
55int ring_create(struct tun_data *tunnel);
56void ring_read(struct packet_ring *ring, int write_fd, int to_ipv6);
57
58#endif