More on protocol implementation Packet parsing Memory management Data structures for lookup.

Slides:



Advertisements
Similar presentations
Hash Tables CS 310 – Professor Roch Weiss Chapter 20 All figures marked with a chapter and section number are copyrighted © 2006 by Pearson Addison-Wesley.
Advertisements

Dynamic Memory Management
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Note on malloc() and slab allocation CS-502 (EMC) Fall A Note on malloc() and Slab Allocation CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
More on protocol implementation Version walks Timers and their problems.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CSE506: Operating Systems Block Cache. CSE506: Operating Systems Address Space Abstraction Given a file, which physical pages store its data? Each file.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
B+-tree and Hashing.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Chapter 4: Trees Radix Search Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
CSC 2300 Data Structures & Algorithms February 27, 2007 Chapter 5. Hashing.
BST Data Structure A BST node contains: A BST contains
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
1 CS 430: Information Discovery Lecture 4 Data Structures for Information Retrieval.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
1 Lecture 19: B-trees and Hash Tables Wednesday, November 12, 2003.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
Memory Allocation CS Introduction to Operating Systems.
ICS 220 – Data Structures and Algorithms Week 7 Dr. Ken Cosh.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
ALGORITHMS FOR ISNE DR. KENNETH COSH WEEK 6.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
March 16 & 21, Csci 2111: Data and File Structures Week 9, Lectures 1 & 2 Indexed Sequential File Access and Prefix B+ Trees.
Trevor Brown – University of Toronto B-slack trees: Space efficient B-trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
IT253: Computer Organization
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
A Study of Balanced Search Trees: Brainstorming a New Balanced Search Tree Anthony Kim, 2005 Computer Systems Research.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Sets of Digital Data CSCI 2720 Fall 2005 Kraemer.
Programming Abstractions Cynthia Lee CS106X. Topics:  Binary Search Tree (BST) › Starting with a dream: binary search in a linked list? › How our dream.
I MPLEMENTING FILES. Contiguous Allocation:  The simplest allocation scheme is to store each file as a contiguous run of disk blocks (a 50-KB file would.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Ofir Luzon Supervisor: Prof. Michael Segal Longest Prefix Match For IP Lookup.
COMP261 Lecture 23 B Trees.
Week 7 - Friday CS221.
Multiway Search Trees Data may not fit into main memory
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CS Introduction to Operating Systems
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
1 Lecture 13 CS2013.
Data Structures Using C++ 2E
Presented by Sams Uddin Ahamed & Najmus Sakib Borson Scapegoat Tree (Data Structure)
CS 261 – Data Structures AVL Trees.
Presentation transcript:

More on protocol implementation Packet parsing Memory management Data structures for lookup

Packet parsing Not too much here. Only a single concern –Do not let the incoming data cause a buffer overflow This means: be extremely careful what you assume about the input –Make sure that you always have enough buffer for the incoming packets –Always verify the lengths of TLVs or other protocol objects before copying them

Memory Problems –Fragmentation –Out of memory errors –Leaks –How to catch corruptions

Memory management A memory manager can be complex –Must be fast –Must minimize fragmentation –Find a best fitting are of memory to allocate for an object so as to minimize fragmentation Allocation/free patterns –Frequency of alloc/free –Large bursts of alloc When the process starts Can optimize by pre-allocating

Slab allocators Slab allocator –Allocate objects from caches (slabs) I.e. get a memory page and carve it up is smaller objects –Cache: some objects may require expensive initialization Do it only once –Centralize: so that the overall system needs are known and memory can be managed effectively But –There is little bit of internal fragmentation Some data at the end of the slab is left unused –Little bit tricky to handle slabs for very large objects –Not all objects are of known size May have to allocate variable size memory Use multiples of fixed sizes and accept some small loss of memory –May hold the whole slab for just few objects

Memory debugging 0xdeadbeef and 0xbaddcafe –Detect when trying to use freed or unallocated memory Put a magic word at the end of the allocated area –Catch buffer overruns at free time Each data object into its own page –Unmap when freed –Any access to it will cause a seg fault Keep a log of uses –Trace who (thread, function) used this buffer

Memory Leaks An object based manager will tell us exactly what objects are leaking –Maybe easier to find the problem Combine with logging to see who did not free There are multiple tools that can help here –Purify, valgrind and more…

Low memory conditions Code that handles low memory conditions is usually not well tested –In a real life situation the system may become completely unusable –Manipulate the memory manager to create such conditions and test the system

Random memory corruptions Be a bit paranoid –Use a hash/checksum in major data structures and occasionally verify it –If something is wrong crash and restart

Lookups Hashes Balanced trees Tries/radix Attacks Walks

Hashes Big problem collisions –Bad hash function –Not well understood pattern –Different patterns may need different hashes –Attacks Universal hashing

Unbalanced Trees Very good for random insertion and access patterns –No need for expensive balancing operations But if things are not random can deteriorate very fast –Very vulnerable to attacks –Can make them look like a linked list

Balanced trees Keep them balanced –AVL The height of the two child sub-trees differ by 1 Red-black All paths from a node to its leaves must have the same number of black nodes –Lookup times independent of insert pattern –But insert times not In some inserts I may have to do more work: rotations Resistant to attacks –Worse that can happen is somebody will force me to do rotations all the time Lookup performance depends on pattern –Splay tree tries to adapt to the lookup pattern –Recently accessed nodes are faster to access again

Radix/Tries Store strings but not only –Anything than can be mapped to a string and has a lexicographic order Does not need to be binary –In most cases it is not Lookup cost depends on the length of the key and not on the number of nodes in the tree –But log(N) is usually better than length of the key Good for longest prefix matches No need for complex balancing Better cache behavior! –Fewer levels means fewer memory accesses and better caching locality Patricia trees to save space

It all has to do with the data Insert pattern –Random enough or pseudo-sequential Lifetime –Ratio of lookups/insert+deletes Lookup pattern Lookup time –Exact? –Prefix? What is being used: –Linux A hash table for each prefix-len for the route table 64-child radix trees for other things –Quagga Binary Radix tree for routes

Algorithmic Attacks Make hashing deteriorate to linear search –Balancing data structures are ok May or may not be feasible –Amount of bits of key –Amount of bits available to the attacker Can handle by –Making hash function harder to predix Use a keyed MD5 hash or similar –Universal hashing A family of hash functions where the probability of collisions is small Need to be carefully chosen to avoid high implementation cost

Walks Need to walk a part or all the tree –Parent pointers, threading of various types They take memory –Simple traversal in-order, pre-order Need to interrupt and come back –The complete walk may take too long and I will have to do other protocol work –What happens with changes in the meanwhile Mostly deletions Locking and delayed deletions

Advanced walks Should be able to walk a subset only –Ie lookup an element and walk from there Use the threading pointers –Return where the walk stopped so the walk can be continued later Walk will handle the locking and the delayed deletion Can have some self timing code so that it yields after it has run for more than a limit –Or a limit on how many tree nodes to walk before stopping Something like –Walk_tree(tree, from, until, &next, time_limit, walk_handler); –Walk_handler() processes each node returns CONTINUE or STOP –If walk is interrupted, next will become the from next time we call walk_tree()