<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel><title>LINUX For You &#187; Concepts</title> <atom:link href="http://www.linuxforu.com/category/teach-me/concepts/feed/" rel="self" type="application/rss+xml" /><link>http://www.linuxforu.com</link> <description>The Complete Magazine on Open Source</description> <lastBuildDate>Tue, 31 Jan 2012 17:22:40 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>Cyber Attacks Explained: Network Sniffing</title><link>http://www.linuxforu.com/2012/01/cyber-attacks-explained-network-sniffing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cyber-attacks-explained-network-sniffing</link> <comments>http://www.linuxforu.com/2012/01/cyber-attacks-explained-network-sniffing/#comments</comments> <pubDate>Mon, 30 Jan 2012 08:47:34 +0000</pubDate> <dc:creator>Prashant Phatak</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[Sysadmins]]></category> <category><![CDATA[anti-sniffer software]]></category> <category><![CDATA[anti-sniffing tools]]></category> <category><![CDATA[attackers]]></category> <category><![CDATA[data security]]></category> <category><![CDATA[denial of service]]></category> <category><![CDATA[denial of service attacks]]></category> <category><![CDATA[DOS]]></category> <category><![CDATA[encryption]]></category> <category><![CDATA[Ethernet]]></category> <category><![CDATA[ethernet packet]]></category> <category><![CDATA[firewall]]></category> <category><![CDATA[http]]></category> <category><![CDATA[ICMP]]></category> <category><![CDATA[ICMP protocol]]></category> <category><![CDATA[intrusion-detection systems]]></category> <category><![CDATA[IP addresses]]></category> <category><![CDATA[IPSec encryption]]></category> <category><![CDATA[LAN]]></category> <category><![CDATA[LFY January 2012]]></category> <category><![CDATA[Mac]]></category> <category><![CDATA[network]]></category> <category><![CDATA[network details]]></category> <category><![CDATA[network interfaces]]></category> <category><![CDATA[network layers]]></category> <category><![CDATA[network packet]]></category> <category><![CDATA[network protocols]]></category> <category><![CDATA[osi layers]]></category> <category><![CDATA[Packet analyzer]]></category> <category><![CDATA[packet sniffer software]]></category> <category><![CDATA[packet-capturing software]]></category> <category><![CDATA[protocol type]]></category> <category><![CDATA[routers]]></category> <category><![CDATA[sniffer software]]></category> <category><![CDATA[sniffers]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[SSL]]></category> <category><![CDATA[TCP]]></category> <category><![CDATA[TCP/IP]]></category> <category><![CDATA[TCP/IP protocol]]></category> <category><![CDATA[UDP]]></category> <category><![CDATA[Wireshark]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=9374</guid> <description><![CDATA[In earlier articles in this series, we have explored denial of service attacks and packet spoofing. This month, let us focus on a wider hacking approach called network sniffing, which is very commonly...]]></description> <content:encoded><![CDATA[<p><img class="aligncenter size-large wp-image-9375" title="Cyber Attack" src="http://cdn.linuxforu.com/wp-content/uploads/2012/01/cyber-attack-590x381.jpg?d9c344" alt="Cyber Attack" width="590" height="381" /></p><div class="introduction">In earlier articles in this series, we have explored <a title="Cyber Attacks Explained: DoS and DDoS" href="http://www.linuxforu.com/2011/11/cyber-attacks-explained-dos-and-ddos/">denial of service attacks</a> and <a title="Cyber Attacks Explained: Packet Spoofing" href="http://www.linuxforu.com/2011/12/cyber-attacks-explained-packet-spoofing/">packet spoofing</a>. This month, let us focus on a wider hacking approach called network sniffing, which is very commonly used by prying hackers. We will look at a few tools typically used as sniffers and also figure out how to protect IT infrastructure from such attacks.</div><p>Sniffing involves capturing, decoding, inspecting and interpreting the information inside a network packet on a TCP/IP network. The purpose is to steal information, usually user IDs, passwords, network details, credit card numbers, etc. Sniffing is generally referred to as a &#8220;passive&#8221; type of attack, wherein the attackers can be silent/invisible on the network. This makes it difficult to detect, and hence it is a dangerous type of attack.</p><p>As we already learnt over the previous months, the TCP/IP packet contains vital information required for two network interfaces to communicate with each other. It contains fields such as source and destination IP addresses, ports, sequence numbers and the protocol type. Each of these fields is crucial for various network layers to function, and especially for the Layer 7 application that makes use of the received data.</p><p>By its very nature, the TCP/IP protocol is only meant for ensuring that a packet is constructed, mounted on an Ethernet packet frame, and reliably delivered from the sender to the receiver across networks. However, it does not by default have mechanisms to ensure data security. Thus, it becomes the responsibility of the upper network layers to ensure that information in the packet is not tampered with.</p><p>To understand why hackers sniff, we need to know what they can get from the network. Figure 1 shows the OSI layers and the information a hacker can steal at each layer by successfully sniffing a network.</p><div id="attachment_9376" class="wp-caption aligncenter" style="width: 590px"><img class="size-large wp-image-9376" title="Mapping of OSI layers" src="http://cdn.linuxforu.com/wp-content/uploads/2012/01/Fig1-590x429.jpg?d9c344" alt="Mapping of OSI layers" width="590" height="429" /><p class="wp-caption-text">Figure 1. Mapping of OSI layers</p></div><p>The sniffing process is used by hackers either to get information directly or to map the technical details of the network in order to create a further attack. Hackers are always in favour of sniffing, because it can be done for a longer time without getting caught.</p><h2>How do they &#8216;sniff&#8217;?</h2><p>Network sniffing uses sniffer software, either open source or commercial. Broadly, there are three ways to sniff a network, as shown in Figure 2.</p><div id="attachment_9377" class="wp-caption aligncenter" style="width: 590px"><img class="size-large wp-image-9377" title="Sniffing attack vectors" src="http://cdn.linuxforu.com/wp-content/uploads/2012/01/Fig2-590x310.jpg?d9c344" alt="Sniffing attack vectors" width="590" height="310" /><p class="wp-caption-text">Figure 2. Sniffing attack vectors</p></div><p>It is important to remember that sniffing can range from Layer 1 through Layer 7. Talking about physical connectivity, a person (who may be an employee of the firm) who is already hooked up to the internal LAN can run tools to directly capture network traffic. Using spoofing techniques, a hacker outside the target network can intercept packets at the firewall level and steal the information. In the latest form of packet sniffing, wide usage of wireless networks has made it easy to sit near the network and penetrate it to get information.</p><p>Regardless of where the hackers are located on the network being sniffed, they use packet capturing or packet sniffer software. Modern packet sniffers are supposed to be used for troubleshooting network problems, but can be used for hacking too. Please refer to the following table, which depicts the ethical and unethical side of sniffer software.</p><table border="0"><thead><tr><td colspan="2">Sniffer tool usage</td></tr><tr><td>Ethical usage</td><td>Unethical usage</td></tr></thead><tbody><tr><td><ul><li>Packet capturing</li><li>Network traffic usage and analysis</li><li>Packet conversion for data analysis</li><li>Network troubleshooting</li></ul></td><td><ul><li>User identity and password stealing</li><li>Email or instant message data stealing</li><li>Packet spoofing and data theft</li><li>Monetary or reputational damage</li></ul></td></tr></tbody></table><p>Regarding the technical details of how sniffing is done, we need to remember that packet capturing software always runs in promiscuous mode, whereby it is capable of intercepting and storing all packets on a network. This also means that, even though the packet is not meant for the network interface on which the sniffer is running, it is captured, stored and analysed.</p><p>Sniffer software contains its own network driver and buffer memory in order to capture a large chunk of packets. Modern sniffers are capable of analysing the captured packets and converting them into sensible statistical information. Now let&#8217;s discuss a few ways of sniffing a network, to understand how hackers get what they want.</p><h3>A LAN sniff</h3><p>A sniffer deployed on an internal LAN can scan the entire IP range promiscuously. This helps in providing further details such as live hosts, open ports, server inventory, etc. Once a list of open ports is gathered, a port-specific vulnerability attack is possible.</p><h3>A protocol sniff</h3><p>This method involves sniffing data related to the network protocols being used. First, a list of protocols is created based on the captured data. This is further segregated to create special sniffers for each attack. For example, in a network sniff capture, if the ICMP protocol is not seen, it is assumed to be blocked. However, if UDP packets are seen, a separate UDP sniffer is started to capture and decipher Telnet, PPP, DNS and other related application details.</p><h3>An ARP sniff</h3><p>In this popular method, the hacker captures a lot of data in order to create a map of IP addresses and the associated MAC addresses. Such a map is further used to create ARP poisoning attacks, packet-spoofing attacks, or to dig into router-based vulnerabilities.</p><h3>TCP session stealing</h3><p>This method is a very basic form of sniffing, in which a network interface in promiscuous mode captures traffic between a source and a destination IP address. Details such as port numbers, service types, TCP sequence numbers and the data itself are of interest to hackers. Upon capturing enough packets, advanced hackers can create fabricated TCP sessions to fool the source and destination, and be the man in the middle to take over the TCP session.</p><h3>Application-level sniffing</h3><p>Usually, from the data packets sniffed and captured, a few intricate application details are found out for information stealing or to create further attacks. As an example, the capture file can be parsed to perform OS fingerprinting, SQL query analysis, reveal application-specific TCP port data information, etc. In another approach, creating a mere list of applications running on a server is good enough to plan an application-specific attack on it.</p><h3>Web password sniffing</h3><p>As the name suggests, HTTP sessions are stolen and parsed for user ID and password stealing. While the Secure Socket Layers (SSL) are incorporated for securing HTTP sessions on the network, there are numerous internal websites that still use standard but less secure encryption. It is easy to capture Base64 or Base128 packets and run a deciphering agent against it to crack the password. In modern sniffers, SSL sessions can also be captured and parsed for information, though this method is not very easy.</p><h2>Detecting sniffers</h2><p>As mentioned earlier, since sniffers work silently, it is very difficult to detect them on a network. There are, however, a few tricks that can provide a clue to a possible sniffer presence. There are two ways to detect a sniffer &#8212; host-based and network-based.</p><p>In host-based detection, you can use small utilities to detect if the NIC is running in a promiscuous mode on any host in a network. Since the basic requirement for a sniffer to work is to put the network interface in &#8220;read all&#8221; mode, disabling it can very effectively help shutting down stray sniffers.</p><p>In case of network-based detection, anti-sniffer software can be run to detect the presence of specific signature packets. In another approach, scripts can be run to check each network host for the presence of known sniffers, processes, etc. Modern anti-virus or anti-spyware software are capable of detecting sniffing software and disabling it.</p><h2>Protection from sniffers</h2><p>While the very first step should be to design a tight perimeter defense system while creating network architecture, there are a few methods that could be deployed to make the infrastructure less sniffer-prone. The following tricks help achieve that to a great extent.</p><p>Disabling promiscuous mode on network interfaces results in shutting down most sniffer software. This can be done by running an admin script as a daily job on the network, or deploying a network policy at the host level to control access to the network card configuration settings.</p><p>Using switched networks can reduce the possibility of a sniffer running on the network. Unlike in a network hub, in a switched network the packets are delivered to the destination and are not visible to all nodes &#8212; thus reducing the chances of someone sniffing it on the way. Also, for network administrators it becomes easy to detect sniffers by focusing on the switched network segments, one at a time.</p><p>Anti-sniffing tools can be used to detect the network interface mode, as well as various processes and software present on servers or network hosts. Modern intrusion-detection systems have this as an integrated feature.</p><p><a href="http://www.linuxforu.com/2012/01/ipsec-vpn-penetration-testing-backtrack-tools/" title="IPsec VPN Penetration Testing with BackTrack Tools">IPSec</a> encryption can be used for token-based packet security in the network infrastructure, if the data is of a confidential nature. IPSec provides data encapsulation and encryption of high standards, and is available on modern routers, firewalls and other network components. Almost all operating systems do support IPSec, and it is widely used in serious IT infrastructure. For session layer protection, SSL and TLS can be used to encrypt traffic.</p><h2>Protecting FOSS systems</h2><p>Let us now look at a few sniffer products, in order to learn about what is used in the FOSS world today. Linux systems use the <code>tcpdump</code> utility, which is an excellent built-in sniffer to capture and store TCP packets. As for third-party open source tools, Wireshark (Ethereal) is very famous due to its GUI interface, and packet-filtering and viewing capabilities. Sniffit, DSniff and Ettercap are similar utilities, but meant for different purposes. DSniff is powerful in terms of capturing SSL traffic.</p><p>FOSS systems have no built-in method to protect themselves from sniffers. The methods described above could pretty much be used for various Linux distros, to make those less vulnerable to sniffer attacks. A smart utility available on Linux distros, called AntiSniff, can be used in a script to detect network interfaces in promiscuous mode.</p><p>Network sniffing is difficult to detect because it is a passive and silent type of attack. There are methods to detect and disable sniffers, and network administrators should incorporate those into their network to protect their IT infrastructure from information loss or theft.<div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2011/12/cyber-attacks-explained-packet-spoofing/" rel="bookmark" class="crp_title">Cyber Attacks Explained: Packet Spoofing</a></li><li><a href="http://www.linuxforu.com/2009/01/it%e2%80%99s-so-easy-to-see-your-network-activity-hah/" rel="bookmark" class="crp_title">It’s So Easy to See Your Network Activity, hah!</a></li><li><a href="http://www.linuxforu.com/2011/11/cyber-attacks-explained-dos-and-ddos/" rel="bookmark" class="crp_title">Cyber Attacks Explained: DoS and DDoS</a></li><li><a href="http://www.linuxforu.com/2011/03/capturing-packets-in-your-c-program-with-libpcap/" rel="bookmark" class="crp_title">Capturing Packets in Your C Program, with Libpcap</a></li><li><a href="http://www.linuxforu.com/2011/06/best-practices-network-security-monitoring/" rel="bookmark" class="crp_title">Best Practices in Network Security Monitoring</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/anti-sniffer-software/" title="anti-sniffer software" rel="tag">anti-sniffer software</a>, <a href="http://www.linuxforu.com/tag/anti-sniffing-tools/" title="anti-sniffing tools" rel="tag">anti-sniffing tools</a>, <a href="http://www.linuxforu.com/tag/attackers/" title="attackers" rel="tag">attackers</a>, <a href="http://www.linuxforu.com/tag/data-security/" title="data security" rel="tag">data security</a>, <a href="http://www.linuxforu.com/tag/denial-of-service/" title="denial of service" rel="tag">denial of service</a>, <a href="http://www.linuxforu.com/tag/denial-of-service-attacks/" title="denial of service attacks" rel="tag">denial of service attacks</a>, <a href="http://www.linuxforu.com/tag/dos/" title="DOS" rel="tag">DOS</a>, <a href="http://www.linuxforu.com/tag/encryption/" title="encryption" rel="tag">encryption</a>, <a href="http://www.linuxforu.com/tag/ethernet/" title="Ethernet" rel="tag">Ethernet</a>, <a href="http://www.linuxforu.com/tag/ethernet-packet/" title="ethernet packet" rel="tag">ethernet packet</a>, <a href="http://www.linuxforu.com/tag/firewall/" title="firewall" rel="tag">firewall</a>, <a href="http://www.linuxforu.com/tag/http/" title="http" rel="tag">http</a>, <a href="http://www.linuxforu.com/tag/icmp/" title="ICMP" rel="tag">ICMP</a>, <a href="http://www.linuxforu.com/tag/icmp-protocol/" title="ICMP protocol" rel="tag">ICMP protocol</a>, <a href="http://www.linuxforu.com/tag/intrusion-detection-systems/" title="intrusion-detection systems" rel="tag">intrusion-detection systems</a>, <a href="http://www.linuxforu.com/tag/ip-addresses/" title="IP addresses" rel="tag">IP addresses</a>, <a href="http://www.linuxforu.com/tag/ipsec-encryption/" title="IPSec encryption" rel="tag">IPSec encryption</a>, <a href="http://www.linuxforu.com/tag/lan/" title="LAN" rel="tag">LAN</a>, <a href="http://www.linuxforu.com/tag/lfy-january-2012/" title="LFY January 2012" rel="tag">LFY January 2012</a>, <a href="http://www.linuxforu.com/tag/mac/" title="Mac" rel="tag">Mac</a>, <a href="http://www.linuxforu.com/tag/network/" title="network" rel="tag">network</a>, <a href="http://www.linuxforu.com/tag/network-details/" title="network details" rel="tag">network details</a>, <a href="http://www.linuxforu.com/tag/network-interfaces/" title="network interfaces" rel="tag">network interfaces</a>, <a href="http://www.linuxforu.com/tag/network-layers/" title="network layers" rel="tag">network layers</a>, <a href="http://www.linuxforu.com/tag/network-packet/" title="network packet" rel="tag">network packet</a>, <a href="http://www.linuxforu.com/tag/network-protocols/" title="network protocols" rel="tag">network protocols</a>, <a href="http://www.linuxforu.com/tag/osi-layers/" title="osi layers" rel="tag">osi layers</a>, <a href="http://www.linuxforu.com/tag/packet-analyzer/" title="Packet analyzer" rel="tag">Packet analyzer</a>, <a href="http://www.linuxforu.com/tag/packet-sniffer-software/" title="packet sniffer software" rel="tag">packet sniffer software</a>, <a href="http://www.linuxforu.com/tag/packet-capturing-software/" title="packet-capturing software" rel="tag">packet-capturing software</a>, <a href="http://www.linuxforu.com/tag/protocol-type/" title="protocol type" rel="tag">protocol type</a>, <a href="http://www.linuxforu.com/tag/routers/" title="routers" rel="tag">routers</a>, <a href="http://www.linuxforu.com/tag/sniffer-software/" title="sniffer software" rel="tag">sniffer software</a>, <a href="http://www.linuxforu.com/tag/sniffers/" title="sniffers" rel="tag">sniffers</a>, <a href="http://www.linuxforu.com/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.linuxforu.com/tag/ssl/" title="SSL" rel="tag">SSL</a>, <a href="http://www.linuxforu.com/tag/tcp/" title="TCP" rel="tag">TCP</a>, <a href="http://www.linuxforu.com/tag/tcpip/" title="TCP/IP" rel="tag">TCP/IP</a>, <a href="http://www.linuxforu.com/tag/tcpip-protocol/" title="TCP/IP protocol" rel="tag">TCP/IP protocol</a>, <a href="http://www.linuxforu.com/tag/udp/" title="UDP" rel="tag">UDP</a>, <a href="http://www.linuxforu.com/tag/wireshark/" title="Wireshark" rel="tag">Wireshark</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2012/01/cyber-attacks-explained-network-sniffing/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>The Next Y2K &#8212; Are We Ready for a Web Without IPv4 Yet?</title><link>http://www.linuxforu.com/2011/06/next-y2k-rip-ipv4/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=next-y2k-rip-ipv4</link> <comments>http://www.linuxforu.com/2011/06/next-y2k-rip-ipv4/#comments</comments> <pubDate>Tue, 31 May 2011 18:54:23 +0000</pubDate> <dc:creator>Puneet Kumar</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[CXOs]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[Sysadmins]]></category> <category><![CDATA[Akamai]]></category> <category><![CDATA[backbone networks]]></category> <category><![CDATA[China]]></category> <category><![CDATA[DNS]]></category> <category><![CDATA[European Union]]></category> <category><![CDATA[Facebook]]></category> <category><![CDATA[Google]]></category> <category><![CDATA[http]]></category> <category><![CDATA[IANA]]></category> <category><![CDATA[ICANN]]></category> <category><![CDATA[India]]></category> <category><![CDATA[Internet Protocol]]></category> <category><![CDATA[IP addresses]]></category> <category><![CDATA[IPv4]]></category> <category><![CDATA[IPV6]]></category> <category><![CDATA[IPv6 protocol]]></category> <category><![CDATA[ISP]]></category> <category><![CDATA[Japan]]></category> <category><![CDATA[Korea]]></category> <category><![CDATA[LFY June 2011]]></category> <category><![CDATA[Limelight Networks]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[operating systems]]></category> <category><![CDATA[TCP/IP]]></category> <category><![CDATA[USAGI]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[Yahoo!]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=4708</guid> <description><![CDATA[Yes, we&#8217;ve have done it again &#8212; brought the world to the brink of another disaster, because we&#8217;ve failed to foresee the exponential growth of the Internet! Read on to learn about why...]]></description> <content:encoded><![CDATA[<p><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/06/warning-ipv4.jpeg?d9c344" alt="Warning signs!" title="Warning signs!" width="300" height="225" class="alignright size-full wp-image-8835" /><div class="introduction">Yes, we&#8217;ve have done it again &#8212; brought the world to the brink of another disaster, because we&#8217;ve failed to foresee the exponential growth of the Internet! Read on to learn about why IPv4 shows threatening signs of becoming the next Y2K disaster.</div><p>The Internet has become an essential part of our lives. Everybody is on the Net, surfing with different types of devices: PCs, laptops, iPads, mobile phones, etc. Each of these needs a unique IP address on the Internet, when directly connected. The present IP (Internet Protocol) addressing system, IPv4, uses a 32-bit number, represented in 4 octets &#8212; for example, 192.16.1.2.</p><h2>The problem</h2><p>Since IPv4 addresses are 32-bit numbers, there are only 2<sup>32</sup>, or just over four billion, possible addresses available &#8212; and they have all been allocated. On February 3, 2011, the Internet Assigned Numbers Authority (IANA), which controls the world-wide distribution of IP addresses, allocated the final pool of IPv4 address space to the five Regional Internet Registries (RIRs). This means that there are no longer any IPv4 addresses available for allocation from the IANA to the five RIRs.</p><p>So does this mean the end of the Net? Well, not really. Luckily, this problem was foreseen, and an alternative to IPv4 developed. The new addressing scheme, IPv6, uses 128-bit numbers for addresses &#8212; hence, there are 2<sup>128</sup> individual addresses available (approximately 3.4×10<sup>38</sup>). All new IP addresses from now will be IPv6.</p><p>What, then, is the &#8220;next Y2K&#8221;? The problem is that IPv4 is NOT directly compatible with IPv6. This means that devices connected via IPv4 (which make up most of the existing Internet) cannot communicate directly with systems connected using IPv6 (and vice versa). If your website is on an IPv4 connection, it won&#8217;t get traffic from IPv6 sites/users and the reverse is also true. Getting traffic is essential to any business on the Net; nobody can afford to lose IPv6 users. Hence, to continue offering customers the same level of service, every organisation needs to be planning IPv6 deployment now.</p><h2>The IPv6 compliance scenario</h2><p>All major US government federal agencies have migrated their infrastructure (network backbones) to using IPv6, and all agency networks were enabled to interface with this infrastructure with effect from June 2008. The US government&#8217;s Chief Information Officers require that all new IT procurements be IPv6 compliant. An IPv6-compliant product or system must be able to receive, process, and transmit or forward (as appropriate) IPv6 packets, and should interoperate with other systems and protocols in both IPv4 and IPv6 modes of operation.</p><p>The US Defense Department, with its $30 billion budget, has been buying only IPv6-compliant networking gear since October 2003, and had already achieved full IPv6 compliance in 2008. ICANN, the company that handles the domain names and IP addresses for the world, has also enabled its servers to be IPv6-compliant. There has been significant IPv6 adoption in Japan and Korea, with production networks and consumers paying for IPv6-based services. China is spending millions developing a new backbone network that is reportedly going to be IPv6. The European Union (EU) has also heavily financed R&amp;D in IPv6 backbone networks and innovative services that leverage many of the beneficial features of IPv6.</p><p>On June 8, 2011, Google, Facebook, Yahoo!, Akamai and Limelight Networks are among some major organisations that will offer their content over IPv6 for a 24-hour &#8220;test flight&#8221;. The goal of this Test Flight Day is to motivate organisations across the industry &#8212; Internet service providers, hardware manufacturers, operating system vendors and Web companies &#8212; to prepare their services for IPv6, to ensure a successful transition as IPv4 addresses run out.</p><p>India will start using IPv6 from March 2012, according to a new roadmap released by the Indian government. All telecom companies and ISPs will have to be IPv6-compliant by the end of next year, and offer IPv6 services thereafter. Federal and state government ministries, departments and public sector companies will switch over to IPv6 services by March 2012.</p><h2>So what do I do?</h2><p>It is imperative for any organisation to be IPv6-ready, in order to remain competitive. The first step is to assess where your organisation stands in relation to IPv6. If your website is hosted by an Internet Service Provider (ISP), ask its customer support personnel the following questions:</p><ul><li>Do you currently provide IPv6 connectivity?</li><li>If not, when do you plan to deploy IPv6 on your customer networks? What is your deployment timeline?</li><li>When will my website be available over IPv6? What extra cost will that entail?</li><li>Do you provide customers with IPv6-compatible modems, or other devices necessary for connecting over IPv6?</li><li>Why have you not provided us any information on IPv6 that would help us put in place all that&#8217;s needed from our side, to be ready for IPv6 compliance?</li></ul><p>Next, carry out an audit of your entire IT infrastructure, and identify the hardware/software that needs to be changed or upgraded. For your hardware (routers, servers, etc), get in touch with the OEM or vendor to check for IPv6 compliance. Ensure that all the equipment is dual stacked (running both IPv4 and IPv6). If the hardware does not support IPv6, find out from the OEM whether a replacement is required, or if an upgrade/patch will suffice.</p><p>For software purchased from a third party, get in touch with the provider to check if the product is already IPv6 compatible, or if there&#8217;s an upgrade available. If no IPv6 upgrade is available, look for an alternative software source. Any software that has been developed in-house may have to be rewritten. Identify and change all hard-coded IPv4 addressees in software code to IPv6. Any application that directly uses network sockets or other network-related programming might have to be rewritten. Most operating systems are IPv6 compatible &#8212; IPv6 support is built in, and you only need to activate it. However, identify any legacy systems running an OS without IPv6 support, and either replace them, or patch/upgrade the OS.</p><h2>I use FOSS; am I affected?</h2><p>IPv6 migration is an issue that affects everyone. It is not restricted to only particular hardware or software. The problem is independent of the technology or software you use, as it stems from a core flaw in the TCP/IP addressing scheme. So yes, people using open source operating systems and applications could also be affected.</p><h3>Linux and IPv6</h3><p>The first IPv6-related network code was added to the Linux kernel 2.1.8 in November 1996 by Pedro Roque. In most Linux configurations these days, IPv6 is active by default, and the required IPv6 kernel modules are loaded automatically. To check whether your current running kernel supports IPv6, look in your <code>/proc</code> filesystem for the pseudo-file <code>/proc/net/if_inet6</code>. If this entry is not present, you can try to load the IPv6 module by executing <code>modprobe ipv6</code>. If successful, this module should be listed by the following command:</p><pre class="brush: text; gutter: false">lsmod | grep -w &#039;ipv6&#039; &amp;&amp; echo &quot;IPv6 module successfully loaded&quot;</pre><p>It is also possible to automatically load the IPv6 module on demand. You have to add the following line to the kernel module-loader configuration file, which is normally <code>/etc/modules.conf</code> or <code>/etc/conf.modules</code>:</p><pre class="brush: text; gutter: false">alias net-pf-10 ipv6 # automatically load IPv6 module on demand</pre><p>If your kernel has no IPv6 support, then you can try one of the following options:</p><ul><li>Update your Linux distribution to a current one that supports IPv6 out-of-the-box.</li><li>Compile a new vanilla kernel.</li><li>Recompile kernel sources given by your Linux distribution.</li><li>Compile a kernel with third-party IPv6 extensions.</li></ul><p>However, it is pertinent to point out that IPv6 support in the kernel and the other basic packages is pretty average in terms of performance, and does not conform to all drafts and RFCs (request for comments). Most people tend to switch off the IPv6-compliance feature, as it makes the DNS queries kind of slow.</p><p>To overcome this, you can make use of the USAGI (Universal Playground for IPv6) kit, which contains an enhanced IPv6-enabled version of both the latest 2.6.x Linux kernel, and the most important networking software packages for Linux. USAGI is a project run by volunteers from various organisations, and aims to develop a production-quality IPv6 protocol stack and IPsec for Linux. Based in Japan, USAGI teams up with other such projects like the WIDE Project, the KAME Project and TAHI, to implement all absent or obsolete IPv6 support in Linux. You can download the latest release of the USAGI kit from <a href="http://www.linux-ipv6.org/">here</a>.</p><h3>Apache and IPv6</h3><p>The Apache HTTP server supports IPv6 on most platforms, allowing Apache to allocate IPv6 sockets and handle requests sent over IPv6. However, to allow the IPv6 socket to handle both IPv4 and IPv6 connections, you will need to tweak the configuration parameter and use generic Listen directives. To verify that Apache is working in dual-stack mode (supporting both IPv4 and IPv6), use the <code>netstat</code> command.</p><p>The writing is on the wall. With IPv4 addresses having been exhausted, the time, cost and complexity of operating a stable and growing Internet will rise significantly, unless IPv6 deployment is well under way. So hurry and make your organisation IPv6-ready, now!</p><div class="imagecredit">Feature image courtesy: <a href="http://www.flickr.com/photos/themaninblue/208820744/">Cameron Adams</a>. Reused under terms of CC-BY-ND 2.0 License.</div><div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2011/09/creating-your-own-server-the-socket-api-part-2/" rel="bookmark" class="crp_title">Creating Your Own Server: The Socket API, Part 2</a></li><li><a href="http://www.linuxforu.com/2011/08/creating-your-own-server-the-socket-api-part-1/" rel="bookmark" class="crp_title">Creating Your Own Server: The Socket API, Part 1</a></li><li><a href="http://www.linuxforu.com/2011/05/advanced-nmap-a-recap/" rel="bookmark" class="crp_title">Advanced Nmap: A Recap</a></li><li><a href="http://www.linuxforu.com/2009/08/linux-network-stack-administration-a-developers-approach/" rel="bookmark" class="crp_title">Linux Network Stack Administration: A Developer&#8217;s Approach</a></li><li><a href="http://www.linuxforu.com/2011/08/indian-government-takes-the-open-source-route/" rel="bookmark" class="crp_title">Indian Government Takes the Open Source Route</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/akamai/" title="Akamai" rel="tag">Akamai</a>, <a href="http://www.linuxforu.com/tag/backbone-networks/" title="backbone networks" rel="tag">backbone networks</a>, <a href="http://www.linuxforu.com/tag/china/" title="China" rel="tag">China</a>, <a href="http://www.linuxforu.com/tag/dns/" title="DNS" rel="tag">DNS</a>, <a href="http://www.linuxforu.com/tag/european-union/" title="European Union" rel="tag">European Union</a>, <a href="http://www.linuxforu.com/tag/facebook/" title="Facebook" rel="tag">Facebook</a>, <a href="http://www.linuxforu.com/tag/google/" title="Google" rel="tag">Google</a>, <a href="http://www.linuxforu.com/tag/http/" title="http" rel="tag">http</a>, <a href="http://www.linuxforu.com/tag/iana/" title="IANA" rel="tag">IANA</a>, <a href="http://www.linuxforu.com/tag/icann/" title="ICANN" rel="tag">ICANN</a>, <a href="http://www.linuxforu.com/tag/india/" title="India" rel="tag">India</a>, <a href="http://www.linuxforu.com/tag/internet-protocol/" title="Internet Protocol" rel="tag">Internet Protocol</a>, <a href="http://www.linuxforu.com/tag/ip-addresses/" title="IP addresses" rel="tag">IP addresses</a>, <a href="http://www.linuxforu.com/tag/ipv4/" title="IPv4" rel="tag">IPv4</a>, <a href="http://www.linuxforu.com/tag/ipv6/" title="IPV6" rel="tag">IPV6</a>, <a href="http://www.linuxforu.com/tag/ipv6-protocol/" title="IPv6 protocol" rel="tag">IPv6 protocol</a>, <a href="http://www.linuxforu.com/tag/isp/" title="ISP" rel="tag">ISP</a>, <a href="http://www.linuxforu.com/tag/japan/" title="Japan" rel="tag">Japan</a>, <a href="http://www.linuxforu.com/tag/korea/" title="Korea" rel="tag">Korea</a>, <a href="http://www.linuxforu.com/tag/lfy-june-2011/" title="LFY June 2011" rel="tag">LFY June 2011</a>, <a href="http://www.linuxforu.com/tag/limelight-networks/" title="Limelight Networks" rel="tag">Limelight Networks</a>, <a href="http://www.linuxforu.com/tag/linux/" title="Linux" rel="tag">Linux</a>, <a href="http://www.linuxforu.com/tag/operating-systems/" title="operating systems" rel="tag">operating systems</a>, <a href="http://www.linuxforu.com/tag/tcpip/" title="TCP/IP" rel="tag">TCP/IP</a>, <a href="http://www.linuxforu.com/tag/usagi/" title="USAGI" rel="tag">USAGI</a>, <a href="http://www.linuxforu.com/tag/web-news/" title="Web" rel="tag">Web</a>, <a href="http://www.linuxforu.com/tag/yahoo/" title="Yahoo!" rel="tag">Yahoo!</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2011/06/next-y2k-rip-ipv4/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Virtual Machines For Abstraction: The Dalvik VM</title><link>http://www.linuxforu.com/2011/06/virtual-machines-for-abstraction-dalvik-vm/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=virtual-machines-for-abstraction-dalvik-vm</link> <comments>http://www.linuxforu.com/2011/06/virtual-machines-for-abstraction-dalvik-vm/#comments</comments> <pubDate>Tue, 31 May 2011 18:43:36 +0000</pubDate> <dc:creator>Vishal Kanaujia</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[Developers]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[Android]]></category> <category><![CDATA[C/C++]]></category> <category><![CDATA[Dalvik]]></category> <category><![CDATA[Dalvik VM]]></category> <category><![CDATA[Google]]></category> <category><![CDATA[heterogeneous systems]]></category> <category><![CDATA[interpreter]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[LFY June 2011]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Lua]]></category> <category><![CDATA[memory management]]></category> <category><![CDATA[mobile devices]]></category> <category><![CDATA[opcode]]></category> <category><![CDATA[operand]]></category> <category><![CDATA[software stack]]></category> <category><![CDATA[SQLite]]></category> <category><![CDATA[System C library]]></category> <category><![CDATA[virtual machines]]></category> <category><![CDATA[VirtualBox]]></category> <category><![CDATA[VMData]]></category> <category><![CDATA[VMs]]></category> <category><![CDATA[VMWare]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=8678</guid> <description><![CDATA[With the rise of heterogeneous systems, there is a requirement for a scalable software system that is cost-effective and maintenance-free. Virtual machines (VMs) provide abstraction from the heterogeneity, and present a low-cost, scalable...]]></description> <content:encoded><![CDATA[<p><img class="alignright size-full wp-image-8680" title="Droid time" src="http://cdn.linuxforu.com/wp-content/uploads/2011/06/android_logo.jpg?d9c344" alt="Droid time" width="350" height="415" /></p><div class="introduction">With the rise of heterogeneous systems, there is a requirement for a scalable software system that is cost-effective and maintenance-free. Virtual machines (VMs) provide abstraction from the heterogeneity, and present a low-cost, scalable software development environment, as in the case of VM-based modern programming languages like Java. In this article, we will cover the fundamental concepts of a VM, using one of the critical components of Google&#8217;s Android OS &#8212; the Dalvik VM.</div><p>A virtual machine (or VM) is an isolated, self-contained implementation that abstracts hardware resources. Typically, it simulates an actual physical machine capable of executing instruction sets. Broadly, VMs are classified based on how they work and their  functionality. There are process VMs, which support the execution of a single process/program (e.g., Java, .NET), and system VMs, which support execution of a complete operating system (e.g., VirtualBox, VMWare).</p><h2>How does a VM work?</h2><p>In general, a VM is an interpreter for an instruction-set. The fundamental task of a compute machine is to carry out an operation. An operation is performed with the execution of a set of instructions. A basic instruction goes through the following cycle of events:</p><ul><li>Instruction fetch, where an instruction is fetched from memory.</li><li>Decoding, which determines the type of the instruction &#8212; the opcode, or the operation it is supposed to perform. Additionally, decoding may also involve fetching operand(s) from memory.</li><li>Execute. The decoded instruction is then executed; the operation is performed on the operands.</li><li>Storing the result in the mentioned register.</li></ul><h2>Which VM implementations to choose</h2><p>Mainly, VMs are implemented in one of two ways &#8212; a register-based VM, or a stack-based VM. The register-based VM computation model assumes registers as the memory, and operands are accessed with explicit addresses coded in an instruction. In the stack-based VM model, memory takes the form of a Last-In-First-Out (LIFO) stack. Every operation boils down to a stack PUSH/POP or STORE/LOAD. These instructions do not need explicit operand addresses, because the operand is always available with the stack pointer (top-of-stack). Examples are Microsoft .NET and the JVM.</p><p>Traditionally, when it came to choosing the VM architecture, people preferred stack-based machines, rather than the register-based architecture prevalent in most real processors. The main reason for this choice was the simplicity of the compiler, which emanates from the fact that stack instructions are small, and have implicit addressing. The basic criteria for choosing a particular VM implementation is the code size of the machine implementation (the smaller the better); and machine efficiency &#8212; which is measured in terms of the number of instructions needed to carry out an operation.</p><p>Implementation-wise, instructions for a stack-based VM are shorter (since it&#8217;ll have only POP/PUSH instructions) and are hence faster to fetch. A register-based VM has long instructions, and usually a quadruple of {opcode, result, operand1, operand2}. However, the advantage of the stack-based VM diminishes in terms of the number of instructions needed to perform an operation. A given task can often be expressed in fewer register-based VM instructions than what is required for a stack VM. For example, an addition operation &#8220;a = b + c&#8221; would need the following instructions for a stack VM:</p><pre class="brush: text; gutter: false">I1: LOAD C
I2: LOAD B
I3: ADD
I4: STORE A</pre><p>The equivalent operation in a register VM can be expressed in a single instruction:</p><pre class="brush: text; gutter: false">I1 &quot;add, a, b, c&quot;</pre><p>The fewer the instructions, the smaller the number of instruction dispatches needed. (Instruction dispatch involves fetching/reading the instruction from memory, and jumping to the corresponding segment of VM code that implements the VM instruction.) Instruction dispatch is an expensive and often unpredictable operation, because in implementation it is essentially a &#8220;switch-case&#8221;, and hence a hard-to-predict indirect branch, unlike an &#8220;if-else&#8221; condition.</p><p>There is a cost involved in the operand access too. In stack VMs, operands are accessed relative to the stack pointer, and hence explicit addressing for operands is not required. On the contrary, register VMs need to provide the location of all operands, by specifying the registers that carry those operands.</p><p>Having all operands in the instruction has its benefits; the execution is faster compared to the stack VM, which needs a small calculation to find out the operand, while register VMs just read the registers.<br /> Also, a stack VM might consume more memory cycles, since it uses a set of registers to simulate a stack. Besides, temporary values often spill out into the main memory, adding more memory cache cycles.</p><p>In register VMs, temporary values usually remain in registers. Stack VMs are unable to use a few optimisations too. For example, in the case of common sub-expressions, which are recalculated each time they appear in the code, a register VM can calculate an expression once, and keep that in a register for all future references.</p><p>These reasons made a register VM model the preferred choice of implementation for the Dalvik VM, where quick execution is the foremost requirement.</p><h2>The Android OS stack</h2><p>Android is a complete software stack that comprises the Linux kernel, middleware and a few useful applications. It is developed, promoted, and maintained by Google. Its software stack is shown in Figure 1.</p><div id="attachment_8685" class="wp-caption aligncenter" style="width: 412px"><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/06/dalvikvm1.jpg?d9c344" alt="Android OS stack" title="Android OS stack" width="412" height="458" class="size-full wp-image-8685" /><p class="wp-caption-text">Figure 1: Android OS stack</p></div><p>The top layer comprises portable user applications, programmed in Java. These rely on the next layer, which exports a rich set of Java APIs, and provides a set of services and a system to facilitate applications, e.g., the content manager, resource manager, view services, etc.</p><p>The next layer comprises C/C++ system libraries and the Android runtime. The common set of system libraries includes:</p><ul><li>System C library (libc)</li><li>Media library</li><li>SQLite (database engine)</li><li>SGL (to support 2-D graphics)</li></ul><p>The Android runtime is a combination of the Dalvik VM and Java core libraries.</p><p>The Linux kernel is the last layer in the software stack, and provides an abstraction of the underlying hardware, also taking care of networking, process and memory management.</p><h2>Dalvik VM</h2><p>Dalvik VM is Google&#8217;s implementation of the Java VM, targeted to run on mobile devices. Dalvik is a process VM; each Android application runs in its own Linux process. Every process on Android has its own instance of the Dalvik VM, which is created along with the process, and destroyed during process termination.</p><p>&#8220;init&#8221; is the parent process for all Android processes. During startup (see Figure 2), in addition to a handful of daemons, &#8220;init&#8221; forks a process named <code>zygote</code>, which loads and initialises an instance of the Dalvik VM and core services. <code>zygote</code> then creates a read socket, and runs a select loop on it. When we run an application, a message is sent to <code>zygote</code>, which forks, and contacts the Android Activity manager service to run the application&#8217;s Java class. Thus, all processes share a copy of DVM, created by the Zygote process, and avoid keeping duplicate pages of DVM.</p><div id="attachment_8686" class="wp-caption aligncenter" style="width: 414px"><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/06/dalvikvm2.jpg?d9c344" alt="The Android runtime" title="The Android runtime" width="414" height="239" class="size-full wp-image-8686" /><p class="wp-caption-text">Figure 2: The Android runtime</p></div><p>Actually, pages for DVM are COW (copy-on-write) among processes; as soon as a process writes to a shared, read-only page, that page becomes local to the process.</p><h3>Why a process VM?</h3><p>This design has many benefits compared to a single system VM instance that serves all processes in the system. If the VM crashes, the whole system would crash, killing all processes. In Android, this problem has been narrowed down to the granularity of a single process.</p><h3>VM internal details</h3><p>The Dalvik VM accepts Dalvik byte-code (<code>*.dex</code> files) as input, instead of the Java byte-code expected by a JVM. The <code>*.dex</code> files are created by the <code>dx</code>tool, which is part of the Android system (see Figure 3). This tool takes Java class files created by the Java compiler as the input.</p><div id="attachment_8683" class="wp-caption aligncenter" style="width: 233px"><img class="size-full wp-image-8683" title="The compilation flow of a Java application on Android" src="http://cdn.linuxforu.com/wp-content/uploads/2011/06/dalvik3.jpg?d9c344" alt="The compilation flow of a Java application on Android" width="233" height="263" /><p class="wp-caption-text">Figure 3: The compilation flow of a Java application on Android</p></div><h3>How is a Dalvik-like register VM implemented?</h3><p>To answer that, let&#8217;s implement a register VM in C &#8212; <code>myvm.c</code>:</p><pre class="brush: c; gutter: true; first-line: 1">/*
 * myvm.c
 */
