/* * Copyright (c) 2018 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if __SIZEOF_POINTER__ >= 8 #define DEFAULT_RESERVED_MB 16384 #else #define DEFAULT_RESERVED_MB 256 #endif static inline clib_pmalloc_chunk_t * get_chunk (clib_pmalloc_page_t * pp, u32 index) { return pool_elt_at_index (pp->chunks, index); } static inline uword pmalloc_size2pages (uword size, u32 log2_page_sz) { return round_pow2 (size, 1ULL << log2_page_sz) >> log2_page_sz; } static inline int pmalloc_validate_numa_node (u32 * numa_node) { if (*numa_node == CLIB_PMALLOC_NUMA_LOCAL) { u32 cpu; if (getcpu (&cpu, numa_node) != 0) return 1; } return 0; } int clib_pmalloc_init (clib_pmalloc_main_t * pm, uword base_addr, uword size) { uword off, pagesize; u64 *pt = 0; int mmap_flags; ASSERT (pm->error == 0); pagesize = clib_mem_get_default_hugepage_size (); pm->def_log2_page_sz = min_log2 (pagesize); pm->sys_log2_page_sz = min_log2 (sysconf (_SC_PAGESIZE)); pm->lookup_log2_page_sz = pm->def_log2_page_sz; /* check if pagemap is accessible */ pt = clib_mem_vm_get_paddr (&pt, pm->sys_log2_page_sz, 1); if (pt == 0 || pt[0] == 0) pm->flags |= CLIB_PMALLOC_F_NO_PAGEMAP; size = size ? size : ((u64) DEFAULT_RESERVED_MB) << 20; size = round_pow2 (size, pagesize); pm->max_pages = size >> pm->def_log2_page_sz; /* reserve VA space for future growth */ mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS; if (base_addr) mmap_flags |= MAP_FIXED; pm->base = mmap (uword_to_pointer (base_addr, void *), size + pagesize, PROT_NONE, mmap_flags, -1, 0); if (pm->base == MAP_FAILED) { pm->error = clib_error_return_unix (0, "failed to reserve %u pages"); return -1; } off = round_pow2 (pointer_to_uword (pm->base), pagesize) - pointer_to_uword (pm->base); /* trim start and end of reservation to be page aligned */ if (off) { munmap (pm->base, off); pm->base += off; } munmap (pm->base + ((uword) pm->max_pages * pagesize), pagesize - off); return 0; } static inline void * alloc_chunk_from_page (clib_pmalloc_main_t * pm, clib_pmalloc_page_t * pp, u32 n_blocks, u32 block_align, u32 numa_node) { clib_pmalloc_chunk_t *c = 0; clib_pmalloc_arena_t *a; void *va; u32 off; u32 alloc_chunk_index; a = pool_elt_at_index (pm->arenas, pp->arena_index); if (pp->chunks == 0) { u32 i, start = 0, prev = ~0; for (i = 0; i < a->subpages_per_page; i++) { pool_get (pp->chunks, c); c->start = start; c->prev = prev; c->size = pp->n_free_blocks / a->subpages_per_page; start += c->size; if (prev == ~0) pp->first_chunk_index = c - pp->chunks; else pp->chunks[prev].next = c - pp->chunks; prev = c - pp->chunks; } c->next = ~0; pp->n_free_chunks = a->subpages_per_page; } if (pp->n_free_blocks < n_blocks) return 0; alloc_chunk_index = pp->first_chunk_index; next_chunk: c = pool_elt_at_index (pp->chunks, alloc_chunk_index); off = (block_align - (c->start & (block_align - 1))) & (block_align - 1); if (c->used || n_blocks + off > c->size) { if (c->next == ~0) return 0; alloc_chunk_index = c->next; goto next_chunk; } /* if alignment is needed create new empty chunk */ if (off) { u32 offset_chunk_index; clib_pmalloc_chunk_t *co; pool_get (pp->chunks, c); pp->n_free_chunks++; offset_chunk_index = alloc_chunk_index; alloc_chunk_index = c - pp->chunks; co = pool_elt_at_index (pp->chunks, offset_chunk_index); c->size = co->size - off; c->next = co->next; c->start = co->start + off; c->prev = offset_chunk_index; co->size = off; co->next = alloc_chunk_index; } c->used = 1; if (c->size > n_blocks) { u32 tail_chunk_index; clib_pmalloc_chunk_t *ct; pool_get (pp->chunks, ct); pp->n_free_chunks++; tail_chunk_index = ct - pp->chunks; c = pool_elt_at_index (pp->chunks, alloc_chunk_index); ct->size = c->size - n_blocks; ct->next = c->next; ct->prev = alloc_chunk_index; ct->start = c->start + n_blocks; c->size = n_blocks; c->next = tail_chunk_index; if (ct->next != ~0) pool_elt_at_index (pp->chunks, ct->next)->prev = tail_chunk_index; } else if (c->next != ~0) pool_elt_at_index (pp->chunks, c->next)->prev = alloc_chunk_index; c = get_chunk (pp, alloc_chunk_index); va = pm->base + ((pp - pm->pages) << pm->def_log2_page_sz) + (c->start << PMALLOC_LOG2_BLOCK_SZ); hash_set (pm->chunk_index_by_va, pointer_to_uword (va), alloc_chunk_index); pp->n_free_blocks -= n_blocks; pp->n_free_chunks--; return va; } static void pmalloc_update_lookup_table (clib_pmalloc_main_t * pm, u32 first, u32 count) { uword seek, va, pa, p; int fd; u32 elts_per_page = 1U << (pm->def_log2_page_sz - pm->lookup_log2_page_sz); vec_validate_aligned (pm->lookup_table, vec_len (pm->pages) * elts_per_page - 1, CLIB_CACHE_LINE_BYTES); p = (uword) first *elts_per_page; if (pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP) { while (p < (uword) elts_per_page * count) { pm->lookup_table[p] = pointer_to_uword (pm->base) + (p << pm->lookup_log2_page_sz); p++; } return; } fd = ope
/*
 * Copyright (c) 2015-2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

define p2p_ethernet_add
{
  u32 client_index;
  u32 context;
  u32 parent_if_index;
  u8  remote_mac[6];
};

define p2p_ethernet_add_reply
{
  u32 context;
  i32 retval;
  u32 sw_if_index;
};

define p2p_ethernet_del
{
  u32 client_index;
  u32 context;
  u32 parent_if_index;
  u8  remote_mac[6];
};

define p2p_ethernet_del_reply
{
  u32 context;
  i32 retval;
};

/*
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
lloc_chunk_t *c; c = pool_elt_at_index (pp->chunks, pp->first_chunk_index); s = format (s, "\n%U%12s%12s%8s%8s%8s%8s", format_white_space, indent + 2, "chunk offset", "size", "used", "index", "prev", "next"); while (1) { s = format (s, "\n%U%12u%12u%8s%8d%8d%8d", format_white_space, indent + 2, c->start << PMALLOC_LOG2_BLOCK_SZ, c->size << PMALLOC_LOG2_BLOCK_SZ, c->used ? "yes" : "no", c - pp->chunks, c->prev, c->next); if (c->next == ~0) break; c = pool_elt_at_index (pp->chunks, c->next); } } return s; } u8 * format_pmalloc (u8 * s, va_list * va) { clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *); int verbose = va_arg (*va, int); u32 indent = format_get_indent (s); clib_pmalloc_page_t *pp; clib_pmalloc_arena_t *a; s = format (s, "used-pages %u reserved-pages %u default-page-size %U " "lookup-page-size %U%s", vec_len (pm->pages), pm->max_pages, format_log2_page_size, pm->def_log2_page_sz, format_log2_page_size, pm->lookup_log2_page_sz, pm->flags & CLIB_PMALLOC_F_NO_PAGEMAP ? " no-pagemap" : ""); if (verbose >= 2) s = format (s, " va-start %p", pm->base); if (pm->error) s = format (s, "\n%Ulast-error: %U", format_white_space, indent + 2, format_clib_error, pm->error); /* *INDENT-OFF* */ pool_foreach (a, pm->arenas, { u32 *page_index; s = format (s, "\n%Uarena '%s' pages %u subpage-size %U numa-node %u", format_white_space, indent + 2, a->name, vec_len (a->page_indices), format_log2_page_size, a->log2_subpage_sz, a->numa_node); if (a->fd != -1) s = format (s, " shared fd %d", a->fd); if (verbose >= 1) vec_foreach (page_index, a->page_indices) { pp = vec_elt_at_index (pm->pages, *page_index); s = format (s, "\n%U%U", format_white_space, indent + 4, format_pmalloc_page, pp, verbose); } }); /* *INDENT-ON* */ return s; } u8 * format_pmalloc_map (u8 * s, va_list * va) { clib_pmalloc_main_t *pm = va_arg (*va, clib_pmalloc_main_t *); u32 index; s = format (s, "%16s %13s %8s", "virtual-addr", "physical-addr", "size"); vec_foreach_index (index, pm->lookup_table) { uword *lookup_val, pa, va; lookup_val = vec_elt_at_index (pm->lookup_table, index); va = pointer_to_uword (pm->base) + ((uword) index << pm->lookup_log2_page_sz); pa = va - *lookup_val; s = format (s, "\n %16p %13p %8U", uword_to_pointer (va, u64), uword_to_pointer (pa, u64), format_log2_page_size, pm->lookup_log2_page_sz); } return s; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */