Saturday, May 26, 2012

[Qt] memory management and implicit sharing

There are some points that we have to know when we program using Qt:

  • The ownership of all child QObjects is transferred to the parent.
    • Automatic deletion by the parent
    • Allocated from the heap (using new)
    • manual deletion is not necessary but it won't cause any problems.
  • All QObjects without a parent must be deleted manually.
  • Pay attention to ownership and responsibilities. Qt does not provide a garbage collection.
Only if you have Classes with QObjects, then child items will be deleted if the parent is deleted:

QObject *parent = new QObject; 
QWidget *child1 = new QWidget(parent); QPushButton *child2 = new QPushButton(parent); delete parent; // child1 and child2 will be deleted automatically!

 



Implicit sharing (IS)
The following example is about how implicit sharing works when test() returns QList object (on stack). This memory address of "a" is shared to "result" and "a" is not destroyed when method:test() is out of scope.
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QList> #include <QtGui> QList<QString> MainWindow::test() { QList<QString> a; // on stack QList<QString> b = QList<QString>(); // on stack QList<QString> *c = new QList<QString>(); // on heap for (int i = 0; i < 10; i++) { /* if we append a reference of QString, it will consume the memory of the QList Object based on the size of string */ a.append("a_helo:"); a.append(QString::number(i)); b.append("b_helo:"); b.append(QString::number(i)); c->append("c_helo:"); c->append(QString::number(i)); } qDebug() << a << " addres:" << &a; qDebug() << b << " addres:" << &b; qDebug() << *c << " addres:" << c; //delete &b; // we cannot manually free the memory of b or c on stack delete c; // OK return a; // Implicit Sharing } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); /* The memory address of result is same with a object in test() because of implicit sharing */ QList<QString> result = test(); QList<QString> result2; qDebug() << result << " addres:" << &result; qDebug() << result2 << " addres:" << &result2; } MainWindow::~MainWindow() { delete ui; }

Monday, May 14, 2012

[Explanation][Trema] The message mechanism in flow_manager app

I give 2 pictures to illustrate the message mechanism in flow_manager app.

1. Request/Reply message using send_request_message() and send_reply_message()


2. (Received) Message using send_message()

Thursday, May 10, 2012

[Open vSwitch] Simply test ovs-ofctl command on GNS3 simulation network


This article is to test ovs-ofctl command on GNS3 simulation network. PC1 and PC2 are  virtual machine on Qemu. U_OVS is emulated with Open vSwitch and an OpenFlow Controller is also on it. Please see the following picture:

1. For how to build the following GNS3 environment, please refer to the URL:
http://brezular.wordpress.com/2011/06/25/part2-openvswich-vlans-trunks-l3-vlan-interface-intervlan-routing-configuration-and-testing/
2. Run > sudo ovs-ofctl show br0

OFPT_FEATURES_REPLY (xid=0x1): ver:0x1, dpid:000008002725cd53
n_tables:1, n_buffers:256
features: capabilities:0x87, actions:0xfff
 1(eth1): addr:08:00:27:95:ef:0b
     config:     0
     state:      LINK_DOWN
     current:    COPPER AUTO_NEG
     advertised: 10MB-HD 10MB-FD 100MB-HD 100MB-FD 1GB-FD COPPER AUTO_NEG
     supported:  10MB-HD 10MB-FD 100MB-HD 100MB-FD 1GB-FD COPPER AUTO_NEG
 2(eth3): addr:08:00:27:25:cd:53
     config:     0
     state:      0
     current:    1GB-FD COPPER AUTO_NEG
     advertised: 10MB-HD 10MB-FD 100MB-HD 100MB-FD 1GB-FD COPPER AUTO_NEG
     supported:  10MB-HD 10MB-FD 100MB-HD 100MB-FD 1GB-FD COPPER AUTO_NEG
 3(eth2): addr:08:00:27:c3:5f:90
     config:     0
     state:      0
     current:    1GB-FD COPPER AUTO_NEG
     advertised: 10MB-HD 10MB-FD 100MB-HD 100MB-FD 1GB-FD COPPER AUTO_NEG
     supported:  10MB-HD 10MB-FD 100MB-HD 100MB-FD 1GB-FD COPPER AUTO_NEG
 LOCAL(br0): addr:08:00:27:25:cd:53
     config:     PORT_DOWN
     state:      LINK_DOWN