#include &quot;stdio.h&quot;

#define NUM_REGS 4
#define TRUE    1
#define FALSE   0
#define INVALID -1

enum opCodes {
    HALT  = 0x0,
    LOAD  = 0x1,
    ADD   = 0x2,
};

/*
 * Register set of the VM
 */
int regs[NUM_REGS];

/*
 * VM specific data for an instruction
 */
struct VMData_ {
  int reg0;
  int reg1;
  int reg2;
  int reg3;
  int op;
  int scal;
};
typedef struct VMData_ VMData;

int  fetch();
void decode(int instruction, VMData *data);
void execute(VMData *data);
void run();

/*
 * Addressing Modes:
 * - Registers used as r0, r1,..rn.
 * - Scalar/ Constant (immediate) values represented as #123
 * - Memory addresses begin with @4556
 */

/*
 * Instruction set:
 * - Load an immediate number (a constant) into a register
 * - Perform an arithmetic sum of two registers (in effect,
 *   adding two numbers)
 * - Halt the machine
 *
 * LOAD reg0 #100
 * LOAD reg1 #200
 * ADD reg2 reg1 reg0  // &#039;reg2&#039; is destination register
 * HALT
 */

/*
 * Instruction codes:
 * Since we have very small number of instructions, we can have
 * instructions that have following structure:
 * - 16-bit instructions
 *
 * Operands get 8-bits, so range of number supported by our VM
 * will be 0-255.
 * The operands gets place from LSB bit position
 * |7|6|5|4|3|2|1|0|
 *
 * Register number can we encoded in 4-bits
 * |11|10|9|8|
 *
 * Rest 4-bits will be used by opcode encoding.
 * |15|14|13|12|
 *
 * So an &quot;LOAD reg0 #20&quot; instruction would assume following encoding:
 * &lt;0001&gt; &lt;0000&gt; &lt;00010100&gt;
 * or 0x1014 is the hex representation of given instruction.
 */

 /*
  * Sample program with an instruction set
  */

