Lecture 2: Linux devices Roei Ben-Harush 2015. 2 Agenda 1 1 Linux Devices About next Assignment 2 2 3 3 Roei Ben-Harush 2015 Reminder.

Slides:



Advertisements
Similar presentations
DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Advertisements

Linux device-driver issues
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 - /
Computer System Laboratory
TSpaces Services Suite: Automating the Development and Management of Web Services Presenter: Kevin McCurley IBM Almaden Research Center Contact: Marcus.
Devices and Drivers (Chapter 7) Khattab Alhabashi UNIX System Administration.
Operating System Structures
USERSPACE I/O Reporter: R 張凱富.
The Journey of a Packet Through the Linux Network Stack
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Lecture 1.2: Linux and networking Roei Ben-Harush 2015.
Essential System Administration 3rd Edition Chapter 2 The Unix Way(Cont.) University Of Palestine.
SiS 315 Graphics Engine Introduction to some capabilities of graphics accelerator hardware.
Embedded Systems Programming Writing Device Drivers.
63 UQC152H3 Advanced OS Writing a Device Driver. 64 The SCULL Device Driver Simple Character Utility for Loading Localities 6 devices types –Scull-03.
Introduction to Kernel
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
C++ fundamentals.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
CS 6560 Operating System Design Lecture 13 Finish File Systems Block I/O Layer.
Lecture 2 TCP/IP Protocol Suite Reference: TCP/IP Protocol Suite, 4 th Edition (chapter 2) 1.
Linux in More Detail Shirley Moore CPS5401 August 29,
14 Publishing a Web Site Section 14.1 Identify the technical needs of a Web server Evaluate Web hosts Compare and contrast internal and external Web hosting.
CSC 322 Operating Systems Concepts Lecture - 4: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS 390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Distributed Computing Fundamentals.
Click Router: Hands on Arvind Venkatesan Shivkumar Kalyanaraman.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
OS2014 PROJECT 2 Supplemental Information. Outline Sequence Diagram of Project 2 Kernel Modules Kernel Sockets Work Queues Synchronization.
Linux Device Model Part 1
CE Operating Systems Lecture 17 File systems – interface and implementation.
Interfacing Device Drivers with the Kernel
User-Space-to-Kernel Interface
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
Chapter 10: File-System Interface Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Jan 1, 2005 File-System Interface.
Click Router: Hands on Alex Newman Arvind Venkatesan Shivkumar Kalyanaraman.
Lecture 4 Mechanisms & Kernel for NOSs. Mechanisms for Network Operating Systems  Network operating systems provide three basic mechanisms that support.
Lecture 19 Linux/Unix – File System
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
ICOM Noack Linux I/O structure Device special files Device switch tables and fops How the kernel finds a device Parts of a device driver or module.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 2.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 5.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Lecture 3 Module Programming and Device Driver (Homework#1 included) Kyu Ho Park Sept. 15, 2015.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Lecture 3: Stateless Packet Filtering. 2 Agenda 1 1 Linux file system - networking sk_buff 2 2 Stateless packet filtering 3 3 About next assignment 4.
Tgt: Framework Target Drivers FUJITA Tomonori NTT Cyber Solutions Laboratories Mike Christie Red Hat, Inc Ottawa Linux.
Getting Started with Linux
Lecture 1.1: Introduction. 2 Agenda 1 1 What is Firewall Types of Firewall 2 2 Firewall in linux kernel 3 3.
Advanced Network Labs & Remote Network Agent
Linux Device Model A device model after 2.5
Introduction to Kernel
The Linux Operating System
Linux Kernel Driver.
Chapter 3: Windows7 Part 4.
Operation System Program 4
Introduction to Linux Device Drivers
Chapter 2: The Linux System Part 2
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Chapter 2: The Linux System Part 5
Computer System Laboratory
Presentation transcript:

Lecture 2: Linux devices Roei Ben-Harush 2015

2 Agenda 1 1 Linux Devices About next Assignment Roei Ben-Harush 2015 Reminder

3 Agenda 1 1 Reminder Roei Ben-Harush 2015 About next Assignment Linux Devices

4 Virtual network Roei Ben-Harush 2015  You’ve all experienced setting up virtual lab with machines and networks

5 What is a Kernel Module  What is a kernel module? –An object file that contains code to extend the running kernel, or so-called base kernel, of an operating system.  Kernel module can be used as a hardware devise manager, or to add new software features –Since we wont use any hardware in this workshop, we ill focus on adding features Roei Ben-Harush 2015

6 Linux networking  We need a way to see the packets  Packets has headers for each layer and data  Linux contains set of useful tools and structures to manage packets over the kernel Roei Ben-Harush 2015

7  Each packet comes with headers, each for every layer.  The layers we are interested in are the network layer and transport layer  We can use the information to decide the verdict for each packet\connection Packet Headers Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: Data HTTP/ OK0x20Last-Modified: Mon, 07 Apr :16:25 GMT0x20Content-Type: image/jpeg0x20Cache-Control: max-age = x20magicmarker: 10x20Content-Length: 47060x20Accept-Ranges: bytes0x20Date: Mon, 07 Apr :35:46 GMT0x20X-Varnish: x20Age: 83980x20Via: 1.1 varnish0x20Connection: keep-alive0x20X-Cache: IP header TCP/UDP header Roei Ben-Harush 2015

8 NetFilter  5 inspection points which can be used to define behaviors for each type of packets Roei Ben-Harush 2015

9 Our Kernel Module – The Firewall!  What will we do with our kernel module?  Register our own functions (AKA: hooks) with the netfilter API, to issue verdicts on packets going in/out/through our Linux box.  Register a char device, to communicate with the user space –Send commands to set module values. –Receive data from the firewall about the state of the system. Roei Ben-Harush 2015

10 Agenda 1 1 Reminder Linux Devices 2 2 About next Assignment 3 3 Roei Ben-Harush 2015

11 Devices  There are three kinds of devices in Linux: –Character devices – read/write single bytes. –Block devices – read/write blocks of bytes. –Network devices – access to internet via physical adapter  Now days Linux kernel has a unified model for all devices –The device model provides a single mechanism for representing devices and describing their topology –For further reading on kobjects, ktypes and ksets click herehere –We deal with more higher level objects Roei Ben-Harush 2014

12 Character Devices  We access char devices via names in the file system –Located under /dev  Every char device has its major number and minor number. –Major #: unique id for each device manager –Minor #: id for each device connected to the device manager  A device can define its own operations on its interface files. –What happens when someone opens/closes/reads/mmaps… a file with our major# ? Roei Ben-Harush 2014

13 Add new char device  To create new char device, we need to allocate him new major# –The function register_chrdev() does it for us  Syntax: –int register_chrdev (unsigned int major, const char* name, const struct file_operations* fops); – major – major device number or 0 for dynamic allocation – minor – name of this range of devices – fops - file operations associated with this devices  Function returns 0 for success or -1 for failure Roei Ben-Harush 2015

14 File Operations  After registering our char device, it’s linked to the major # –We will create a representation of it in the file system with mknod or the function device_create, if used with sysfs –Can be seen under /dev/  The “struct file_operations (AKA: fops)” contains mainly pointers to functions.  It is used to plant our own implementations to various file system calls, like opening or closing a file, and much more.  First, we define and implement our functions, with the right signature.  Then, we build an instance of this struct, and use it when we register our char device. Roei Ben-Harush 2014

15 Example  A scenario: ls -l /dev/my_device* crw-rw-rw- 1 root root 250, 0 Aug 15 12:07 /dev/my_device1 cr--r--r-- 1 root root 250, 1 Aug 15 12:07 /dev/my_device2 cat /dev/my_device2 Hello device2’s World!  The ‘cat’ called our implementations of open, read, and release(close). –We need to support these functions if it’s different from the default operation  Owner can be set also through macro: SET_MODULE_OWNER(&fops); Roei Ben-Harush 2014 struct file_operations my_fops = {.owner = THIS_MODULE,.open = my_open_func,.read = my_read_func,.release = my_release_func };

16 Remove char device  When we want to remove a device from the system (usually in the cleanup_module function) we will use unregister_chrdev()  Syntax: –unregister_chrdev(unsigned int major, const char *name) – major – the major related to the device we want to remove – name - the name of the device  Function returns 0 for success or -1 for failure Roei Ben-Harush 2015

17 sysfs  A brilliant way to view the devices topology as a file system  It ties kobjects to directories and files  Enables users (in userspace) to manipulate variables in the devices  The sysfs is mounted in /sys  Our interest is by the high level class description of the devices –/sys/  We can create devices under this CLASS_NAME –/sys/ / –/sys/class/my_class/my_first_device/dev_att Roei Ben-Harush 2014

18 Device Class  Device class is a concept introduced in recent kernel versions.  Helps us maintain a logical hierarchy of devices (not to be confused with char devices!)  Every device has the char-device’s major#, and a minor# of its own. Roei Ben-Harush 2014 my_classdevice1device2

19 sysfs (cont)  Just as in read and write, we will have to implement input and output to the sysfs files  When we will create device files we will have to define device_attributes –Pointer to show function –Pointer to store function  We can just use –echo “whatever” > /sys/ / /store –Where is the catch? –We can only move data that is smaller than PAGE_SIZE –A convention is to use human readable data Roei Ben-Harush 2014

20 Sysfs (AKA: /sys)  To create such file: –Create read/write(show/store) functions. –Create a Device Attribute for the file. –Register the Device Attribute using sysfs API, under your desired device.  A scenario: cat /sys/class/my_class/my_first_device/num_of_eggs 2 echo spam > /sys/class/my_class/my_second_device/worm_whole Roei Ben-Harush 2014

21 How to work with sysfs  First, we need to register our device –register_chrdev(unsigned int major, const char *name, struct file_operations *fops); –We will set major to 0 to let the system dynamically give us a major  Later, we will create a class to see under /sys/class –struct class * device_class = class_create (struct module *owner, const char *name); –Owner: the module owner, usually set to the macro THIS_MODULE –Name: the name you picked for the class –The function returns struct class Roei Ben-Harush 2014

22 Continue to set up the environment  After we created the class folder, we now want to put our device in it. We create device which will link our device into the class –struct device * device_create (struct class * class, struct device * parent, dev_t devt, void * drvdata, const char * fmt,...); –Class: the struct class that this device should be registered to, which we created. –Parent: pointer to the parent struct device of this new device, if any (usually NULL) –Devt: the dev_t for the char device to be added – we need to make a unique number for this device. We have a macro which provide us this number: MKDEV(major number, minor number) –Drvdata: the data to be added to the device for callbacks (usually NULL) –Fmt - string for the device's name Roei Ben-Harush 2014

23 Continue to set up the environment  We have now put our device in the class, and we can reach it’s default attributes.  We don’t care about the default, and we will usually need to implement 2 new ones (if we need them): –Show: show data to the userspace, similar to read –Store: send data to the kernel space  After implementation, we link the device with these attributes with the macro DEVICE_ATTR(name,mode,show,store)  We will use device_create_file(struct device *device, const struct device_attribute *entry); to have our new attributes in the device folder Roei Ben-Harush 2014

24 References  Further reference: –Linux Device Drivers, Third EditionLinux Device Drivers, Third Edition –An excellent free e-book, contains all you need and don’t need to know about kernel modules. –Written for kernel 2.6, but not a lot changed since. –Kernel Headers and Documentation –On your machine –e.g. /usr/src/linux-headers-`uname -r`/include/linux/ip.h –On the net –LXR or any other cross-reference site.LXR – –The hardest to read, but probably the most useful. –Google, Stackoverflow, and Wikipedia has everything you need Roei Ben-Harush 2014

25 Agenda 1 1 Reminder Linux Devices 2 2 About next Assignment 3 3 Roei Ben-Harush 2015

26 Assignment 2  This assignment we will continue to learn about Linux kernel features and APIs which will help us along the way  We will need to implement connection between your module and a user-space C program  Use sysfs as you learned in this lecture  Sysfs is a popular way to connect to the kernel, use the internet to help you figure out what you don’t inderstand  Make sure your program prints exactly what it should – no more, no less, pay attention to spaces. Roei Ben-Harush 2015

27 Assignment 2  Every feature from assignment 1 should stay and work in the firewall.  Assignment 3 will also be published. If you finish assignment 2, I recommend start working on assignment 3 as soon as possible, this assignment is a major leap forward in terms of completion.  Feel free to contact us for any question.  Happy Passover Roei Ben-Harush 2015