OFPT_GET_CONFIG_REPLY (xid=0x3): frags=normal miss_send_len=0

3. After PC1 and PC2 ping each other, run > sudo ovs-ofctl dump-flows br0
NXST_FLOW reply (xid=0x4):
 cookie=0x0, duration=17.125s, table=0, n_packets=14, n_bytes=1372, idle_timeout=5,priority=65535,icmp,in_port=2,vlan_tci=0x0000,dl_src=00:aa:00:3c:24:00,dl_dst=00:aa:00:4d:1d:00,nw_src=192.168.1.2,nw_dst=192.168.1.1,nw_tos=0,icmp_type=8,icmp_code=0 actions=output:3
 cookie=0x0, duration=18.13s, table=0, n_packets=15, n_bytes=1470, idle_timeout=5,priority=65535,icmp,in_port=3,vlan_tci=0x0000,dl_src=00:aa:00:4d:1d:00,dl_dst=00:aa:00:3c:24:00,nw_src=192.168.1.1,nw_dst=192.168.1.2,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:2


p.s:
the script of PC1 configuration
Login is tc without password set.
Assign IP address 192.168.1.1/24 to eth0 and make it persistent after next reboot of Microcore
sudo hostname PC1
sudo ifconfig eth0 192.168.1.1 netmask 255.255.255.0
echo "hostname PC1" >> /opt/bootlocal.sh
echo "ifconfig eth0 192.168.1.1 netmask 255.255.255.0" >> /opt/bootlocal.sh
/usr/bin/filetool.sh -b

Monday, May 7, 2012

[Explanation][Trema] The Register Event in Routing Switch App

If we want to understand how Routing Switch App works in Trema, we have to know the event register first. Here is a simple explanation about the register event in Routing Switch App, which is related Topology Ap.

Wednesday, May 2, 2012

[How to] Find a key word or Replace it with other string on Linux

Please refer to these examples:

Find the key word "ProcessType=6" in text files under current path (included sub folder)
> find ./ -name "*.txt" -exec grep -n -H "ProcessType=6" {} \;

Replace the key word "python2.4" with "python2.6" in python files under /opt/stack (included sub folder)
> find /opt/stack -name "*.py" -exec sed -i 's/python2.4/python2.6/' '{}' \;

Based on the script above, you will be able to find or replace a key word quickly and efficiently.

Sunday, April 29, 2012

[How to] Install Qt SDK on Ubuntu

1. Download Qt SDK
wget http://www.developer.nokia.com/dp?uri=http%3A%2F%2Fsw.nokia.com%2Fid%2F14b2039c-0e1f-4774-a4f2-9aa60b6d5313%2FQt_SDK_Lin64_offlinehttp://www.developer.nokia.com/dp?uri=http%3A%2F%2Fsw.nokia.com%2Fid%2F14b2039c-0e1f-4774-a4f2-9aa60b6d5313%2FQt_SDK_Lin64_offline


2. Change the file's mode for execution and run it.
> chmod u+x Qt_SDK_Lin64_offline_v1_2_en.run ( e.g )
> ./Qt_SDK_Lin64_offline_v1_2_en.run ( e.g. )

3. Install g++ compiler
> sudo apt-get install g++

4. If the error message ( Gtk-WARNING **: Unable to locate theme engine in module_path: "pixmap" ) shows when running Qt Creator, you need to run:
> sudo apt-get install gtk2-engines-pixbuf

[How to] Install Boost library on Ubuntu

> sudo apt-get install libboost-all-dev
That's it.

And also, there are plenty of examples as follows:
http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/examples.html

Tuesday, April 24, 2012

Look for a Qt library for network topology

Qanava could be my solution for network topology based on Qt4. But, it had been not maintained anymore...

main page:
http://gna.org/projects/qanava

git repository
http://repo.or.cz/w/qanava.git

Document 
Qanava Manual v0.1.0

Monday, April 23, 2012

[How to] Use tcpdump to look at LLDP packet

Here is an example about how to use tcpdump tool to look at LLDP information

> sudo tcpdump -c 1 -lv -v -i eth0 -a -e -s 1514 ether proto 0x88cc