unsigned int code[] = {0x1014,
                       0x110A,
                       0x2201,
                       0x0000};

/*
 * Instruction cycle: Fetch, Decode, Execute
 */

/*
 * Current state of machine: It&#039;s a binary true/false
 */

int running = TRUE;

/*
 * Fetch
 */
int fetch()
{
  /*
   * Program Counter
   */
  static int pc = 0;

  if (pc == NUM_REGS)
    return INVALID;

  return code[pc++];
}

void decode(int instr, VMData *t)
{
  t-&gt;op   = (instr &amp; 0xF000) &gt;&gt; 12;
  t-&gt;reg1 = (instr &amp; 0x0F00) &gt;&gt; 8;
  t-&gt;reg2 = (instr &amp; 0x00F0) &gt;&gt; 4;
  t-&gt;reg3 = (instr &amp; 0x000F);
  t-&gt;scal = (instr &amp; 0x00FF);
}

void execute(VMData *t)
{
  switch(t-&gt;op) {
    case 1:
      /* LOAD */
      printf(&quot;\nLOAD REG%d %d\n&quot;, t-&gt;reg1, t-&gt;scal);
      regs[t-&gt;reg1] = t-&gt;scal;
      break;

    case 2:
      /* ADD */
      printf(&quot;\nADD %d %d\n&quot;, regs[t-&gt;reg2], regs[t-&gt;reg3]);
      regs[t-&gt;reg1] = regs[t-&gt;reg2] + regs[t-&gt;reg3];
      printf(&quot;\nResult: %d\n&quot;, regs[t-&gt;reg1]);
      break;

    case 0:
    default:
      /* Halt the machine */
      printf(&quot;\nHalt!\n&quot;);
      running = FALSE;
      break;
    }
}

void run()
{
  int instr;
  VMData t;

  while(running)
  {
    instr = fetch();

    if (INVALID == instr)
      break;

    decode(instr, &amp;t);
    execute(&amp;t);
  }
}

