Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #ifndef __included_ssvm_fifo_segment_h__ |
| 16 | #define __included_ssvm_fifo_segment_h__ |
| 17 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 18 | #include <svm/ssvm.h> |
Florin Coras | d3e83a9 | 2018-01-16 02:40:18 -0800 | [diff] [blame] | 19 | #include <svm/svm_fifo.h> |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 20 | #include <vppinfra/lock.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 21 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 22 | typedef enum |
| 23 | { |
| 24 | FIFO_SEGMENT_FREELIST_NONE = -1, |
| 25 | FIFO_SEGMENT_RX_FREELIST = 0, |
| 26 | FIFO_SEGMENT_TX_FREELIST, |
| 27 | FIFO_SEGMENT_N_FREELISTS |
| 28 | } svm_fifo_segment_freelist_t; |
| 29 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 30 | #define FIFO_SEGMENT_MIN_FIFO_SIZE 4096 |
Florin Coras | ca03186 | 2018-09-24 13:58:05 -0700 | [diff] [blame] | 31 | #define FIFO_SEGMENT_MAX_FIFO_SIZE (2 << 30) /* 2GB max fifo size */ |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 32 | #define FIFO_SEGMENT_ALLOC_CHUNK_SIZE 32 /* Allocation quantum */ |
| 33 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 34 | #define FIFO_SEGMENT_F_IS_PREALLOCATED (1 << 0) |
| 35 | #define FIFO_SEGMENT_F_WILL_DELETE (1 << 1) |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 36 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 37 | typedef struct |
| 38 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 39 | svm_fifo_t *fifos; /**< Linked list of active RX fifos */ |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 40 | svm_fifo_t **free_fifos; /**< Freelists, by fifo size */ |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 41 | u32 n_active_fifos; /**< Number of active fifos */ |
| 42 | u8 flags; /**< Segment flags */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 43 | } svm_fifo_segment_header_t; |
| 44 | |
| 45 | typedef struct |
| 46 | { |
| 47 | ssvm_private_t ssvm; |
| 48 | svm_fifo_segment_header_t *h; |
| 49 | } svm_fifo_segment_private_t; |
| 50 | |
| 51 | typedef struct |
| 52 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 53 | volatile u32 lock; |
| 54 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 55 | /** pool of segments */ |
| 56 | svm_fifo_segment_private_t *segments; |
| 57 | /* Where to put the next one */ |
| 58 | u64 next_baseva; |
| 59 | u32 timeout_in_seconds; |
| 60 | } svm_fifo_segment_main_t; |
| 61 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 62 | typedef struct |
| 63 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 64 | ssvm_segment_type_t segment_type; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 65 | char *segment_name; |
| 66 | u32 segment_size; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 67 | u32 *new_segment_indices; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 68 | int memfd_fd; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 69 | } svm_fifo_segment_create_args_t; |
| 70 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 71 | #define svm_fifo_segment_flags(_seg) _seg->h->flags |
| 72 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 73 | static inline svm_fifo_segment_private_t * |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 74 | svm_fifo_segment_get_segment (svm_fifo_segment_main_t * sm, u32 segment_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 75 | { |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 76 | return pool_elt_at_index (sm->segments, segment_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 79 | static inline u8 |
| 80 | svm_fifo_segment_has_fifos (svm_fifo_segment_private_t * fifo_segment) |
| 81 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 82 | return fifo_segment->h->fifos != 0; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 85 | static inline svm_fifo_t * |
| 86 | svm_fifo_segment_get_fifo_list (svm_fifo_segment_private_t * fifo_segment) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 87 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 88 | return fifo_segment->h->fifos; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 91 | int svm_fifo_segment_init (svm_fifo_segment_private_t * s); |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 92 | int svm_fifo_segment_create (svm_fifo_segment_main_t * sm, |
| 93 | svm_fifo_segment_create_args_t * a); |
| 94 | int svm_fifo_segment_create_process_private (svm_fifo_segment_main_t * sm, |
| 95 | svm_fifo_segment_create_args_t |
| 96 | *); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 97 | void svm_fifo_segment_preallocate_fifo_pairs (svm_fifo_segment_private_t * s, |
| 98 | u32 rx_fifo_size, |
| 99 | u32 tx_fifo_size, |
| 100 | u32 * n_fifo_pairs); |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 101 | int svm_fifo_segment_attach (svm_fifo_segment_main_t * sm, |
| 102 | svm_fifo_segment_create_args_t * a); |
| 103 | void svm_fifo_segment_delete (svm_fifo_segment_main_t * sm, |
| 104 | svm_fifo_segment_private_t * s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 105 | |
| 106 | svm_fifo_t *svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s, |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 107 | u32 data_size_in_bytes, |
| 108 | svm_fifo_segment_freelist_t index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 109 | void svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 110 | svm_fifo_t * f, |
| 111 | svm_fifo_segment_freelist_t index); |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 112 | void svm_fifo_segment_main_init (svm_fifo_segment_main_t * sm, u64 baseva, |
| 113 | u32 timeout_in_seconds); |
| 114 | u32 svm_fifo_segment_index (svm_fifo_segment_main_t * sm, |
| 115 | svm_fifo_segment_private_t * s); |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 116 | u32 svm_fifo_segment_num_fifos (svm_fifo_segment_private_t * fifo_segment); |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 117 | u32 svm_fifo_segment_num_free_fifos (svm_fifo_segment_private_t * |
| 118 | fifo_segment, u32 fifo_size_in_bytes); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 119 | void svm_fifo_segment_info (svm_fifo_segment_private_t * seg, uword * address, |
| 120 | u64 * size); |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 121 | |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 122 | svm_fifo_segment_private_t |
| 123 | * svm_fifo_segment_segments_pool (svm_fifo_segment_main_t * sm); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 124 | |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 125 | format_function_t format_svm_fifo_segment; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 126 | format_function_t format_svm_fifo_segment_type; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 127 | |
| 128 | #endif /* __included_ssvm_fifo_segment_h__ */ |
| 129 | |
| 130 | /* |
| 131 | * fd.io coding-style-patch-verification: ON |
| 132 | * |
| 133 | * Local Variables: |
| 134 | * eval: (c-set-style "gnu") |
| 135 | * End: |
| 136 | */ |