Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 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 | */ |
| 17 | /* |
| 18 | * Copyright (C) 2011-2014 Universita` di Pisa. All rights reserved. |
| 19 | * |
| 20 | * Redistribution and use in source and binary forms, with or without |
| 21 | * modification, are permitted provided that the following conditions |
| 22 | * are met: |
| 23 | * |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in the |
| 28 | * documentation and/or other materials provided with the distribution. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 31 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 34 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 35 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 36 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 37 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 38 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 39 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 40 | * SUCH DAMAGE. |
| 41 | */ |
| 42 | |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 43 | #include <vppinfra/lock.h> |
| 44 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 45 | typedef struct |
| 46 | { |
| 47 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 48 | clib_spinlock_t lockp; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 49 | u8 *host_if_name; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 50 | uword if_index; |
| 51 | u32 hw_if_index; |
| 52 | u32 sw_if_index; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 53 | u32 clib_file_index; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 54 | |
| 55 | u32 per_interface_next_index; |
| 56 | u8 is_admin_up; |
| 57 | |
| 58 | /* netmap */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 59 | struct nmreq *req; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 60 | u16 mem_region; |
| 61 | int fd; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 62 | struct netmap_if *nifp; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 63 | u16 first_tx_ring; |
| 64 | u16 last_tx_ring; |
| 65 | u16 first_rx_ring; |
| 66 | u16 last_rx_ring; |
| 67 | |
| 68 | } netmap_if_t; |
| 69 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 70 | typedef struct |
| 71 | { |
| 72 | char *mem; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 73 | u32 region_size; |
| 74 | int refcnt; |
| 75 | } netmap_mem_region_t; |
| 76 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 77 | typedef struct |
| 78 | { |
| 79 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 80 | netmap_if_t *interfaces; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 81 | |
| 82 | /* bitmap of pending rx interfaces */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 83 | uword *pending_input_bitmap; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 84 | |
| 85 | /* rx buffer cache */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 86 | u32 **rx_buffers; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 87 | |
| 88 | /* hash of host interface names */ |
| 89 | mhash_t if_index_by_host_if_name; |
| 90 | |
| 91 | /* vector of memory regions */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 92 | netmap_mem_region_t *mem_regions; |
Mohsin KAZMI | a7575ea | 2016-06-16 06:58:34 +0200 | [diff] [blame] | 93 | |
| 94 | /* first cpu index */ |
| 95 | u32 input_cpu_first_index; |
| 96 | |
| 97 | /* total cpu count */ |
| 98 | u32 input_cpu_count; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 99 | } netmap_main_t; |
| 100 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame^] | 101 | extern netmap_main_t netmap_main; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 102 | extern vnet_device_class_t netmap_device_class; |
| 103 | extern vlib_node_registration_t netmap_input_node; |
| 104 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 105 | int netmap_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set, |
| 106 | u8 is_pipe, u8 is_master, u32 * sw_if_index); |
| 107 | int netmap_delete_if (vlib_main_t * vm, u8 * host_if_name); |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 108 | |
| 109 | |
| 110 | /* Macros and helper functions from sys/net/netmap_user.h */ |
| 111 | |
Matus Fabian | 82e29c4 | 2016-05-11 04:49:46 -0700 | [diff] [blame] | 112 | #ifdef _NET_NETMAP_H_ |
| 113 | |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 114 | #define _NETMAP_OFFSET(type, ptr, offset) \ |
| 115 | ((type)(void *)((char *)(ptr) + (offset))) |
| 116 | |
| 117 | #define NETMAP_IF(_base, _ofs) _NETMAP_OFFSET(struct netmap_if *, _base, _ofs) |
| 118 | |
| 119 | #define NETMAP_TXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *, \ |
| 120 | nifp, (nifp)->ring_ofs[index] ) |
| 121 | |
| 122 | #define NETMAP_RXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *, \ |
| 123 | nifp, (nifp)->ring_ofs[index + (nifp)->ni_tx_rings + 1] ) |
| 124 | |
| 125 | #define NETMAP_BUF(ring, index) \ |
| 126 | ((char *)(ring) + (ring)->buf_ofs + ((index)*(ring)->nr_buf_size)) |
| 127 | |
| 128 | #define NETMAP_BUF_IDX(ring, buf) \ |
| 129 | ( ((char *)(buf) - ((char *)(ring) + (ring)->buf_ofs) ) / \ |
| 130 | (ring)->nr_buf_size ) |
| 131 | |
| 132 | static inline uint32_t |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 133 | nm_ring_next (struct netmap_ring *ring, uint32_t i) |
Mohsin Kazmi | 8f15e92 | 2016-05-02 14:04:57 +0200 | [diff] [blame] | 134 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 135 | return (PREDICT_FALSE (i + 1 == ring->num_slots) ? 0 : i + 1); |
Mohsin Kazmi | 8f15e92 | 2016-05-02 14:04:57 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | * Return 1 if we have pending transmissions in the tx ring. |
| 141 | * When everything is complete ring->head = ring->tail + 1 (modulo ring size) |
| 142 | */ |
| 143 | static inline int |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 144 | nm_tx_pending (struct netmap_ring *ring) |
Mohsin Kazmi | 8f15e92 | 2016-05-02 14:04:57 +0200 | [diff] [blame] | 145 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 146 | return nm_ring_next (ring, ring->tail) != ring->head; |
Mohsin Kazmi | 8f15e92 | 2016-05-02 14:04:57 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static inline uint32_t |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 150 | nm_ring_space (struct netmap_ring *ring) |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 151 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 152 | int ret = ring->tail - ring->cur; |
| 153 | if (ret < 0) |
| 154 | ret += ring->num_slots; |
| 155 | return ret; |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 156 | } |
Matus Fabian | 82e29c4 | 2016-05-11 04:49:46 -0700 | [diff] [blame] | 157 | #endif |
Damjan Marion | 108c731 | 2016-04-20 05:04:20 +0200 | [diff] [blame] | 158 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * fd.io coding-style-patch-verification: ON |
| 162 | * |
| 163 | * Local Variables: |
| 164 | * eval: (c-set-style "gnu") |
| 165 | * End: |
| 166 | */ |