int main()
{
  run();
  return 0;
}</pre><p>The primary components of a register VM are:</p><ol><li>A register set: Simulated with an array of integers acting as read/write memory cells.</li><li>An instruction set: What operations, and how many operands, should we support?<ul><li>Our primitive VM supports addition, load and halt operations.</li><li>Addressing mode: Operands are part of the instructions, so an instruction decode would unveil the associated operands.</li></ul></li><li>Execution unit: The engine works similar to a CPU:<ul><li>It fetches the next instruction from the program.</li><li>It decodes the instruction to figure out the type of operation and operands.</li><li>It executes the decoded instruction.</li></ul></li></ol><h5>References</h5><ul><li><a href="http://www.dalvikvm.com/">Dalvik Virtual Machine</a></li><li><a href="http://www.lua.org/">Lua programming language</a></li><li><a href="http://en.wikipedia.org/wiki/Stack_machine#Virtual_stack_machines">Wikipedia on virtual stack machines</a></li><li>Paper titled &#8220;Virtual Machine Showdown: Stack Versus Registers&#8221; [<a href="http://www.usenix.org/events/vee05/full_papers/p153-yunhe.pdf">PDF</a>]</li></ul><div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2011/08/gnusim8085-review/" rel="bookmark" class="crp_title">Reviewing the GNUSim8085</a></li><li><a href="http://www.linuxforu.com/2011/02/handling-interrupts-part-2/" rel="bookmark" class="crp_title">Handling Interrupts &#8211; Part 2</a></li><li><a href="http://www.linuxforu.com/2011/07/accessing-x86-specific-io-mapped-hardware-in-linux/" rel="bookmark" class="crp_title">Device Drivers, Part 8: Accessing x86-Specific I/O-Mapped Hardware</a></li><li><a href="http://www.linuxforu.com/2011/05/android-fragments-api/" rel="bookmark" class="crp_title">Android Fragments API</a></li><li><a href="http://www.linuxforu.com/2009/01/my-own-phone-dialler-only-on-android/" rel="bookmark" class="crp_title">My Own Phone Dialler, Only on Android</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/android/" title="Android" rel="tag">Android</a>, <a href="http://www.linuxforu.com/tag/cc/" title="C/C++" rel="tag">C/C++</a>, <a href="http://www.linuxforu.com/tag/dalvik/" title="Dalvik" rel="tag">Dalvik</a>, <a href="http://www.linuxforu.com/tag/dalvik-vm/" title="Dalvik VM" rel="tag">Dalvik VM</a>, <a href="http://www.linuxforu.com/tag/google/" title="Google" rel="tag">Google</a>, <a href="http://www.linuxforu.com/tag/heterogeneous-systems/" title="heterogeneous systems" rel="tag">heterogeneous systems</a>, <a href="http://www.linuxforu.com/tag/interpreter/" title="interpreter" rel="tag">interpreter</a>, <a href="http://www.linuxforu.com/tag/java/" title="Java" rel="tag">Java</a>, <a href="http://www.linuxforu.com/tag/lfy-june-2011/" title="LFY June 2011" rel="tag">LFY June 2011</a>, <a href="http://www.linuxforu.com/tag/linux/" title="Linux" rel="tag">Linux</a>, <a href="http://www.linuxforu.com/tag/lua/" title="Lua" rel="tag">Lua</a>, <a href="http://www.linuxforu.com/tag/memory-management/" title="memory management" rel="tag">memory management</a>, <a href="http://www.linuxforu.com/tag/mobile-devices/" title="mobile devices" rel="tag">mobile devices</a>, <a href="http://www.linuxforu.com/tag/opcode/" title="opcode" rel="tag">opcode</a>, <a href="http://www.linuxforu.com/tag/operand/" title="operand" rel="tag">operand</a>, <a href="http://www.linuxforu.com/tag/software-stack/" title="software stack" rel="tag">software stack</a>, <a href="http://www.linuxforu.com/tag/sqlite/" title="SQLite" rel="tag">SQLite</a>, <a href="http://www.linuxforu.com/tag/system-c-library/" title="System C library" rel="tag">System C library</a>, <a href="http://www.linuxforu.com/tag/virtual-machines/" title="virtual machines" rel="tag">virtual machines</a>, <a href="http://www.linuxforu.com/tag/virtualbox/" title="VirtualBox" rel="tag">VirtualBox</a>, <a href="http://www.linuxforu.com/tag/vmdata/" title="VMData" rel="tag">VMData</a>, <a href="http://www.linuxforu.com/tag/vms/" title="VMs" rel="tag">VMs</a>, <a href="http://www.linuxforu.com/tag/vmware/" title="VMWare" rel="tag">VMWare</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2011/06/virtual-machines-for-abstraction-dalvik-vm/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Graphics Using Xlib, Part 1</title><link>http://www.linuxforu.com/2011/06/graphics-using-xlib-1/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=graphics-using-xlib-1</link> <comments>http://www.linuxforu.com/2011/06/graphics-using-xlib-1/#comments</comments> <pubDate>Tue, 31 May 2011 18:41:23 +0000</pubDate> <dc:creator>Amol Takkalki</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[Developers]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[API]]></category> <category><![CDATA[C]]></category> <category><![CDATA[client server architecture]]></category> <category><![CDATA[client/server]]></category> <category><![CDATA[display hardware]]></category> <category><![CDATA[graphical user interfaces]]></category> <category><![CDATA[grapnics]]></category> <category><![CDATA[GUI]]></category> <category><![CDATA[LFY June 2011]]></category> <category><![CDATA[MIT]]></category> <category><![CDATA[network protocols]]></category> <category><![CDATA[Robert Schiefler]]></category> <category><![CDATA[Sean MacEntee]]></category> <category><![CDATA[UI]]></category> <category><![CDATA[unix]]></category> <category><![CDATA[user applications]]></category> <category><![CDATA[window manager]]></category> <category><![CDATA[x consortium]]></category> <category><![CDATA[X server]]></category> <category><![CDATA[X Window System]]></category> <category><![CDATA[Xlib]]></category> <category><![CDATA[xlib library]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=4825</guid> <description><![CDATA[This article introduces you to the X Window System, with the aim of helping developers understand how to use the Xlib library effectively. The X Window System has a very distinct place in...]]></description> <content:encoded><![CDATA[<p><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/07/x-graphics-350x262.jpg?d9c344" alt="Ain&#039;t no sunshine without graphics" title="Ain&#039;t no sunshine without graphics" width="350" height="262" class="alignright size-medium wp-image-8121" /><div class="introduction">This article introduces you to the X Window System, with the aim of helping developers understand how to use the Xlib library effectively.</div><p>The X Window System has a very distinct place in Linux history. Originally developed at MIT in 1984, X works with multiple applications simultaneously, generating text and graphics in monochrome, colour and bitmap display. X also provides facilities for generating multi-font text and two-dimensional structures like lines, polygons and circles. X even provides a basic framework for building graphical user interfaces. X is simply another application sitting on the top of the Linux kernel.</p><p>To start with, you may be confused between X Window System and Xlib. Actually, X is fundamentally composed of network protocols, which are quite complex, and consist of bits and message transfers. To reduce complexity, different libraries handle the complexity internally, providing programmers with an API. Xlib is the library for the C programming language.</p><h2>The history and design of X</h2><p>X was created as a research project in the early 80s. The committee responsible for X is the MIT X Consortium. The lead programmer was Robert Schiefler. In the early 90s, X was ported to run on Linux &#8212; an architecture that was known as the XFree86 system. XFree86 provides a client/server interface between the display hardware and the desktop environment.</p><p>With X, it was possible, for the first time, to write programs for an entire class of machines, rather than limited to a particular hardware manufacturer. Programmers could write programs that ran on almost any combination of hardwarepossibly without even recompiling them.</p><p>Let us now look at the client-server architecture of X. The server is the program that controls the display, while user applications are clients. The server passes user inputs to the respective/active client program, and displays the information provided by the client program. The server acts as an intermediary between clients running on local or remote systems, and the resources of the local or remote system.</p><p>The design of X introduced the idea of X terminals &#8212; thin clients that ran only an X server and connected to a large server computer to actually run useful programs. These were executed on the server system, and were displayed to the user on the terminal. The X client-server architecture even enabled users to have X programs display their output on a Windows system, while the program itself ran on a UNIX machine &#8212; which actually is one of the most significant features of X. The following figure gives a schematic diagram of how this works.</p><div id="attachment_8122" class="wp-caption aligncenter" style="width: 329px"><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/07/Schematic-diagram-explaining-X.jpg?d9c344" alt="Schematic diagram explaining the working of X" title="Schematic diagram explaining the working of X" width="329" height="420" class="size-full wp-image-8122" /><p class="wp-caption-text">Schematic diagram explaining the working of X</p></div><p>X clients (programs) are written using Xlib to communicate with the X protocol. The X protocol defines each packet of information that is transferred between the server and Xlib, which is in one of the following forms:</p><ul><li>Request (from Xlib to the server): Information like specifications for circles.</li><li>Reply (from server to Xlib): In certain requests, there is a reply.</li><li>Events (from server to Xlib): Information of the user action like a mouse click.</li><li>Errors (to the client): Errors are handled by an error-handling routine in Xlib.</li></ul><p>X clients do not actually control display-related attributes like the window size or position. In fact, clients must not be dependent on a particular window configuration, though a client can give hints about how long, and where it would like to be displayed. The screen layout and the style of user interaction with the system, are managed by an X Window manager.</p><p>Some of you might be wondering how the requests are handled. Well, Xlib groups requests, instead of sending them one by one. This grouping also helps improve network performance, as the longer and less frequent transfers reduce the overall load.</p><p>Normally, after an event arrives at a client, the application generates more requests to drawfor example, highlighting the border of a button when the user moves the mouse pointer over it. These responses stay queued in Xlib, until all the events that have already arrived have been processed.</p><p>The schematic diagram above gives a clear picture about the whole process. When an application starts, all the requests that create the initial appearance (like a splash image) are queued up by Xlib. After the initial loading is done, the application waits for user input from the server. After the user activity is detected, the server sends the requests to Xlib.</p><p>Please note that the server does not queue or group requests; it acts immediately on any request received from the user or Xlib. Here, Xlib sends the requests to the client in the order in which they were received. In case there is an error, the application throws an error message to the server, and it generates the necessary output.</p><h2>What&#8217;s next?</h2><p>This article was purely theoretical, and aimed to generate awareness about Xlib. I would personally recommend that you understand all the concepts, to better enable you to write a program in Xlib with a proper flow. This is important, because you have to open and close a connection with the server; undetanding the flow lets you know which function to use at which point in time.</p><p>In the <a href="http://www.linuxforu.com/2011/07/graphics-using-xlib-2-writing-programs/" title="Graphics Using Xlib, Part 2: Writing Programs">next article</a>, I will demonstrate a simple program for plotting a series of points in a graph. The concepts explained in this part will definitely have an impact on your further understanding. See you soon!</p><div class="imagecredit">Feature image courtesy: <a href="http://www.flickr.com/photos/smemon/6421487495/">Sean MacEntee</a>. Reused under the terms of CC-BY 2.0 License.</div><div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2011/07/graphics-using-xlib-2-writing-programs/" rel="bookmark" class="crp_title">Graphics Using Xlib, Part 2: Writing Programs</a></li><li><a href="http://www.linuxforu.com/2011/08/creating-your-own-server-the-socket-api-part-1/" rel="bookmark" class="crp_title">Creating Your Own Server: The Socket API, Part 1</a></li><li><a href="http://www.linuxforu.com/2011/10/socket-api-part-3-concurrent-servers/" rel="bookmark" class="crp_title">The Socket API, Part 3: Concurrent Servers</a></li><li><a href="http://www.linuxforu.com/2011/11/socket-api-part-4-datagrams/" rel="bookmark" class="crp_title">The Socket API, Part 4: Datagrams</a></li><li><a href="http://www.linuxforu.com/2011/06/shared-computing-where-pci-performs-usb-lags/" rel="bookmark" class="crp_title">Shared Computing &#8212; Where PCI Performs and USB Lags</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/api/" title="API" rel="tag">API</a>, <a href="http://www.linuxforu.com/tag/c/" title="C" rel="tag">C</a>, <a href="http://www.linuxforu.com/tag/client-server-architecture/" title="client server architecture" rel="tag">client server architecture</a>, <a href="http://www.linuxforu.com/tag/clientserver/" title="client/server" rel="tag">client/server</a>, <a href="http://www.linuxforu.com/tag/display-hardware/" title="display hardware" rel="tag">display hardware</a>, <a href="http://www.linuxforu.com/tag/graphical-user-interfaces/" title="graphical user interfaces" rel="tag">graphical user interfaces</a>, <a href="http://www.linuxforu.com/tag/grapnics/" title="grapnics" rel="tag">grapnics</a>, <a href="http://www.linuxforu.com/tag/gui/" title="GUI" rel="tag">GUI</a>, <a href="http://www.linuxforu.com/tag/lfy-june-2011/" title="LFY June 2011" rel="tag">LFY June 2011</a>, <a href="http://www.linuxforu.com/tag/mit/" title="MIT" rel="tag">MIT</a>, <a href="http://www.linuxforu.com/tag/network-protocols/" title="network protocols" rel="tag">network protocols</a>, <a href="http://www.linuxforu.com/tag/robert-schiefler/" title="Robert Schiefler" rel="tag">Robert Schiefler</a>, <a href="http://www.linuxforu.com/tag/sean-macentee/" title="Sean MacEntee" rel="tag">Sean MacEntee</a>, <a href="http://www.linuxforu.com/tag/ui/" title="UI" rel="tag">UI</a>, <a href="http://www.linuxforu.com/tag/unix/" title="unix" rel="tag">unix</a>, <a href="http://www.linuxforu.com/tag/user-applications/" title="user applications" rel="tag">user applications</a>, <a href="http://www.linuxforu.com/tag/window-manager/" title="window manager" rel="tag">window manager</a>, <a href="http://www.linuxforu.com/tag/x-consortium/" title="x consortium" rel="tag">x consortium</a>, <a href="http://www.linuxforu.com/tag/x-server/" title="X server" rel="tag">X server</a>, <a href="http://www.linuxforu.com/tag/x-window-system/" title="X Window System" rel="tag">X Window System</a>, <a href="http://www.linuxforu.com/tag/xlib/" title="Xlib" rel="tag">Xlib</a>, <a href="http://www.linuxforu.com/tag/xlib-library/" title="xlib library" rel="tag">xlib library</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2011/06/graphics-using-xlib-1/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>A Simple Guide to Database Design in MySQL</title><link>http://www.linuxforu.com/2011/05/a-simple-guide-to-database-design-in-mysql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-simple-guide-to-database-design-in-mysql</link> <comments>http://www.linuxforu.com/2011/05/a-simple-guide-to-database-design-in-mysql/#comments</comments> <pubDate>Sat, 30 Apr 2011 18:45:33 +0000</pubDate> <dc:creator>Siddharth Mankad</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[For You & Me]]></category> <category><![CDATA[How-Tos]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[Tools / Apps]]></category> <category><![CDATA[acid]]></category> <category><![CDATA[Amazon]]></category> <category><![CDATA[Anthony Mills]]></category> <category><![CDATA[API]]></category> <category><![CDATA[database driver]]></category> <category><![CDATA[database management system]]></category> <category><![CDATA[Durval Castro]]></category> <category><![CDATA[e commerce site]]></category> <category><![CDATA[Gene Bellinger]]></category> <category><![CDATA[information databases]]></category> <category><![CDATA[interface driver]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[jdbc]]></category> <category><![CDATA[LFY May 2011]]></category> <category><![CDATA[Murphy's Law]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[odbc]]></category> <category><![CDATA[Oracle]]></category> <category><![CDATA[phpMyAdmin]]></category> <category><![CDATA[product catalogues]]></category> <category><![CDATA[RDBMS]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Sun]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=4612</guid> <description><![CDATA[As the title says, this is an introduction to databases, their purpose and features. It deals with how to design and create a simple database in MySQL. Starting off with the concepts of...]]></description> <content:encoded><![CDATA[<p><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/05/db-design-590x261.jpg?d9c344" alt="Designing a DB" title="Designing a DB" width="590" height="261" class="aligncenter size-large wp-image-9079" /><div class="introduction">As the title says, this is an introduction to databases, their purpose and features. It deals with how to design and create a simple database in MySQL. Starting off with the concepts of data and information, it is also a good read for those new to databases.</div><p>It&#8217;s a Saturday morning, as I feed these words into a word processor. I have a browser running in the background, with a page on eBay open in one tab, and Engadget in another. The sheer simplicity of browsing through the product catalogues on eBay, or reading the latest and greatest tech news on Engadget, is living proof of the power of the database. The information age, the social networking age, the collaborative age, or whatever &#8220;age&#8221; you may want to call it, all revolve around data.</p><p>Shouldn&#8217;t the information age be revolving around, well, information? At this point, I would like to highlight a key difference between information and data. Information is processed data. From a philosophical standpoint, it has been wonderfully put by Gene Bellinger, Durval Castro and Anthony Mills in their <a href="http://www.systems-thinking.org/dikw/dikw.htm">article, aptly titled, &#8220;Data, Information, Knowledge, and Wisdom&#8221;</a>; the cumulative order would be <em>Data &lt; Information &lt; Knowledge &lt; Understanding &lt; Wisdom</em>.</p><p>As far as computing goes in day-to-day contexts, the buck stops at information databases, which, as the word suggests, deal with what underlies information, i.e., data. Consider the product page on eBay. All you see is information, the cohesive form, which consists of the product images, the description, the price, information about the seller, etc. The data is beneath this processed presentation in the browser.</p><p>In this example, the individual components are data &#8212; like the words in the description. The words, by themselves, can&#8217;t stand independently on the page. Only when they are in context with one another, and with other data, do they convey something that makes sense. When they are processed, they turn into information.</p><p>The domain of databases is vast, and fills volumes and volumes of mammoth publications. The scope of this article is to give you a taste and understanding of databases, how to go about designing one, and I will conclude with designing an actual but hypothetical database, and implementing it.</p><h2>So, what are databases?</h2><p>Databases are structures that hold data. The software that enables the flow of data through these structures is called a database management system or DBMS. The most widely used system of DBMS is something called RDBMS or Relational DBMS. This simply means that the data is stored in tables; moreover, whatever relationships that exist within the data are stored within tables.</p><p>To put it simply, there are three parts that make a database:</p><ol><li><strong>Tables:</strong> We all know what tables are &#8212; a matrix of rows and columns. In databases, it&#8217;s the same. Each row is a record, or a unit of data. A record (row) can have several columns or fields. Each field is like an attribute of that record.</li><li><strong>Queries:</strong> A query is a question posed to the database, to retrieve a specific set of records, based on conditions supplied in the query.</li><li><strong>Views:</strong> These are virtual tables, or (a set of) stored queries.</li></ol><p>At a physical level, the data is stored in data files specific to the DBMS. Examples of modern-day RDBMSs that are widely used include Oracle, MySQL, etc. Oracle is the largest commercially available RDBMS, and MySQL (earlier acquired by Sun, and subsequently by Oracle) is a free and open source RDBMS that is very well-known.</p><h2>Some other (important) concepts</h2><p>A database is accessed, read from and written to, using an interface that allows applications to store and retrieve data. This interface is called an interface driver or database driver. There is usually more than one driver for an RDBMS. ODBC (Open DataBase Connectivity) is a popular database driver. So, if you write a program that needs to store and read data, you would use the API (Application Programming Interface &#8212; a collection of functions) provided by ODBC to do so. Similarly, there is JDBC (Java DataBase Connectivity) and others.</p><p>Let&#8217;s suppose you are making a payment online, and the power goes out at the least opportune moment, say, just when you hit the <em>Pay</em> button (as we all know, Murphy&#8217;s Law never fails us; it&#8217;s like gravity, I guess). Did the money get deducted from your bank account? If it did, did the merchant receive it? If so, does the system know that it happened successfully?</p><p>There are so many steps involved after you push that <em>Pay</em> button on Amazon, eBay or any e-commerce site; like the various messages exchanged between the participating banks, the merchant and the buyer (you). Once the power comes back, you can see what happened: the money has gone from your account, but the order status is <em>Unpaid</em>. The seller says he didn&#8217;t receive the money.</p><p>This is where the concept of a transaction comes in. If one step in the multitude of steps fails, then the action won&#8217;t happen. It&#8217;s an all-or-nothing scenario. Either your purchase will be complete, with all steps successful, or nothing happens. There is no in-between state. So, in our example, the bank will roll back the changes, and the money will reappear (depending, of course, on the bank&#8217;s time policies).</p><h2>It&#8217;s about ACID</h2><p>ACID stands for atomicity, consistency, isolation and durability. We have just seen what transactions are all about. The concept of ACID is nothing but a set of properties that define a successful execution of a transaction.</p><p>The all-or-nothing attribute of the transaction is called atomicity. The data must be valid for the transaction to be successful; this is the consistency of the transaction. In the above example, it&#8217;s a guaranteed possibility that there will be many buyers making purchases on the site. The RDBMS must take care that each transaction is isolated from the rest, to prevent data with errors. This is the isolation of the transaction.</p><p>And finally, we have durability, which is an assurance that the RDBMS makes that when the transaction completes successfully, it stays that way—irrespective of any form of failure that may occur once the transaction completes. It&#8217;s also a way to make sure that the transaction is rolled back if it has failed intermediately.</p><h2>Getting in touch with the data: SQL</h2><p>The theory part is fundamentally in place now. But how does one actually communicate with an RDBMS? This is done by what&#8217;s called SQL (Structured Query Language; pronounced &#8220;sequel&#8221;). SQL is pretty easy to understand, since it&#8217;s like broken English. I&#8217;ll give you an example: There is a table that stores the names, addresses, cities, countries and phone numbers of college students. I want the RDBMS to show me all students who are from, say, Mumbai. This would be the SQL statement for that query:</p><pre class="brush: sql; gutter: false; first-line: 1">SELECT * FROM STUDENT_MASTER WHERE city=&#039;Mumbai&#039;;</pre><p>Here, the asterisk (<code>*</code>) is synonymous to all &#8212; it&#8217;s a wild-card character. The query will return all rows where the city is Mumbai. This SQL statement can be passed on to the RDBMS via an API function call in code, or through an application like phpMyAdmin. Upon receiving the query, the system returns the records as per the query. Suppose we want only names of the students from Mumbai, and not whole records (we don&#8217;t want their addresses, phone numbers or countries of origin), the query would then become:</p><pre class="brush: sql; gutter: false; first-line: 1">SELECT name FROM STUDENT_MASTER WHERE city=&#039;Mumbai&#039;;</pre><p>Note that the word Mumbai is in quotes because it is text. Numbers won&#8217;t have quotes in a query. And the statement must end in a semi-colon.</p><p>For the next part of this article, I will be using MySQL to create the database. Here&#8217;s a quick way to go about installing and using it:</p><ul><li>Windows users: Download MySQL from its website (it&#8217;s called the MySQL Community Server). Just run the installer, and you should be okay.</li><li>Linux users: Use the package manager and install <code>mysql</code>; alternatively, you can build it from source, if you wish to.</li></ul><p>You can start MySQL through the command prompt (Windows) or terminal (Linux), by running <code>mysql -u root -p</code>. It will ask you for a password; keep it blank since the default is blank. You can change it later.</p><h2>Types of SQL statements</h2><p>SQL statements can be classified into the following four categories:</p><ul><li><strong>DDL (Data Definition Language):</strong> This class of statement is used to create/destroy or define/change database components. Examples: <code>CREATE</code>, <code>DROP</code>, <code>RENAME</code>.</li><li><strong>DML (Data Manipulation Language):</strong> As the name suggests, these statements manipulate the data itself, and the views related to it. Examples: <code>SELECT</code>, <code>INSERT</code>.</li><li><strong>DCL (Data Control Language):</strong> These statements control access to the data. There are only two of them: <code>GRANT</code> and <code>REVOKE</code>.</li><li><strong>TCL (Transaction Control Language):</strong> Transactions are handled using these statements. Examples: <code>COMMIT</code>, <code>ROLLBACK</code>.</li></ul><h2>Designing a database</h2><p>Here&#8217;s where we design a database for some simple data. Let&#8217;s say you want to make a database to store the names, addresses, phone numbers and birthdays of your friends and relatives. The first step is to see what kind of data you want to store:</p><ul><li>Names</li><li>Addresses</li><li>Phone Numbers</li><li>Birthdays</li></ul><p>Let&#8217;s begin by creating the database in MySQL:</p><pre class="brush: sql; gutter: false">mysql&gt; CREATE DATABASE mycontacts;
Query OK, 1 row affected (0.00 sec)

mysql&gt; USE mycontacts;
Database changed</pre><p>The <code>USE</code> command tells MySQL which database you want to be working on in this session. You can list the available databases using the following command:</p><pre class="brush: sql; gutter: false">mysql&gt; show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mycontacts         |
| mysql              |
| phpmyadmin         |
+--------------------+
4 rows in set (0.05 sec)</pre><p>Now let&#8217;s go table by table, and see what columns to put into each. These are the columns I put in:</p><ol><li>Names:<ul><li>contact_id</li><li>name</li></ul></li><li>Addresses:<ul><li>contact_id</li><li>address_line1</li><li>address_line2</li><li>city</li><li>country</li><li>email</li></ul></li><li>Phone Numbers:<ul><li>contact_id</li><li>landline</li><li>mobile</li></ul></li><li>Birthdays:<ul><li>contact_id</li><li>birthdate</li></ul></li></ol><p>So, we can now make four tables: <code>NAMES_MASTER</code>, <code>ADDRESS_MASTER</code>, <code>PHONE_MASTER</code> and <code>BIRTHDAY_MASTER</code>. The MySQL command for making <code>NAMES_MASTER</code> would be:</p><pre class="brush: sql; gutter: false; first-line: 1">mysql&gt; CREATE TABLE NAMES_MASTER (
       contact_id INT,
       name VARCHAR(50)
       );
