Tang-Nanzhao conflicts in Annan

Tang–Nanzhao conflicts in Annan (modern-day northern Vietnam) was a period of intense chaos and warfare in Tang's Protectorate General to Pacify the South between local rebel forces, the kingdom of Nanzhao in modern-day Yunnan and the Tang dynasty of China that lasted from 854 to 866. It ended in the defeat of Nanzhao and the retaking of Annan by the Tang general, Gao Pian, although the Tang would later lose the region for good in 880.
Prelude (854–857).
In 854, Li Zhuo became jiedushi of Annan. He reduced the amount of salt traded to the mountain chiefs in the west in exchange for horses. The mountains chiefs responded by launching raids on Chinese garrisons. When Li Zhuo began suffering defeats, Đỗ Tồn Thành, a military commander, allied himself with the tribal chiefs against Li. In the next year, Li Zhuo killed Đỗ Tồn Thành as well as the chieftain of the Qidong Man in Aizhou (Nghệ An, central Vietnam). The Đỗ tribe had been a powerful Vietnamese family in Thanh Hoá and Nghệ An since 5-6th century. These actions provoked the natives into an alliance with Nanzhao. Fan Chuo, a Tang official in Annan reported: "…The native chiefs within the frontiers were subsequently seduced by the Man rebels…" and "again became close friends with them. As days passed and months came, we gradually had to encounter raids and sudden attacks. This caused a number of places to fall into rebel hands."
Early phase (857–862).
In modern day Phú Thọ and Hòa Bình Province on the western frontier of the protectorate, the local general Lý Do Độc who led an army of 6,000, and was assisted by seven commanders called "Lords of the Ravines", submitted to Nanzhao. The Nanzhao king Meng Shilong (蒙世隆) sent a Trustee of the East to deliver a letter to Do Độc soliciting his submission. Lý Do Độc and the Lords of the Ravine accepted the offer of vassalage by the Nanzhao kin, who sent the Trustee one of his daughters to marry Lý Do Độc's eldest son. In 858, Nanzhao dispatched military forces to the region. In the meantime, local chiefs led raids that brought warfare to villages in the heart of the protectorate. In 857, Song Ya was sent to Annan to deal with the situation, but was recalled to deal with another rebellion after only two months. His replacement, Li Hongfu, only held nominal control over the protectorate, which was actually controlled by Luo Xinggong (羅行恭), who commanded 2,000 well trained soldiers. In 858, the Tang court sent a new jiedushi, Wang Shi, to protect Annan. He banished Luo Xinggong, saw off a Nanzhao reconnaissance force, and defeated an invasion by the mountain tribes. The Tang garrisons were upgraded with heavy-armored cavalry and infantry and Songping was fortified with a reed palisade. In the same year, a serious rebellion broke out in Yongzhou, Guangxi. The situation in Yong held the threat of severing land communication between Annan and the empire, so a special army was established there to deal with rebels and to insure communications. This army was called the Yellow Head Army, for the soldiers wore yellow bands around their heads. In early autumn, local people were agitated by a rumor that the Yellow Head Army had embarked to attack them by surprise. One evening they surrounded Songping and demanded that Wang Shi return north and allow them to fortify the city against the Yellow Head Army. Shi was eating his evening meal when this commotion broke out. It is reported that, paying no heed to the mutineers, he leisurely finished his meal. Then, dressed in his battle gear, he appeared on the wall with his generals and admonished the crowd of rebels, who dispersed. The next morning, Shi’s troops captured and beheaded some ringleaders of the affair.
In 860, Wang Shi was recalled to deal with a rebellion elsewhere. The new jiedushi, Li Hu, arrived at Songping and executed Đỗ Tồn Thành's son, Đỗ Thủ Trừng, who according to Chinese sources was involved in a mutiny years earlier, probably due to the death of his father at the hands of Li Zhuo four years earlier. This alienated many of the powerful local clans of Annan. Anti-Tang Vietnamese allied with highland people, who appealed to Nanzhao for help, and as a result invaded the area in 860, briefly taking Songping before being driven out by a Tang army the next year. Prior to Li Hu's arrival, Nanzhao had already seized Bozhou. When Li Hu led an army to retake Bozhou, the Đỗ family gathered 30,000 men, including contingents from Nanzhao to attack the Tang. When Li Hu returned, he learned the Vietnamese rebels and Nanzhao had taken control over Annan. On 17 January 861, Songping fell to the rebels and Hu fled to Yongzhou. In summer 861, Li Hu retook Songping on 21 July but Nanzhao forces moved around and seized Yongzhou. Li Hu was banished to Hainan island and was replaced by Wang Kuan. Wang Kuan and the Tang court sought local cooperation by recognizing the power of the Đỗ family, granting a posthumous title to Đỗ Tồn Thành along with an apology for the deaths of him and his son and an admission that Li Hu had exceeded his authority.
Nanzhao offensive (863).
In 863, Nanzhao returned with an invasion force numbering 50,000 with the aid of the local people and besieged Annan's capital Songping in mid-January. On 20 January, the defenders led by Cai Xi killed a hundred of the besiegers. Five days later, Cai Xi captured, tortured, and killed a group of enemies known as the Puzi "Man". A local official named Liang Ke was related to them and defected as a result. On 28 January, an enemy Buddhist monk, possibly Indian, was wounded by an arrow while strutting to and fro naked outside the southern walls. On 14 February, Cai Xi shot down 200 enemies and over 30 horses using a mounted crossbow from the walls. By 28 February, most of Cai Xi's followers had perished, and he himself had been wounded several times by arrows and stones. The enemy commander, Yang Sijin, penetrated the inner city. Cai Xi tried to escape by boat, but it capsized midstream, drowning him. The 400 remaining defenders wanted to flee as well, but could not find any boats, so they chose to make a last stand at the eastern gate. Ambushing a group of enemy cavalry, they killed over 2,000 enemy troops and 300 horses before Yang sent reinforcements from the inner city. After taking Songping, on 20 June Nanzhao laid siege to Junzhou (modern Haiphong). A Nanzhao and rebel fleet of 4,000 men led by a "Thổ" chief named Chu Đạo Cổ (Zhu Daogu, 朱道古) was attacked by a local commander, who rammed their vessels and sank 30 boats, drowning them. In total, the invasion destroyed Tang armies in Annan numbering over 150,000. Although initially welcomed by the local Vietnamese in ousting Tang control, Nanzhao turned on them, ravaging the local population and countryside. Both Chinese and Vietnamese sources note that the Vietnamese fled to the mountains to avoid destruction. A government-in-exile for the protectorate was established in Haimen (near modern-day Hạ Long) with Song Rong in charge. Ten thousand soldiers from Shandong and all other armies of the empire were called and concentrated at Halong Bay for reconquering Annan. A supply fleet of 1,000 ships from Fujian was organized. 
Tang counterattack (864–866).
The Tang launched a counterattack in 864 under Gao Pian, a general who had made his reputation fighting the Türks and the Tanguts in the north. In September 865, Gao's 5,000 troops surprised a Nanzhao army of 50,000 while they were collecting rice from the villages and routed them. Gao captured large quantities of rice, which he used to feed his army. A jealous governor, Li Weizhou, accused Gao of stalling to meet the enemy, and reported him to the throne. The court sent another general named Wang Yanqian to replace Gao. In the meantime, Gao had been reinforced by 7,000 men who arrived overland under the command of Wei Zhongzai. In early 866, Gao's 12,000 men defeated a fresh Nanzhao army and chased them back to the mountains. He then laid siege to Songping but had to leave command due to the arrival of Li Weizhou and Wang Yanqian. He was later reinstated after sending his aid, Zeng Gun, to the capital and he returned with a reinstatement. Gao completed the retaking of Annan in fall 866, executing the enemy general, Duan Qiuqian, and beheading 30,000 of his men.
Aftermath.
Gao Pian rebuilt the capital citadel, repairing 5,000 meters of damaged wall, reconstructing 400,000 bays for its residents, and named it Đại La. He also renamed Annan to Jinghai Jun (lit. Peaceful Sea Army). More than half of local rebels fled into the mountains at this time. This may well have sealed the separation of Muong from Vietnamese, which historians such as Henri Maspero suggest based on linguistic evidence, that took place at the end of Tang rule in Annan. 
A campaign against local aboriginals in Annan was conducted from 874-879. In 877, troops deployed from Annan in Guangxi mutined. In 880, the army in Annan mutinied, took the city of Đại La, and forced the military commissioner Zeng Gun to flee, ending de facto Chinese control in Vietnam.