The result:
tcpdump: listening on eth4, link-type EN10MB (Ethernet), capture size 1514 bytes
13:30:46.426056 08:00:27:09:61:e9 (oui Unknown) > 01:80:c2:00:00:0e (oui Unknown), ethertype LLDP (0x88cc), length 156: LLDP, length 142
    Chassis ID TLV (1), length 7
      Subtype MAC address (4): 08:00:27:09:61:e9 (oui Unknown)
      0x0000:  0408 0027 0961 e9
    Port ID TLV (2), length 7
      Subtype MAC address (3): 08:00:27:09:61:e9 (oui Unknown)
      0x0000:  0308 0027 0961 e9
    Time to Live TLV (3), length 2: TTL 120s
      0x0000:  0078
    System Name TLV (5), length 15: Ubuntu-Devstack
      0x0000:  5562 756e 7475 2d44 6576 7374 6163 6b
    System Description TLV (6), length 43
      Ubuntu 11.10\0x0a Linux 3.0.0-14-generic x86_64
      0x0000:  5562 756e 7475 2031 312e 3130 0a20 4c69
      0x0010:  6e75 7820 332e 302e 302d 3134 2d67 656e
      0x0020:  6572 6963 2078 3836 5f36 34
    System Capabilities TLV (7), length 4
      System  Capabilities [Bridge, WLAN AP, Router] (0x001c)
      Enabled Capabilities [Bridge, Router] (0x0014)
      0x0000:  001c 0014
    Management Address TLV (8), length 12
      Management Address length 5, AFI IPv4 (1): Ubuntu-Devstack.local
      Interface Index Interface Numbering (2): 5
      0x0000:  0501 c0a8 7a01 0200 0000 0500
    Port Description TLV (4), length 4: eth4
      0x0000:  6574 6834
    Organization specific TLV (127), length 9: OUI IEEE 802.3 Private (0x00120f)
      Link aggregation Subtype (3)
        aggregation status [supported], aggregation port ID 0
      0x0000:  0012 0f03 0100 0000 00
    Organization specific TLV (127), length 9: OUI IEEE 802.3 Private (0x00120f)
      MAC/PHY configuration/status Subtype (1)
        autonegotiation [supported, enabled] (0x03)
        PMD autoneg capability [10BASE-T hdx, 10BASE-T fdx, 100BASE-TX hdx, 100BASE-TX fdx, 1000BASE-T fdx] (0x6c01)
        MAU type 1000BASET fdx (0x001e)
      0x0000:  0012 0f01 036c 0100 1e
    Organization specific TLV (127), length 6: OUI IEEE 802.3 Private (0x00120f)
      Max frame size Subtype (4)
        MTU size 0
      0x0000:  0012 0f04 0000
    End TLV (0), length 0



The following is the argument list for reference:
 -a    將網絡地址和廣播地址轉變成名字
 -d    將匹配信息包的代碼以人們能夠理解的彙編格式給出
 -dd    將匹配信息包的代碼以c語言程序段的格式給出
 -ddd   將匹配信息包的代碼以十進制的形式給出
 -e    在輸出行打印出數據鏈路層的頭部信息
 -f    將外部的Internet地址以數字的形式打印出來
 -l    使標準輸出變為緩衝行形式( line buffered mode )
 -n    不把網絡地址轉換成名字
 -t    在輸出的每一行不打印時間戳
 -v    輸出一個稍微詳細的信息,例如在ip包中可以包括ttl和服務類型的信息
 -vv    輸出詳細的報文信息
 -c    在收到指定的包的數目後,tcpdump就會停止
 -F    從指定的文件中讀取表達式,忽略其它的表達式
 -i    指定監聽的網絡接口
 -r    從指定的文件中讀取包(這些包一般通過-w選項產生)
 -w    直接將包寫入文件中,並不分析和打印出來
 -T    將監聽到的包直接解釋為指定的類型的報文,常見的類型有rpc (遠程過程調用)和snmp(簡單網絡管理協議;)

Wednesday, April 18, 2012

[ZeroMQ] The new solution for building up distributed system

    I have used TIBCO Rendezvous (RV Message), ActiveMQ (JMS), and RabbitMQ(AMQP) before. They all have message broker, which means all the messages will be send to broker (centralized control) first, and then the client or node will receives them later. But, ZeroMQ has totally different story. I personally believe that it could be a very good solution for building up distributed system.

http://www.zeromq.org/

    And also, this article gives the short introduction and summarizes the important items including "communication transport", "End Point Implementation", "The Socket Object", and so on about ZeroMQ. This content is very uesful for beginner to get to know and read first.
http://www.coastrd.com/zeromq-messaging