Query OK, 0 rows affected (0.14 sec)</pre><p>Other tables can be made similarly. <code>VARCHAR</code> and <code>INT</code> are data-types. <code>INT</code> stands for integer. <code>VARCHAR</code> is a variable character. It&#8217;s a dynamically changing field, whose size depends on the entered data. The <code>VARCHAR(50)</code> means the field can have a maximum size of fifty characters. Other supported data-types are listed in the MySQL documentation.</p><p>Now let&#8217;s explore the concept of primary and secondary keys. If you notice, the field <code>contact_id</code> is common to all tables. This is to relate the tables by that common field. Here, the <code>contact_id</code> in <code>NAMES_MASTER</code> is the primary key, and the rest are secondary keys. In other words, <code>contact_id</code> is the reference for all the data in each table (as the secondary key), where it finally relates to the name of the contact (as primary key).</p><p>Once a table is made, you can populate it using a tool called <a href="http://www.phpmyadmin.net/">phpMyAdmin</a>. Doing justice to using phpMyAdmin would require another article; hence, I won&#8217;t cover this aspect here. You can also populate using the <code>INSERT</code> statement:</p><pre class="brush: sql; gutter: false; first-line: 1">INSERT into table_name (column1, column2...)
                              values (value1, value2...);</pre><p>At the end of this exercise, you will have a database ready to use. This, frankly, is just the starting point in designing a database. I hope it proved helpful to you in understanding what goes on beneath the surface in cyberspace, at least when it comes to managing data.<div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2011/08/connecting-to-mysql-with-python-and-php/" rel="bookmark" class="crp_title">Connecting to MySQL With Python and PHP</a></li><li><a href="http://www.linuxforu.com/2009/05/database-programming-in-python/" rel="bookmark" class="crp_title">Database Programming in Python</a></li><li><a href="http://www.linuxforu.com/2012/01/foss-is-fun-dbas-get-basics-right/" rel="bookmark" class="crp_title">FOSS is __FUN__: Get the Basics Right</a></li><li><a href="http://www.linuxforu.com/2011/10/lets-play-with-codeigniter/" rel="bookmark" class="crp_title">Let&#8217;s Play with CodeIgniter</a></li><li><a href="http://www.linuxforu.com/2011/08/comprehensive-lamp-guide-part-2-mysql/" rel="bookmark" class="crp_title">The Comprehensive LAMP Guide &#8212; Part 2 (MySQL)</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/acid/" title="acid" rel="tag">acid</a>, <a href="http://www.linuxforu.com/tag/amazon/" title="Amazon" rel="tag">Amazon</a>, <a href="http://www.linuxforu.com/tag/anthony-mills/" title="Anthony Mills" rel="tag">Anthony Mills</a>, <a href="http://www.linuxforu.com/tag/api/" title="API" rel="tag">API</a>, <a href="http://www.linuxforu.com/tag/database-driver/" title="database driver" rel="tag">database driver</a>, <a href="http://www.linuxforu.com/tag/database-management-system/" title="database management system" rel="tag">database management system</a>, <a href="http://www.linuxforu.com/tag/durval-castro/" title="Durval Castro" rel="tag">Durval Castro</a>, <a href="http://www.linuxforu.com/tag/e-commerce-site/" title="e commerce site" rel="tag">e commerce site</a>, <a href="http://www.linuxforu.com/tag/gene-bellinger/" title="Gene Bellinger" rel="tag">Gene Bellinger</a>, <a href="http://www.linuxforu.com/tag/information-databases/" title="information databases" rel="tag">information databases</a>, <a href="http://www.linuxforu.com/tag/interface-driver/" title="interface driver" rel="tag">interface driver</a>, <a href="http://www.linuxforu.com/tag/java/" title="Java" rel="tag">Java</a>, <a href="http://www.linuxforu.com/tag/jdbc/" title="jdbc" rel="tag">jdbc</a>, <a href="http://www.linuxforu.com/tag/lfy-may-2011/" title="LFY May 2011" rel="tag">LFY May 2011</a>, <a href="http://www.linuxforu.com/tag/murphys-law/" title="Murphy&#039;s Law" rel="tag">Murphy&#039;s Law</a>, <a href="http://www.linuxforu.com/tag/mysql/" title="MySQL" rel="tag">MySQL</a>, <a href="http://www.linuxforu.com/tag/odbc/" title="odbc" rel="tag">odbc</a>, <a href="http://www.linuxforu.com/tag/oracle/" title="Oracle" rel="tag">Oracle</a>, <a href="http://www.linuxforu.com/tag/phpmyadmin/" title="phpMyAdmin" rel="tag">phpMyAdmin</a>, <a href="http://www.linuxforu.com/tag/product-catalogues/" title="product catalogues" rel="tag">product catalogues</a>, <a href="http://www.linuxforu.com/tag/rdbms/" title="RDBMS" rel="tag">RDBMS</a>, <a href="http://www.linuxforu.com/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.linuxforu.com/tag/sun/" title="Sun" rel="tag">Sun</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2011/05/a-simple-guide-to-database-design-in-mysql/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Layer 7 Load Balancers</title><link>http://www.linuxforu.com/2011/04/layer-7-load-balancers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=layer-7-load-balancers</link> <comments>http://www.linuxforu.com/2011/04/layer-7-load-balancers/#comments</comments> <pubDate>Thu, 31 Mar 2011 18:41:48 +0000</pubDate> <dc:creator>Prashant Phatak</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[Sysadmins]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[broken web]]></category> <category><![CDATA[BSD]]></category> <category><![CDATA[Cisco]]></category> <category><![CDATA[Citrix]]></category> <category><![CDATA[Crossroads. Ultra Monkey]]></category> <category><![CDATA[data-centre infrastructure]]></category> <category><![CDATA[DNS]]></category> <category><![CDATA[Fedora]]></category> <category><![CDATA[http]]></category> <category><![CDATA[LFY April 2011]]></category> <category><![CDATA[load balance]]></category> <category><![CDATA[load balancers]]></category> <category><![CDATA[load-balancing]]></category> <category><![CDATA[load-balancing device]]></category> <category><![CDATA[LVS]]></category> <category><![CDATA[open source load-balancing solutions]]></category> <category><![CDATA[page requests]]></category> <category><![CDATA[Red Hat]]></category> <category><![CDATA[request processing]]></category> <category><![CDATA[resource limit]]></category> <category><![CDATA[server farm]]></category> <category><![CDATA[server performance]]></category> <category><![CDATA[SSL]]></category> <category><![CDATA[unix]]></category> <category><![CDATA[virtual server]]></category> <category><![CDATA[web portals]]></category> <category><![CDATA[Web requests]]></category> <category><![CDATA[Web servers]]></category> <category><![CDATA[web services infrastructure]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=9250</guid> <description><![CDATA[Linux has proven itself as a rock-solid operating system platform for industry-leading software appliances and applications, one of which is for load-balancing. As global Internet traffic increases, it demands an increased throughput from...]]></description> <content:encoded><![CDATA[<p><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/04/load-balance.jpg?d9c344" alt="Load balancing?" title="Load balancing?" width="300" height="239" class="alignright size-full wp-image-9252" /><div class="introduction">Linux has proven itself as a rock-solid operating system platform for industry-leading software appliances and applications, one of which is for load-balancing. As global Internet traffic increases, it demands an increased throughput from the existing infrastructure.</p><p>It is crucial to deliver content fast; this is especially true for businesses whose only interface with clients is their Web portals. Load balancers add great value in this case, and also provide multiple other functionalities. This article explains new trends in this well-known product category, which are not adequately explored by IT managers and systems administrators.</p></div><p>Why is there a need for load balancers? While managing a Web services infrastructure, Web administrators often find it a challenge to cope with increased website hits, while maintaining high availability of the servers. This situation gets even tougher when a new Web application or functionality is released, attracting more users per day.</p><p>Optimisation of server performance is thus a continuous job. Consider a Web server hosting a site running a few applications. When the site gains more users, there are many more page requests. Serving each request uses a definite amount of CPU, memory and network resources. Adding powerful resources can only solve the problem to some extent, while introducing other challenges. When the Web server hits the ceiling in terms of its resource limit, it starts dropping Web requests, which results in a bad user experience &#8212; a &#8220;broken&#8221; Web page.</p><p>And if the Web server goes down, for some reason, the entire site becomes non-functional. This can certainly result in a loss of reputation, and in some cases, also a monetary loss for the organisation. To preempt such situations, IT management teams must deploy load-balancing solutions in the data-centre infrastructure. We will soon discuss how a load balancer can not only distribute traffic, but also help ease network operations tasks.</p><h2>How does a load balancer work?</h2><p>First-generation balancing devices were implemented around BSD UNIX versions. A new trend of balancing products is typically in the form of an appliance running a Linux distribution; some enterprise-grade appliances use Red Hat or similar Linux flavours.</p><div id="attachment_9253" class="wp-caption aligncenter" style="width: 590px"><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/04/Typical-load-balancer-setup-590x379.jpg?d9c344" alt="Typical load balancer setup" title="Typical load balancer setup" width="590" height="379" class="size-large wp-image-9253" /><p class="wp-caption-text">Figure 1: Typical load balancer setup</p></div><p>Functionally, a load balancer can balance traffic by distributing it among two or more servers. Figure 1 shows a typical Web farm configuration with a load-balancing device that acts as a front-end server to handle all Web requests. Each silo hosts a different set of applications, whereas all servers in a given silo host identical applications.</p><p>From the configuration point of view, the device is configured with two separate IP ranges. One is used to handle incoming traffic, and the other, called virtual servers, is used to connect to the nodes under its control. Thus, it forms an agent service between the requesting client and the responding server. It also acts on the requests intelligently, based on the configured rules, to choose a recipient node with the least workload at that particular time.</p><p>Rules define how a request should be handled, and also how to handle special conditions such as node preference, session management, etc. The load-balancing device then makes a separate TCP connection with the recipient Web server, and redirects the requests to it, while it keeps track of the request processing.</p><p>In the technical sense, a load balancer balances underlying TCP connections, rather than actual Web requests. It is a misconception that a load balancer checks resource utilisation (such as CPU, memory, etc.) on a controlled server. In reality, it simply checks the network response time of a server, which is a result of the server&#8217;s overall resource utilisation. Since it acts as a catalyst in improving the scalability of a server farm, it maintains data for each node under its control, like the number of requests processed in history, the response time by each host for requests, the fault trend of each host, etc.</p><p>In earlier days, load balancing solutions were implemented around simple round-robin techniques, which did help distribute the load, but did not provide fault tolerance features, since they lacked the necessary intelligence. In today&#8217;s advanced data centres, load balancers are used to effectively distribute traffic for Web servers, databases, queue managers, DNS servers, email and SMTP traffic, and almost all applications which use IP traffic. Balancing DNS servers helps distribute DNS queries to servers that are dispersed geographically, which is useful for disaster-recovery implementations.</p><h2>Using load balancers to achieve fault tolerance</h2><p>In a server farm, servers often experience downtime due to unforeseen resource failure or scheduled maintenance. These resource failures can be at the hardware level or simply at the software application level. In a business-critical infrastructure, such situations should be transparent, never affecting the user. As discussed earlier, since the balancing device maintains separate TCP connectivity with the controlled node, it can be further used to achieve fault tolerance.</p><p>A configurable &#8220;heart beat&#8221;, called a monitor, is maintained by the balancer with each node. This can be a simple ICMP ping, or an FTP/HTTP connection to retrieve data. Upon an appropriate response from the node, the load balancer becomes aware that the node is live, and marks it as an active participant eligible for the balancing process. If the server or its application resource fails, the balancer waits for a certain period of time for a &#8220;heart beat&#8221; from the node; upon non-compliance, it marks that node as a non-participant, and removes it from the silo.</p><p>Once marked thus, the load balancer doesn&#8217;t send traffic to that node. However, it still keeps polling to see if the node is back online, and if found to be so, marks the node as an active participant again and starts sending traffic to it. If a fault situation occurs while the request is being transferred to a node, modern load balancers are capable of detecting that too, and taking (configurable) corrective action.</p><p>This feature can further be explored by the operations team for maintenance purposes. A service instance can be configured on a node &#8212; for example, a separate Web instance running under a separate IP address, with a dummy page on it. A monitor can be configured to access that page periodically. If the server is to be taken offline for maintenance purposes, the operations person can stop the dummy site, which results in the server being marked as a non-participant.</p><p>It can then be shut down or have other administration work done on it. Once maintenance is completed, the dummy site service can be started again, bringing the server back into the silo. This feature can be further extended by configuring many such monitors at the application level that can be reported upon in a dashboard via a network monitoring product, for an operations admin view.</p><h2>Layer 7 load balancing</h2><p>Earlier versions of load balancers used to work at the OSI model Layer 2 (link aggregation), or Layer 4 (IP-based). Since the requests flow through the balancing devices, it made sense to read into the requests at Layer 7, to bring additional flexibility in balancing techniques. Adding such flexibility offers higher scalability, better manageability and high availability.</p><p>Layer 7 load balancing primarily operates on the following three techniques:</p><ol><li>URL parsing</li><li>HTTP header interception</li><li>Cookie interception</li></ol><p>Typically, a Layer 7 rule structure looks somewhat like the one shown below. However, the exact syntax varies for each vendor and device model. As seen in the example, a request is first parsed based on the virtual directory being accessed, then by a particular cookie field&#8217;s content, and is finally sent to a default pool, if the first two conditions are not matched.</p><pre class="brush: text; gutter: false; first-line: 1">{
if (http_qstring, &quot;/&quot;) = &quot;mydir&quot;
   sendto server_pool1
else{
if cookie(&quot;mycookie&quot;) contains &quot;logon_cookie&quot;
   sendto server_pool2
else {
   sendto server_pool3
}</pre><p>Since the request is intercepted and interpreted at Layer 7, the possibilities of adding intelligence grow exponentially. Rules can be configured to distribute traffic based on a field in the HTTP header, the source IP address or custom cookie fields, to name just a few. There are endless possibilities to make intelligent traffic distributions.</p><p>For example, if the incoming request is from a smartphone, it can be sent to servers hosting mobile applications. If the request is for a URL that hosts a simple HTML-based site, it can be routed to an economical server farm. If a login cookie is not present in the request, it can be sent to a login server, avoiding loading down other busy servers.</p><p>As the Layer 7 rules bring programmability to balancing techniques, they can further be explored for the benefit of the technology operations staff. When a roll-out of a newer version is planned in an existing database server farm, the new set of servers can be configured as a separate pool to perform migration mock-tests, and can be brought online once the tests are passed.</p><p>In case the roll-out experiences problems, merely switching pools back to the original settings can achieve a rollback with minimum downtime. As another example, many mission-critical Web farms require to maintain legacy server operating systems for stability reasons, while new applications demand the latest and greatest platforms. In such cases, separate server pools can be configured for new applications, and traffic distribution can be achieved by checking Web request URLs at Layer 7.</p><p>Load balancing at Layer 7 also helps improve the return on investment (RoI) of an IT infrastructure. Consider a Web portal which caters to a high volume of users with Web pages that are content rich, with JavaScript and images. Since the scripts and images don&#8217;t change quite often, these can be treated as static content, and hosted on a separate set of servers. As a result, the Web servers running important business logic use fewer resources, which means that we can accommodate more users per server, or host more applications per server, and thus reduce the effective cost of hosting. This also proves that a carefully configured Layer 7 load balancer can achieve higher application performance throughputs on a given data-centre infrastructure footprint.</p><h2>Additional features in a load balancer appliance</h2><p>Besides powerful traffic distribution features, most industry-grade modern load balancers also come with features which are essential to take additional tasks from the managed nodes, or other infrastructure components. SSL negotiation is one such feature that can handle heavy volumes of SSL handshaking &#8212; which would otherwise take a performance toll on Web servers. Another great feature is cookie persistence, which helps applications stick to a particular server, in order to maintain a stateful session with it.</p><p>Many new load-balancer trends provide admin features such as traffic monitoring and TCP buffering; security features such as content filtering, and an intrusion detection firewall; and also performance-based features such as HTTP caching, HTTP compression, etc. Since a load-balancing device is a front-end component in a server farm, it comes equipped with high-speed network ports, such as Gigabit Ethernet and fibre connections.</p><h2>Open source load-balancing solutions</h2><p>Multiple vendors provide industry-grade enterprise load-balancing solutions, such as F5 networks (BigIP), Citrix Netscaler, Cisco, Coyote Point, etc. These devices are rich in features, provide flexible rule programmability, and exhibit high performance throughput &#8212; but they do come with a price tag and support cost.</p><p>For those who are interested in FOSS, there are multiple distributions available on the Linux platform, which offer features from simple load balancing to full-featured appliance-grade products. Let&#8217;s look at three such &#8216;most wanted&#8217; solutions.</p><p><a href="http://www.linuxforu.com/2009/05/balancing-traffic-across-data-centres-using-lvs/" title="Balancing Traffic Across Data Centres Using LVS">LVS (Linux Virtual Server)</a> is one famous solution, which has proved to be industry-grade software, and can be used to build highly scalable and available Linux cluster servers to cater to high volumes of Web requests. It comes with <a href="http://www.linux-vs.org">ample documentation</a>, which helps build a load-balanced farm, step by step.</p><p><a href="http://www.ultramonkey.org/">Ultra Monkey</a> is another interesting solution, which provides failover features in addition to basic load balancing: if one load-balancer device fails, the other can take over to provide device-level fault tolerance. It supports multiple Linux flavours such as Fedora, Debian, etc.</p><p>Another powerful, but lesser-known implementation is <a href="http://crossroads.e-tunity.com">Crossroads for Linux</a>, which is a TCP-based load balancer providing a very basic form of traffic distribution. The beauty of this product is that its source code can be easily modified to serve just one task, such as DNS or Web balancing, without any bells and whistles &#8212; thus achieving a very high performance for that single purpose.</p><p>Configuring Layer 7 rules on a load balancer is an art, and needs a deep understanding of networking protocols and server operations. Features of load balancers can also be used as an aid to the operations and maintenance tasks.</p><div class="imagecredit">Feature image courtesy: <a href="http://www.flickr.com/photos/72213316@N00/5531444032/">Frank Kovalchek</a>. Reused under the terms of CC-BY 2.0 License.</div><div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2009/05/balancing-traffic-across-data-centres-using-lvs/" rel="bookmark" class="crp_title">Balancing Traffic Across Data Centres Using LVS</a></li><li><a href="http://www.linuxforu.com/2009/02/building-a-highly-available-web-server-cluster/" rel="bookmark" class="crp_title">Building A Highly-Available Web Server Cluster</a></li><li><a href="http://www.linuxforu.com/2011/04/cnn-ibn%e2%80%99s-rocking-affair-with-postgresql/" rel="bookmark" class="crp_title">CNN-IBN’s Rocking Affair with PostgreSQL</a></li><li><a href="http://www.linuxforu.com/2009/03/building-a-highly-available-nginx-reverse-proxy-using-heartbeat/" rel="bookmark" class="crp_title">Building A Highly Available Nginx Reverse-Proxy Using Heartbeat</a></li><li><a href="http://www.linuxforu.com/2009/01/glassfish-part-1-architecture-community-et-al/" rel="bookmark" class="crp_title">GlassFish, Part 1: Architecture, Community, et al.</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/broken-web/" title="broken web" rel="tag">broken web</a>, <a href="http://www.linuxforu.com/tag/bsd/" title="BSD" rel="tag">BSD</a>, <a href="http://www.linuxforu.com/tag/cisco/" title="Cisco" rel="tag">Cisco</a>, <a href="http://www.linuxforu.com/tag/citrix/" title="Citrix" rel="tag">Citrix</a>, <a href="http://www.linuxforu.com/tag/crossroads-ultra-monkey/" title="Crossroads. Ultra Monkey" rel="tag">Crossroads. Ultra Monkey</a>, <a href="http://www.linuxforu.com/tag/data-centre-infrastructure/" title="data-centre infrastructure" rel="tag">data-centre infrastructure</a>, <a href="http://www.linuxforu.com/tag/dns/" title="DNS" rel="tag">DNS</a>, <a href="http://www.linuxforu.com/tag/fedora/" title="Fedora" rel="tag">Fedora</a>, <a href="http://www.linuxforu.com/tag/http/" title="http" rel="tag">http</a>, <a href="http://www.linuxforu.com/tag/lfy-april-2011/" title="LFY April 2011" rel="tag">LFY April 2011</a>, <a href="http://www.linuxforu.com/tag/load-balance/" title="load balance" rel="tag">load balance</a>, <a href="http://www.linuxforu.com/tag/load-balancers/" title="load balancers" rel="tag">load balancers</a>, <a href="http://www.linuxforu.com/tag/load-balancing/" title="load-balancing" rel="tag">load-balancing</a>, <a href="http://www.linuxforu.com/tag/load-balancing-device/" title="load-balancing device" rel="tag">load-balancing device</a>, <a href="http://www.linuxforu.com/tag/lvs/" title="LVS" rel="tag">LVS</a>, <a href="http://www.linuxforu.com/tag/open-source-load-balancing-solutions/" title="open source load-balancing solutions" rel="tag">open source load-balancing solutions</a>, <a href="http://www.linuxforu.com/tag/page-requests/" title="page requests" rel="tag">page requests</a>, <a href="http://www.linuxforu.com/tag/red-hat/" title="Red Hat" rel="tag">Red Hat</a>, <a href="http://www.linuxforu.com/tag/request-processing/" title="request processing" rel="tag">request processing</a>, <a href="http://www.linuxforu.com/tag/resource-limit/" title="resource limit" rel="tag">resource limit</a>, <a href="http://www.linuxforu.com/tag/server-farm/" title="server farm" rel="tag">server farm</a>, <a href="http://www.linuxforu.com/tag/server-performance/" title="server performance" rel="tag">server performance</a>, <a href="http://www.linuxforu.com/tag/ssl/" title="SSL" rel="tag">SSL</a>, <a href="http://www.linuxforu.com/tag/unix/" title="unix" rel="tag">unix</a>, <a href="http://www.linuxforu.com/tag/virtual-server/" title="virtual server" rel="tag">virtual server</a>, <a href="http://www.linuxforu.com/tag/web-portals/" title="web portals" rel="tag">web portals</a>, <a href="http://www.linuxforu.com/tag/web-requests/" title="Web requests" rel="tag">Web requests</a>, <a href="http://www.linuxforu.com/tag/web-servers/" title="Web servers" rel="tag">Web servers</a>, <a href="http://www.linuxforu.com/tag/web-services-infrastructure/" title="web services infrastructure" rel="tag">web services infrastructure</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2011/04/layer-7-load-balancers/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Understanding Middleware with Apache ActiveMQ</title><link>http://www.linuxforu.com/2011/04/understanding-middleware-with-apache-activemq/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=understanding-middleware-with-apache-activemq</link> <comments>http://www.linuxforu.com/2011/04/understanding-middleware-with-apache-activemq/#comments</comments> <pubDate>Thu, 31 Mar 2011 18:37:15 +0000</pubDate> <dc:creator>Siddharth Mankad</dc:creator> <category><![CDATA[Applications]]></category> <category><![CDATA[Concepts]]></category> <category><![CDATA[Developers]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[Sysadmins]]></category> <category><![CDATA[AJAX]]></category> <category><![CDATA[Apache]]></category> <category><![CDATA[apache ActiveMQ]]></category> <category><![CDATA[C/C++]]></category> <category><![CDATA[CORBA]]></category> <category><![CDATA[enterprise computing]]></category> <category><![CDATA[enterprise environments]]></category> <category><![CDATA[ESB]]></category> <category><![CDATA[J2EE]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Java DataBase Connectivity]]></category> <category><![CDATA[Java Messaging Service]]></category> <category><![CDATA[Judith Hurwitz]]></category> <category><![CDATA[LFY April 2011]]></category> <category><![CDATA[middleware]]></category> <category><![CDATA[Object Management Group]]></category> <category><![CDATA[python]]></category> <category><![CDATA[ruby]]></category> <category><![CDATA[SSL]]></category> <category><![CDATA[transport protocols]]></category> <category><![CDATA[UDP]]></category> <category><![CDATA[Web services]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=4309</guid> <description><![CDATA[Even though &#8220;middleware&#8221; might seem like a close cousin of Tolkien&#8217;s Middle Earth, it&#8217;s a fairly modern term in computer software and services, and has nothing to do with hobbits, wizards or adventures...]]></description> <content:encoded><![CDATA[<p><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/04/opener1.jpg?d9c344" alt="Middleware basics" title="Middleware basics" width="350" height="337" class="alignright size-full wp-image-9219" /><div class="introduction">Even though &#8220;middleware&#8221; might seem like a close cousin of Tolkien&#8217;s Middle Earth, it&#8217;s a fairly modern term in computer software and services, and has nothing to do with hobbits, wizards or adventures involving a ring. Middleware is nothing but software that literally sits &#8220;in the middle&#8221;, like a bridge, between other software. Read on as I shed light on one specific example of middleware, Apache ActiveMQ.</div><p>Before we explore Apache&#8217;s middleware offering, it&#8217;s important to know the various types of middleware out there. Middleware are classified into types based on the primary functions they perform. A popular classification is by Judith Hurwitz, in her paper, <em><a href="http://web.archive.org/web/20061017153344/http://www.dbmsmag.com/9801d04.html">Sorting Out Middleware</a></em>, according to which middleware can be:</p><ul><li><strong>Embedded middleware:</strong> As the name suggests, this type of middleware handles embedded applications (software or firmware).</li><li><strong>RPC middleware:</strong> RPC (Remote Procedure Call) middleware communicates with applications using calls</li><li><strong>Object request broker:</strong> Here, applications can send and receive objects.</li><li><strong>SQL-oriented data access middleware:</strong> Primarily meant for database-related applications, this class of middleware sits between applications that communicate with databases.</li><li><strong>Message-oriented middleware:</strong> This is where Apache ActiveMQ fits in. Message-oriented middleware mediate the communication or messages, between applications.</li></ul><h2>Introducing Apache ActiveMQ</h2><p>One of the best open source solutions for message broking is Apache ActiveMQ. It is a prime product used in enterprise environments, because it supports advanced features such as multiple instances to store messages, and clustering environments.</p><p>The backbone of ActiveMQ is what&#8217;s known as JMS &#8212; the Java Messaging Service. JMS is an API implementation within J2EE (Java Enterprise) which is specifically for message-oriented applications.</p><h2>Features of ActiveMQ</h2><p>There is a long list of features on the <a href="http://activemq.apache.org/">ActiveMQ website</a>, but I will highlight and discuss the key ones:</p><ul><li>Cross-language compliance and compatibility: This is an important feature, since applications may be written in different languages like C/C++, Python, Ruby, Java, etc. Allowing cross-language compatibility allows communication between very diverse applications, and allows seamless integration. This is probably the most important feature for message-oriented middleware.</li><li>AJAX and REST support: Web services and applications can easily communicate, since AJAX and REST are part of the core of Web services and applications out there.</li><li>Compatible with various transport protocols: Important because messages can be transferred effectively only if there is a wide variety of protocols for transport. Examples of transport protocols include TCP (Transmission Control Protocol), UDP (User Datagram Protocol) and SSL (Secure Socket Layer).</li><li>Support for persistence: In computing terms, persistence is the capability of an application to preserve its state. In other words, it&#8217;s the ability of the application to remember. This is done by the program recording what it needs to remember in text files or databases. In the context of ActiveMQ, persistence is used mainly for the database-linked persistence of applications.</li><li>High-performance computing and high availability: In simple words, this is nothing but ensuring that any form of failure doesn&#8217;t affect the user. This is achieved mainly through clustering. Here, many instances of the application (either on the same machine, but usually spread across multiple physical machines) are running, in such a way that if one instance fails, another one takes over. This mission-critical feature is a big plus in ActiveMQ.</li></ul><h2>Usage scenario</h2><p>The product is great, sounds completely robust and the features are fine and dandy, but where is it used? I will demonstrate a scenario where ActiveMQ (or usually any message-broking middleware) would work. For the sake of an explanation, I will cite a specific scenario where ActiveMQ behaves like a message bus &#8212; or, more specifically, an enterprise service bus. Figure 1 is a diagram that will explain it.</p><div id="attachment_9218" class="wp-caption aligncenter" style="width: 427px"><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/04/ESB.jpeg?d9c344" alt="The Enterprise Service Bus (ESB) concept" title="The Enterprise Service Bus (ESB) concept" width="427" height="472" class="size-full wp-image-9218" /><p class="wp-caption-text">Figure 1: The Enterprise Service Bus (ESB) concept</p></div><h2>Other popular open source middleware</h2><p>Apart from ActiveMQ, the world of enterprise computing is full of other open source middleware. Here are some examples, with a short description of each.</p><ul><li><strong>JDBC:</strong> The famous Java DataBase Connectivity API is an SQL-Oriented Data Access middleware. It allows Java programs to have database access, and thus be persistent. Another similar one is ODBC (Open DataBase Connectivity).</li><li><strong>CORBA:</strong> Developed by the Object Management Group (OMG), the <a href="http://www.omg.org/spec/CORBA/">Common Object Request Broker Architecture</a> is a class of middleware that fits the object-broker type. Essentially, it allows communication between many applications and software components to exchange objects, even when they are written in different languages and are running on different computers. Note that CORBA is a standard, and not middleware, per se. A popular open source implementation of CORBA is <a href="http://www.mico.org">MICO</a> (MICO is a recursive acronym for Mico Is COrba).</li></ul><p>This is an introduction to the concept of middleware. It goes way deeper than the scope of this article, but hopefully what you read here will help you as a starting point. Happy learning!<div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2011/07/oracle-charts-out-java-future/" rel="bookmark" class="crp_title">Oracle Charts Out Java&#8217;s Future</a></li><li><a href="http://www.linuxforu.com/2009/01/glassfish-part-1-architecture-community-et-al/" rel="bookmark" class="crp_title">GlassFish, Part 1: Architecture, Community, et al.</a></li><li><a href="http://www.linuxforu.com/2011/04/exploring-software-extending-j2ee-applications-using-grails/" rel="bookmark" class="crp_title">Exploring Software: Extending J2EE Applications Using Grails</a></li><li><a href="http://www.linuxforu.com/2011/09/newbie-guide-android-rooting-recovery-flashing/" rel="bookmark" class="crp_title">A Novice&#8217;s Guide to Android Rooting, Recovery and Flashing</a></li><li><a href="http://www.linuxforu.com/2010/07/an-os-for-personal-computing/" rel="bookmark" class="crp_title">An OS for Personal Computing</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/ajax/" title="AJAX" rel="tag">AJAX</a>, <a href="http://www.linuxforu.com/tag/apache/" title="Apache" rel="tag">Apache</a>, <a href="http://www.linuxforu.com/tag/apache-activemq/" title="apache ActiveMQ" rel="tag">apache ActiveMQ</a>, <a href="http://www.linuxforu.com/tag/cc/" title="C/C++" rel="tag">C/C++</a>, <a href="http://www.linuxforu.com/tag/corba/" title="CORBA" rel="tag">CORBA</a>, <a href="http://www.linuxforu.com/tag/enterprise-computing/" title="enterprise computing" rel="tag">enterprise computing</a>, <a href="http://www.linuxforu.com/tag/enterprise-environments/" title="enterprise environments" rel="tag">enterprise environments</a>, <a href="http://www.linuxforu.com/tag/esb/" title="ESB" rel="tag">ESB</a>, <a href="http://www.linuxforu.com/tag/j2ee/" title="J2EE" rel="tag">J2EE</a>, <a href="http://www.linuxforu.com/tag/java/" title="Java" rel="tag">Java</a>, <a href="http://www.linuxforu.com/tag/java-database-connectivity/" title="Java DataBase Connectivity" rel="tag">Java DataBase Connectivity</a>, <a href="http://www.linuxforu.com/tag/java-messaging-service/" title="Java Messaging Service" rel="tag">Java Messaging Service</a>, <a href="http://www.linuxforu.com/tag/judith-hurwitz/" title="Judith Hurwitz" rel="tag">Judith Hurwitz</a>, <a href="http://www.linuxforu.com/tag/lfy-april-2011/" title="LFY April 2011" rel="tag">LFY April 2011</a>, <a href="http://www.linuxforu.com/tag/middleware/" title="middleware" rel="tag">middleware</a>, <a href="http://www.linuxforu.com/tag/object-management-group/" title="Object Management Group" rel="tag">Object Management Group</a>, <a href="http://www.linuxforu.com/tag/python/" title="python" rel="tag">python</a>, <a href="http://www.linuxforu.com/tag/ruby/" title="ruby" rel="tag">ruby</a>, <a href="http://www.linuxforu.com/tag/ssl/" title="SSL" rel="tag">SSL</a>, <a href="http://www.linuxforu.com/tag/transport-protocols/" title="transport protocols" rel="tag">transport protocols</a>, <a href="http://www.linuxforu.com/tag/udp/" title="UDP" rel="tag">UDP</a>, <a href="http://www.linuxforu.com/tag/web-services/" title="Web services" rel="tag">Web services</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2011/04/understanding-middleware-with-apache-activemq/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Device Drivers, Part 4: Linux Character Drivers</title><link>http://www.linuxforu.com/2011/02/linux-character-drivers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linux-character-drivers</link> <comments>http://www.linuxforu.com/2011/02/linux-character-drivers/#comments</comments> <pubDate>Mon, 31 Jan 2011 18:43:45 +0000</pubDate> <dc:creator>Anil Kumar Pugalia</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[Developers]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[Alessandro Rubini]]></category> <category><![CDATA[API]]></category> <category><![CDATA[audio drivers]]></category> <category><![CDATA[camera drivers]]></category> <category><![CDATA[character device driver]]></category> <category><![CDATA[character driver]]></category> <category><![CDATA[constructor and destructor]]></category> <category><![CDATA[device drivers]]></category> <category><![CDATA[Greg Kroah-Hartman]]></category> <category><![CDATA[Jonathan Corbet]]></category> <category><![CDATA[kernel space]]></category> <category><![CDATA[LFY February 2011]]></category> <category><![CDATA[linux device drivers]]></category> <category><![CDATA[Linux Device Drivers Series]]></category> <category><![CDATA[network device drivers]]></category> <category><![CDATA[serial drivers]]></category> <category><![CDATA[space character]]></category> <category><![CDATA[VFS]]></category> <category><![CDATA[video drivers]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=4155</guid> <description><![CDATA[This article, which is part of the series on Linux device drivers, deals with the various concepts of character drivers and their implementation.]]></description> <content:encoded><![CDATA[<p><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/02/main-page-image-590x295.jpg?d9c344" alt="Lessons on Linux Device Drivers" title="Lessons on Linux Device Drivers" width="590" height="295" class="aligncenter size-large wp-image-7961" /><div class="introduction">This article, which is part of the <a href="http://www.linuxforu.com/tag/linux-device-drivers-series/">series on Linux device drivers</a>, deals with the various concepts related to character drivers and their implementation.</div><p>Shweta, at her PC in her hostel room, was all set to explore the characters of Linux character drivers, before it was taught in class. She recalled the following lines from professor Gopi&#8217;s class: &#8220;&#8230; today&#8217;s first driver would be the template for any driver you write in Linux. Writing any specialised/advanced driver is just a matter of what gets filled into its constructor and destructor&#8230;&#8221;</p><p>With that, she took out the first driver&#8217;s code, and pulled out various reference books, to start writing a character driver on her own. She also downloaded the <a href="http://lwn.net/Kernel/LDD3/">online book</a>, <em>Linux Device Drivers</em> by Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman. Here is the summary of what she learnt.</p><h2>W&#8217;s of character drivers</h2><p>We already know what drivers are, and why we need them. What is so special about character drivers? If we write drivers for byte-oriented operations (or, in C lingo, character-oriented operations), then we refer to them as character drivers. Since the majority of devices are byte-oriented, the majority of device drivers are character device drivers.</p><p>Take, for example, serial drivers, audio drivers, video drivers, camera drivers, and basic I/O drivers. In fact, all device drivers that are neither storage nor network device drivers are some type of a character driver. Let&#8217;s look into the commonalities of these character drivers, and how Shweta wrote one of them.</p><h2>The complete connection</h2><div id="attachment_7963" class="wp-caption aligncenter" style="width: 590px"><a href="http://cdn.linuxforu.com/wp-content/uploads/2011/02/figure_7_character_driver_overview.png?d9c344"><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/02/figure_7_character_driver_overview-590x331.png?d9c344" alt="Character driver overview" title="Character driver overview" width="590" height="331" class="size-large wp-image-7963" /></a><p class="wp-caption-text">Figure 1: Character driver overview</p></div><p>As shown in Figure 1, for any user-space application to operate on a byte-oriented device (in hardware space), it should use the corresponding character device driver (in kernel space). Character driver usage is done through the corresponding character device file(s), linked to it through the virtual file system (VFS). What this means is that an application does the usual file operations on the character device file. Those operations are translated to the corresponding functions in the linked character device driver by the VFS. Those functions then do the final low-level access to the actual device to achieve the desired results.</p><p>Note that though the application does the usual file operations, their outcome may not be the usual ones. Rather, they would be as driven by the corresponding functions in the device driver. For example, a write followed by a read may not fetch what has just been written to the character device file, unlike for regular files. Remember that this is the usual expected behaviour for device files. Let&#8217;s take an audio device file as an example. What we write into it is the audio data we want to play back, say through a speaker. However, the read would get us audio data that we are recording, say through a microphone. The recorded data need not be the played-back data.</p><p>In this complete connection from the application to the device, there are four major entities involved:</p><ol><li>Application</li><li>Character device file</li><li>Character device driver</li><li>Character device</li></ol><p>The interesting thing is that all of these can exist independently on a system, without the other being present. The mere existence of these on a system doesn&#8217;t mean they are linked to form the complete connection. Rather, they need to be explicitly connected. An application gets connected to a device file by invoking the open system call on the device file.</p><p>Device file(s) are linked to the device driver by specific registrations done by the driver. The driver is linked to a device by its device-specific low-level operations. Thus we form the complete connection. With this, note that the character device file is not the actual device, but just a place-holder for the actual device.</p><h2>Major and minor numbers</h2><p>The connection between the application and the device file is based on the name of the device file. However, the connection between the device file and the device driver is based on the number of the device file, not the name. This allows a user-space application to have any name for the device file, and enables the kernel-space to have a trivial index-based linkage between the device file and the device driver. This device file number is more commonly referred to as the <code>&lt;major, minor&gt;</code> pair, or the major and minor numbers of the device file.</p><p>Earlier (till kernel 2.4), one major number was for one driver, and the minor number used to represent the sub-functionalities of the driver. With kernel 2.6, this distinction is no longer mandatory; there could be multiple drivers under the same major number, but obviously, with different minor number ranges.</p><p>However, this is more common with the non-reserved major numbers, and standard major numbers are typically preserved for single drivers. For example, 4 for serial interfaces, 13 for mice, 14 for audio devices, and so on. The following command would list the various character device files on your system:</p><pre class="brush: text; gutter: false">$ ls -l /dev/ | grep "^c"</pre><h2>&lt;major, minor&gt; related support in kernel 2.6</h2><p>Type (defined in kernel header <code>linux/types.h</code>):</p><ul><li><code>dev_t</code> contains both major and minor numbers</li></ul><p>Macros (defined in kernel header <code>linux/kdev_t.h</code>):</p><ul><li><code>MAJOR(dev_t dev)</code> extracts the major number from <code>dev</code></li><li><code>MINOR(dev_t dev)</code> extracts the minor number from <code>dev</code></li><li><code>MKDEV(int major, int minor)</code> creates the <code>dev</code> from major and minor.</li></ul><p>Connecting the device file with the device driver involves two steps:</p><ol><li>Registering for the <code>&lt;major, minor&gt;</code> range of device files.</li><li>Linking the device file operations to the device driver functions.</li></ol><p>The first step is achieved using either of the following two APIs, defined in the kernel header <code>linux/fs.h</code>:</p><pre class="brush: c; gutter: false">+ int register_chrdev_region(dev_t first, unsigned int cnt, char *name);
+ int alloc_chrdev_region(dev_t *first, unsigned int firstminor, unsigned int cnt, char *name);</pre><p>The first API registers the cnt number of device file numbers, starting from first, with the given name. The second API dynamically figures out a free major number, and registers the cnt number of device file numbers starting from <code>&lt;the free major, firstminor&gt;</code>, with the given name. In either case, the <code>/proc/devices</code> kernel window lists the name with the registered major number. With this information, Shweta added the following into the first driver code:</p><pre class="brush: c; gutter: false">#include &lt;linux/types.h&gt;
#include &lt;linux/kdev_t.h&gt;
#include &lt;linux/fs.h&gt;

static dev_t first; // Global variable for the first device number</pre><p>In the constructor, she added:</p><pre class="brush: c; gutter: false">if (alloc_chrdev_region(&amp;first, 0, 3, "Shweta") &lt; 0)
{
    return -1;
}
printk(KERN_INFO "&lt;Major, Minor&gt;: &lt;%d, %d&gt;\n", MAJOR(first), MINOR(first));</pre><p>In the destructor, she added:</p><pre class="brush: c; gutter: false">unregister_chrdev_region(first, 3);</pre><p>It&#8217;s all put together, as follows:</p><pre class="brush: c; gutter: true; first-line: 1">#include &lt;linux/module.h&gt;
#include &lt;linux/version.h&gt;
#include &lt;linux/kernel.h&gt;
#include &lt;linux/types.h&gt;
#include &lt;linux/kdev_t.h&gt;
#include &lt;linux/fs.h&gt;

static dev_t first; // Global variable for the first device number

static int __init ofcd_init(void) /* Constructor */
{
    printk(KERN_INFO "Namaskar: ofcd registered");
    if (alloc_chrdev_region(&amp;first, 0, 3, "Shweta") &lt; 0)
    {
        return -1;
    }
    printk(KERN_INFO "&lt;Major, Minor&gt;: &lt;%d, %d&gt;\n", MAJOR(first), MINOR(first));
    return 0;
}

static void __exit ofcd_exit(void) /* Destructor */
{
    unregister_chrdev_region(first, 3);
    printk(KERN_INFO "Alvida: ofcd unregistered");
}

module_init(ofcd_init);
module_exit(ofcd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia &lt;email_at_sarika-pugs_dot_com&gt;");
MODULE_DESCRIPTION("Our First Character Driver");</pre><p>Then, Shweta repeated the usual steps that she&#8217;d learnt for the first driver:</p><ul><li>Build the driver (<code>.ko</code> file) by running <code>make</code>.</li><li>Load the driver using <code>insmod</code>.</li><li>List the loaded modules using <code>lsmod</code>.</li><li>Unload the driver using <code>rmmod</code>.</li></ul><h2>Summing up</h2><p>Additionally, before unloading the driver, she peeped into the <code>/proc/devices</code> kernel window to look for the registered major number with the name &#8220;Shweta&#8221;, using <code>cat /proc/devices</code>. It was right there. However, she couldn&#8217;t find any device file created under <code>/dev</code> with the same major number, so she created them by hand, using <code>mknod</code>, and then tried reading and writing those. Figure 2 shows all these steps.</p><div id="attachment_7964" class="wp-caption aligncenter" style="width: 590px"><a href="http://cdn.linuxforu.com/wp-content/uploads/2011/02/figure_8_char_dev_file_experiments.png?d9c344"><img src="http://cdn.linuxforu.com/wp-content/uploads/2011/02/figure_8_char_dev_file_experiments-590x448.png?d9c344" alt="Character device file experiments" title="Character device file experiments" width="590" height="448" class="size-large wp-image-7964" /></a><p class="wp-caption-text">Figure 2: Character device file experiments</p></div><p>Please note that the major number <code>250</code> may vary from system to system, based on availability. Figure 2 also shows the results Shweta got from reading and writing one of the device files. That reminded her that the second step to connect the device file with the device driver &#8212; which is linking the device file operations to the device driver functions &#8212; was not yet done. She realised that she needed to dig around for more information to complete this step, and also to figure out the reason for the missing device files under <code>/dev</code>.</p><p>We will deal with her further learning in our next article.<div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2011/04/character-device-files-creation-operations/" rel="bookmark" class="crp_title">Device Drivers, Part 5: Character Device Files &#8212; Creation &#038; Operations</a></li><li><a href="http://www.linuxforu.com/2011/05/decoding-character-device-file-operations/" rel="bookmark" class="crp_title">Device Drivers, Part 6: Decoding Character Device File Operations</a></li><li><a href="http://www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/" rel="bookmark" class="crp_title">Device Drivers, Part 13: Data Transfer to and from USB Devices</a></li><li><a href="http://www.linuxforu.com/2011/10/usb-drivers-in-linux-1/" rel="bookmark" class="crp_title">Device Drivers, Part 11: USB Drivers in Linux</a></li><li><a href="http://www.linuxforu.com/2010/12/writing-your-first-linux-driver/" rel="bookmark" class="crp_title">Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/alessandro-rubini/" title="Alessandro Rubini" rel="tag">Alessandro Rubini</a>, <a href="http://www.linuxforu.com/tag/api/" title="API" rel="tag">API</a>, <a href="http://www.linuxforu.com/tag/audio-drivers/" title="audio drivers" rel="tag">audio drivers</a>, <a href="http://www.linuxforu.com/tag/camera-drivers/" title="camera drivers" rel="tag">camera drivers</a>, <a href="http://www.linuxforu.com/tag/character-device-driver/" title="character device driver" rel="tag">character device driver</a>, <a href="http://www.linuxforu.com/tag/character-driver/" title="character driver" rel="tag">character driver</a>, <a href="http://www.linuxforu.com/tag/constructor-and-destructor/" title="constructor and destructor" rel="tag">constructor and destructor</a>, <a href="http://www.linuxforu.com/tag/device-drivers/" title="device drivers" rel="tag">device drivers</a>, <a href="http://www.linuxforu.com/tag/greg-kroah-hartman/" title="Greg Kroah-Hartman" rel="tag">Greg Kroah-Hartman</a>, <a href="http://www.linuxforu.com/tag/jonathan-corbet/" title="Jonathan Corbet" rel="tag">Jonathan Corbet</a>, <a href="http://www.linuxforu.com/tag/kernel-space/" title="kernel space" rel="tag">kernel space</a>, <a href="http://www.linuxforu.com/tag/lfy-february-2011/" title="LFY February 2011" rel="tag">LFY February 2011</a>, <a href="http://www.linuxforu.com/tag/linux-device-drivers/" title="linux device drivers" rel="tag">linux device drivers</a>, <a href="http://www.linuxforu.com/tag/linux-device-drivers-series/" title="Linux Device Drivers Series" rel="tag">Linux Device Drivers Series</a>, <a href="http://www.linuxforu.com/tag/network-device-drivers/" title="network device drivers" rel="tag">network device drivers</a>, <a href="http://www.linuxforu.com/tag/serial-drivers/" title="serial drivers" rel="tag">serial drivers</a>, <a href="http://www.linuxforu.com/tag/space-character/" title="space character" rel="tag">space character</a>, <a href="http://www.linuxforu.com/tag/vfs/" title="VFS" rel="tag">VFS</a>, <a href="http://www.linuxforu.com/tag/video-drivers/" title="video drivers" rel="tag">video drivers</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2011/02/linux-character-drivers/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Device Drivers, Part 3: Kernel C Extras in a Linux Driver</title><link>http://www.linuxforu.com/2011/01/kernel-c-extras-in-a-linux-device-driver/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=kernel-c-extras-in-a-linux-device-driver</link> <comments>http://www.linuxforu.com/2011/01/kernel-c-extras-in-a-linux-device-driver/#comments</comments> <pubDate>Fri, 31 Dec 2010 18:43:46 +0000</pubDate> <dc:creator>Anil Kumar Pugalia</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[Developers]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[C]]></category> <category><![CDATA[Compiler]]></category> <category><![CDATA[configuration file]]></category> <category><![CDATA[device drivers]]></category> <category><![CDATA[error output]]></category> <category><![CDATA[Fedora]]></category> <category><![CDATA[GCC]]></category> <category><![CDATA[gcc extensions]]></category> <category><![CDATA[insmod]]></category> <category><![CDATA[kernel]]></category> <category><![CDATA[LFY January 2011]]></category> <category><![CDATA[linux device drivers]]></category> <category><![CDATA[Linux Device Drivers Series]]></category> <category><![CDATA[log file]]></category> <category><![CDATA[Mandriva]]></category> <category><![CDATA[message logging]]></category> <category><![CDATA[ring buffer]]></category> <category><![CDATA[serial port]]></category> <category><![CDATA[syslog daemon]]></category> <category><![CDATA[ubuntu]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=3894</guid> <description><![CDATA[This article in the series on Linux device drivers deals with the kernel&#8217;s message logging, and kernel-specific GCC extensions. Enthused by how Pugs impressed their professor in the last class, Shweta wanted to...]]></description> <content:encoded><![CDATA[<p><img class="aligncenter size-large wp-image-7951" title="Kernel C Extras" src="http://cdn.linuxforu.com/wp-content/uploads/2011/01/OPENER-KERNELC-590x284.jpg?d9c344" alt="Kernel C Extras" width="590" height="284" /></p><div class="introduction">This article in the <a href="http://www.linuxforu.com/tag/linux-device-drivers-series/">series on Linux device drivers</a> deals with the kernel&#8217;s message logging, and kernel-specific GCC extensions.</div><p>Enthused by how Pugs impressed their professor in the last class, Shweta wanted to do so too. And there was soon an opportunity: finding out where the output of printk had gone. So, as soon as she entered the lab, she grabbed the best system, logged in, and began work. Knowing her professor well, she realised that he would have dropped a hint about the possible solution in the previous class itself. Going over what had been taught, she remembered the error output demonstration from <code>insmod vfat.ko</code> &#8212; running <code>dmesg | tail</code>. She immediately tried that, and found the <code>printk</code> output there.</p><p>But how did it come to be here? A tap on her shoulder roused her from her thoughts. &#8220;Shall we go for a coffee?&#8221; proposed Pugs.</p><p>&#8220;But I need to &#8211;&#8221;.</p><p>&#8220;I know what you&#8217;re thinking about,&#8221; interrupted Pugs. &#8220;Let&#8217;s go, I&#8217;ll explain you all about dmesg.&#8221;</p><h2>Kernel message logging</h2><p>Over coffee, Pugs began his explanation.</p><p>As far as parameters are concerned, <code>printf</code> and <code>printk</code> are the same, except that when programming for the kernel, we don&#8217;t bother about the float formats <code>%f</code>, <code>%lf</code> and the like. However, unlike <code>printf</code>, <code>printk</code> is not designed to dump its output to some console.</p><p>In fact, it cannot do so; it is something in the background, and executes like a library, only when triggered either from hardware-space or user-space. All <code>printk</code> calls put their output into the (log) ring buffer of the kernel. Then, the <code>syslog</code> daemon running in user-space picks them up for final processing and redirection to various devices, as configured in the configuration file <code>/etc/syslog.conf</code>.</p><p>You must have observed the out-of-place macro <code>KERN_INFO</code>, in the <code>printk</code> calls, in the <a title="Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom" href="http://www.linuxforu.com/2010/12/writing-your-first-linux-driver/">last article</a>. That is actually a constant string, which gets concatenated with the format string after it, into a single string. Note that there is no comma (,) between them; they are not two separate arguments. There are eight such macros defined in <code>linux/kernel.h</code> in the kernel source, namely:</p><pre class="brush: c; gutter: false">#define KERN_EMERG "&lt;0&gt;"   /* system is unusable                */
#define KERN_ALERT "&lt;1&gt;"   /* action must be taken immediately	*/
#define KERN_CRIT "&lt;2&gt;"    /* critical conditions		*/
#define KERN_ERR "&lt;3&gt;"     /* error conditions			*/
#define KERN_WARNING "&lt;4&gt;" /* warning conditions		*/
#define KERN_NOTICE "&lt;5&gt;"  /* normal but significant condition	*/
#define KERN_INFO "&lt;6&gt;"    /* informational			*/
#define KERN_DEBUG "&lt;7&gt;"   /* debug-level messages		*/</pre><p>Now depending on these log levels (i.e., the first three characters in the format string), the <code>syslog</code> user-space daemon redirects the corresponding messages to their configured locations. A typical destination is the log file <code>/var/log/messages</code>, for all log levels. Hence, all the <code>printk</code> outputs are, by default, in that file. However, they can be configured differently &#8212; to a serial port (like <code>/dev/ttyS0</code>), for instance, or to all consoles, like what typically happens for <code>KERN_EMERG</code>.</p><p>Now, <code>/var/log/messages</code> is buffered, and contains messages not only from the kernel, but also from various daemons running in user-space. Moreover, this file is often not readable by a normal user. Hence, a user-space utility, <code>dmesg</code>, is provided to directly parse the kernel ring buffer, and dump it to standard output. Figure 1 shows snippets from the two.</p><div id="attachment_7952" class="wp-caption aligncenter" style="width: 590px"><a href="http://cdn.linuxforu.com/wp-content/uploads/2011/01/figure_6_linuxDriver-kernels_message_logging.png?d9c344"><img class="size-large wp-image-7952" title="Kernel’s message logging" src="http://cdn.linuxforu.com/wp-content/uploads/2011/01/figure_6_linuxDriver-kernels_message_logging-590x448.png?d9c344" alt="Kernel’s message logging" width="590" height="448" /></a><p class="wp-caption-text">Figure 1: Kernel’s message logging</p></div><h2>Kernel-specific GCC extensions</h2><p>Shweta, frustrated since she could no longer show off as having discovered all these on her own, retorted, &#8220;Since you have explained all about printing in the kernel, why don&#8217;t you also tell me about the weird C in the driver as well &#8212; the special keywords <code>__init</code>, <code>__exit</code>, etc.&#8221;</p><p>These are not special keywords. Kernel C is not &#8220;weird C&#8221;, but just standard C with some additional extensions from the C compiler, GCC. Macros <code>__init</code> and <code>__exit</code> are just two of these extensions. However, these do not have any relevance in case we are using them for a dynamically loadable driver, but only when the same code gets built into the kernel. All functions marked with <code>__init</code> get placed inside the <code>init</code> section of the kernel image automatically, by GCC, during kernel compilation; and all functions marked with <code>__exit</code> are placed in the <code>exit</code> section of the kernel image.</p><p>What is the benefit of this? All functions with <code>__init</code> are supposed to be executed only once during bootup (and not executed again till the next bootup). So, once they are executed during bootup, the kernel frees up RAM by removing them (by freeing the <code>init</code> section). Similarly, all functions in the <code>exit</code> section are supposed to be called during system shutdown.</p><p>Now, if the system is shutting down anyway, why do you need to do any cleaning up? Hence, the <code>exit</code> section is not even loaded into the kernel &#8212; another cool optimisation. This is a beautiful example of how the kernel and GCC work hand-in-hand to achieve a lot of optimisation, and many other tricks that we will see as we go along. And that is why the Linux kernel can only be compiled using GCC-based compilers &#8212; a closely knit bond.</p><h2>The kernel function’s return guidelines</h2><p>While returning from coffee, Pugs kept praising OSS and the community that&#8217;s grown around it. Do you know why different individuals are able to come together and contribute excellently without any conflicts, and in a project as huge as Linux, at that? There are many reasons, but most important amongst them is that they all follow and abide by inherent coding guidelines.</p><p>Take, for example, the kernel programming guideline for returning values from a function. Any kernel function needing error handling, typically returns an integer-like type &#8212; and the return value again follows a guideline. For an error, we return a negative number: a minus sign appended with a macro that is available through the kernel header <code>linux/errno.h</code>, that includes the various error number headers under the kernel sources &#8212; namely, <code>asm/errno.h</code>, <code>asm-generic/errno.h</code>, <code>asm-generic/errno-base.h</code>.</p><p>For success, zero is the most common return value, unless there is some additional information to be provided. In that case, a positive value is returned, the value indicating the information, such as the number of bytes transferred by the function.</p><h2>Kernel C = pure C</h2><p>Once back in the lab, Shweta remembered their professor mentioning that no <code>/usr/include</code> headers can be used for kernel programming. But Pugs had said that kernel C is just standard C with some GCC extensions. Why this conflict?</p><p>Actually this is not a conflict. Standard C is pure C &#8212; just the language. The headers are not part of it. Those are part of the standard libraries built in for C programmers, based on the concept of reusing code.</p><p>Does that mean that all standard libraries, and hence, all ANSI standard functions, are not part of &#8220;pure&#8221; C? Yes, that&#8217;s right. Then, was it really tough coding the kernel?</p><p>Well, not for this reason. In reality, kernel developers have evolved their own set of required functions, which are all part of the kernel code. The <code>printk</code> function is just one of them. Similarly, many string functions, memory functions, and more, are all part of the kernel source, under various directories like <code>kernel</code>, <code>ipc</code>, <code>lib</code>, and so on, along with the corresponding headers under the <code>include/linux</code> directory.</p><p>&#8220;Oh yes! That is why we need to have the kernel source to build a driver,&#8221; agreed Shweta.</p><p>&#8220;If not the complete source, at least the headers are a must. And that is why we have separate packages to install the complete kernel source, or just the kernel headers,&#8221; added Pugs.</p><p>&#8220;In the lab, all the sources are set up. But if I want to try out drivers on my Linux system in my hostel room, how do I go about it?&#8221; asked Shweta.</p><p>&#8220;Our lab has Fedora, where the kernel sources are typically installed under <code>/usr/src/kernels/&lt;kernel-version&gt;</code>, unlike the standard <code>/usr/src/linux</code>. Lab administrators must have installed it using the command-line <code>yum install kernel-devel</code>. I use Mandriva, and installed the kernel sources using <code>urpmi kernel-source</code>,&#8221; replied Pugs.</p><p>&#8220;But I have Ubuntu,&#8221; Shweta said.</p><p>&#8220;Okay! For that, just use <code>apt-get</code> utility to fetch the source &#8212; possibly <code>apt-get install linux-source</code>,&#8221; replied Pugs.</p><h2>Summing up</h2><p>The lab session was almost over when Shweta suddenly asked, out of curiosity, &#8220;Hey Pugs, what&#8217;s the next topic we are going to learn in our Linux device drivers class?&#8221;</p><p>&#8220;Hmm&#8230; most probably character drivers,&#8221; threw back Pugs.</p><p>With this information, Shweta hurriedly packed her bag and headed towards her room to set up the kernel sources, and try out the next driver on her own. &#8220;In case you get stuck, just give me a call,&#8221; smiled Pugs.<div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2010/12/writing-your-first-linux-driver/" rel="bookmark" class="crp_title">Device Drivers, Part 2: Writing Your First Linux Driver in the Classroom</a></li><li><a href="http://www.linuxforu.com/2011/05/decoding-character-device-file-operations/" rel="bookmark" class="crp_title">Device Drivers, Part 6: Decoding Character Device File Operations</a></li><li><a href="http://www.linuxforu.com/2011/09/kernel-space-debuggers-in-linux/" rel="bookmark" class="crp_title">Device Drivers, Part 10: Kernel-Space Debuggers in Linux</a></li><li><a href="http://www.linuxforu.com/2011/02/linux-character-drivers/" rel="bookmark" class="crp_title">Device Drivers, Part 4: Linux Character Drivers</a></li><li><a href="http://www.linuxforu.com/2011/04/character-device-files-creation-operations/" rel="bookmark" class="crp_title">Device Drivers, Part 5: Character Device Files &#8212; Creation &#038; Operations</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/c/" title="C" rel="tag">C</a>, <a href="http://www.linuxforu.com/tag/compiler/" title="Compiler" rel="tag">Compiler</a>, <a href="http://www.linuxforu.com/tag/configuration-file/" title="configuration file" rel="tag">configuration file</a>, <a href="http://www.linuxforu.com/tag/device-drivers/" title="device drivers" rel="tag">device drivers</a>, <a href="http://www.linuxforu.com/tag/error-output/" title="error output" rel="tag">error output</a>, <a href="http://www.linuxforu.com/tag/fedora/" title="Fedora" rel="tag">Fedora</a>, <a href="http://www.linuxforu.com/tag/gcc/" title="GCC" rel="tag">GCC</a>, <a href="http://www.linuxforu.com/tag/gcc-extensions/" title="gcc extensions" rel="tag">gcc extensions</a>, <a href="http://www.linuxforu.com/tag/insmod/" title="insmod" rel="tag">insmod</a>, <a href="http://www.linuxforu.com/tag/kernel/" title="kernel" rel="tag">kernel</a>, <a href="http://www.linuxforu.com/tag/lfy-january-2011/" title="LFY January 2011" rel="tag">LFY January 2011</a>, <a href="http://www.linuxforu.com/tag/linux-device-drivers/" title="linux device drivers" rel="tag">linux device drivers</a>, <a href="http://www.linuxforu.com/tag/linux-device-drivers-series/" title="Linux Device Drivers Series" rel="tag">Linux Device Drivers Series</a>, <a href="http://www.linuxforu.com/tag/log-file/" title="log file" rel="tag">log file</a>, <a href="http://www.linuxforu.com/tag/mandriva/" title="Mandriva" rel="tag">Mandriva</a>, <a href="http://www.linuxforu.com/tag/message-logging/" title="message logging" rel="tag">message logging</a>, <a href="http://www.linuxforu.com/tag/ring-buffer/" title="ring buffer" rel="tag">ring buffer</a>, <a href="http://www.linuxforu.com/tag/serial-port/" title="serial port" rel="tag">serial port</a>, <a href="http://www.linuxforu.com/tag/syslog-daemon/" title="syslog daemon" rel="tag">syslog daemon</a>, <a href="http://www.linuxforu.com/tag/ubuntu/" title="ubuntu" rel="tag">ubuntu</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2011/01/kernel-c-extras-in-a-linux-device-driver/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Device Drivers, Part 1: Linux Device Drivers for Your Girl Friend</title><link>http://www.linuxforu.com/2010/11/understanding-linux-device-drivers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=understanding-linux-device-drivers</link> <comments>http://www.linuxforu.com/2010/11/understanding-linux-device-drivers/#comments</comments> <pubDate>Sun, 31 Oct 2010 18:47:02 +0000</pubDate> <dc:creator>Anil Kumar Pugalia</dc:creator> <category><![CDATA[Concepts]]></category> <category><![CDATA[Developers]]></category> <category><![CDATA[Overview]]></category> <category><![CDATA[bus]]></category> <category><![CDATA[bus drivers]]></category> <category><![CDATA[device controllers]]></category> <category><![CDATA[device drivers]]></category> <category><![CDATA[display controllers]]></category> <category><![CDATA[hard disk controllers]]></category> <category><![CDATA[hardware protocols]]></category> <category><![CDATA[LFY November 2010]]></category> <category><![CDATA[linux device drivers]]></category> <category><![CDATA[Linux Device Drivers Series]]></category> <category><![CDATA[software device driver]]></category> <category><![CDATA[software programming]]></category><guid isPermaLink="false">http://www.linuxforu.com/?p=7921</guid> <description><![CDATA[This series on Linux device drivers aims to present the usually technical topic in a way that is more interesting to a wider cross-section of readers. &#8220;After a week of hard work, we...]]></description> <content:encoded><![CDATA[<p><img src="http://cdn.linuxforu.com/wp-content/uploads/2010/11/ldd0.jpeg?d9c344" alt="Driven..." title="Driven..." width="350" height="425" class="alignright size-full wp-image-7931" /><div class="introduction">This <a href="http://www.linuxforu.com/tag/linux-device-drivers-series/">series on Linux device drivers</a> aims to present the usually technical topic in a way that is more interesting to a wider cross-section of readers.</div><p>&#8220;After a week of hard work, we finally got our driver working,&#8221; were Pugs&#8217; first words when he met his girlfriend, Shweta.</p><p>&#8220;Why? What was your driver up to? Was he sick? And what hard work did you do?&#8221; asked Shweta. Confused, Pugs responded, &#8220;What are you talking about?&#8221;</p><p>Now it was Shweta&#8217;s turn to look puzzled, as she replied, &#8220;Why ask me? You tell me &#8212; which of your drivers are you talking about?&#8221;</p><p>When understanding dawned on him, Pugs groaned, &#8220;Ah c&#8217;mon! Not my car drivers &#8212; I am talking about a device driver on my computer.&#8221;</p><p>&#8220;I know about car and bus drivers, pilots, and even screwdrivers; but what is this &#8216;device driver&#8217;?&#8221; queried Shweta, puzzled.</p><p>That was all it took to launch Pugs into a passionate explanation of device drivers for the newbie &#8212; in particular, Linux device drivers, which he had been working on for many years.</p><h2>Of drivers and buses</h2><p>A driver drives, manages, controls, directs and monitors the entity under its command. What a bus driver does with a bus, a device driver does with a computer device (any piece of hardware connected to a computer) like a mouse, keyboard, monitor, hard disk, Web-camera, clock, and more.</p><p>Further, a &#8220;pilot&#8221; could be a person or even an automatic system monitored by a person (an auto-pilot system in airliners, for example). Similarly, a specific piece of hardware could be controlled by a piece of software (a device driver), or could be controlled by another hardware device, which in turn could be managed by a software device driver. In the latter case, such a controlling device is commonly called a device controller. This, being a device itself, often also needs a driver, which is commonly referred to as a bus driver.</p><p>General examples of device controllers include hard disk controllers, display controllers, and audio controllers that in turn manage devices connected to them. More technical examples would be an IDE controller, PCI controller, USB controller, SPI controller, I2C controller, etc. Pictorially, this whole concept can be depicted as in Figure 1.</p><div id="attachment_7928" class="wp-caption aligncenter" style="width: 590px"><a href="http://cdn.linuxforu.com/wp-content/uploads/2010/11/ldd1.jpg?d9c344"><img src="http://cdn.linuxforu.com/wp-content/uploads/2010/11/ldd1-590x297.jpg?d9c344" alt="Device and driver interaction" title="Device and driver interaction" width="590" height="297" class="size-large wp-image-7928" /></a><p class="wp-caption-text">Figure 1: Device and driver interaction</p></div><p>Device controllers are typically connected to the CPU through their respectively named buses (collection of physical lines) &#8212; for example, the PCI bus, the IDE bus, etc. In today&#8217;s embedded world, we encounter more micro-controllers than CPUs; these are the CPU plus various device controllers built onto a single chip. This effective embedding of device controllers primarily reduces cost and space, making it suitable for embedded systems. In such cases, the buses are integrated into the chip itself. Does this change anything for the drivers, or more generically, on the software front?</p><p>The answer is, not much &#8212; except that the bus drivers corresponding to the embedded device controllers are now developed under the architecture-specific umbrella.</p><h2>Drivers have two parts</h2><p>Bus drivers provide hardware-specific interfaces for the corresponding hardware protocols, and are the bottom-most horizontal software layers of an operating system (OS). Over these sit the actual device drivers. These operate on the underlying devices using the horizontal layer interfaces, and hence are device-specific. However, the whole idea of writing these drivers is to provide an abstraction to the user, and so, at the other &#8220;end&#8221;, these do provide an interface (which varies from OS to OS). In short, a device driver has two parts, which are: a) device-specific, and b) OS-specific. Refer to Figure 2.</p><div id="attachment_7930" class="wp-caption aligncenter" style="width: 590px"><a href="http://cdn.linuxforu.com/wp-content/uploads/2010/11/ldd2.jpg?d9c344"><img src="http://cdn.linuxforu.com/wp-content/uploads/2010/11/ldd2-590x378.jpg?d9c344" alt="Linux device driver partition" title="Linux device driver partition" width="590" height="378" class="size-large wp-image-7930" /></a><p class="wp-caption-text">Figure 2: Linux device driver partition</p></div><p>The device-specific portion of a device driver remains the same across all operating systems, and is more about understanding and decoding the device data sheets than software programming. A data sheet for a device is a document with technical details of the device, including its operation, performance, programming, etc. &#8212; in short a device user manual.</p><p>Later, I shall show some examples of decoding data sheets as well. However, the OS-specific portion is the one that is tightly coupled with the OS mechanisms of user interfaces, and thus differentiates a Linux device driver from a Windows device driver and from a MacOS device driver.</p><h2>Verticals</h2><p>In Linux, a device driver provides a &#8220;system call&#8221; interface to the user; this is the boundary line between the so-called kernel space and user-space of Linux, as shown in Figure 2. Figure 3 provides further classification.</p><div id="attachment_7929" class="wp-caption aligncenter" style="width: 590px"><a href="http://cdn.linuxforu.com/wp-content/uploads/2010/11/ldd3.jpg?d9c344"><img src="http://cdn.linuxforu.com/wp-content/uploads/2010/11/ldd3-590x350.jpg?d9c344" alt="Linux kernel overview" title="Linux kernel overview" width="590" height="350" class="size-large wp-image-7929" /></a><p class="wp-caption-text">Figure 3: Linux kernel overview</p></div><p>Based on the OS-specific interface of a driver, in Linux, a driver is broadly classified into three verticals:</p><ul><li>Packet-oriented or the network vertical</li><li>Block-oriented or the storage vertical</li><li>Byte-oriented or the character vertical</li></ul><p>The CPU vertical and memory vertical, taken together with the other three verticals, give the complete overview of the Linux kernel, like any textbook definition of an OS: &#8220;An OS performs 5 management functions: CPU/process, memory, network, storage, device I/O.&#8221; Though these two verticals could be classified as device drivers, where CPU and memory are the respective devices, they are treated differently, for many reasons.</p><p>These are the core functionalities of any OS, be it a micro-kernel or a monolithic kernel. More often than not, adding code in these areas is mainly a Linux porting effort, which is typically done for a new CPU or architecture. Moreover, the code in these two verticals cannot be loaded or unloaded on the fly, unlike the other three verticals. Henceforth, when we talk about Linux device drivers, we mean to talk only about the latter three verticals in Figure 3.</p><p>Let&#8217;s get a little deeper into these three verticals. The network vertical consists of two parts: a) the network protocol stack, and b)the network interface card (NIC) device drivers, or simply network device drivers, which could be for Ethernet, Wi-Fi, or any other network horizontals. Storage, again, consists of two parts: a) File-system drivers, to decode the various formats on different partitions, and b) Block device drivers for various storage (hardware) protocols, i.e., horizontals like IDE, SCSI, MTD, etc.</p><p>With this, you may wonder if that is the only set of devices for which you need drivers (or for which Linux has drivers). Hold on a moment; you certainly need drivers for the whole lot of devices that interface with the system, and Linux does have drivers for them. However, their byte-oriented cessibility puts all of them under the character vertical &#8212; this is, in reality, the majority bucket. In fact, because of the vast number of drivers in this vertical, character drivers have been further sub-classified &#8212; so you have tty drivers, input drivers, console drivers, frame-buffer drivers, sound drivers, etc. The typical horizontals here would be RS232, PS/2, VGA, I2C, I2S, SPI, etc.</p><h2>Multiple-vertical drivers</h2><p>One final note on the complete picture (placement of all the drivers in the Linux driver ecosystem): the horizontals like USB, PCI, etc, span below multiple verticals. Why is that?</p><p>Simple &#8212; you already know that you can have a USB Wi-Fi dongle, a USB pen drive, and a USB-to-serial converter &#8212; all are USB, but come under three different verticals!</p><p>In Linux, bus drivers or the horizontals, are often split into two parts, or even two drivers: a) device controller-specific, and b) an abstraction layer over that for the verticals to interface, commonly called cores. A classic example would be the USB controller drivers ohci, ehci, etc., and the USB abstraction, usbcore.</p><h2>Summing up</h2><p>So, to conclude, a device driver is a piece of software that drives a device, though there are so many classifications. In case it drives only another piece of software, we call it just a driver. Examples are file-system drivers, usbcore, etc. Hence, all device drivers are drivers, but all drivers are not device drivers.</p><p>&#8220;Hey, Pugs, hold on; we&#8217;re getting late for class, and you know what kind of trouble we can get into. Let&#8217;s continue from here, later,&#8221; exclaimed Shweta.</p><p>Jumping up, Pugs finished his explanation: &#8220;Okay. This is the basic theory about device drivers. If you&#8217;re interested, later, I can show you the code, and all that we have been doing for the various kinds of drivers.&#8221; And they hurried towards their classroom.<div id="crp_related"><h5>Related Posts:</h5><ul><li><a href="http://www.linuxforu.com/2011/02/linux-character-drivers/" rel="bookmark" class="crp_title">Device Drivers, Part 4: Linux Character Drivers</a></li><li><a href="http://www.linuxforu.com/2011/10/usb-drivers-in-linux-1/" rel="bookmark" class="crp_title">Device Drivers, Part 11: USB Drivers in Linux</a></li><li><a href="http://www.linuxforu.com/2011/05/decoding-character-device-file-operations/" rel="bookmark" class="crp_title">Device Drivers, Part 6: Decoding Character Device File Operations</a></li><li><a href="http://www.linuxforu.com/2011/04/character-device-files-creation-operations/" rel="bookmark" class="crp_title">Device Drivers, Part 5: Character Device Files &#8212; Creation &#038; Operations</a></li><li><a href="http://www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/" rel="bookmark" class="crp_title">Device Drivers, Part 13: Data Transfer to and from USB Devices</a></li></ul></div>Tags: <a href="http://www.linuxforu.com/tag/bus/" title="bus" rel="tag">bus</a>, <a href="http://www.linuxforu.com/tag/bus-drivers/" title="bus drivers" rel="tag">bus drivers</a>, <a href="http://www.linuxforu.com/tag/device-controllers/" title="device controllers" rel="tag">device controllers</a>, <a href="http://www.linuxforu.com/tag/device-drivers/" title="device drivers" rel="tag">device drivers</a>, <a href="http://www.linuxforu.com/tag/display-controllers/" title="display controllers" rel="tag">display controllers</a>, <a href="http://www.linuxforu.com/tag/hard-disk-controllers/" title="hard disk controllers" rel="tag">hard disk controllers</a>, <a href="http://www.linuxforu.com/tag/hardware-protocols/" title="hardware protocols" rel="tag">hardware protocols</a>, <a href="http://www.linuxforu.com/tag/lfy-november-2010/" title="LFY November 2010" rel="tag">LFY November 2010</a>, <a href="http://www.linuxforu.com/tag/linux-device-drivers/" title="linux device drivers" rel="tag">linux device drivers</a>, <a href="http://www.linuxforu.com/tag/linux-device-drivers-series/" title="Linux Device Drivers Series" rel="tag">Linux Device Drivers Series</a>, <a href="http://www.linuxforu.com/tag/software-device-driver/" title="software device driver" rel="tag">software device driver</a>, <a href="http://www.linuxforu.com/tag/software-programming/" title="software programming" rel="tag">software programming</a><br /> ]]></content:encoded> <wfw:commentRss>http://www.linuxforu.com/2010/11/understanding-linux-device-drivers/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Content Delivery Network via cdn.linuxforu.com

Served from: www.linuxforu.com @ 2012-02-08 09:41:42 -->
